Index: Python/getargs.c =================================================================== --- Python/getargs.c (révision 81910) +++ Python/getargs.c (copie de travail) @@ -935,10 +935,15 @@ count = convertbuffer(arg, p, &buf); if (count < 0) return converterr(buf, arg, msgbuf, bufsize); - else if (*format == '#') { + if (*format == '#') { FETCH_SIZE; STORE_SIZE(count); format++; + } else { + if (strlen(*p) != count) + return converterr( + "string without null bytes or None", + arg, msgbuf, bufsize); } break; } Index: Lib/test/test_getargs2.py =================================================================== --- Lib/test/test_getargs2.py (révision 81910) +++ Lib/test/test_getargs2.py (copie de travail) @@ -293,8 +293,22 @@ else: self.fail('TypeError should have been raised') +class String_TestCase(unittest.TestCase): + def test_format_y(self): + from _testcapi import getargs_y + self.assertEqual(b'bytes', getargs_y(b'bytes')) + self.assertRaises(TypeError, getargs_y, b'a\0c') + self.assertRaises(TypeError, getargs_y, "unicode") + self.assertRaises(TypeError, getargs_y, bytearray(b'abc')) + self.assertRaises(TypeError, getargs_y, memoryview(b'abc')) + def test_main(): - tests = [Signed_TestCase, Unsigned_TestCase, Tuple_TestCase, Keywords_TestCase] + tests = [ + Signed_TestCase, + Unsigned_TestCase, + Tuple_TestCase, + Keywords_TestCase, + String_TestCase] try: from _testcapi import getargs_L, getargs_K except ImportError: Index: Modules/_testcapimodule.c =================================================================== --- Modules/_testcapimodule.c (révision 81910) +++ Modules/_testcapimodule.c (copie de travail) @@ -956,6 +956,15 @@ } #endif +static PyObject * +getargs_y(PyObject *self, PyObject *args) +{ + char *str; + if (!PyArg_ParseTuple(args, "y", &str)) + return NULL; + return PyBytes_FromString(str); +} + /* This function not only tests the 'k' getargs code, but also the PyLong_AsUnsignedLongMask() and PyLong_AsUnsignedLongMask() functions. */ static PyObject * @@ -2055,6 +2064,7 @@ {"getargs_i", getargs_i, METH_VARARGS}, {"getargs_l", getargs_l, METH_VARARGS}, {"getargs_n", getargs_n, METH_VARARGS}, + {"getargs_y", getargs_y, METH_VARARGS}, #ifdef HAVE_LONG_LONG {"getargs_L", getargs_L, METH_VARARGS}, {"getargs_K", getargs_K, METH_VARARGS}, @@ -2062,11 +2072,11 @@ {"test_long_long_and_overflow", (PyCFunction)test_long_long_and_overflow, METH_NOARGS}, {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS}, +#endif {"codec_incrementalencoder", (PyCFunction)codec_incrementalencoder, METH_VARARGS}, {"codec_incrementaldecoder", (PyCFunction)codec_incrementaldecoder, METH_VARARGS}, -#endif {"test_s_code", (PyCFunction)test_s_code, METH_NOARGS}, {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS}, {"test_Z_code", (PyCFunction)test_Z_code, METH_NOARGS},