Skip to content

Commit cdf1ef3

Browse files
committed
refactor _PyRun_SimpleStringFlagsWithName to follow goto pattern in _PyRun_SimpleFileObject
1 parent a57209e commit cdf1ef3

1 file changed

Lines changed: 16 additions & 9 deletions

File tree

Python/pythonrun.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -554,32 +554,39 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
554554

555555
int
556556
_PyRun_SimpleStringFlagsWithName(const char *command, const char* name, PyCompilerFlags *flags) {
557+
int ret = -1;
558+
557559
PyObject *main_module = PyImport_AddModuleRef("__main__");
558560
if (main_module == NULL) {
559-
return -1;
561+
return ret;
560562
}
561563
PyObject *dict = PyModule_GetDict(main_module); // borrowed ref
562564

565+
PyObject *the_name = NULL;
563566
PyObject *res = NULL;
564567
if (name == NULL) {
565568
res = PyRun_StringFlags(command, Py_file_input, dict, dict, flags);
566569
} else {
567-
PyObject* the_name = PyUnicode_FromString(name);
568-
if (!the_name) {
570+
the_name = PyUnicode_FromString(name);
571+
if (the_name == NULL) {
569572
PyErr_Print();
570-
return -1;
573+
goto done;
571574
}
572575
res = _PyRun_StringFlagsWithName(command, the_name, Py_file_input, dict, dict, flags, 0);
573-
Py_DECREF(the_name);
574576
}
575-
Py_DECREF(main_module);
576577
if (res == NULL) {
577578
PyErr_Print();
578-
return -1;
579+
goto done;
579580
}
580581

581-
Py_DECREF(res);
582-
return 0;
582+
ret = 0;
583+
584+
done:
585+
Py_DECREF(main_module);
586+
Py_XDECREF(the_name);
587+
Py_XDECREF(res);
588+
589+
return ret;
583590
}
584591

585592
int

0 commit comments

Comments
 (0)