Author: Armin Rigo <[email protected]>
Branch: py3.5
Changeset: r89513:47871510f0e3
Date: 2017-01-12 15:45 +0100
http://bitbucket.org/pypy/pypy/changeset/47871510f0e3/
Log: Delay adding __package__, __loader__ and __spec__ in a new module's
dict to the actual call of __init__()
diff --git a/pypy/interpreter/module.py b/pypy/interpreter/module.py
--- a/pypy/interpreter/module.py
+++ b/pypy/interpreter/module.py
@@ -14,7 +14,7 @@
_frozen = False
- def __init__(self, space, w_name, w_dict=None, add_package=True):
+ def __init__(self, space, w_name, w_dict=None):
self.space = space
if w_dict is None:
w_dict = space.newdict(module=True)
@@ -22,9 +22,6 @@
self.w_name = w_name
if w_name is not None:
space.setitem(w_dict, space.new_interned_str('__name__'), w_name)
- # 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):
@@ -76,7 +73,7 @@
def descr_module__new__(space, w_subtype, __args__):
module = space.allocate_instance(Module, w_subtype)
- Module.__init__(module, space, None, add_package=False)
+ Module.__init__(module, space, None)
return space.wrap(module)
def descr_module__init__(self, w_name, w_doc=None):
@@ -84,8 +81,10 @@
self.w_name = w_name
if w_doc is None:
w_doc = space.w_None
- space.setitem(self.w_dict, space.new_interned_str('__name__'), w_name)
- space.setitem(self.w_dict, space.new_interned_str('__doc__'), w_doc)
+ w_dict = self.w_dict
+ space.setitem(w_dict, space.new_interned_str('__name__'), w_name)
+ space.setitem(w_dict, space.new_interned_str('__doc__'), w_doc)
+ init_extra_module_attrs(space, space.wrap(self))
def descr__reduce__(self, space):
w_name = space.finditem(self.w_dict, space.wrap('__name__'))
@@ -143,3 +142,12 @@
raise oefmt(space.w_TypeError, "%N.__dict__ is not a dictionary",
self)
return space.call_function(space.w_list, w_dict)
+
+
+def init_extra_module_attrs(space, w_mod):
+ w_dict = w_mod.getdict(space)
+ if w_dict is None:
+ return
+ for extra in ['__package__', '__loader__', '__spec__']:
+ w_attr = space.new_interned_str(extra)
+ space.call_method(w_dict, 'setdefault', w_attr, space.w_None)
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
@@ -215,3 +215,8 @@
'__package__': None,
'__loader__': None,
'__spec__': None}
+
+ def test_module_new_makes_empty_dict(self):
+ import sys
+ m = type(sys).__new__(type(sys))
+ assert not m.__dict__
diff --git a/pypy/module/_pickle_support/maker.py
b/pypy/module/_pickle_support/maker.py
--- a/pypy/module/_pickle_support/maker.py
+++ b/pypy/module/_pickle_support/maker.py
@@ -27,7 +27,7 @@
return space.wrap(fu)
def module_new(space, w_name, w_dict):
- new_mod = Module(space, w_name, w_dict, add_package=False)
+ new_mod = Module(space, w_name, w_dict)
return space.wrap(new_mod)
def method_new(space, __args__):
diff --git a/pypy/module/imp/importing.py b/pypy/module/imp/importing.py
--- a/pypy/module/imp/importing.py
+++ b/pypy/module/imp/importing.py
@@ -4,7 +4,7 @@
import sys, os, stat
-from pypy.interpreter.module import Module
+from pypy.interpreter.module import Module, init_extra_module_attrs
from pypy.interpreter.gateway import interp2app, unwrap_spec
from pypy.interpreter.typedef import TypeDef, generic_new_descr
from pypy.interpreter.error import OperationError, oefmt
@@ -113,11 +113,13 @@
space.setattr(w_mod, w('__doc__'), space.w_None)
if pkgdir is not None:
space.setattr(w_mod, w('__path__'), space.newlist([w(pkgdir)]))
+ init_extra_module_attrs(space, w_mod)
def add_module(space, w_name):
w_mod = check_sys_modules(space, w_name)
if w_mod is None:
w_mod = space.wrap(Module(space, w_name))
+ init_extra_module_attrs(space, w_mod)
space.sys.setmodule(w_mod)
return w_mod
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit