summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2018-12-06 09:41:16 (GMT)
committerGitHub <noreply@github.com>2018-12-06 09:41:16 (GMT)
commitf9d8b686285926c985cfe88a8392a9a497c0a916 (patch)
tree70aed1c33ff4dfbe8170d14a001dd524385a0bab
parentaf1f977575331623547d53247d99be8953a13b9f (diff)
downloadcpython-f9d8b686285926c985cfe88a8392a9a497c0a916.zip
cpython-f9d8b686285926c985cfe88a8392a9a497c0a916.tar.gz
cpython-f9d8b686285926c985cfe88a8392a9a497c0a916.tar.bz2
bpo-35384: The repr of ctypes.CArgObject no longer fails for non-ascii character. (GH-10863)
(cherry picked from commit 3ffa8b9ba190101f674a0e524e482a83ed09cccd) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
-rw-r--r--Lib/ctypes/test/test_bytes.py1
-rw-r--r--Modules/_ctypes/callproc.c26
2 files changed, 23 insertions, 4 deletions
diff --git a/Lib/ctypes/test/test_bytes.py b/Lib/ctypes/test/test_bytes.py
index 20fa056..092ec5a 100644
--- a/Lib/ctypes/test/test_bytes.py
+++ b/Lib/ctypes/test/test_bytes.py
@@ -12,6 +12,7 @@ class BytesTest(unittest.TestCase):
x.value = "y"
c_char.from_param(b"x")
self.assertRaises(TypeError, c_char.from_param, "x")
+ self.assertIn('xbd', repr(c_char.from_param(b"\xbd")))
(c_char * 3)(b"a", b"b", b"c")
self.assertRaises(TypeError, c_char * 3, "a", "b", "c")
diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c
index 04fbc01..d1c190f 100644
--- a/Modules/_ctypes/callproc.c
+++ b/Modules/_ctypes/callproc.c
@@ -452,6 +452,12 @@ PyCArg_dealloc(PyCArgObject *self)
PyObject_Del(self);
}
+static int
+is_literal_char(unsigned char c)
+{
+ return c < 128 && _PyUnicode_IsPrintable(c) && c != '\\' && c != '\'';
+}
+
static PyObject *
PyCArg_repr(PyCArgObject *self)
{
@@ -498,8 +504,14 @@ PyCArg_repr(PyCArgObject *self)
break;
case 'c':
- sprintf(buffer, "<cparam '%c' (%c)>",
- self->tag, self->value.c);
+ if (is_literal_char((unsigned char)self->value.c)) {
+ sprintf(buffer, "<cparam '%c' ('%c')>",
+ self->tag, self->value.c);
+ }
+ else {
+ sprintf(buffer, "<cparam '%c' ('\\x%02x')>",
+ self->tag, (unsigned char)self->value.c);
+ }
break;
/* Hm, are these 'z' and 'Z' codes useful at all?
@@ -514,8 +526,14 @@ PyCArg_repr(PyCArgObject *self)
break;
default:
- sprintf(buffer, "<cparam '%c' at %p>",
- self->tag, self);
+ if (is_literal_char((unsigned char)self->tag)) {
+ sprintf(buffer, "<cparam '%c' at %p>",
+ (unsigned char)self->tag, self);
+ }
+ else {
+ sprintf(buffer, "<cparam 0x%02x at %p>",
+ (unsigned char)self->tag, self);
+ }
break;
}
return PyUnicode_FromString(buffer);