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

Skip to content

Commit a62207c

Browse files
author
Victor Stinner
committed
Issue #9425: Create run_command() subfunction
Use PyUnicode_AsUTF8String() instead of _PyUnicode_AsString()
1 parent 6c6f851 commit a62207c

1 file changed

Lines changed: 23 additions & 15 deletions

File tree

Modules/main.c

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,28 @@ static int RunMainFromImporter(wchar_t *filename)
253253
}
254254
}
255255

256+
static int
257+
run_command(wchar_t *command, PyCompilerFlags *cf)
258+
{
259+
PyObject *unicode, *bytes;
260+
int ret;
261+
262+
unicode = PyUnicode_FromWideChar(command, -1);
263+
if (unicode == NULL)
264+
goto error;
265+
bytes = PyUnicode_AsUTF8String(unicode);
266+
Py_DECREF(unicode);
267+
if (bytes == NULL)
268+
goto error;
269+
ret = PyRun_SimpleStringFlags(PyBytes_AsString(bytes), cf);
270+
Py_DECREF(bytes);
271+
return ret != 0;
272+
273+
error:
274+
PyErr_Print();
275+
return 1;
276+
}
277+
256278

257279
/* Main program */
258280

@@ -564,22 +586,8 @@ Py_Main(int argc, wchar_t **argv)
564586
}
565587

566588
if (command) {
567-
char *commandStr;
568-
PyObject *commandObj = PyUnicode_FromWideChar(
569-
command, wcslen(command));
589+
sts = run_command(command, &cf);
570590
free(command);
571-
if (commandObj != NULL)
572-
commandStr = _PyUnicode_AsString(commandObj);
573-
else
574-
commandStr = NULL;
575-
if (commandStr != NULL) {
576-
sts = PyRun_SimpleStringFlags(commandStr, &cf) != 0;
577-
Py_DECREF(commandObj);
578-
}
579-
else {
580-
PyErr_Print();
581-
sts = 1;
582-
}
583591
} else if (module) {
584592
sts = RunModule(module, 1);
585593
}

0 commit comments

Comments
 (0)