[pypy-svn] r8598 - pypy/dist/pypy/interpreter
ludal at codespeak.net
ludal at codespeak.net
Wed Jan 26 12:41:30 CET 2005
Author: ludal
Date: Wed Jan 26 12:41:30 2005
New Revision: 8598
Modified:
pypy/dist/pypy/interpreter/function.py
pypy/dist/pypy/interpreter/typedef.py
Log:
* added __module__ attribute to function objects
* functions' __dict__ attribute only accepts dict instances
* modules' __dict__ attribute is readonly
Modified: pypy/dist/pypy/interpreter/function.py
==============================================================================
--- pypy/dist/pypy/interpreter/function.py (original)
+++ pypy/dist/pypy/interpreter/function.py Wed Jan 26 12:41:30 2005
@@ -24,6 +24,7 @@
self.closure = closure # normally, list of Cell instances or None
self.defs_w = defs_w # list of w_default's
self.w_func_dict = space.newdict([])
+ self.w_module = None
def __repr__(self):
# return "function %s.%s" % (self.space, self.name)
@@ -41,6 +42,9 @@
return self.w_func_dict
def setdict(self, w_dict):
+ space = self.space
+ if not space.is_true(space.isinstance( w_dict, space.w_dict )):
+ raise OperationError( space.w_TypeError, space.wrap("setting function's dictionary to a non-dict") )
self.w_func_dict = w_dict
def descr_function_get(self, w_obj, w_cls=None):
@@ -80,6 +84,23 @@
self = space.interpclass_w(w_self)
self.w_doc = space.w_None
+ def fget___module__(space, w_self):
+ self = space.interpclass_w(w_self)
+ if self.w_module is None:
+ if self.w_func_globals is not None and not space.is_w(self.w_func_globals, space.w_None):
+ self.w_module = space.call_method( self.w_func_globals, "get", space.wrap("__name__") )
+ else:
+ self.w_module = space.w_None
+ return self.w_module
+
+ def fset___module__(space, w_self, w_module ):
+ self = space.interpclass_w(w_self)
+ self.w_module = space.w_module
+
+ def fdel___module__(space, w_self, w_module ):
+ self = space.interpclass_w(w_self)
+ self.w_module = space.w_None
+
class Method(Wrappable):
"""A method is a function bound to a specific instance or class."""
Modified: pypy/dist/pypy/interpreter/typedef.py
==============================================================================
--- pypy/dist/pypy/interpreter/typedef.py (original)
+++ pypy/dist/pypy/interpreter/typedef.py Wed Jan 26 12:41:30 2005
@@ -218,7 +218,7 @@
Module.typedef = TypeDef("module",
__new__ = interp2app(Module.descr_module__new__.im_func),
__init__ = interp2app(Module.descr_module__init__.im_func),
- __dict__ = interp_dict_descr,
+ __dict__ = GetSetProperty(descr_get_dict), # module dictionaries are readonly attributes
__getattr__ = interp2app(Module.descr_module__getattr__.im_func),
)
@@ -226,6 +226,15 @@
Function.fset_func_doc,
Function.fdel_func_doc)
+# __module__ attribute lazily gets its value from the w_globals
+# at the time of first invocation. This is not 100% compatible but
+# avoid problems at the time we construct the first functions when
+# it's not really possible to do a get or getitem on dictionaries
+# (mostly because wrapped exceptions don't exist at that time)
+getset___module__ = GetSetProperty(Function.fget___module__,
+ Function.fset___module__,
+ Function.fdel___module__)
+
Function.typedef = TypeDef("function",
__call__ = interp2app(Function.descr_function_call.im_func),
__get__ = interp2app(Function.descr_function_get.im_func),
@@ -237,7 +246,8 @@
func_globals = interp_attrproperty_w('w_func_globals'),
__doc__ = getset_func_doc,
__name__ = interp_attrproperty('name'),
- __dict__ = interp_dict_descr,
+ __dict__ = GetSetProperty(descr_get_dict, descr_set_dict),
+ __module__ = getset___module__,
# XXX func_closure, etc.pp
)
More information about the Pypy-commit
mailing list