Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 906b88f

Browse files
committed
Issue #12380: PyArg_ParseTuple now accepts a bytearray for the 'c' format.
As a side effect, this now allows the rjust, ljust and center methods of bytes and bytearray to accept a bytearray argument. Patch by Petri Lehtinen
1 parent 66d2be8 commit 906b88f

6 files changed

Lines changed: 53 additions & 4 deletions

File tree

Doc/c-api/arg.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,11 @@ Numbers
260260
``n`` (:class:`int`) [Py_ssize_t]
261261
Convert a Python integer to a C :c:type:`Py_ssize_t`.
262262

263-
``c`` (:class:`bytes` of length 1) [char]
264-
Convert a Python byte, represented as a :class:`bytes` object of length 1,
265-
to a C :c:type:`char`.
263+
``c`` (:class:`bytes` or :class:`bytearray` of length 1) [char]
264+
Convert a Python byte, represented as a :class:`bytes` or
265+
:class:`bytearray` object of length 1, to a C :c:type:`char`.
266+
267+
.. versionchanged:: 3.3 Allow :class:`bytearray` objects
266268

267269
``C`` (:class:`str` of length 1) [int]
268270
Convert a Python character, represented as a :class:`str` object of

Lib/test/test_bytes.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,27 @@ def test_strip_string_error(self):
475475
self.assertRaises(TypeError, self.type2test(b'abc').lstrip, 'b')
476476
self.assertRaises(TypeError, self.type2test(b'abc').rstrip, 'b')
477477

478+
def test_center(self):
479+
# Fill character can be either bytes or bytearray (issue 12380)
480+
b = self.type2test(b'abc')
481+
for fill_type in (bytes, bytearray):
482+
self.assertEqual(b.center(7, fill_type(b'-')),
483+
self.type2test(b'--abc--'))
484+
485+
def test_ljust(self):
486+
# Fill character can be either bytes or bytearray (issue 12380)
487+
b = self.type2test(b'abc')
488+
for fill_type in (bytes, bytearray):
489+
self.assertEqual(b.ljust(7, fill_type(b'-')),
490+
self.type2test(b'abc----'))
491+
492+
def test_rjust(self):
493+
# Fill character can be either bytes or bytearray (issue 12380)
494+
b = self.type2test(b'abc')
495+
for fill_type in (bytes, bytearray):
496+
self.assertEqual(b.rjust(7, fill_type(b'-')),
497+
self.type2test(b'----abc'))
498+
478499
def test_ord(self):
479500
b = self.type2test(b'\0A\x7f\x80\xff')
480501
self.assertEqual([ord(b[i:i+1]) for i in range(len(b))],

Lib/test/test_getargs2.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,15 @@ def test_surrogate_keyword(self):
294294
self.fail('TypeError should have been raised')
295295

296296
class Bytes_TestCase(unittest.TestCase):
297+
def test_c(self):
298+
from _testcapi import getargs_c
299+
self.assertRaises(TypeError, getargs_c, b'abc') # len > 1
300+
self.assertEqual(getargs_c(b'a'), b'a')
301+
self.assertEqual(getargs_c(bytearray(b'a')), b'a')
302+
self.assertRaises(TypeError, getargs_c, memoryview(b'a'))
303+
self.assertRaises(TypeError, getargs_c, 's')
304+
self.assertRaises(TypeError, getargs_c, None)
305+
297306
def test_s(self):
298307
from _testcapi import getargs_s
299308
self.assertEqual(getargs_s('abc\xe9'), b'abc\xc3\xa9')

Misc/NEWS

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ Core and Builtins
238238
- Issue #11386: bytearray.pop() now throws IndexError when the bytearray is
239239
empty, instead of OverflowError.
240240

241+
- Issue #12380: The rjust, ljust and center methods of bytes and bytearray
242+
now accept a bytearray argument.
243+
241244
Library
242245
-------
243246

@@ -1282,6 +1285,8 @@ C-API
12821285
- Issue #12173: The first argument of PyImport_ImportModuleLevel is now `const
12831286
char *` instead of `char *`.
12841287

1288+
- Issue #12380: PyArg_ParseTuple now accepts a bytearray for the 'c' format.
1289+
12851290
Documentation
12861291
-------------
12871292

@@ -6680,4 +6685,4 @@ Docs
66806685
----
66816686

66826687

6683-
**(For information about older versions, consult the HISTORY file.)**
6688+
**(For information about older versions, consult the HISTORY file.)**

Modules/_testcapimodule.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,15 @@ test_k_code(PyObject *self)
10021002
return Py_None;
10031003
}
10041004

1005+
static PyObject *
1006+
getargs_c(PyObject *self, PyObject *args)
1007+
{
1008+
char c;
1009+
if (!PyArg_ParseTuple(args, "c", &c))
1010+
return NULL;
1011+
return PyBytes_FromStringAndSize(&c, 1);
1012+
}
1013+
10051014
static PyObject *
10061015
getargs_s(PyObject *self, PyObject *args)
10071016
{
@@ -2289,6 +2298,7 @@ static PyMethodDef TestMethods[] = {
22892298
(PyCFunction)test_long_long_and_overflow, METH_NOARGS},
22902299
{"test_L_code", (PyCFunction)test_L_code, METH_NOARGS},
22912300
#endif
2301+
{"getargs_c", getargs_c, METH_VARARGS},
22922302
{"getargs_s", getargs_s, METH_VARARGS},
22932303
{"getargs_s_star", getargs_s_star, METH_VARARGS},
22942304
{"getargs_s_hash", getargs_s_hash, METH_VARARGS},

Python/getargs.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,8 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
828828
char *p = va_arg(*p_va, char *);
829829
if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1)
830830
*p = PyBytes_AS_STRING(arg)[0];
831+
else if (PyByteArray_Check(arg) && PyByteArray_Size(arg) == 1)
832+
*p = PyByteArray_AS_STRING(arg)[0];
831833
else
832834
return converterr("a byte string of length 1", arg, msgbuf, bufsize);
833835
break;

0 commit comments

Comments
 (0)