Skip to content

[3.9] closes bpo-42938: Replace snprintf with Python unicode formatting in ctypes param reprs. #24247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Lib/ctypes/test/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,49 @@ def __dict__(self):
with self.assertRaises(ZeroDivisionError):
WorseStruct().__setstate__({}, b'foo')

def test_parameter_repr(self):
from ctypes import (
c_bool,
c_char,
c_wchar,
c_byte,
c_ubyte,
c_short,
c_ushort,
c_int,
c_uint,
c_long,
c_ulong,
c_longlong,
c_ulonglong,
c_float,
c_double,
c_longdouble,
c_char_p,
c_wchar_p,
c_void_p,
)
self.assertRegex(repr(c_bool.from_param(True)), r"^<cparam '\?' at 0x[A-Fa-f0-9]+>$")
self.assertEqual(repr(c_char.from_param(97)), "<cparam 'c' ('a')>")
self.assertRegex(repr(c_wchar.from_param('a')), r"^<cparam 'u' at 0x[A-Fa-f0-9]+>$")
self.assertEqual(repr(c_byte.from_param(98)), "<cparam 'b' (98)>")
self.assertEqual(repr(c_ubyte.from_param(98)), "<cparam 'B' (98)>")
self.assertEqual(repr(c_short.from_param(511)), "<cparam 'h' (511)>")
self.assertEqual(repr(c_ushort.from_param(511)), "<cparam 'H' (511)>")
self.assertRegex(repr(c_int.from_param(20000)), r"^<cparam '[li]' \(20000\)>$")
self.assertRegex(repr(c_uint.from_param(20000)), r"^<cparam '[LI]' \(20000\)>$")
self.assertRegex(repr(c_long.from_param(20000)), r"^<cparam '[li]' \(20000\)>$")
self.assertRegex(repr(c_ulong.from_param(20000)), r"^<cparam '[LI]' \(20000\)>$")
self.assertRegex(repr(c_longlong.from_param(20000)), r"^<cparam '[liq]' \(20000\)>$")
self.assertRegex(repr(c_ulonglong.from_param(20000)), r"^<cparam '[LIQ]' \(20000\)>$")
self.assertEqual(repr(c_float.from_param(1.5)), "<cparam 'f' (1.5)>")
self.assertEqual(repr(c_double.from_param(1.5)), "<cparam 'd' (1.5)>")
self.assertEqual(repr(c_double.from_param(1e300)), "<cparam 'd' (1e+300)>")
self.assertRegex(repr(c_longdouble.from_param(1.5)), r"^<cparam ('d' \(1.5\)|'g' at 0x[A-Fa-f0-9]+)>$")
self.assertRegex(repr(c_char_p.from_param(b'hihi')), "^<cparam 'z' \(0x[A-Fa-f0-9]+\)>$")
self.assertRegex(repr(c_wchar_p.from_param('hihi')), "^<cparam 'Z' \(0x[A-Fa-f0-9]+\)>$")
self.assertRegex(repr(c_void_p.from_param(0x12)), r"^<cparam 'P' \(0x0*12\)>$")

################################################################

if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Avoid static buffers when computing the repr of :class:`ctypes.c_double` and
:class:`ctypes.c_longdouble` values.
51 changes: 19 additions & 32 deletions Modules/_ctypes/callproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -489,58 +489,47 @@ is_literal_char(unsigned char c)
static PyObject *
PyCArg_repr(PyCArgObject *self)
{
char buffer[256];
switch(self->tag) {
case 'b':
case 'B':
sprintf(buffer, "<cparam '%c' (%d)>",
return PyUnicode_FromFormat("<cparam '%c' (%d)>",
self->tag, self->value.b);
break;
case 'h':
case 'H':
sprintf(buffer, "<cparam '%c' (%d)>",
return PyUnicode_FromFormat("<cparam '%c' (%d)>",
self->tag, self->value.h);
break;
case 'i':
case 'I':
sprintf(buffer, "<cparam '%c' (%d)>",
return PyUnicode_FromFormat("<cparam '%c' (%d)>",
self->tag, self->value.i);
break;
case 'l':
case 'L':
sprintf(buffer, "<cparam '%c' (%ld)>",
return PyUnicode_FromFormat("<cparam '%c' (%ld)>",
self->tag, self->value.l);
break;

case 'q':
case 'Q':
sprintf(buffer,
#ifdef MS_WIN32
"<cparam '%c' (%I64d)>",
#else
"<cparam '%c' (%lld)>",
#endif
return PyUnicode_FromFormat("<cparam '%c' (%lld)>",
self->tag, self->value.q);
break;
case 'd':
sprintf(buffer, "<cparam '%c' (%f)>",
self->tag, self->value.d);
break;
case 'f':
sprintf(buffer, "<cparam '%c' (%f)>",
self->tag, self->value.f);
break;

case 'f': {
PyObject *f = PyFloat_FromDouble((self->tag == 'f') ? self->value.f : self->value.d);
if (f == NULL) {
return NULL;
}
PyObject *result = PyUnicode_FromFormat("<cparam '%c' (%R)>", self->tag, f);
Py_DECREF(f);
return result;
}
case 'c':
if (is_literal_char((unsigned char)self->value.c)) {
sprintf(buffer, "<cparam '%c' ('%c')>",
return PyUnicode_FromFormat("<cparam '%c' ('%c')>",
self->tag, self->value.c);
}
else {
sprintf(buffer, "<cparam '%c' ('\\x%02x')>",
return PyUnicode_FromFormat("<cparam '%c' ('\\x%02x')>",
self->tag, (unsigned char)self->value.c);
}
break;

/* Hm, are these 'z' and 'Z' codes useful at all?
Shouldn't they be replaced by the functionality of c_string
Expand All @@ -549,22 +538,20 @@ PyCArg_repr(PyCArgObject *self)
case 'z':
case 'Z':
case 'P':
sprintf(buffer, "<cparam '%c' (%p)>",
return PyUnicode_FromFormat("<cparam '%c' (%p)>",
self->tag, self->value.p);
break;

default:
if (is_literal_char((unsigned char)self->tag)) {
sprintf(buffer, "<cparam '%c' at %p>",
return PyUnicode_FromFormat("<cparam '%c' at %p>",
(unsigned char)self->tag, (void *)self);
}
else {
sprintf(buffer, "<cparam 0x%02x at %p>",
return PyUnicode_FromFormat("<cparam 0x%02x at %p>",
(unsigned char)self->tag, (void *)self);
}
break;
}
return PyUnicode_FromString(buffer);
}

static PyMemberDef PyCArgType_members[] = {
Expand Down