[pypy-svn] r11843 - pypy/dist/pypy/interpreter

pedronis at codespeak.net pedronis at codespeak.net
Tue May 3 13:31:19 CEST 2005


Author: pedronis
Date: Tue May  3 13:31:18 2005
New Revision: 11843

Modified:
   pypy/dist/pypy/interpreter/function.py
   pypy/dist/pypy/interpreter/gateway.py
   pypy/dist/pypy/interpreter/typedef.py
Log:
changes to pass test_new: implementd function.__new__



Modified: pypy/dist/pypy/interpreter/function.py
==============================================================================
--- pypy/dist/pypy/interpreter/function.py	(original)
+++ pypy/dist/pypy/interpreter/function.py	Tue May  3 13:31:18 2005
@@ -10,6 +10,7 @@
 from pypy.interpreter.baseobjspace import Wrappable
 from pypy.interpreter.argument import Arguments
 from pypy.interpreter.eval import Code
+from pypy.interpreter.gateway import NoneNotWrapped
 
 class Function(Wrappable):
     """A function is a code object captured with some environment:
@@ -50,6 +51,43 @@
 
     # unwrapping is done through unwrap_specs in typedef.py
 
+    def descr_method__new__(space, w_subtype, w_code, w_globals, w_name=None, w_argdefs=None, w_closure=NoneNotWrapped):
+        code = space.interpclass_w(w_code)
+        if code is None or not isinstance(code, Code):
+            raise OperationError(space.w_TypeError, space.wrap("expected code"))
+        if not space.is_true(space.isinstance(w_globals, space.w_dict)):
+            raise OperationError(space.w_TypeError, space.wrap("expected dict"))
+        if not space.is_w(w_name, space.w_None):
+            name = space.str_w(w_name)
+        else:
+            name = None
+        if not space.is_w(w_argdefs, space.w_None):
+            defs_w = space.unpackiterable(w_argdefs)
+        else:
+            defs_w = []
+        if w_closure is None:
+            closure = None
+        elif not space.is_w(space.type(w_closure), space.w_tuple):
+            raise OperationError(space.w_TypeError, space.wrap("invalid closure"))
+        else:
+            from pypy.interpreter.pycode import PyCode
+            from pypy.interpreter.nestedscope import Cell
+            closure_w = space.unpackiterable(w_closure)
+            n = len(closure_w)
+            if not isinstance(code, PyCode) or len(code.co_freevars) == 0:
+                raise OperationError(space.w_ValueError, space.wrap("no closure needed"))
+            elif len(code.co_freevars) != n:
+                raise OperationError(space.w_ValueError, space.wrap("closure is wrong size"))                
+            closure = []
+            for w_cell in closure_w:
+                cell = space.interpclass_w(w_cell)
+                if not isinstance(cell, Cell):
+                    raise OperationError(space.w_TypeError, space.wrap("non-cell in closure"))
+                closure.append(cell)
+        func = space.allocate_instance(Function, w_subtype)
+        func.__init__(space, code, w_globals, defs_w, closure, name)
+        return space.wrap(func)
+
     def descr_function_get(self, w_obj, w_cls=None):
         space = self.space
         wrap = space.wrap

Modified: pypy/dist/pypy/interpreter/gateway.py
==============================================================================
--- pypy/dist/pypy/interpreter/gateway.py	(original)
+++ pypy/dist/pypy/interpreter/gateway.py	Tue May  3 13:31:18 2005
@@ -9,6 +9,8 @@
 
 import types, sys, md5, os
 
+NoneNotWrapped = object()
+
 from pypy.tool import hack
 from pypy.interpreter.error import OperationError 
 from pypy.interpreter import eval
@@ -22,8 +24,6 @@
 # internal non-translatable parts: 
 from pypy.tool.getpy import py  # XXX from interpreter/ we get py.py 
 
-NoneNotWrapped = object()
-
 class Signature:
     "NOT_RPYTHON"
     def __init__(self, func=None, argnames=None, varargname=None,

Modified: pypy/dist/pypy/interpreter/typedef.py
==============================================================================
--- pypy/dist/pypy/interpreter/typedef.py	(original)
+++ pypy/dist/pypy/interpreter/typedef.py	Tue May  3 13:31:18 2005
@@ -391,6 +391,7 @@
 getset_func_dict = GetSetProperty(descr_get_dict, descr_set_dict, cls=Function)
 
 Function.typedef = TypeDef("function",
+    __new__ = interp2app(Function.descr_method__new__.im_func),                           
     __call__ = interp2app(Function.descr_function_call,
                           unwrap_spec=['self', Arguments]),
     __get__ = interp2app(Function.descr_function_get),



More information about the Pypy-commit mailing list