Skip to contentSkip to conversation

Makefile: Remove usage of deprecated Python "has_key" method

PATCH In review David Aguilar 4 participants 6 messages Jun 15, 2016 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Makefile: Remove usage of deprecated Python "has_key" method

"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.

---
 Makefile |    5 ++---
 1 files changed, 2 insertions(+), 3 deletions(-)
Makefile +2 -3
index 3a6c6ea..806ccdf 100644
@@ -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' \
 	    [email protected] >$@+ && \
 	chmod +x $@+ && \
Signature · 1 line
1.7.0.3.291.g5e4f6
#1 lore raw
Quoted 3 lines · David Aguilar

"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?

Quoted 27 lines

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.

---
 Makefile |    5 ++---
 1 files changed, 2 insertions(+), 3 deletions(-)
Makefile +2 -3
index 3a6c6ea..806ccdf 100644
@@ -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' \
 	    [email protected] >$@+ && \
 	chmod +x $@+ && \
Signature · 1 line
1.7.0.3.291.g5e4f6
#2 lore raw
Quoted 8 lines · Junio C Hamano
Quoted 3 lines · David Aguilar

"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.

Signature · 1 line
		David
#3 lore raw
Quoted 15 lines · David Aguilar
Quoted 8 lines · Junio C Hamano
Quoted 3 lines · David Aguilar

"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.

Quoted 5 lines

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.

#4 lore raw
In reply to David Aguilar

Hi,

Quoted 7 lines · David Aguilar
Quoted 6 lines · Junio C Hamano
Quoted 3 lines · David Aguilar

"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().

Quoted 7 lines
Quoted 2 lines

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

Signature · 2 lines
Cheers,
Ray Chuan
Acked-by on v1 1/1
#5 lore raw
In reply to David Aguilar
Quoted 9 lines · David Aguilar

"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.

Signature · 2 lines
Johan Herland, <[email protected]>
www.herland.net