bpo-20180: Simplify char_converter in Argument Clinic. (GH-9828)

Fix also handling non-ascii default values.
This commit is contained in:
Serhiy Storchaka 2018-12-25 11:10:05 +02:00 committed by GitHub
parent 837c7dc1ed
commit 65ce60aef1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 32 deletions

View File

@ -349,6 +349,7 @@ test_char_converter
k: char = b'?' k: char = b'?'
l: char = b'\\' l: char = b'\\'
m: char = b'\000' m: char = b'\000'
n: char = b'\377'
/ /
[clinic start generated code]*/ [clinic start generated code]*/
@ -356,7 +357,7 @@ test_char_converter
PyDoc_STRVAR(test_char_converter__doc__, PyDoc_STRVAR(test_char_converter__doc__,
"test_char_converter($module, a=b\'A\', b=b\'\\x07\', c=b\'\\x08\', d=b\'\\t\',\n" "test_char_converter($module, a=b\'A\', b=b\'\\x07\', c=b\'\\x08\', d=b\'\\t\',\n"
" e=b\'\\n\', f=b\'\\x0b\', g=b\'\\x0c\', h=b\'\\r\', i=b\'\"\',\n" " e=b\'\\n\', f=b\'\\x0b\', g=b\'\\x0c\', h=b\'\\r\', i=b\'\"\',\n"
" j=b\"\'\", k=b\'?\', l=b\'\\\\\', m=b\'\\x00\', /)\n" " j=b\"\'\", k=b\'?\', l=b\'\\\\\', m=b\'\\x00\', n=b\'\\xff\', /)\n"
"--\n" "--\n"
"\n"); "\n");
@ -366,31 +367,32 @@ PyDoc_STRVAR(test_char_converter__doc__,
static PyObject * static PyObject *
test_char_converter_impl(PyObject *module, char a, char b, char c, char d, test_char_converter_impl(PyObject *module, char a, char b, char c, char d,
char e, char f, char g, char h, char i, char j, char e, char f, char g, char h, char i, char j,
char k, char l, char m); char k, char l, char m, char n);
static PyObject * static PyObject *
test_char_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs) test_char_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
{ {
PyObject *return_value = NULL; PyObject *return_value = NULL;
char a = 'A'; char a = 'A';
char b = '\a'; char b = '\x07';
char c = '\b'; char c = '\x08';
char d = '\t'; char d = '\t';
char e = '\n'; char e = '\n';
char f = '\v'; char f = '\x0b';
char g = '\f'; char g = '\x0c';
char h = '\r'; char h = '\r';
char i = '\"'; char i = '"';
char j = '\''; char j = '\'';
char k = '\?'; char k = '?';
char l = '\\'; char l = '\\';
char m = '\0'; char m = '\x00';
char n = '\xff';
if (!_PyArg_ParseStack(args, nargs, "|ccccccccccccc:test_char_converter", if (!_PyArg_ParseStack(args, nargs, "|cccccccccccccc:test_char_converter",
&a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k, &l, &m)) { &a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k, &l, &m, &n)) {
goto exit; goto exit;
} }
return_value = test_char_converter_impl(module, a, b, c, d, e, f, g, h, i, j, k, l, m); return_value = test_char_converter_impl(module, a, b, c, d, e, f, g, h, i, j, k, l, m, n);
exit: exit:
return return_value; return return_value;
@ -399,8 +401,8 @@ exit:
static PyObject * static PyObject *
test_char_converter_impl(PyObject *module, char a, char b, char c, char d, test_char_converter_impl(PyObject *module, char a, char b, char c, char d,
char e, char f, char g, char h, char i, char j, char e, char f, char g, char h, char i, char j,
char k, char l, char m) char k, char l, char m, char n)
/*[clinic end generated code: output=d9b268767e933c77 input=40431047c768ec24]*/ /*[clinic end generated code: output=14c61e8ee78f3d47 input=e42330417a44feac]*/
/*[clinic input] /*[clinic input]
test_unsigned_char_converter test_unsigned_char_converter

View File

@ -2562,29 +2562,14 @@ class char_converter(CConverter):
format_unit = 'c' format_unit = 'c'
c_ignored_default = "'\0'" c_ignored_default = "'\0'"
# characters which need to be escaped in C code
_escapes = {x: r'\%d' % x for x in range(7)}
_escapes.update({
0x07: r'\a',
0x08: r'\b',
0x09: r'\t',
0x0A: r'\n',
0x0B: r'\v',
0x0C: r'\f',
0x0D: r'\r',
0x22: r'\"',
0x27: r'\'',
0x3F: r'\?',
0x5C: r'\\',
})
def converter_init(self): def converter_init(self):
if isinstance(self.default, self.default_type): if isinstance(self.default, self.default_type):
if len(self.default) != 1: if len(self.default) != 1:
fail("char_converter: illegal default value " + repr(self.default)) fail("char_converter: illegal default value " + repr(self.default))
c_ord = self.default[0] self.c_default = repr(bytes(self.default))[1:]
self.c_default = "'%s'" % self._escapes.get(c_ord, chr(c_ord)) if self.c_default == '"\'"':
self.c_default = r"'\''"
@add_legacy_c_converter('B', bitwise=True) @add_legacy_c_converter('B', bitwise=True)