Skip to content

bpo-20180: Simplify char_converter in Argument Clinic. #9828

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
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
30 changes: 16 additions & 14 deletions Lib/test/clinic.test
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,15 @@ test_char_converter
k: char = b'?'
l: char = b'\\'
m: char = b'\000'
n: char = b'\377'
/

[clinic start generated code]*/

PyDoc_STRVAR(test_char_converter__doc__,
"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"
" 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");

Expand All @@ -366,31 +367,32 @@ PyDoc_STRVAR(test_char_converter__doc__,
static PyObject *
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 k, char l, char m);
char k, char l, char m, char n);

static PyObject *
test_char_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
{
PyObject *return_value = NULL;
char a = 'A';
char b = '\a';
char c = '\b';
char b = '\x07';
char c = '\x08';
char d = '\t';
char e = '\n';
char f = '\v';
char g = '\f';
char f = '\x0b';
char g = '\x0c';
char h = '\r';
char i = '\"';
char i = '"';
char j = '\'';
char k = '\?';
char k = '?';
char l = '\\';
char m = '\0';
char m = '\x00';
char n = '\xff';

if (!_PyArg_ParseStack(args, nargs, "|ccccccccccccc:test_char_converter",
&a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k, &l, &m)) {
if (!_PyArg_ParseStack(args, nargs, "|cccccccccccccc:test_char_converter",
&a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k, &l, &m, &n)) {
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:
return return_value;
Expand All @@ -399,8 +401,8 @@ exit:
static PyObject *
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 k, char l, char m)
/*[clinic end generated code: output=d9b268767e933c77 input=40431047c768ec24]*/
char k, char l, char m, char n)
/*[clinic end generated code: output=14c61e8ee78f3d47 input=e42330417a44feac]*/

/*[clinic input]
test_unsigned_char_converter
Expand Down
21 changes: 3 additions & 18 deletions Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2562,29 +2562,14 @@ class char_converter(CConverter):
format_unit = 'c'
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):
if isinstance(self.default, self.default_type):
if len(self.default) != 1:
fail("char_converter: illegal default value " + repr(self.default))

c_ord = self.default[0]
self.c_default = "'%s'" % self._escapes.get(c_ord, chr(c_ord))
self.c_default = repr(bytes(self.default))[1:]
if self.c_default == '"\'"':
self.c_default = r"'\''"


@add_legacy_c_converter('B', bitwise=True)
Expand Down