Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0a3ddad

Browse files
author
Victor Stinner
committed
Issue #9425: Create run_file() subfunction
* Call Py_MakePendingCalls() before converting the filename from wchar_t* to char* * Use PyUnicode_AsUTF8String() instead of _PyUnicode_AsString()
1 parent af43e9a commit 0a3ddad

1 file changed

Lines changed: 36 additions & 24 deletions

File tree

Modules/main.c

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,40 @@ run_command(wchar_t *command, PyCompilerFlags *cf)
275275
return 1;
276276
}
277277

278+
static int
279+
run_file(FILE *fp, const wchar_t *filename, PyCompilerFlags *p_cf)
280+
{
281+
PyObject *unicode, *bytes = NULL;
282+
char *filename_str;
283+
int run;
284+
285+
/* call pending calls like signal handlers (SIGINT) */
286+
if (Py_MakePendingCalls() == -1) {
287+
PyErr_Print();
288+
return 1;
289+
}
290+
291+
if (filename) {
292+
unicode = PyUnicode_FromWideChar(filename, wcslen(filename));
293+
if (unicode != NULL) {
294+
bytes = PyUnicode_AsUTF8String(unicode);
295+
Py_DECREF(unicode);
296+
}
297+
if (bytes != NULL)
298+
filename_str = PyBytes_AsString(bytes);
299+
else {
300+
PyErr_Clear();
301+
filename_str = "<decoding error>";
302+
}
303+
}
304+
else
305+
filename_str = "<stdin>";
306+
307+
run = PyRun_AnyFileExFlags(fp, filename_str, filename != NULL, p_cf);
308+
Py_XDECREF(bytes);
309+
return run != 0;
310+
}
311+
278312

279313
/* Main program */
280314

@@ -644,30 +678,8 @@ Py_Main(int argc, wchar_t **argv)
644678
}
645679
}
646680

647-
if (sts==-1) {
648-
PyObject *filenameObj = NULL;
649-
char *p_cfilename = "<stdin>";
650-
if (filename) {
651-
filenameObj = PyUnicode_FromWideChar(
652-
filename, wcslen(filename));
653-
if (filenameObj != NULL)
654-
p_cfilename = _PyUnicode_AsString(filenameObj);
655-
else
656-
p_cfilename = "<decoding error>";
657-
}
658-
/* call pending calls like signal handlers (SIGINT) */
659-
if (Py_MakePendingCalls() == -1) {
660-
PyErr_Print();
661-
sts = 1;
662-
} else {
663-
sts = PyRun_AnyFileExFlags(
664-
fp,
665-
p_cfilename,
666-
filename != NULL, &cf) != 0;
667-
}
668-
Py_XDECREF(filenameObj);
669-
}
670-
681+
if (sts == -1)
682+
sts = run_file(fp, filename, &cf);
671683
}
672684

673685
/* Check this environment variable at the end, to give programs the

0 commit comments

Comments
 (0)