[pypy-svn] pypy pyarg-parsebuffer: A test for some parts of `t#` and part of an implementation

exarkun commits-noreply at bitbucket.org
Tue Apr 19 22:00:58 CEST 2011


Author: Jean-Paul Calderone <exarkun at twistedmatrix.com>
Branch: pyarg-parsebuffer
Changeset: r43490:6a2f0310d664
Date: 2011-04-19 12:59 -0400
http://bitbucket.org/pypy/pypy/changeset/6a2f0310d664/

Log:	A test for some parts of `t#` and part of an implementation

diff --git a/pypy/module/cpyext/src/getargs.c b/pypy/module/cpyext/src/getargs.c
--- a/pypy/module/cpyext/src/getargs.c
+++ b/pypy/module/cpyext/src/getargs.c
@@ -1268,24 +1268,28 @@
 	}
 		
 	case 't': { /* 8-bit character buffer, read-only access */
-    Py_FatalError("'t' unsupported");
-#if 0
 		char **p = va_arg(*p_va, char **);
 		PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
 		Py_ssize_t count;
-		
+                printf("this far\n");
+
+#if 0
 		if (*format++ != '#')
 			return converterr(
 				"invalid use of 't' format character", 
 				arg, msgbuf, bufsize);
+#endif
 		if (!PyType_HasFeature(arg->ob_type,
-				       Py_TPFLAGS_HAVE_GETCHARBUFFER) ||
-		    pb == NULL || pb->bf_getcharbuffer == NULL ||
-		    pb->bf_getsegcount == NULL)
+				       Py_TPFLAGS_HAVE_GETCHARBUFFER)
+#if 0
+		    || pb == NULL || pb->bf_getcharbuffer == NULL ||
+		    pb->bf_getsegcount == NULL
+#endif
+                    )
 			return converterr(
 				"string or read-only character buffer",
 				arg, msgbuf, bufsize);
-
+#if 0
 		if (pb->bf_getsegcount(arg, NULL) != 1)
 			return converterr(
 				"string or single-segment read-only buffer",
@@ -1295,16 +1299,23 @@
 			return converterr(
 				"string or pinned buffer",
 				arg, msgbuf, bufsize);
-
+#endif
+                printf("this far!\n");
+                printf("%p\n", pb->bf_getcharbuffer);
 		count = pb->bf_getcharbuffer(arg, 0, p);
+                printf("after\n");
+#if 0
 		if (count < 0)
 			return converterr("(unspecified)", arg, msgbuf, bufsize);
+#endif
 		{
+                    printf("fetch size\n");
 			FETCH_SIZE;
+                        printf("did that\n");
 			STORE_SIZE(count);
+                        printf("store size done\n");
 		}
 		break;
-#endif
 	}
 	default:
 		return converterr("impossible<bad format char>", arg, msgbuf, bufsize);

diff --git a/pypy/module/cpyext/test/test_getargs.py b/pypy/module/cpyext/test/test_getargs.py
--- a/pypy/module/cpyext/test/test_getargs.py
+++ b/pypy/module/cpyext/test/test_getargs.py
@@ -124,3 +124,21 @@
             return PyString_FromStringAndSize(buf.buf, buf.len);
             ''')
         assert 'foo\0bar\0baz' == pybuffer('foo\0bar\0baz')
+
+
+    def test_pyarg_parse_charbuf_and_length(self):
+        """
+        The `t#` format specifier can be used to parse a read-only 8-bit
+        character buffer into a char* and int giving its length in bytes.
+        """
+        charbuf = self.import_parser(
+            '''
+            char *buf;
+            int len;
+            if (!PyArg_ParseTuple(args, "t#", &buf, &len)) {
+                return NULL;
+            }
+            return PyString_FromStringAndSize(buf, len);
+            ''')
+        raises(TypeError, "charbuf(10)")
+        assert 'foo\0bar\0baz' == charbuf('foo\0bar\0baz')


More information about the Pypy-commit mailing list