diff options
author | Benjamin Peterson <benjamin@python.org> | 2021-01-18 20:47:05 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-18 20:47:05 (GMT) |
commit | 916610ef90a0d0761f08747f7b0905541f0977c7 (patch) | |
tree | 9babe01acffa1ce7dc90989bc31da73a626dabc2 /Modules/_ctypes | |
parent | 6a809fa01f59bb00f4029d35d132cd87553554c3 (diff) | |
download | cpython-916610ef90a0d0761f08747f7b0905541f0977c7.zip cpython-916610ef90a0d0761f08747f7b0905541f0977c7.tar.gz cpython-916610ef90a0d0761f08747f7b0905541f0977c7.tar.bz2 |
closes bpo-42938: Replace snprintf with Python unicode formatting in ctypes param reprs. (24239)
Diffstat (limited to 'Modules/_ctypes')
-rw-r--r-- | Modules/_ctypes/callproc.c | 51 |
1 files changed, 19 insertions, 32 deletions
diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c index 40a05a4..56ccc2f 100644 --- a/Modules/_ctypes/callproc.c +++ b/Modules/_ctypes/callproc.c @@ -487,58 +487,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 @@ -547,22 +536,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[] = { |