[pypy-svn] r28729 - in pypy/dist/pypy: interpreter objspace/cpy objspace/cpy/test rpython/rctypes/tool translator/c

arigo at codespeak.net arigo at codespeak.net
Mon Jun 12 23:09:16 CEST 2006


Author: arigo
Date: Mon Jun 12 23:09:15 2006
New Revision: 28729

Modified:
   pypy/dist/pypy/interpreter/mixedmodule.py
   pypy/dist/pypy/objspace/cpy/capi.py
   pypy/dist/pypy/objspace/cpy/objspace.py
   pypy/dist/pypy/objspace/cpy/test/test_objspace.py
   pypy/dist/pypy/rpython/rctypes/tool/compilemodule.py
   pypy/dist/pypy/translator/c/node.py
Log:
Progress towards 'compilemodule _sre': pdb debugging support,
small fixes, addition of some missing space.xyz() methods,
some unicode support.


Modified: pypy/dist/pypy/interpreter/mixedmodule.py
==============================================================================
--- pypy/dist/pypy/interpreter/mixedmodule.py	(original)
+++ pypy/dist/pypy/interpreter/mixedmodule.py	Mon Jun 12 23:09:15 2006
@@ -142,7 +142,8 @@
                 if is_type:
                     return space.gettypeobject(value.typedef)
 
-                assert isinstance(value, W_Root), (
+                W_Object = getattr(space, 'W_Object', ()) # for cpyobjspace
+                assert isinstance(value, (W_Root, W_Object)), (
                     "interpleveldef %s.%s must return a wrapped object "
                     "(got %r instead)" % (pkgroot, spec, value))
                 return value 

Modified: pypy/dist/pypy/objspace/cpy/capi.py
==============================================================================
--- pypy/dist/pypy/objspace/cpy/capi.py	(original)
+++ pypy/dist/pypy/objspace/cpy/capi.py	Mon Jun 12 23:09:15 2006
@@ -28,6 +28,8 @@
     _include_dirs_ = [ctypes_platform.get_python_include_dir()]
     
     Py_ssize_t = ctypes_platform.SimpleType('Py_ssize_t')
+    Py_UNICODE = ctypes_platform.SimpleType('Py_UNICODE')
+    Py_UNICODE_WIDE = ctypes_platform.Defined('Py_UNICODE_WIDE')
 
     Py_LT = ctypes_platform.ConstantInteger('Py_LT')
     Py_LE = ctypes_platform.ConstantInteger('Py_LE')
@@ -61,6 +63,10 @@
 ###########################################################
 # ____________________ Object Protocol ____________________
 
+PyObject_Size = cpyapi.PyObject_Size
+PyObject_Size.argtypes = [W_Object]
+PyObject_Size.restype = Py_ssize_t
+
 PyObject_GetAttr = cpyapi.PyObject_GetAttr
 PyObject_GetAttr.argtypes = [W_Object, W_Object]
 PyObject_GetAttr.restype = W_Object
@@ -157,6 +163,10 @@
 PyInt_AsLong.argtypes = [W_Object]
 PyInt_AsLong.restype = c_long
 
+PyInt_AsUnsignedLongMask = cpyapi.PyInt_AsUnsignedLongMask
+PyInt_AsUnsignedLongMask.argtypes = [W_Object]
+PyInt_AsUnsignedLongMask.restype = c_ulong
+
 PyFloat_FromDouble = cpyapi.PyFloat_FromDouble
 PyFloat_FromDouble.argtypes = [c_double]
 PyFloat_FromDouble.restype = W_Object
@@ -165,6 +175,10 @@
 PyFloat_AsDouble.argtypes = [W_Object]
 PyFloat_AsDouble.restype = c_double 
 
+PyLong_FromUnsignedLong = cpyapi.PyLong_FromUnsignedLong
+PyLong_FromUnsignedLong.argtypes = [c_ulong]
+PyLong_FromUnsignedLong.restype = W_Object
+
 
 ###################################################
 # ____________________ Strings ____________________
@@ -179,7 +193,26 @@
 
 PyString_AsString = cpyapi.PyString_AsString
 PyString_AsString.argtypes = [W_Object]
-PyString_AsString.restype = c_char_p
+PyString_AsString.restype = POINTER(c_char)
+
+PyString_Size = cpyapi.PyString_Size
+PyString_Size.argtypes = [W_Object]
+PyString_Size.restype = Py_ssize_t
+
+if Py_UNICODE_WIDE: PyUnicode_AsUnicode = cpyapi.PyUnicodeUCS4_AsUnicode
+else:               PyUnicode_AsUnicode = cpyapi.PyUnicodeUCS2_AsUnicode
+PyUnicode_AsUnicode.argtypes = [W_Object]
+PyUnicode_AsUnicode.restype = POINTER(Py_UNICODE)
+
+if Py_UNICODE_WIDE: PyUnicode_FromUnicode = cpyapi.PyUnicodeUCS4_FromUnicode
+else:               PyUnicode_FromUnicode = cpyapi.PyUnicodeUCS2_FromUnicode
+PyUnicode_FromUnicode.argtypes = [POINTER(Py_UNICODE), Py_ssize_t]
+PyUnicode_FromUnicode.restype = W_Object
+
+if Py_UNICODE_WIDE: PyUnicode_FromOrdinal = cpyapi.PyUnicodeUCS4_FromOrdinal
+else:               PyUnicode_FromOrdinal = cpyapi.PyUnicodeUCS2_FromOrdinal
+PyUnicode_FromOrdinal.argtypes = [Py_UNICODE]
+PyUnicode_FromOrdinal.restype = W_Object
 
 
 ##################################################
@@ -270,7 +303,7 @@
 
 RAW_PyErr_Occurred = pythonapi.PyErr_Occurred
 RAW_PyErr_Occurred.argtypes = []
-RAW_PyErr_Occurred.restype = c_int
+RAW_PyErr_Occurred.restype = c_void_p
 RAW_PyErr_Occurred._rctypes_pyerrchecker_ = None
 
 RAW_PyErr_Fetch = pythonapi.PyErr_Fetch

Modified: pypy/dist/pypy/objspace/cpy/objspace.py
==============================================================================
--- pypy/dist/pypy/objspace/cpy/objspace.py	(original)
+++ pypy/dist/pypy/objspace/cpy/objspace.py	Mon Jun 12 23:09:15 2006
@@ -4,6 +4,7 @@
 from pypy.interpreter.error import OperationError
 from pypy.interpreter.function import Function
 from pypy.interpreter.typedef import GetSetProperty
+from pypy.rpython.rarithmetic import r_uint
 
 
 class CPyObjSpace(baseobjspace.ObjSpace):
@@ -14,6 +15,7 @@
         self.w_int   = W_Object(int)
         self.w_tuple = W_Object(tuple)
         self.w_str   = W_Object(str)
+        self.w_unicode = W_Object(unicode)
         self.w_None  = W_Object(None)
         self.w_False = W_Object(False)
         self.w_True  = W_Object(True)
@@ -51,6 +53,11 @@
             return PyString_FromStringAndSize(x, len(x))
         if isinstance(x, float): 
             return PyFloat_FromDouble(x)
+        if isinstance(x, r_uint):
+            return PyLong_FromUnsignedLong(x)
+        # if we arrive here during RTyping, then the problem is *not* the %r
+        # in the format string, but it's that someone is calling space.wrap()
+        # on a strange object.
         raise TypeError("wrap(%r)" % (x,))
     wrap._annspecialcase_ = "specialize:wrap"
 
@@ -80,8 +87,8 @@
     getitem = staticmethod(PyObject_GetItem)
     setitem = staticmethod(PyObject_SetItem)
     int_w   = staticmethod(PyInt_AsLong)
+    uint_w  = staticmethod(PyInt_AsUnsignedLongMask)
     float_w = staticmethod(PyFloat_AsDouble)
-    str_w   = staticmethod(PyString_AsString)
     iter    = staticmethod(PyObject_GetIter)
     type    = staticmethod(PyObject_Type)
     str     = staticmethod(PyObject_Str)
@@ -90,6 +97,18 @@
     add     = staticmethod(PyNumber_Add)
     sub     = staticmethod(PyNumber_Subtract)
 
+    def len(self, w_obj):
+        return self.wrap(PyObject_Size(w_obj))
+
+    def str_w(self, w_obj):
+        # XXX inefficient
+        p = PyString_AsString(w_obj)
+        length = PyString_Size(w_obj)
+        buf = create_string_buffer(length)
+        for i in range(length):
+            buf[i] = p[i]
+        return buf.raw
+
     def call_function(self, w_callable, *args_w):
         args_w += (None,)
         return PyObject_CallFunctionObjArgs(w_callable, *args_w)
@@ -106,6 +125,20 @@
         PyString_InternInPlace(byref(w_s))
         return w_s
 
+    def newstring(self, bytes_w):
+        length = len(bytes_w)
+        buf = ctypes.create_string_buffer(length)
+        for i in range(length):
+            buf[i] = chr(self.int_w(bytes_w[i]))
+        return PyString_FromStringAndSize(buf, length)
+
+    def newunicode(self, codes):
+        # XXX inefficient
+        lst = [PyUnicode_FromOrdinal(code) for code in codes]
+        w_lst = self.newlist(lst)
+        w_emptyunicode = PyUnicode_FromUnicode(None, 0)
+        return self.call_method(w_emptyunicode, 'join', w_lst)
+
     def newint(self, intval):
         return PyInt_FromLong(intval)
 
@@ -166,6 +199,25 @@
         else:
             return self.w_False
 
+    def ord(self, w_obj):
+        w_type = self.type(w_obj)
+        if self.is_true(self.issubtype(w_type, self.w_str)):
+            length = PyObject_Size(w_obj)
+            if length == 1:
+                s = self.str_w(w_obj)
+                return self.wrap(ord(s[0]))
+            errtype = 'string of length %d' % length
+        elif self.is_true(self.issubtype(w_type, self.w_unicode)):
+            length = PyObject_Size(w_obj)
+            if length == 1:
+                p = PyUnicode_AsUnicode(w_obj)
+                return self.wrap(p[0])
+            errtype = 'unicode string of length %d' % length
+        else:
+            errtype = self.str_w(self.getattr(w_type, self.wrap('__name__')))
+        msg = 'expected a character, but %s found' % errtype
+        raise OperationError(self.w_TypeError, self.wrap(msg))
+
     def exec_(self, statement, w_globals, w_locals, hidden_applevel=False):
         "NOT_RPYTHON"
         from types import CodeType

Modified: pypy/dist/pypy/objspace/cpy/test/test_objspace.py
==============================================================================
--- pypy/dist/pypy/objspace/cpy/test/test_objspace.py	(original)
+++ pypy/dist/pypy/objspace/cpy/test/test_objspace.py	Mon Jun 12 23:09:15 2006
@@ -15,7 +15,12 @@
     w_res = space.add(space.wrap(1.0), space.wrap(1.5))
     assert space.eq_w(w_res, space.wrap(2.5))
     res = space.float_w(w_res)
-    assert res == 2.5 
+    assert res == 2.5
+
+def test_str_w():
+    space = CPyObjSpace()
+    w_string = space.wrap('abc\x00def')
+    assert space.str_w(w_string) == 'abc\x00def'
 
 def test_demo():
     from pypy.module._demo import demo
@@ -29,3 +34,27 @@
     w1 = space.wrap('abc')
     w2 = space.wrap(11)
     raises_w(space, space.w_TypeError, space.sub, w1, w2)
+
+def test_newstring():
+    space = CPyObjSpace()
+    w = space.newstring([space.wrap(65), space.wrap(66)])
+    assert space.str_w(w) == 'AB'
+
+def test_newunicode():
+    space = CPyObjSpace()
+    w = space.newunicode([65, 66])
+    assert space.is_w(space.type(w), space.w_unicode)
+    for i in range(2):
+        code = space.int_w(space.ord(space.getitem(w, space.wrap(i))))
+        assert code == 65+i
+
+def test_ord():
+    space = CPyObjSpace()
+    w = space.wrap('A')
+    assert space.int_w(space.ord(w)) == 65
+    w = space.wrap('\x00')
+    assert space.int_w(space.ord(w)) == 0
+    w = space.newunicode([65])
+    assert space.int_w(space.ord(w)) == 65
+    w = space.newunicode([0])
+    assert space.int_w(space.ord(w)) == 0

Modified: pypy/dist/pypy/rpython/rctypes/tool/compilemodule.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/tool/compilemodule.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/tool/compilemodule.py	Mon Jun 12 23:09:15 2006
@@ -58,9 +58,53 @@
 
     driver = TranslationDriver(extmod_name=modname)
     driver.setup(__init__, [object], policy=CPyAnnotatorPolicy(space))
-    driver.proceed(['compile_c'])
+    try:
+        driver.proceed(['compile_c'])
+    except SystemExit:
+        raise
+    except:
+        debug(driver)
+        raise SystemExit(1)
     return driver.cbuilder.c_ext_module
 
+
+def debug(drv):
+    # XXX unify some code with pypy.translator.goal.translate
+    from pypy.translator.tool.pdbplus import PdbPlusShow
+    from pypy.translator.driver import log
+    t = drv.translator
+    class options:
+        huge = 100
+
+    tb = None
+    import traceback
+    errmsg = ["Error:\n"]
+    exc, val, tb = sys.exc_info()
+    errmsg.extend([" %s" % line for line in traceback.format_exception(exc, val, tb)])
+    block = getattr(val, '__annotator_block', None)
+    if block:
+        class FileLike:
+            def write(self, s):
+                errmsg.append(" %s" % s)
+        errmsg.append("Processing block:\n")
+        t.about(block, FileLike())
+    log.ERROR(''.join(errmsg))
+
+    log.event("start debugger...")
+
+    def server_setup(port=None):
+        if port is not None:
+            from pypy.translator.tool.graphserver import run_async_server
+            serv_start, serv_show, serv_stop = self.async_server = run_async_server(t, options, port)
+            return serv_start, serv_show, serv_stop
+        else:
+            from pypy.translator.tool.graphserver import run_server_for_inprocess_client
+            return run_server_for_inprocess_client(t, options)
+
+    pdb_plus_show = PdbPlusShow(t)
+    pdb_plus_show.start(tb, server_setup, graphic=True)
+
+
 def main(argv):
     if len(argv) != 2:
         print >> sys.stderr, __doc__

Modified: pypy/dist/pypy/translator/c/node.py
==============================================================================
--- pypy/dist/pypy/translator/c/node.py	(original)
+++ pypy/dist/pypy/translator/c/node.py	Mon Jun 12 23:09:15 2006
@@ -109,7 +109,7 @@
         else:
             # field names have to start with 'c_' or be meant for names that
             # vanish from the C source, like 'head' if 'inline_head' is set
-            raise Exception("field %r should not be accessed in this way" % (
+            raise ValueError("field %r should not be accessed in this way" % (
                 name,))
 
     def c_struct_field_type(self, name):
@@ -172,8 +172,12 @@
             if FIELD_T is Void:
                 yield '-1'
             else:
-                cname = self.c_struct_field_name(name)
-                yield 'offsetof(struct %s, %s)' % (self.name, cname)
+                try:
+                    cname = self.c_struct_field_name(name)
+                except ValueError:
+                    yield '-1'
+                else:
+                    yield 'offsetof(struct %s, %s)' % (self.name, cname)
 
 
 class ArrayDefNode:



More information about the Pypy-commit mailing list