Skip to content

Commit 7f2b8e2

Browse files
committed
pythongh-105922: Refactor PyRun_InteractiveOneObjectEx()
Refactor PyRun_InteractiveOneObjectEx(), _PyRun_SimpleFileObject() and PyRun_SimpleStringFlags(): * Declare variables closer to where they are defined * Rename variables to use name longer than 1 character * Use PyImport_AddModule() with Py_XNewRef()
1 parent 1858db7 commit 7f2b8e2

File tree

1 file changed

+108
-74
lines changed

1 file changed

+108
-74
lines changed

Python/pythonrun.c

Lines changed: 108 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -179,90 +179,118 @@ PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flag
179179
}
180180

181181

182-
/* A PyRun_InteractiveOneObject() auxiliary function that does not print the
183-
* error on failure. */
182+
// Call _PyParser_ASTFromFile() with sys.stdin.encoding, sys.ps1 and sys.ps2
184183
static int
185-
PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename,
186-
PyCompilerFlags *flags)
184+
pyrun_one_parse_ast(FILE *fp, PyObject *filename,
185+
PyCompilerFlags *flags, PyArena *arena, mod_ty *pmod)
187186
{
188-
PyObject *m, *d, *v, *w, *oenc = NULL;
189-
mod_ty mod;
190-
PyArena *arena;
191-
const char *ps1 = "", *ps2 = "", *enc = NULL;
192-
int errcode = 0;
193187
PyThreadState *tstate = _PyThreadState_GET();
194188

189+
// Get sys.stdin.encoding (as UTF-8)
190+
PyObject *attr; // borrowed ref
191+
PyObject *encoding_obj = NULL;
192+
const char *encoding = NULL;
195193
if (fp == stdin) {
196-
/* Fetch encoding from sys.stdin if possible. */
197-
v = _PySys_GetAttr(tstate, &_Py_ID(stdin));
198-
if (v && v != Py_None) {
199-
oenc = PyObject_GetAttr(v, &_Py_ID(encoding));
200-
if (oenc)
201-
enc = PyUnicode_AsUTF8(oenc);
202-
if (!enc)
203-
PyErr_Clear();
194+
attr = _PySys_GetAttr(tstate, &_Py_ID(stdin));
195+
if (attr && attr != Py_None) {
196+
encoding_obj = PyObject_GetAttr(attr, &_Py_ID(encoding));
197+
if (encoding_obj) {
198+
encoding = PyUnicode_AsUTF8(encoding_obj);
199+
if (!encoding) {
200+
PyErr_Clear();
201+
}
202+
}
204203
}
205204
}
206-
v = _PySys_GetAttr(tstate, &_Py_ID(ps1));
207-
if (v != NULL) {
208-
v = PyObject_Str(v);
209-
if (v == NULL)
205+
206+
// Get sys.ps1 (as UTF-8)
207+
attr = _PySys_GetAttr(tstate, &_Py_ID(ps1));
208+
PyObject *ps1_obj = NULL;
209+
const char *ps1 = "";
210+
if (attr != NULL) {
211+
ps1_obj = PyObject_Str(attr);
212+
if (ps1_obj == NULL) {
210213
PyErr_Clear();
211-
else if (PyUnicode_Check(v)) {
212-
ps1 = PyUnicode_AsUTF8(v);
214+
}
215+
else if (PyUnicode_Check(ps1_obj)) {
216+
ps1 = PyUnicode_AsUTF8(ps1_obj);
213217
if (ps1 == NULL) {
214218
PyErr_Clear();
215219
ps1 = "";
216220
}
217221
}
218222
}
219-
w = _PySys_GetAttr(tstate, &_Py_ID(ps2));
220-
if (w != NULL) {
221-
w = PyObject_Str(w);
222-
if (w == NULL)
223+
224+
// Get sys.ps2 (as UTF-8)
225+
attr = _PySys_GetAttr(tstate, &_Py_ID(ps2));
226+
PyObject *ps2_obj = NULL;
227+
const char *ps2 = "";
228+
if (attr != NULL) {
229+
ps2_obj = PyObject_Str(attr);
230+
if (ps2_obj == NULL) {
223231
PyErr_Clear();
224-
else if (PyUnicode_Check(w)) {
225-
ps2 = PyUnicode_AsUTF8(w);
232+
}
233+
else if (PyUnicode_Check(ps2_obj)) {
234+
ps2 = PyUnicode_AsUTF8(ps2_obj);
226235
if (ps2 == NULL) {
227236
PyErr_Clear();
228237
ps2 = "";
229238
}
230239
}
231240
}
232-
arena = _PyArena_New();
233-
if (arena == NULL) {
234-
Py_XDECREF(v);
235-
Py_XDECREF(w);
236-
Py_XDECREF(oenc);
237-
return -1;
238-
}
239-
240-
mod = _PyParser_ASTFromFile(fp, filename, enc, Py_single_input,
241-
ps1, ps2, flags, &errcode, arena);
242241

243-
Py_XDECREF(v);
244-
Py_XDECREF(w);
245-
Py_XDECREF(oenc);
246-
if (mod == NULL) {
247-
_PyArena_Free(arena);
242+
int errcode = 0;
243+
*pmod = _PyParser_ASTFromFile(fp, filename, encoding,
244+
Py_single_input, ps1, ps2,
245+
flags, &errcode, arena);
246+
Py_XDECREF(ps1_obj);
247+
Py_XDECREF(ps2_obj);
248+
Py_XDECREF(encoding_obj);
249+
250+
if (*pmod == NULL) {
248251
if (errcode == E_EOF) {
249252
PyErr_Clear();
250253
return E_EOF;
251254
}
252255
return -1;
253256
}
254-
m = PyImport_AddModuleObject(&_Py_ID(__main__));
255-
if (m == NULL) {
257+
return 0;
258+
}
259+
260+
261+
/* A PyRun_InteractiveOneObject() auxiliary function that does not print the
262+
* error on failure. */
263+
static int
264+
PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename,
265+
PyCompilerFlags *flags)
266+
{
267+
PyArena *arena = _PyArena_New();
268+
if (arena == NULL) {
269+
return -1;
270+
}
271+
272+
mod_ty mod;
273+
int parse_res = pyrun_one_parse_ast(fp, filename, flags, arena, &mod);
274+
if (parse_res != 0) {
275+
_PyArena_Free(arena);
276+
return parse_res;
277+
}
278+
279+
PyObject *module = Py_XNewRef(PyImport_AddModuleObject(&_Py_ID(__main__)));
280+
if (module == NULL) {
256281
_PyArena_Free(arena);
257282
return -1;
258283
}
259-
d = PyModule_GetDict(m);
260-
v = run_mod(mod, filename, d, d, flags, arena);
284+
PyObject *mod_dict = PyModule_GetDict(module); // borrowed ref
285+
286+
PyObject *res = run_mod(mod, filename, mod_dict, mod_dict, flags, arena);
261287
_PyArena_Free(arena);
262-
if (v == NULL) {
288+
Py_DECREF(module);
289+
if (res == NULL) {
263290
return -1;
264291
}
265-
Py_DECREF(v);
292+
Py_DECREF(res);
293+
266294
flush_io();
267295
return 0;
268296
}
@@ -376,22 +404,22 @@ int
376404
_PyRun_SimpleFileObject(FILE *fp, PyObject *filename, int closeit,
377405
PyCompilerFlags *flags)
378406
{
379-
PyObject *m, *d, *v;
380-
int set_file_name = 0, ret = -1;
407+
int ret = -1;
381408

382-
m = PyImport_AddModule("__main__");
383-
if (m == NULL)
409+
PyObject *module = Py_XNewRef(PyImport_AddModule("__main__"));
410+
if (module == NULL)
384411
return -1;
385-
Py_INCREF(m);
386-
d = PyModule_GetDict(m);
387-
if (_PyDict_GetItemStringWithError(d, "__file__") == NULL) {
412+
PyObject *dict = PyModule_GetDict(module); // borrowed ref
413+
414+
int set_file_name = 0;
415+
if (_PyDict_GetItemStringWithError(dict, "__file__") == NULL) {
388416
if (PyErr_Occurred()) {
389417
goto done;
390418
}
391-
if (PyDict_SetItemString(d, "__file__", filename) < 0) {
419+
if (PyDict_SetItemString(dict, "__file__", filename) < 0) {
392420
goto done;
393421
}
394-
if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) {
422+
if (PyDict_SetItemString(dict, "__cached__", Py_None) < 0) {
395423
goto done;
396424
}
397425
set_file_name = 1;
@@ -402,6 +430,7 @@ _PyRun_SimpleFileObject(FILE *fp, PyObject *filename, int closeit,
402430
goto done;
403431
}
404432

433+
PyObject *v;
405434
if (pyc) {
406435
FILE *pyc_fp;
407436
/* Try to run a pyc file. First, re-open in binary */
@@ -415,42 +444,43 @@ _PyRun_SimpleFileObject(FILE *fp, PyObject *filename, int closeit,
415444
goto done;
416445
}
417446

418-
if (set_main_loader(d, filename, "SourcelessFileLoader") < 0) {
447+
if (set_main_loader(dict, filename, "SourcelessFileLoader") < 0) {
419448
fprintf(stderr, "python: failed to set __main__.__loader__\n");
420449
ret = -1;
421450
fclose(pyc_fp);
422451
goto done;
423452
}
424-
v = run_pyc_file(pyc_fp, d, d, flags);
453+
v = run_pyc_file(pyc_fp, dict, dict, flags);
425454
} else {
426455
/* When running from stdin, leave __main__.__loader__ alone */
427456
if (PyUnicode_CompareWithASCIIString(filename, "<stdin>") != 0 &&
428-
set_main_loader(d, filename, "SourceFileLoader") < 0) {
457+
set_main_loader(dict, filename, "SourceFileLoader") < 0) {
429458
fprintf(stderr, "python: failed to set __main__.__loader__\n");
430459
ret = -1;
431460
goto done;
432461
}
433-
v = pyrun_file(fp, filename, Py_file_input, d, d,
462+
v = pyrun_file(fp, filename, Py_file_input, dict, dict,
434463
closeit, flags);
435464
}
436465
flush_io();
437466
if (v == NULL) {
438-
Py_CLEAR(m);
467+
Py_CLEAR(module);
439468
PyErr_Print();
440469
goto done;
441470
}
442471
Py_DECREF(v);
443472
ret = 0;
473+
444474
done:
445475
if (set_file_name) {
446-
if (PyDict_DelItemString(d, "__file__")) {
476+
if (PyDict_DelItemString(dict, "__file__")) {
447477
PyErr_Clear();
448478
}
449-
if (PyDict_DelItemString(d, "__cached__")) {
479+
if (PyDict_DelItemString(dict, "__cached__")) {
450480
PyErr_Clear();
451481
}
452482
}
453-
Py_XDECREF(m);
483+
Py_XDECREF(module);
454484
return ret;
455485
}
456486

@@ -472,17 +502,21 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
472502
int
473503
PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
474504
{
475-
PyObject *m, *d, *v;
476-
m = PyImport_AddModule("__main__");
477-
if (m == NULL)
505+
PyObject *module = Py_XNewRef(PyImport_AddModule("__main__"));
506+
if (module == NULL) {
478507
return -1;
479-
d = PyModule_GetDict(m);
480-
v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
481-
if (v == NULL) {
508+
}
509+
PyObject *dict = PyModule_GetDict(module); // borrowed ref
510+
511+
PyObject *res = PyRun_StringFlags(command, Py_file_input,
512+
dict, dict, flags);
513+
Py_DECREF(module);
514+
if (res == NULL) {
482515
PyErr_Print();
483516
return -1;
484517
}
485-
Py_DECREF(v);
518+
519+
Py_DECREF(res);
486520
return 0;
487521
}
488522

0 commit comments

Comments
 (0)