Patchwork [1,of,3] demandimport: pass proper level to __import__ in Python 3

login
register
mail settings
Submitter Gregory Szorc
Date May 10, 2014, 11:39 p.m.
Message ID <c59dd2bd4a28c4ce6438.1399765195@vm-ubuntu-main.gateway.sonic.net>
Download mbox | patch
Permalink /patch/4726/
State Accepted
Commit 74be3fb1e3b8dffcd618528c3cbc489b858095dd
Headers show

Comments

Gregory Szorc - May 10, 2014, 11:39 p.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1399759045 25200
#      Sat May 10 14:57:25 2014 -0700
# Node ID c59dd2bd4a28c4ce6438f15c9cfa612bb1e74162
# Parent  bcddddcf0b540b1d98f0dc1f1a1bef9337e2e567
demandimport: pass proper level to __import__ in Python 3

demandimport was failing in Python 3 with a ValueError because
__import__'s level=-1 has gone away (-1 means to try both relative
and absolute imports and relative imports don't exist in Python 3).

With this patch, demandimport still doesn't work in Python 3 (it
fails when importing a non-package module).
Pierre-Yves David - May 11, 2014, 4:10 a.m.
Ok, new (softer) rule you need to learn about: Avoids having too many in 
flight.

You already have 3 series currently in review. sending more series (even 
unrelated) are not going to help.

There is two actions you can take to help here:

1. applying feedback you received on your series (or replying to 
question asked)
2. Doing some review yourself.
Pierre-Yves David - May 11, 2014, 4:14 a.m.
On 05/10/2014 09:10 PM, Pierre-Yves David wrote:
> Ok, new (softer) rule you need to learn about: Avoids having too many in
> flight.

I've update the Flow Control section of Contribution changes to reflect 
this.

Patch

diff --git a/mercurial/demandimport.py b/mercurial/demandimport.py
--- a/mercurial/demandimport.py
+++ b/mercurial/demandimport.py
@@ -23,15 +23,19 @@  These imports will not be delayed:
   from a import *
   b = __import__(a)
 '''
 
-import __builtin__, os
+import __builtin__, os, sys
 _origimport = __import__
 
 nothing = object()
 
 try:
-    _origimport(__builtin__.__name__, {}, {}, None, -1)
+    # Python 3 doesn't have relative imports nor level -1.
+    level = -1
+    if sys.version_info[0] >= 3:
+        level = 0
+    _origimport(__builtin__.__name__, {}, {}, None, level)
 except TypeError: # no level argument
     def _import(name, globals, locals, fromlist, level):
         "call _origimport with no level argument"
         return _origimport(name, globals, locals, fromlist)
@@ -54,9 +58,9 @@  def _hgextimport(importfunc, name, globa
         return importfunc(hgextname, globals, *args)
 
 class _demandmod(object):
     """module demand-loader and proxy"""
-    def __init__(self, name, globals, locals, level=-1):
+    def __init__(self, name, globals, locals, level=level):
         if '.' in name:
             head, rest = name.split('.', 1)
             after = [rest]
         else:
@@ -104,9 +108,9 @@  class _demandmod(object):
     def __setattr__(self, attr, val):
         self._load()
         setattr(self._module, attr, val)
 
-def _demandimport(name, globals=None, locals=None, fromlist=None, level=-1):
+def _demandimport(name, globals=None, locals=None, fromlist=None, level=level):
     if not locals or name in ignore or fromlist == ('*',):
         # these cases we can't really delay
         return _hgextimport(_import, name, globals, locals, fromlist, level)
     elif not fromlist: