Skip to content

Commit bfda4db

Browse files
[3.8] bpo-40179: Fix translation of #elif in Argument Clinic (GH-19364) (GH-19583)
Co-authored-by: Ammar Askar <[email protected]> (cherry picked from commit 12446e6)
1 parent 904dd06 commit bfda4db

File tree

4 files changed

+125
-24
lines changed

4 files changed

+125
-24
lines changed

Lib/test/clinic.test

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3260,3 +3260,108 @@ exit:
32603260
static PyObject *
32613261
test_keyword_only_parameter_impl(PyObject *module, PyBytesObject *co_lnotab)
32623262
/*[clinic end generated code: output=f25914b402039493 input=303df5046c7e37a3]*/
3263+
3264+
3265+
/*[clinic input]
3266+
output push
3267+
output preset buffer
3268+
[clinic start generated code]*/
3269+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=5bff3376ee0df0b5]*/
3270+
3271+
#ifdef CONDITION_A
3272+
/*[clinic input]
3273+
test_preprocessor_guarded_condition_a
3274+
[clinic start generated code]*/
3275+
3276+
static PyObject *
3277+
test_preprocessor_guarded_condition_a_impl(PyObject *module)
3278+
/*[clinic end generated code: output=ad012af18085add6 input=8edb8706a98cda7e]*/
3279+
#elif CONDITION_B
3280+
/*[clinic input]
3281+
test_preprocessor_guarded_elif_condition_b
3282+
[clinic start generated code]*/
3283+
3284+
static PyObject *
3285+
test_preprocessor_guarded_elif_condition_b_impl(PyObject *module)
3286+
/*[clinic end generated code: output=615f2dee82b138d1 input=53777cebbf7fee32]*/
3287+
#else
3288+
/*[clinic input]
3289+
test_preprocessor_guarded_else
3290+
[clinic start generated code]*/
3291+
3292+
static PyObject *
3293+
test_preprocessor_guarded_else_impl(PyObject *module)
3294+
/*[clinic end generated code: output=13af7670aac51b12 input=6657ab31d74c29fc]*/
3295+
#endif
3296+
3297+
/*[clinic input]
3298+
dump buffer
3299+
output pop
3300+
[clinic start generated code]*/
3301+
3302+
#if defined(CONDITION_A)
3303+
3304+
PyDoc_STRVAR(test_preprocessor_guarded_condition_a__doc__,
3305+
"test_preprocessor_guarded_condition_a($module, /)\n"
3306+
"--\n"
3307+
"\n");
3308+
3309+
#define TEST_PREPROCESSOR_GUARDED_CONDITION_A_METHODDEF \
3310+
{"test_preprocessor_guarded_condition_a", (PyCFunction)test_preprocessor_guarded_condition_a, METH_NOARGS, test_preprocessor_guarded_condition_a__doc__},
3311+
3312+
static PyObject *
3313+
test_preprocessor_guarded_condition_a(PyObject *module, PyObject *Py_UNUSED(ignored))
3314+
{
3315+
return test_preprocessor_guarded_condition_a_impl(module);
3316+
}
3317+
3318+
#endif /* defined(CONDITION_A) */
3319+
3320+
#if !defined(CONDITION_A) && (CONDITION_B)
3321+
3322+
PyDoc_STRVAR(test_preprocessor_guarded_elif_condition_b__doc__,
3323+
"test_preprocessor_guarded_elif_condition_b($module, /)\n"
3324+
"--\n"
3325+
"\n");
3326+
3327+
#define TEST_PREPROCESSOR_GUARDED_ELIF_CONDITION_B_METHODDEF \
3328+
{"test_preprocessor_guarded_elif_condition_b", (PyCFunction)test_preprocessor_guarded_elif_condition_b, METH_NOARGS, test_preprocessor_guarded_elif_condition_b__doc__},
3329+
3330+
static PyObject *
3331+
test_preprocessor_guarded_elif_condition_b(PyObject *module, PyObject *Py_UNUSED(ignored))
3332+
{
3333+
return test_preprocessor_guarded_elif_condition_b_impl(module);
3334+
}
3335+
3336+
#endif /* !defined(CONDITION_A) && (CONDITION_B) */
3337+
3338+
#if !defined(CONDITION_A) && !(CONDITION_B)
3339+
3340+
PyDoc_STRVAR(test_preprocessor_guarded_else__doc__,
3341+
"test_preprocessor_guarded_else($module, /)\n"
3342+
"--\n"
3343+
"\n");
3344+
3345+
#define TEST_PREPROCESSOR_GUARDED_ELSE_METHODDEF \
3346+
{"test_preprocessor_guarded_else", (PyCFunction)test_preprocessor_guarded_else, METH_NOARGS, test_preprocessor_guarded_else__doc__},
3347+
3348+
static PyObject *
3349+
test_preprocessor_guarded_else(PyObject *module, PyObject *Py_UNUSED(ignored))
3350+
{
3351+
return test_preprocessor_guarded_else_impl(module);
3352+
}
3353+
3354+
#endif /* !defined(CONDITION_A) && !(CONDITION_B) */
3355+
3356+
#ifndef TEST_PREPROCESSOR_GUARDED_CONDITION_A_METHODDEF
3357+
#define TEST_PREPROCESSOR_GUARDED_CONDITION_A_METHODDEF
3358+
#endif /* !defined(TEST_PREPROCESSOR_GUARDED_CONDITION_A_METHODDEF) */
3359+
3360+
#ifndef TEST_PREPROCESSOR_GUARDED_ELIF_CONDITION_B_METHODDEF
3361+
#define TEST_PREPROCESSOR_GUARDED_ELIF_CONDITION_B_METHODDEF
3362+
#endif /* !defined(TEST_PREPROCESSOR_GUARDED_ELIF_CONDITION_B_METHODDEF) */
3363+
3364+
#ifndef TEST_PREPROCESSOR_GUARDED_ELSE_METHODDEF
3365+
#define TEST_PREPROCESSOR_GUARDED_ELSE_METHODDEF
3366+
#endif /* !defined(TEST_PREPROCESSOR_GUARDED_ELSE_METHODDEF) */
3367+
/*[clinic end generated code: output=3804bb18d454038c input=3fc80c9989d2f2e1]*/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed translation of ``#elif`` in Argument Clinic.

Modules/clinic/posixmodule.c.h

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tools/clinic/cpp.py

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -141,23 +141,15 @@ def pop_stack():
141141
token = fields[0].lower()
142142
condition = ' '.join(fields[1:]).strip()
143143

144-
if_tokens = {'if', 'ifdef', 'ifndef'}
145-
all_tokens = if_tokens | {'elif', 'else', 'endif'}
146-
147-
if token not in all_tokens:
148-
return
149-
150-
# cheat a little here, to reuse the implementation of if
151-
if token == 'elif':
152-
pop_stack()
153-
token = 'if'
154-
155-
if token in if_tokens:
144+
if token in {'if', 'ifdef', 'ifndef', 'elif'}:
156145
if not condition:
157146
self.fail("Invalid format for #" + token + " line: no argument!")
158-
if token == 'if':
147+
if token in {'if', 'elif'}:
159148
if not self.is_a_simple_defined(condition):
160149
condition = "(" + condition + ")"
150+
if token == 'elif':
151+
previous_token, previous_condition = pop_stack()
152+
self.stack.append((previous_token, negate(previous_condition)))
161153
else:
162154
fields = condition.split()
163155
if len(fields) != 1:
@@ -166,18 +158,21 @@ def pop_stack():
166158
condition = 'defined(' + symbol + ')'
167159
if token == 'ifndef':
168160
condition = '!' + condition
161+
token = 'if'
169162

170-
self.stack.append(("if", condition))
171-
if self.verbose:
172-
print(self.status())
173-
return
163+
self.stack.append((token, condition))
174164

175-
previous_token, previous_condition = pop_stack()
165+
elif token == 'else':
166+
previous_token, previous_condition = pop_stack()
167+
self.stack.append((previous_token, negate(previous_condition)))
176168

177-
if token == 'else':
178-
self.stack.append(('else', negate(previous_condition)))
179169
elif token == 'endif':
180-
pass
170+
while pop_stack()[0] != 'if':
171+
pass
172+
173+
else:
174+
return
175+
181176
if self.verbose:
182177
print(self.status())
183178

0 commit comments

Comments
 (0)