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

Skip to content

Commit 74c29c7

Browse files
committed
Make test_cmd_line_scripts pass by using a unicode string instead of
a bytes string to hold argv0 in RunMainFromImporter(). Also changed the code lay-out a bit to be more readable (for me :-), and print any unexpected errors rather than suppressing them.
1 parent 82f013b commit 74c29c7

1 file changed

Lines changed: 16 additions & 14 deletions

File tree

Modules/main.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -187,28 +187,31 @@ static int RunMainFromImporter(char *filename)
187187
{
188188
PyObject *argv0 = NULL, *importer = NULL;
189189

190-
if (
191-
(argv0 = PyString_FromString(filename)) &&
192-
(importer = PyImport_GetImporter(argv0)) &&
193-
(importer->ob_type != &PyNullImporter_Type))
190+
if ((argv0 = PyUnicode_DecodeFSDefault(filename)) &&
191+
(importer = PyImport_GetImporter(argv0)) &&
192+
(importer->ob_type != &PyNullImporter_Type))
194193
{
195194
/* argv0 is usable as an import source, so
196195
put it in sys.path[0] and import __main__ */
197196
PyObject *sys_path = NULL;
198-
if (
199-
(sys_path = PySys_GetObject("path")) &&
200-
!PyList_SetItem(sys_path, 0, argv0)
201-
) {
197+
if ((sys_path = PySys_GetObject("path")) &&
198+
!PyList_SetItem(sys_path, 0, argv0))
199+
{
202200
Py_INCREF(argv0);
203-
Py_CLEAR(importer);
201+
Py_DECREF(importer);
204202
sys_path = NULL;
205203
return RunModule("__main__", 0) != 0;
206204
}
207205
}
208-
PyErr_Clear();
209-
Py_CLEAR(argv0);
210-
Py_CLEAR(importer);
211-
return -1;
206+
Py_XDECREF(argv0);
207+
Py_XDECREF(importer);
208+
if (PyErr_Occurred()) {
209+
PyErr_Print();
210+
return 1;
211+
}
212+
else {
213+
return -1;
214+
}
212215
}
213216

214217

@@ -590,4 +593,3 @@ Py_GetArgcArgv(int *argc, char ***argv)
590593
#ifdef __cplusplus
591594
}
592595
#endif
593-

0 commit comments

Comments
 (0)