summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2009-04-02 00:33:55 +0000
committerBenjamin Peterson <benjamin@python.org>2009-04-02 00:33:55 +0000
commit7fe9853596189cb250f895800edfa19147bbb0f1 (patch)
tree5907e37a8dba72265b68f0dd59f9d4368927ac90
parent605b9d9fe8509f9da305b2ef7a7774419e47094d (diff)
downloadcpython-git-7fe9853596189cb250f895800edfa19147bbb0f1.tar.gz
make 'c' only accept bytes and 'C' only unicode #5499
-rw-r--r--Doc/c-api/arg.rst6
-rw-r--r--Lib/test/buffer_tests.py6
-rw-r--r--Misc/NEWS3
-rw-r--r--Python/getargs.c14
4 files changed, 15 insertions, 14 deletions
diff --git a/Doc/c-api/arg.rst b/Doc/c-api/arg.rst
index dfedf972fe..a554bc9300 100644
--- a/Doc/c-api/arg.rst
+++ b/Doc/c-api/arg.rst
@@ -208,9 +208,13 @@ variable(s) whose address should be passed.
Convert a Python integer to a C :ctype:`Py_ssize_t`.
``c`` (string of length 1) [char]
- Convert a Python character, represented as a string of length 1, to a C
+ Convert a Python character, represented as a byte string of length 1, to a C
:ctype:`char`.
+``C`` (string of length 1) [int]
+ Covert a Python character, represented as a unicode string of length 1, to a
+ C :ctype:`int`.
+
``f`` (float) [float]
Convert a Python floating point number to a C :ctype:`float`.
diff --git a/Lib/test/buffer_tests.py b/Lib/test/buffer_tests.py
index 8696eab61d..58689cdb34 100644
--- a/Lib/test/buffer_tests.py
+++ b/Lib/test/buffer_tests.py
@@ -112,7 +112,7 @@ class MixinBytesBufferCommonTests(object):
self.assertEqual(b'abc ', self.marshal(b'abc').ljust(6))
self.assertEqual(b'abc', self.marshal(b'abc').ljust(3))
self.assertEqual(b'abc', self.marshal(b'abc').ljust(2))
- self.assertEqual(b'abc*******', self.marshal(b'abc').ljust(10, '*'))
+ self.assertEqual(b'abc*******', self.marshal(b'abc').ljust(10, b'*'))
self.assertRaises(TypeError, self.marshal(b'abc').ljust)
def test_rjust(self):
@@ -120,7 +120,7 @@ class MixinBytesBufferCommonTests(object):
self.assertEqual(b' abc', self.marshal(b'abc').rjust(6))
self.assertEqual(b'abc', self.marshal(b'abc').rjust(3))
self.assertEqual(b'abc', self.marshal(b'abc').rjust(2))
- self.assertEqual(b'*******abc', self.marshal(b'abc').rjust(10, '*'))
+ self.assertEqual(b'*******abc', self.marshal(b'abc').rjust(10, b'*'))
self.assertRaises(TypeError, self.marshal(b'abc').rjust)
def test_center(self):
@@ -128,7 +128,7 @@ class MixinBytesBufferCommonTests(object):
self.assertEqual(b' abc ', self.marshal(b'abc').center(6))
self.assertEqual(b'abc', self.marshal(b'abc').center(3))
self.assertEqual(b'abc', self.marshal(b'abc').center(2))
- self.assertEqual(b'***abc****', self.marshal(b'abc').center(10, '*'))
+ self.assertEqual(b'***abc****', self.marshal(b'abc').center(10, b'*'))
self.assertRaises(TypeError, self.marshal(b'abc').center)
def test_swapcase(self):
diff --git a/Misc/NEWS b/Misc/NEWS
index b02e785684..8e9fbf455b 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 3.1 alpha 2?
Core and Builtins
-----------------
+- Issue #5499: The 'c' code for argument parsing functions now only accepts a
+ byte, and the 'C' code only accepts a unicode character.
+
- Issue #1665206: Remove the last eager import in _warnings.c and make it lazy.
- Fix a segfault when running test_exceptions with coverage, caused by
diff --git a/Python/getargs.c b/Python/getargs.c
index 3ab59b3e42..a89ee6f858 100644
--- a/Python/getargs.c
+++ b/Python/getargs.c
@@ -776,24 +776,18 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
char *p = va_arg(*p_va, char *);
if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1)
*p = PyBytes_AS_STRING(arg)[0];
- else if (PyUnicode_Check(arg) &&
- PyUnicode_GET_SIZE(arg) == 1 &&
- PyUnicode_AS_UNICODE(arg)[0] < 256)
- *p = (char)PyUnicode_AS_UNICODE(arg)[0];
else
- return converterr("char < 256", arg, msgbuf, bufsize);
+ return converterr("a byte string of length 1", arg, msgbuf, bufsize);
break;
}
case 'C': {/* unicode char */
int *p = va_arg(*p_va, int *);
- if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1)
- *p = PyBytes_AS_STRING(arg)[0];
- else if (PyUnicode_Check(arg) &&
- PyUnicode_GET_SIZE(arg) == 1)
+ if (PyUnicode_Check(arg) &&
+ PyUnicode_GET_SIZE(arg) == 1)
*p = PyUnicode_AS_UNICODE(arg)[0];
else
- return converterr("char", arg, msgbuf, bufsize);
+ return converterr("a unicode character", arg, msgbuf, bufsize);
break;
}