Jean-Paul Calderone avatar Jean-Paul Calderone committed 1033def

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

Comments (0)

Files changed (2)

pypy/module/cpyext/src/getargs.c

 	}
 		
 	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",
 			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);

pypy/module/cpyext/test/test_getargs.py

             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')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.