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

trundle at codespeak.net trundle at codespeak.net
Sat Apr 3 02:59:36 CEST 2010


Author: trundle
Date: Sat Apr  3 02:59:34 2010
New Revision: 73322

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_STRING and T_STRING_INPLACE members.


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	Sat Apr  3 02:59:34 2010
@@ -22,7 +22,9 @@
 
 /* Types */
 #define T_INT		1
+#define T_STRING	5
 #define T_OBJECT	6
+#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
 				   converting to None. */

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	Sat Apr  3 02:59:34 2010
@@ -6,6 +6,7 @@
 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.typeobjectdefs import PyMemberDef
 
 
@@ -17,6 +18,15 @@
     if member_type == structmemberdefs.T_INT:
         result = rffi.cast(rffi.INTP, addr)
         w_result = space.wrap(result[0])
+    elif member_type == structmemberdefs.T_STRING:
+        result = rffi.cast(rffi.CCHARPP, addr)
+        if result[0]:
+            w_result = PyString_FromString(space, result[0])
+        else:
+            w_result = space.w_None
+    elif member_type == structmemberdefs.T_STRING_INPLACE:
+        result = rffi.cast(rffi.CCHARP, addr)
+        w_result = PyString_FromString(space, result)
     elif member_type in [structmemberdefs.T_OBJECT,
                          structmemberdefs.T_OBJECT_EX]:
         obj_ptr = rffi.cast(PyObjectP, addr)
@@ -40,7 +50,9 @@
     member_type = rffi.cast(lltype.Signed, w_member.c_type)
     flags = rffi.cast(lltype.Signed, w_member.c_flags)
 
-    if flags & structmemberdefs.READONLY:
+    if (flags & structmemberdefs.READONLY or
+        member_type in [structmemberdefs.T_STRING,
+                        structmemberdefs.T_STRING_INPLACE]):
         raise OperationError(space.w_TypeError,
                              space.wrap("readonly attribute"))
     elif w_value is None:
@@ -51,7 +63,7 @@
         elif member_type != structmemberdefs.T_OBJECT:
             raise OperationError(space.w_TypeError,
                              space.wrap("can't delete numeric/char attribute"))
-        
+
     if member_type == structmemberdefs.T_INT:
         w_long_value = PyInt_AsLong(space, w_value)
         array = rffi.cast(rffi.INTP, 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	Sat Apr  3 02:59:34 2010
@@ -1,5 +1,7 @@
 T_INT = 1
+T_STRING = 5
 T_OBJECT = 6
+T_STRING_INPLACE = 13
 T_OBJECT_EX = 16
 
 READONLY = RO = 1

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	Sat Apr  3 02:59:34 2010
@@ -5,6 +5,8 @@
 	PyObject_HEAD
     int	foo;		/* the context holder */
     PyObject *foo_object;
+    char *foo_string;
+    char foo_string_inplace[5];
 } fooobject;
 
 static PyTypeObject footype;
@@ -20,6 +22,8 @@
 
 	foop->foo = 42;
 	foop->foo_object = NULL;
+	foop->foo_string = "Hello from PyPy";
+	strncpy(foop->foo_string_inplace, "spam", 5);
 	return foop;
 }
 
@@ -48,9 +52,17 @@
 	return (PyObject *)foop;
 }
 
+static PyObject *
+foo_unset(fooobject *self)
+{
+    self->foo_string = NULL;
+    Py_RETURN_NONE;
+}
+
 
 static PyMethodDef foo_methods[] = {
 	{"copy",      (PyCFunction)foo_copy,      METH_NOARGS,  NULL},
+	{"unset_string_member", (PyCFunction)foo_unset, METH_NOARGS, NULL},
 	{NULL, NULL}			     /* sentinel */
 };
 
@@ -105,6 +117,10 @@
      "A Python object."},
     {"object_member_ex", T_OBJECT_EX, offsetof(fooobject, foo_object), 0,
      "A Python object."},
+    {"string_member", T_STRING, offsetof(fooobject, foo_string), 0,
+     "A string."},
+    {"string_member_inplace", T_STRING_INPLACE,
+     offsetof(fooobject, foo_string_inplace), 0, "An inplace string."},
     {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	Sat Apr  3 02:59:34 2010
@@ -54,6 +54,15 @@
         del obj.object_member_ex
         raises(AttributeError, "del obj.object_member_ex")
 
+        assert obj.string_member == "Hello from PyPy"
+        raises(TypeError, "obj.string_member = 42")
+        raises(TypeError, "del obj.string_member")
+        obj.unset_string_member()
+        assert obj.string_member is None
+        assert obj.string_member_inplace == "spam"
+        raises(TypeError, "obj.string_member_inplace = 42")
+        raises(TypeError, "del obj.string_member_inplace")
+
         a = module.fooType
         assert "cannot create" in raises(TypeError, "a()").value.message
         skip("In Progress")



More information about the Pypy-commit mailing list