[pypy-svn] r73345 - in pypy/branch/cpython-extension/pypy/module/cpyext: . include test

trundle at codespeak.net trundle at codespeak.net
Sun Apr 4 01:59:20 CEST 2010


Author: trundle
Date: Sun Apr  4 01:59:18 2010
New Revision: 73345

Modified:
   pypy/branch/cpython-extension/pypy/module/cpyext/include/structmember.h
   pypy/branch/cpython-extension/pypy/module/cpyext/structmember.py
   pypy/branch/cpython-extension/pypy/module/cpyext/structmemberdefs.py
   pypy/branch/cpython-extension/pypy/module/cpyext/test/foo.c
   pypy/branch/cpython-extension/pypy/module/cpyext/test/test_typeobject.py
Log:
Add T_CHAR member.


Modified: pypy/branch/cpython-extension/pypy/module/cpyext/include/structmember.h
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/include/structmember.h	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/include/structmember.h	Sun Apr  4 01:59:18 2010
@@ -24,6 +24,7 @@
 #define T_INT		1
 #define T_STRING	5
 #define T_OBJECT	6
+#define T_CHAR		7	/* 1-character string */
 #define T_STRING_INPLACE 13     /* Strings contained in the structure */
 #define T_OBJECT_EX	16	/* Like T_OBJECT, but raises AttributeError
 				   when the value is NULL, instead of

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/structmember.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/structmember.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/structmember.py	Sun Apr  4 01:59:18 2010
@@ -6,7 +6,8 @@
 from pypy.module.cpyext.intobject import PyInt_AsLong
 from pypy.module.cpyext.pyerrors import PyErr_Occurred
 from pypy.module.cpyext.pyobject import PyObject, Py_DecRef, from_ref, make_ref
-from pypy.module.cpyext.stringobject import PyString_FromString
+from pypy.module.cpyext.stringobject import (PyString_FromString,
+                                             PyString_FromStringAndSize)
 from pypy.module.cpyext.typeobjectdefs import PyMemberDef
 
 
@@ -27,6 +28,9 @@
     elif member_type == structmemberdefs.T_STRING_INPLACE:
         result = rffi.cast(rffi.CCHARP, addr)
         w_result = PyString_FromString(space, result)
+    elif member_type == structmemberdefs.T_CHAR:
+        result = rffi.cast(rffi.CCHARP, addr)
+        w_result = space.wrap(result[0])
     elif member_type in [structmemberdefs.T_OBJECT,
                          structmemberdefs.T_OBJECT_EX]:
         obj_ptr = rffi.cast(PyObjectP, addr)
@@ -68,6 +72,13 @@
         w_long_value = PyInt_AsLong(space, w_value)
         array = rffi.cast(rffi.INTP, addr)
         array[0] = rffi.cast(rffi.INT, w_long_value)
+    elif member_type == structmemberdefs.T_CHAR:
+        str_value = space.str_w(w_value)
+        if len(str_value) != 1:
+            raise OperationError(space.w_TypeError,
+                                 space.wrap("string of length 1 expected"))
+        array = rffi.cast(rffi.CCHARP, addr)
+        array[0] = str_value
     elif member_type in [structmemberdefs.T_OBJECT,
                          structmemberdefs.T_OBJECT_EX]:
         array = rffi.cast(PyObjectP, addr)

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/structmemberdefs.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/structmemberdefs.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/structmemberdefs.py	Sun Apr  4 01:59:18 2010
@@ -1,6 +1,7 @@
 T_INT = 1
 T_STRING = 5
 T_OBJECT = 6
+T_CHAR = 7
 T_STRING_INPLACE = 13
 T_OBJECT_EX = 16
 

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/test/foo.c
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/test/foo.c	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/test/foo.c	Sun Apr  4 01:59:18 2010
@@ -121,6 +121,7 @@
      "A string."},
     {"string_member_inplace", T_STRING_INPLACE,
      offsetof(fooobject, foo_string_inplace), 0, "An inplace string."},
+    {"char_member", T_CHAR, offsetof(fooobject, foo_string_inplace), 0, NULL},
     {NULL}  /* Sentinel */
 };
 

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/test/test_typeobject.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/test/test_typeobject.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/test/test_typeobject.py	Sun Apr  4 01:59:18 2010
@@ -64,6 +64,11 @@
         assert obj.string_member_inplace == "spam"
         raises(TypeError, "obj.string_member_inplace = 42")
         raises(TypeError, "del obj.string_member_inplace")
+        assert obj.char_member == "s"
+        obj.char_member = "a"
+        assert obj.char_member == "a"
+        raises(TypeError, "obj.char_member = 'spam'")
+        raises(TypeError, "obj.char_member = 42")
 
         #skip("In progress") # not at all, how does this fail for you amaury?
 



More information about the Pypy-commit mailing list