Patchwork [3,of,4] demandimport: look for 'mod' suffix as alternative name for module reference

login
register
mail settings
Submitter Yuya Nishihara
Date May 24, 2017, 3:38 p.m.
Message ID <c96f6277a21b5a37f4cc.1495640316@mimosa>
Download mbox | patch
Permalink /patch/20882/
State Accepted
Headers show

Comments

Yuya Nishihara - May 24, 2017, 3:38 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1493390776 -32400
#      Fri Apr 28 23:46:16 2017 +0900
# Node ID c96f6277a21b5a37f4cc8e4f37b89f79aa77e423
# Parent  d9a46f311039ea73791d530d7dae8d05803decaf
demandimport: look for 'mod' suffix as alternative name for module reference

It's widely used in our codebase.

Patch

diff --git a/hgdemandimport/demandimportpy2.py b/hgdemandimport/demandimportpy2.py
--- a/hgdemandimport/demandimportpy2.py
+++ b/hgdemandimport/demandimportpy2.py
@@ -132,8 +132,11 @@  class _demandmod(object):
                 subload(mod, x)
 
             # Replace references to this proxy instance with the actual module.
-            if locals and locals.get(head) is self:
-                locals[head] = mod
+            if locals:
+                if locals.get(head) is self:
+                    locals[head] = mod
+                elif locals.get(head + r'mod') is self:
+                    locals[head + r'mod'] = mod
 
             for modname in modrefs:
                 modref = sys.modules.get(modname, None)
diff --git a/tests/test-demandimport.py b/tests/test-demandimport.py
--- a/tests/test-demandimport.py
+++ b/tests/test-demandimport.py
@@ -52,6 +52,9 @@  print("hgweb =", f(hgweb))
 import re as fred
 print("fred =", f(fred))
 
+import re as remod
+print("remod =", f(remod))
+
 import sys as re
 print("re =", f(re))
 
@@ -59,6 +62,9 @@  print("fred =", f(fred))
 print("fred.sub =", f(fred.sub))
 print("fred =", f(fred))
 
+remod.escape  # use remod
+print("remod =", f(remod))
+
 print("re =", f(re))
 print("re.stderr =", f(re.stderr))
 print("re =", f(re))
diff --git a/tests/test-demandimport.py.out b/tests/test-demandimport.py.out
--- a/tests/test-demandimport.py.out
+++ b/tests/test-demandimport.py.out
@@ -9,10 +9,12 @@  hgweb = <unloaded module 'hgweb'>
 hgweb_mod = <unloaded module 'hgweb_mod'>
 hgweb = <module 'mercurial.hgweb' from '?'>
 fred = <unloaded module 're'>
+remod = <unloaded module 're'>
 re = <unloaded module 'sys'>
 fred = <unloaded module 're'>
 fred.sub = <function sub at 0x?>
 fred = <proxied module 're'>
+remod = <module 're' from '?'>
 re = <unloaded module 'sys'>
 re.stderr = <open file '<whatever>', mode 'w' at 0x?>
 re = <proxied module 'sys'>