[pypy-svn] r22069 - in pypy/branch/arre-experiments/pypy: annotation interpreter rpython translator/tool

ac at codespeak.net ac at codespeak.net
Thu Jan 12 19:54:25 CET 2006


Author: ac
Date: Thu Jan 12 19:54:24 2006
New Revision: 22069

Modified:
   pypy/branch/arre-experiments/pypy/annotation/bookkeeper.py
   pypy/branch/arre-experiments/pypy/annotation/description.py
   pypy/branch/arre-experiments/pypy/interpreter/argument.py
   pypy/branch/arre-experiments/pypy/rpython/callparse.py
   pypy/branch/arre-experiments/pypy/translator/tool/cbuild.py
Log:
Slight refactoring.

Modified: pypy/branch/arre-experiments/pypy/annotation/bookkeeper.py
==============================================================================
--- pypy/branch/arre-experiments/pypy/annotation/bookkeeper.py	(original)
+++ pypy/branch/arre-experiments/pypy/annotation/bookkeeper.py	Thu Jan 12 19:54:24 2006
@@ -632,6 +632,7 @@
     For the Arguments class: if it really needs other operations, it means
     that the call pattern is too complex for R-Python.
     """
+    w_tuple = SomeTuple
     def newtuple(self, items_s):
         return SomeTuple(items_s)
 
@@ -646,6 +647,13 @@
             return s_obj.items
         raise CallPatternTooComplex, "'*' argument must be SomeTuple"
 
+    def is_w(self, one, other):
+        return one is other
+
+    def type(self, item):
+        return type(item)
+    
+
 class CallPatternTooComplex(Exception):
     pass
 

Modified: pypy/branch/arre-experiments/pypy/annotation/description.py
==============================================================================
--- pypy/branch/arre-experiments/pypy/annotation/description.py	(original)
+++ pypy/branch/arre-experiments/pypy/annotation/description.py	Thu Jan 12 19:54:24 2006
@@ -202,7 +202,7 @@
         try:
             inputcells = args.match_signature(signature, defs_s)
         except ArgErr, e:
-            raise TypeError, "signature mismatch: %s" % e.getmsg(args, self.name)
+            raise TypeError, "signature mismatch: %s" % e.getmsg(self.name)
         return inputcells
 
     def specialize(self, inputcells):

Modified: pypy/branch/arre-experiments/pypy/interpreter/argument.py
==============================================================================
--- pypy/branch/arre-experiments/pypy/interpreter/argument.py	(original)
+++ pypy/branch/arre-experiments/pypy/interpreter/argument.py	Thu Jan 12 19:54:24 2006
@@ -35,7 +35,6 @@
         "Return a new Arguments with a new argument inserted first."
         return ArgumentsPrepended(self, w_firstarg)
     
-
 class ArgumentsPrepended(AbstractArguments):
     def __init__(self, args, w_firstarg):
         self.args = args
@@ -63,29 +62,30 @@
     def _rawshape(self, nextra=0):
         return self.args._rawshape(nextra + 1)
 
-    def _unpack_stararg(self):
-        self.args._unpack_stararg()
-        
     def parse(self, fnname, signature, defaults_w=[]):
         """Parse args and kwargs to initialize a frame
         according to the signature of code object.
         """
-        self._unpack_stararg()
         try:
             return self.match_signature(signature, defaults_w)
         except ArgErr, e:
             raise OperationError(self.args.space.w_TypeError,
-                                 self.args.space.wrap(e.getmsg(self, fnname)))
+                                 self.args.space.wrap(e.getmsg(fnname)))
 
     def match_signature(self, signature, defaults_w=[]):
         """Parse args and kwargs according to the signature of a code object,
         or raise an ArgErr in case of failure.
         """
         argnames, varargname, kwargname = signature
-        scope_w = self.args.match_signature((argnames[1:], varargname, kwargname), defaults_w)
+        try:
+            scope_w = self.args.match_signature((argnames[1:], varargname, kwargname), defaults_w)
+        except ArgErrCount, e:
+            e.num_args += 1 # Add our first arg
+            raise
+        
         if len(argnames) == 0:
             if varargname is None:
-                raise ArgErrCount(signature, defaults_w, 0)
+                raise ArgErrCount(self, signature, defaults_w, 0)
             space = self.args.space
             if kwargname is not None:
                 scope_w[-2] = space.newtuple([self.w_firstarg] + space.unpackiterable(scope_w[-2]))
@@ -100,7 +100,12 @@
         data_w.insert(0, self.w_firstarg)
         return (shape_cnt + 1, shape_keys, shape_star, shape_stst), data_w
 
-        
+    def num_args(self):
+        return self.args.num_args() + 1
+
+    def num_kwds(self):
+        return self.args.num_kwds()
+    
 class Arguments(AbstractArguments):
     """
     Collects the arguments of a function call.
@@ -120,6 +125,14 @@
         self.w_stararg = w_stararg
         self.w_starstararg = w_starstararg
 
+    def num_args(self):
+        self._unpack()
+        return len(self.arguments_w)
+
+    def num_kwds(self):
+        self._unpack()
+        return len(self.kwds_w)
+        
     def __repr__(self):
         if self.w_starstararg is not None:
             return 'Arguments(%s, %s, %s, %s)' % (self.arguments_w,
@@ -140,6 +153,11 @@
 
     def unpack(self):
         "Return a ([w1,w2...], {'kw':w3...}) pair."
+        self._unpack()
+        return self.arguments_w, self.kwds_w
+
+    def _unpack(self):
+        "unpack the *arg and **kwd into w_arguments and kwds_w"
         # --- unpack the * argument now ---
         if self.w_stararg is not None:
             self.arguments_w += self.space.unpackiterable(self.w_stararg)
@@ -172,7 +190,6 @@
                 d[key] = space.getitem(w_starstararg, w_key)
             self.kwds_w = d
             self.w_starstararg = None
-        return self.arguments_w, self.kwds_w
 
     def has_keywords(self):
         return bool(self.kwds_w) or (self.w_starstararg is not None and
@@ -209,25 +226,16 @@
 
     ###  Parsing for function calls  ###
 
-    def _unpack_stararg(self):
-        space = self.space
-        # If w_stararg is not exactly a tuple, unpack it now:
-        # self.match_signature() assumes that it can use it directly for
-        # a matching *arg in the callee's signature.
-        if self.w_stararg is not None:
-            if not space.is_w(space.type(self.w_stararg), space.w_tuple):
-                self.unpack()
-
     def parse(self, fnname, signature, defaults_w=[]):
         """Parse args and kwargs to initialize a frame
         according to the signature of code object.
         """
-        self._unpack_stararg()
         try:
             return self.match_signature(signature, defaults_w)
         except ArgErr, e:
             raise OperationError(self.space.w_TypeError,
-                                 self.space.wrap(e.getmsg(self, fnname)))
+                                 self.space.wrap(e.getmsg(fnname)))
+
 
     def match_signature(self, signature, defaults_w=[]):
         """Parse args and kwargs according to the signature of a code object,
@@ -244,13 +252,16 @@
         if self.w_stararg is not None:
             # There is a case where we don't have to unpack() a w_stararg:
             # if it matches exactly a *arg in the signature.
-            if len(self.arguments_w) == co_argcount and varargname is not None:
+            if (len(self.arguments_w) == co_argcount and
+                varargname is not None and
+                self.space.is_w(self.space.type(self.w_stararg),
+                                self.space.w_tuple)):
                 pass
             else:
-                self.unpack()   # sets self.w_stararg to None
+                self._unpack()   # sets self.w_stararg to None
         # always unpack the ** arguments
         if self.w_starstararg is not None:
-            self.unpack()
+            self._unpack()
 
         args_w = self.arguments_w
         kwds_w = self.kwds_w
@@ -301,7 +312,7 @@
             else:      # shortcut for the non-unpack() case above
                 scope_w.append(self.w_stararg)
         elif len(args_w) > co_argcount:
-            raise ArgErrCount(signature, defaults_w, 0)
+            raise ArgErrCount(self, signature, defaults_w, 0)
 
         # collect extra keyword arguments into the **kwarg
         if kwargname is not None:
@@ -314,7 +325,7 @@
             raise ArgErrUnknownKwds(remainingkwds_w)
 
         if missing:
-            raise ArgErrCount(signature, defaults_w, missing)
+            raise ArgErrCount(self, signature, defaults_w, missing)
         return scope_w
 
     ### Argument <-> list of w_objects together with "shape" information
@@ -350,29 +361,32 @@
 
 class ArgErr(Exception):
     
-    def getmsg(self, args, fnname):
+    def getmsg(self, fnname):
         raise NotImplementedError
 
 class ArgErrCount(ArgErr):
 
-    def __init__(self, signature, defaults_w, missing_args):
+    def __init__(self, args, signature, defaults_w, missing_args):
         self.signature    = signature
         self.defaults_w   = defaults_w
         self.missing_args = missing_args
-
-    def getmsg(self, args, fnname):
+        self.num_args = args.num_args()
+        self.num_kwds = args.num_kwds()
+        
+    def getmsg(self, fnname):
+        args = None
         argnames, varargname, kwargname = self.signature
-        args_w, kwds_w = args.unpack()
-        if kwargname is not None or (kwds_w and self.defaults_w):
+        #args_w, kwds_w = args.unpack()
+        if kwargname is not None or (self.num_kwds and self.defaults_w):
             msg2 = "non-keyword "
             if self.missing_args:
                 required_args = len(argnames) - len(self.defaults_w)
                 nargs = required_args - self.missing_args
             else:
-                nargs = len(args_w)
+                nargs = self.num_args
         else:
             msg2 = ""
-            nargs = len(args_w) + len(kwds_w)
+            nargs = self.num_args + self.num_kwds
         n = len(argnames)
         if n == 0:
             msg = "%s() takes no %sargument (%d given)" % (
@@ -388,7 +402,7 @@
             else:
                 msg1 = "at least"
                 n -= defcount
-                if not kwds_w:  # msg "f() takes at least X non-keyword args"
+                if not self.num_kwds:  # msg "f() takes at least X non-keyword args"
                     msg2 = ""   # is confusing if no kwd arg actually provided
             if n == 1:
                 plural = ""
@@ -408,7 +422,7 @@
     def __init__(self, argname):
         self.argname = argname
 
-    def getmsg(self, args, fnname):
+    def getmsg(self, fnname):
         msg = "%s() got multiple values for keyword argument '%s'" % (
             fnname,
             self.argname)
@@ -417,16 +431,18 @@
 class ArgErrUnknownKwds(ArgErr):
 
     def __init__(self, kwds_w):
-        self.kwds_w = kwds_w
+        self.kwd_name = ''
+        self.num_kwds = len(kwds_w)
+        if self.num_kwds == 1:
+            self.kwd_name = kwds_w.keys()[0]
 
-    def getmsg(self, args, fnname):
-        kwds_w = self.kwds_w
-        if len(kwds_w) == 1:
+    def getmsg(self, fnname):
+        if self.num_kwds == 1:
             msg = "%s() got an unexpected keyword argument '%s'" % (
                 fnname,
-                kwds_w.keys()[0])
+                self.kwd_name)
         else:
             msg = "%s() got %d unexpected keyword arguments" % (
                 fnname,
-                len(kwds_w))
+                self.num_kwds)
         return msg

Modified: pypy/branch/arre-experiments/pypy/rpython/callparse.py
==============================================================================
--- pypy/branch/arre-experiments/pypy/rpython/callparse.py	(original)
+++ pypy/branch/arre-experiments/pypy/rpython/callparse.py	Thu Jan 12 19:54:24 2006
@@ -8,28 +8,6 @@
     pass
 
 
-# for parsing call arguments
-class RPythonCallsSpace:
-    """Pseudo Object Space providing almost no real operation.
-    For the Arguments class: if it really needs other operations, it means
-    that the call pattern is too complex for R-Python.
-    """
-    def newtuple(self, items):
-        return NewTupleHolder(items)
-
-    def newdict(self, stuff):
-        raise CallPatternTooComplex, "'**' argument"
-
-    def unpackiterable(self, it, expected_length=None):
-        if it.is_tuple():
-            items = it.items()
-            if (expected_length is not None and
-                expected_length != len(items)):
-                raise ValueError
-            return items
-        raise CallPatternTooComplex, "'*' argument must be a tuple"
-
-
 def getrinputs(rtyper, graph):
     """Return the list of reprs of the input arguments to the 'graph'."""
     return [rtyper.bindingrepr(v) for v in graph.getargs()]
@@ -69,7 +47,7 @@
     try:
         holders = arguments.match_signature(signature, defs_h)
     except ArgErr, e:
-        raise TyperError, "signature mismatch: %s" % e.getmsg(arguments, graph.name)
+        raise TyperError, "signature mismatch: %s" % e.getmsg(graph.name)
 
     assert len(holders) == len(rinputs), "argument parsing mismatch"
     vlist = []
@@ -171,3 +149,32 @@
         r_tup, v_tuple = self.holder.access(hop)
         v = r_tup.getitem(hop, v_tuple, index)
         return hop.llops.convertvar(v, r_tup.items_r[index], repr)
+
+# for parsing call arguments
+class RPythonCallsSpace:
+    """Pseudo Object Space providing almost no real operation.
+    For the Arguments class: if it really needs other operations, it means
+    that the call pattern is too complex for R-Python.
+    """
+    w_tuple = NewTupleHolder
+    def newtuple(self, items):
+        return NewTupleHolder(items)
+
+    def newdict(self, stuff):
+        raise CallPatternTooComplex, "'**' argument"
+
+    def unpackiterable(self, it, expected_length=None):
+        if it.is_tuple():
+            items = it.items()
+            if (expected_length is not None and
+                expected_length != len(items)):
+                raise ValueError
+            return items
+        raise CallPatternTooComplex, "'*' argument must be a tuple"
+
+    def is_w(self, one, other):
+        return one is other
+
+    def type(self, item):
+        return type(item)
+    

Modified: pypy/branch/arre-experiments/pypy/translator/tool/cbuild.py
==============================================================================
--- pypy/branch/arre-experiments/pypy/translator/tool/cbuild.py	(original)
+++ pypy/branch/arre-experiments/pypy/translator/tool/cbuild.py	Thu Jan 12 19:54:24 2006
@@ -271,14 +271,15 @@
                 self.libraries.append('m')
             if 'pthread' not in self.libraries:
                 self.libraries.append('pthread')
-            self.compile_extra += ['-O2', '-pthread']
-            self.link_extra += ['-pthread']
+            self.compile_extra += ['-g', '-O2', '-pthread']
+            self.link_extra += ['-g', '-pthread']
         if sys.platform == 'darwin':
             if '/sw/include' not in self.include_dirs:
                 self.include_dirs.append('/sw/include')
             if '/sw/lib' not in self.library_dirs:
                 self.library_dirs.append('/sw/lib')
-            self.compile_extra += ['-O2']
+            self.compile_extra += ['-g', '-O2']
+            self.link_extra += ['-g']
 
         if outputfilename is None:
             self.outputfilename = py.path.local(cfilenames[0]).new(ext=ext)



More information about the Pypy-commit mailing list