Skip to content

Commit 7fe9853

Browse files
committed
make 'c' only accept bytes and 'C' only unicode python#5499
1 parent 605b9d9 commit 7fe9853

4 files changed

Lines changed: 15 additions & 14 deletions

File tree

Doc/c-api/arg.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,13 @@ variable(s) whose address should be passed.
208208
Convert a Python integer to a C :ctype:`Py_ssize_t`.
209209

210210
``c`` (string of length 1) [char]
211-
Convert a Python character, represented as a string of length 1, to a C
211+
Convert a Python character, represented as a byte string of length 1, to a C
212212
:ctype:`char`.
213213

214+
``C`` (string of length 1) [int]
215+
Covert a Python character, represented as a unicode string of length 1, to a
216+
C :ctype:`int`.
217+
214218
``f`` (float) [float]
215219
Convert a Python floating point number to a C :ctype:`float`.
216220

Lib/test/buffer_tests.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,23 +112,23 @@ def test_ljust(self):
112112
self.assertEqual(b'abc ', self.marshal(b'abc').ljust(6))
113113
self.assertEqual(b'abc', self.marshal(b'abc').ljust(3))
114114
self.assertEqual(b'abc', self.marshal(b'abc').ljust(2))
115-
self.assertEqual(b'abc*******', self.marshal(b'abc').ljust(10, '*'))
115+
self.assertEqual(b'abc*******', self.marshal(b'abc').ljust(10, b'*'))
116116
self.assertRaises(TypeError, self.marshal(b'abc').ljust)
117117

118118
def test_rjust(self):
119119
self.assertEqual(b' abc', self.marshal(b'abc').rjust(10))
120120
self.assertEqual(b' abc', self.marshal(b'abc').rjust(6))
121121
self.assertEqual(b'abc', self.marshal(b'abc').rjust(3))
122122
self.assertEqual(b'abc', self.marshal(b'abc').rjust(2))
123-
self.assertEqual(b'*******abc', self.marshal(b'abc').rjust(10, '*'))
123+
self.assertEqual(b'*******abc', self.marshal(b'abc').rjust(10, b'*'))
124124
self.assertRaises(TypeError, self.marshal(b'abc').rjust)
125125

126126
def test_center(self):
127127
self.assertEqual(b' abc ', self.marshal(b'abc').center(10))
128128
self.assertEqual(b' abc ', self.marshal(b'abc').center(6))
129129
self.assertEqual(b'abc', self.marshal(b'abc').center(3))
130130
self.assertEqual(b'abc', self.marshal(b'abc').center(2))
131-
self.assertEqual(b'***abc****', self.marshal(b'abc').center(10, '*'))
131+
self.assertEqual(b'***abc****', self.marshal(b'abc').center(10, b'*'))
132132
self.assertRaises(TypeError, self.marshal(b'abc').center)
133133

134134
def test_swapcase(self):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 3.1 alpha 2?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #5499: The 'c' code for argument parsing functions now only accepts a
16+
byte, and the 'C' code only accepts a unicode character.
17+
1518
- Issue #1665206: Remove the last eager import in _warnings.c and make it lazy.
1619

1720
- Fix a segfault when running test_exceptions with coverage, caused by

Python/getargs.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -776,24 +776,18 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
776776
char *p = va_arg(*p_va, char *);
777777
if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1)
778778
*p = PyBytes_AS_STRING(arg)[0];
779-
else if (PyUnicode_Check(arg) &&
780-
PyUnicode_GET_SIZE(arg) == 1 &&
781-
PyUnicode_AS_UNICODE(arg)[0] < 256)
782-
*p = (char)PyUnicode_AS_UNICODE(arg)[0];
783779
else
784-
return converterr("char < 256", arg, msgbuf, bufsize);
780+
return converterr("a byte string of length 1", arg, msgbuf, bufsize);
785781
break;
786782
}
787783

788784
case 'C': {/* unicode char */
789785
int *p = va_arg(*p_va, int *);
790-
if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1)
791-
*p = PyBytes_AS_STRING(arg)[0];
792-
else if (PyUnicode_Check(arg) &&
793-
PyUnicode_GET_SIZE(arg) == 1)
786+
if (PyUnicode_Check(arg) &&
787+
PyUnicode_GET_SIZE(arg) == 1)
794788
*p = PyUnicode_AS_UNICODE(arg)[0];
795789
else
796-
return converterr("char", arg, msgbuf, bufsize);
790+
return converterr("a unicode character", arg, msgbuf, bufsize);
797791
break;
798792
}
799793

0 commit comments

Comments
 (0)