[Python-checkins] r53981 - in python/trunk: Lib/test/test_compile.py Python/compile.c

jeremy.hylton python-checkins at python.org
Tue Feb 27 02:02:00 CET 2007


Author: jeremy.hylton
Date: Tue Feb 27 02:01:59 2007
New Revision: 53981

Modified:
   python/trunk/Lib/test/test_compile.py
   python/trunk/Python/compile.c
Log:
Fix long-standing bug in name mangling for package imports

Reported by Mike Verdone.


Modified: python/trunk/Lib/test/test_compile.py
==============================================================================
--- python/trunk/Lib/test/test_compile.py	(original)
+++ python/trunk/Lib/test/test_compile.py	Tue Feb 27 02:01:59 2007
@@ -394,6 +394,19 @@
         del d[..., ...]
         self.assertEqual((Ellipsis, Ellipsis) in d, False)
 
+    def test_mangling(self):
+        class A:
+            def f():
+                __mangled = 1
+                __not_mangled__ = 2
+                import __mangled_mod
+                import __package__.module
+
+        self.assert_("_A__mangled" in A.f.func_code.co_varnames)
+        self.assert_("__not_mangled__" in A.f.func_code.co_varnames)
+        self.assert_("_A__mangled_mod" in A.f.func_code.co_varnames)
+        self.assert_("__package__" in A.f.func_code.co_varnames)
+
 def test_main():
     test_support.run_unittest(TestSpecifics)
 

Modified: python/trunk/Python/compile.c
==============================================================================
--- python/trunk/Python/compile.c	(original)
+++ python/trunk/Python/compile.c	Tue Feb 27 02:01:59 2007
@@ -194,7 +194,17 @@
 	}
 	p = PyString_AsString(privateobj);
 	nlen = strlen(name);
-	if (name[nlen-1] == '_' && name[nlen-2] == '_') {
+        /* Don't mangle __id__ or names with dots.
+
+           The only time a name with a dot can occur is when
+           we are compiling an import statement that has a 
+           package name.
+
+           TODO(jhylton): Decide whether we want to support
+           mangling of the module name, e.g. __M.X.
+        */
+	if ((name[nlen-1] == '_' && name[nlen-2] == '_') 
+            || strchr(name, '.')) {
 		Py_INCREF(ident);
 		return ident; /* Don't mangle __whatever__ */
 	}
@@ -2243,7 +2253,7 @@
 		return compiler_error(c, "can not assign to __debug__");
 	}
 
-	mangled = _Py_Mangle(c->u->u_private, name);
+mangled = _Py_Mangle(c->u->u_private, name);
 	if (!mangled)
 		return 0;
 


More information about the Python-checkins mailing list