About `PyArg_ParseTuple` parsing

This question should probably start with a PR I submitted (gh-84016: Implement missing `PyObject_CopyToObject` in `PEP3118` by rruuaanng · Pull Request #124747 · python/cpython · GitHub). It seems that he will have a segmentation error when building on Linux and Windows on the CI server (including my own Ubuntu, but my Windows 11 will not have this error and runs well). Following is the debug snippet. It’s running on my Ubuntu.

(gdb) b PyObject_CopyToObject
Breakpoint 1 at 0xc96f0: file Objects/abstract.c, line 739.
(gdb) r
Starting program: /root/cpython/python
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Python 3.14.0a0 (heads/gh84016:611ad22985, Oct  1 2024, 20:47:38) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>  import _testcapi;_testcapi.object_copy_to_object(bytes(3), 'abc', 'C')
  File "<python-input-0>", line 1
    import _testcapi;_testcapi.object_copy_to_object(bytes(3), 'abc', 'C')
IndentationError: unexpected indent
>>> import _testcapi;_testcapi.object_copy_to_object(bytes(3), 'abc', 'C')

Breakpoint 1, PyObject_CopyToObject (obj=0x7ffff7000000, buf=0x7ffff77800b8, len=3, fort=67 'C') at Objects/abstract.c:739
739     {
(gdb) n
745         if (!PyObject_CheckBuffer(obj)) {
(gdb) n

Program received signal SIGSEGV, Segmentation fault.
0x000055555561d710 in PyObject_CheckBuffer (obj=<optimized out>) at Objects/abstract.c:313
313         PyBufferProcs *tp_as_buffer = Py_TYPE(obj)->tp_as_buffer;
0x000055555561d702 in PyObject_CheckBuffer (obj=<optimized out>) at Objects/abstract.c:313
313         PyBufferProcs *tp_as_buffer = Py_TYPE(obj)->tp_as_buffer;
(gdb) p Py_TYPE(obj)
value has been optimized out
(gdb) p Py_TYPE(obj)->tp_as_buffer
value has been optimized out
(gdb) n

Following it working fine(running on my Win11).

>>> import asd
>>> asd.test(bytearray(3), 'abc', 'C')
bytearray(b'abc')
>>> import _testcapi
>>> _testcapi.object_copy_to_object(bytearray(3), 'abc', 'C')
bytearray(b'abc')

In Modules/_testcapi/abstract.c

static PyObject *
object_copy_to_object(PyObject *self, PyObject *args)
{
    PyObject *obj;
    Py_ssize_t len;
    int result;
    char *buf, fort;

    if (!PyArg_ParseTuple(args, "Os#C", &obj, &buf, &len, &fort)) {
        return NULL;
    }
    result = PyObject_CopyToObject(obj, buf, len, fort);
    if (result < 0) {
        return NULL;
    }
    Py_INCREF(obj);
    return obj;
}

I wonder if it is because I used PyArg_ParseTuple incorrectly that the obj parameter is an abnormal value.
Actually, what I can’t understand is why this error doesn’t occur on my computer.

The issue is with format unit C. Use c instead.

Oh, thank you. It seems like he’s normal now.