# [PATCH] Makefile: Remove usage of deprecated Python "has_key" method

6 messages from 2010-03-28 to 2010-03-29. Participants: David Aguilar, Junio C Hamano, Tay Ray Chuan, Johan Herland.
Thread: https://gitlist.dev/t/23207

## David Aguilar, 2010-03-28 00:45

Subject: [PATCH] Makefile: Remove usage of deprecated Python "has_key" method
Message-ID: <1269737112-21631-1-git-send-email-davvid@gmail.com>
URL: https://gitlist.dev/e/1269737112-21631-1-git-send-email-davvid%40gmail.com

```
"has_key" is a deprecated dictionary method in Python 2.6+.
Simplify the sys.path manipulation for installed scripts by
passing a default value to os.getenv().

SCRIPT_PYTHON is currently empty but this future-proofs us.
It also fixes things for users who maintain local git forks
with their own SCRIPT_PYTHON additions.

Signed-off-by: David Aguilar <davvid@gmail.com>
---
 Makefile |    5 ++---
 1 files changed, 2 insertions(+), 3 deletions(-)

diff --git a/Makefile b/Makefile
index 3a6c6ea..806ccdf 100644
--- a/Makefile
+++ b/Makefile
@@ -1609,9 +1609,8 @@ $(patsubst %.py,%,$(SCRIPT_PYTHON)): % : %.py
 	    -e '}' \
 	    -e 's|^import sys.*|&; \\\
 	           import os; \\\
-	           sys.path[0] = os.environ.has_key("GITPYTHONLIB") and \\\
-	                         os.environ["GITPYTHONLIB"] or \\\
-	                         "@@INSTLIBDIR@@"|' \
+	           sys.path.insert(0, os.getenv("GITPYTHONLIB",\
+	                                        "@@INSTLIBDIR@@"));|' \
 	    -e 's|@@INSTLIBDIR@@|'"$$INSTLIBDIR"'|g' \
 	    $@.py >$@+ && \
 	chmod +x $@+ && \
-- 
1.7.0.3.291.g5e4f6

```

## Junio C Hamano, 2010-03-28 16:38

Subject: Re: [PATCH] Makefile: Remove usage of deprecated Python "has_key" method
Message-ID: <7vy6hc4d0h.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vy6hc4d0h.fsf%40alter.siamese.dyndns.org
In-Reply-To: <1269737112-21631-1-git-send-email-davvid@gmail.com>

```
David Aguilar <davvid@gmail.com> writes:

> "has_key" is a deprecated dictionary method in Python 2.6+.
> Simplify the sys.path manipulation for installed scripts by
> passing a default value to os.getenv().

It looks like the old code was replacing sys.path[0] but you are
prepending this.  Doesn't that change also make a difference?

> SCRIPT_PYTHON is currently empty but this future-proofs us.
> It also fixes things for users who maintain local git forks
> with their own SCRIPT_PYTHON additions.
>
> Signed-off-by: David Aguilar <davvid@gmail.com>
> ---
>  Makefile |    5 ++---
>  1 files changed, 2 insertions(+), 3 deletions(-)
>
> diff --git a/Makefile b/Makefile
> index 3a6c6ea..806ccdf 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -1609,9 +1609,8 @@ $(patsubst %.py,%,$(SCRIPT_PYTHON)): % : %.py
>  	    -e '}' \
>  	    -e 's|^import sys.*|&; \\\
>  	           import os; \\\
> -	           sys.path[0] = os.environ.has_key("GITPYTHONLIB") and \\\
> -	                         os.environ["GITPYTHONLIB"] or \\\
> -	                         "@@INSTLIBDIR@@"|' \
> +	           sys.path.insert(0, os.getenv("GITPYTHONLIB",\
> +	                                        "@@INSTLIBDIR@@"));|' \
>  	    -e 's|@@INSTLIBDIR@@|'"$$INSTLIBDIR"'|g' \
>  	    $@.py >$@+ && \
>  	chmod +x $@+ && \
> -- 
> 1.7.0.3.291.g5e4f6

```

## David Aguilar, 2010-03-28 21:54

Subject: Re: [PATCH] Makefile: Remove usage of deprecated Python "has_key" method
Message-ID: <20100328215442.GA31195@gmail.com>
URL: https://gitlist.dev/e/20100328215442.GA31195%40gmail.com
In-Reply-To: <7vy6hc4d0h.fsf@alter.siamese.dyndns.org>

```
On Sun, Mar 28, 2010 at 09:38:54AM -0700, Junio C Hamano wrote:
> David Aguilar <davvid@gmail.com> writes:
> 
> > "has_key" is a deprecated dictionary method in Python 2.6+.
> > Simplify the sys.path manipulation for installed scripts by
> > passing a default value to os.getenv().
> 
> It looks like the old code was replacing sys.path[0] but you are
> prepending this.  Doesn't that change also make a difference?

The original code replaced sys.path[0] which is ''
(aka the current directory).  It's a little odd to
rely on the 0th element being something that is safe
to remove.

By prepending the path we have the same intended effect without
having to know that the 0th element is something that is
safe to remove.

Does removing '' break relative imports? (It might...)

Due to the portability concerns with relative imports the
recommendation is to always use absolute imports.  Thus, this
shouldn't hurt us in practice if we stick to absolute imports,
but I figured I'd mention it as another reason why prepending
might be preferred over replacing.

-- 
		David

```

## Tay Ray Chuan, 2010-03-29 00:23

Subject: Re: [PATCH] Makefile: Remove usage of deprecated Python "has_key" method
Message-ID: <be6fef0d1003281723h4f8788bdi3c5baa3f92724c11@mail.gmail.com>
URL: https://gitlist.dev/e/be6fef0d1003281723h4f8788bdi3c5baa3f92724c11%40mail.gmail.com
In-Reply-To: <20100328215442.GA31195@gmail.com>

```
Hi,

On Mon, Mar 29, 2010 at 5:54 AM, David Aguilar <davvid@gmail.com> wrote:
> On Sun, Mar 28, 2010 at 09:38:54AM -0700, Junio C Hamano wrote:
>> David Aguilar <davvid@gmail.com> writes:
>>
>> > "has_key" is a deprecated dictionary method in Python 2.6+.
>> > Simplify the sys.path manipulation for installed scripts by
>> > passing a default value to os.getenv().
>>
>> It looks like the old code was replacing sys.path[0] but you are
>> prepending this.  Doesn't that change also make a difference?
>
> The original code replaced sys.path[0] which is ''
> (aka the current directory).  It's a little odd to
> rely on the 0th element being something that is safe
> to remove.

more accurately, it's "directory containing the script that was used
to invoke the Python interpreter":

  http://docs.python.org/library/sys.html#sys.path

-- 
Cheers,
Ray Chuan

```

## Johan Herland, 2010-03-29 02:16

Subject: Re: [PATCH] Makefile: Remove usage of deprecated Python "has_key" method
Message-ID: <201003290416.40774.johan@herland.net>
URL: https://gitlist.dev/e/201003290416.40774.johan%40herland.net
In-Reply-To: <1269737112-21631-1-git-send-email-davvid@gmail.com>

```
On Sunday 28 March 2010, David Aguilar wrote:
> "has_key" is a deprecated dictionary method in Python 2.6+.
> Simplify the sys.path manipulation for installed scripts by
> passing a default value to os.getenv().
> 
> SCRIPT_PYTHON is currently empty but this future-proofs us.
> It also fixes things for users who maintain local git forks
> with their own SCRIPT_PYTHON additions.
> 
> Signed-off-by: David Aguilar <davvid@gmail.com>

Acked-by: Johan Herland <johan@herland.net>

-- 
Johan Herland, <johan@herland.net>
www.herland.net

```

## Junio C Hamano, 2010-03-29 04:08

Subject: Re: [PATCH] Makefile: Remove usage of deprecated Python "has_key" method
Message-ID: <7v8w9bkbwo.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v8w9bkbwo.fsf%40alter.siamese.dyndns.org
In-Reply-To: <20100328215442.GA31195@gmail.com>

```
David Aguilar <davvid@gmail.com> writes:

> On Sun, Mar 28, 2010 at 09:38:54AM -0700, Junio C Hamano wrote:
>> David Aguilar <davvid@gmail.com> writes:
>> 
>> > "has_key" is a deprecated dictionary method in Python 2.6+.
>> > Simplify the sys.path manipulation for installed scripts by
>> > passing a default value to os.getenv().
>> 
>> It looks like the old code was replacing sys.path[0] but you are
>> prepending this.  Doesn't that change also make a difference?
>
> The original code replaced sys.path[0] which is ''
> (aka the current directory).  It's a little odd to
> rely on the 0th element being something that is safe
> to remove.
>
> By prepending the path we have the same intended effect without
> having to know that the 0th element is something that is
> safe to remove.
>
> Does removing '' break relative imports? (It might...)

That is exactly why I asked if it was an improvement that was not
described in the log message.

I'll queue with Johan's Ack, perhaps rewording the message a bit.
Thanks.

```
