Skip to content

Commit 65ce60a

Browse files
bpo-20180: Simplify char_converter in Argument Clinic. (GH-9828)
Fix also handling non-ascii default values.
1 parent 837c7dc commit 65ce60a

File tree

2 files changed

+19
-32
lines changed

2 files changed

+19
-32
lines changed

Lib/test/clinic.test

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -349,14 +349,15 @@ test_char_converter
349349
k: char = b'?'
350350
l: char = b'\\'
351351
m: char = b'\000'
352+
n: char = b'\377'
352353
/
353354

354355
[clinic start generated code]*/
355356

356357
PyDoc_STRVAR(test_char_converter__doc__,
357358
"test_char_converter($module, a=b\'A\', b=b\'\\x07\', c=b\'\\x08\', d=b\'\\t\',\n"
358359
" e=b\'\\n\', f=b\'\\x0b\', g=b\'\\x0c\', h=b\'\\r\', i=b\'\"\',\n"
359-
" j=b\"\'\", k=b\'?\', l=b\'\\\\\', m=b\'\\x00\', /)\n"
360+
" j=b\"\'\", k=b\'?\', l=b\'\\\\\', m=b\'\\x00\', n=b\'\\xff\', /)\n"
360361
"--\n"
361362
"\n");
362363

@@ -366,31 +367,32 @@ PyDoc_STRVAR(test_char_converter__doc__,
366367
static PyObject *
367368
test_char_converter_impl(PyObject *module, char a, char b, char c, char d,
368369
char e, char f, char g, char h, char i, char j,
369-
char k, char l, char m);
370+
char k, char l, char m, char n);
370371

371372
static PyObject *
372373
test_char_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
373374
{
374375
PyObject *return_value = NULL;
375376
char a = 'A';
376-
char b = '\a';
377-
char c = '\b';
377+
char b = '\x07';
378+
char c = '\x08';
378379
char d = '\t';
379380
char e = '\n';
380-
char f = '\v';
381-
char g = '\f';
381+
char f = '\x0b';
382+
char g = '\x0c';
382383
char h = '\r';
383-
char i = '\"';
384+
char i = '"';
384385
char j = '\'';
385-
char k = '\?';
386+
char k = '?';
386387
char l = '\\';
387-
char m = '\0';
388+
char m = '\x00';
389+
char n = '\xff';
388390

389-
if (!_PyArg_ParseStack(args, nargs, "|ccccccccccccc:test_char_converter",
390-
&a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k, &l, &m)) {
391+
if (!_PyArg_ParseStack(args, nargs, "|cccccccccccccc:test_char_converter",
392+
&a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k, &l, &m, &n)) {
391393
goto exit;
392394
}
393-
return_value = test_char_converter_impl(module, a, b, c, d, e, f, g, h, i, j, k, l, m);
395+
return_value = test_char_converter_impl(module, a, b, c, d, e, f, g, h, i, j, k, l, m, n);
394396

395397
exit:
396398
return return_value;
@@ -399,8 +401,8 @@ exit:
399401
static PyObject *
400402
test_char_converter_impl(PyObject *module, char a, char b, char c, char d,
401403
char e, char f, char g, char h, char i, char j,
402-
char k, char l, char m)
403-
/*[clinic end generated code: output=d9b268767e933c77 input=40431047c768ec24]*/
404+
char k, char l, char m, char n)
405+
/*[clinic end generated code: output=14c61e8ee78f3d47 input=e42330417a44feac]*/
404406

405407
/*[clinic input]
406408
test_unsigned_char_converter

Tools/clinic/clinic.py

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2562,29 +2562,14 @@ class char_converter(CConverter):
25622562
format_unit = 'c'
25632563
c_ignored_default = "'\0'"
25642564

2565-
# characters which need to be escaped in C code
2566-
_escapes = {x: r'\%d' % x for x in range(7)}
2567-
_escapes.update({
2568-
0x07: r'\a',
2569-
0x08: r'\b',
2570-
0x09: r'\t',
2571-
0x0A: r'\n',
2572-
0x0B: r'\v',
2573-
0x0C: r'\f',
2574-
0x0D: r'\r',
2575-
0x22: r'\"',
2576-
0x27: r'\'',
2577-
0x3F: r'\?',
2578-
0x5C: r'\\',
2579-
})
2580-
25812565
def converter_init(self):
25822566
if isinstance(self.default, self.default_type):
25832567
if len(self.default) != 1:
25842568
fail("char_converter: illegal default value " + repr(self.default))
25852569

2586-
c_ord = self.default[0]
2587-
self.c_default = "'%s'" % self._escapes.get(c_ord, chr(c_ord))
2570+
self.c_default = repr(bytes(self.default))[1:]
2571+
if self.c_default == '"\'"':
2572+
self.c_default = r"'\''"
25882573

25892574

25902575
@add_legacy_c_converter('B', bitwise=True)

0 commit comments

Comments
 (0)