[pypy-commit] pypy py3.5: Py3.5 always adds these three names to all new modules (thanks Tiberium)

arigo pypy.commits at gmail.com
Sun Dec 4 17:30:58 EST 2016


Author: Armin Rigo <arigo at tunes.org>
Branch: py3.5
Changeset: r88888:5e500ed90cd8
Date: 2016-12-04 23:30 +0100
http://bitbucket.org/pypy/pypy/changeset/5e500ed90cd8/

Log:	Py3.5 always adds these three names to all new modules (thanks
	Tiberium)

diff --git a/pypy/interpreter/module.py b/pypy/interpreter/module.py
--- a/pypy/interpreter/module.py
+++ b/pypy/interpreter/module.py
@@ -22,11 +22,9 @@
         self.w_name = w_name
         if w_name is not None:
             space.setitem(w_dict, space.new_interned_str('__name__'), w_name)
-        if add_package:
-            # add the __package__ attribute only when created from internal
-            # code, but not when created from Python code (as in CPython)
-            space.setitem(w_dict, space.new_interned_str('__package__'),
-                          space.w_None)
+        # add these three attributes always ('add_package' is no longer used)
+        for extra in ['__package__', '__loader__', '__spec__']:
+            space.setitem(w_dict, space.new_interned_str(extra), space.w_None)
         self.startup_called = False
 
     def _cleanup_(self):
diff --git a/pypy/interpreter/test/test_module.py b/pypy/interpreter/test/test_module.py
--- a/pypy/interpreter/test/test_module.py
+++ b/pypy/interpreter/test/test_module.py
@@ -181,7 +181,7 @@
 
         assert sys.__package__ == ''
         assert os.__package__ == ''
-        assert not hasattr(type(sys)('foo'), '__package__')
+        assert type(sys)('foo').__package__ is None
 
     def test_name_nonascii(self):
         import sys
@@ -206,3 +206,12 @@
     def test_weakrefable(self):
         import weakref
         weakref.ref(weakref)
+
+    def test_all_dict_content(self):
+        import sys
+        m = type(sys)('foo')
+        assert m.__dict__ == {'__name__': 'foo',
+                              '__doc__': None,
+                              '__package__': None,
+                              '__loader__': None,
+                              '__spec__': None}


More information about the pypy-commit mailing list