Skip to content

Commit b5cf308

Browse files
authored
bpo-32381: Fix PyRun_SimpleFileExFlags() encoding (GH-23642) (GH-23692) (GH-23696)
Fix encoding name when running a ".pyc" file on Windows: PyRun_SimpleFileExFlags() now uses the correct encoding to decode the filename. * Add pyrun_file() subfunction. * Add pyrun_simple_file() subfunction. * PyRun_SimpleFileExFlags() now calls _Py_fopen_obj() rather than _Py_fopen(). (cherry picked from commit b6d98c1) (cherry picked from commit f0e42ae)
1 parent 801f351 commit b5cf308

2 files changed

Lines changed: 135 additions & 92 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix encoding name when running a ``.pyc`` file on Windows:
2+
:c:func:`PyRun_SimpleFileExFlags()` now uses the correct encoding to decode
3+
the filename.

‎Python/pythonrun.c‎

Lines changed: 132 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,15 @@ extern grammar _PyParser_Grammar; /* From graminit.c */
6363
static void flush_io(void);
6464
static PyObject *run_mod(mod_ty, PyObject *, PyObject *, PyObject *,
6565
PyCompilerFlags *, PyArena *);
66-
static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
66+
static PyObject *run_pyc_file(FILE *, PyObject *, PyObject *,
6767
PyCompilerFlags *);
6868
static void err_input(perrdetail *);
6969
static void err_free(perrdetail *);
7070
static int PyRun_InteractiveOneObjectEx(FILE *, PyObject *, PyCompilerFlags *);
71+
static PyObject* pyrun_file(FILE *fp, PyObject *filename, int start,
72+
PyObject *globals, PyObject *locals, int closeit,
73+
PyCompilerFlags *flags);
74+
7175

7276
/* Parse input from a file and execute it */
7377
int
@@ -300,112 +304,119 @@ PyRun_InteractiveOneFlags(FILE *fp, const char *filename_str, PyCompilerFlags *f
300304
the file type, and, if we may close it, at the first few bytes. */
301305

302306
static int
303-
maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
307+
maybe_pyc_file(FILE *fp, PyObject *filename, int closeit)
304308
{
305-
if (strcmp(ext, ".pyc") == 0)
309+
PyObject *ext = PyUnicode_FromString(".pyc");
310+
if (ext == NULL) {
311+
return -1;
312+
}
313+
Py_ssize_t endswith = PyUnicode_Tailmatch(filename, ext, 0, PY_SSIZE_T_MAX, +1);
314+
Py_DECREF(ext);
315+
if (endswith) {
306316
return 1;
317+
}
307318

308319
/* Only look into the file if we are allowed to close it, since
309320
it then should also be seekable. */
310-
if (closeit) {
311-
/* Read only two bytes of the magic. If the file was opened in
312-
text mode, the bytes 3 and 4 of the magic (\r\n) might not
313-
be read as they are on disk. */
314-
unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
315-
unsigned char buf[2];
316-
/* Mess: In case of -x, the stream is NOT at its start now,
317-
and ungetc() was used to push back the first newline,
318-
which makes the current stream position formally undefined,
319-
and a x-platform nightmare.
320-
Unfortunately, we have no direct way to know whether -x
321-
was specified. So we use a terrible hack: if the current
322-
stream position is not 0, we assume -x was specified, and
323-
give up. Bug 132850 on SourceForge spells out the
324-
hopelessness of trying anything else (fseek and ftell
325-
don't work predictably x-platform for text-mode files).
326-
*/
327-
int ispyc = 0;
328-
if (ftell(fp) == 0) {
329-
if (fread(buf, 1, 2, fp) == 2 &&
330-
((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
331-
ispyc = 1;
332-
rewind(fp);
333-
}
334-
return ispyc;
321+
if (!closeit) {
322+
return 0;
335323
}
336-
return 0;
324+
325+
/* Read only two bytes of the magic. If the file was opened in
326+
text mode, the bytes 3 and 4 of the magic (\r\n) might not
327+
be read as they are on disk. */
328+
unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
329+
unsigned char buf[2];
330+
/* Mess: In case of -x, the stream is NOT at its start now,
331+
and ungetc() was used to push back the first newline,
332+
which makes the current stream position formally undefined,
333+
and a x-platform nightmare.
334+
Unfortunately, we have no direct way to know whether -x
335+
was specified. So we use a terrible hack: if the current
336+
stream position is not 0, we assume -x was specified, and
337+
give up. Bug 132850 on SourceForge spells out the
338+
hopelessness of trying anything else (fseek and ftell
339+
don't work predictably x-platform for text-mode files).
340+
*/
341+
int ispyc = 0;
342+
if (ftell(fp) == 0) {
343+
if (fread(buf, 1, 2, fp) == 2 &&
344+
((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
345+
ispyc = 1;
346+
rewind(fp);
347+
}
348+
return ispyc;
337349
}
338350

351+
339352
static int
340-
set_main_loader(PyObject *d, const char *filename, const char *loader_name)
353+
set_main_loader(PyObject *d, PyObject *filename, const char *loader_name)
341354
{
342-
PyObject *filename_obj, *bootstrap, *loader_type = NULL, *loader;
343-
int result = 0;
344-
345-
filename_obj = PyUnicode_DecodeFSDefault(filename);
346-
if (filename_obj == NULL)
347-
return -1;
348355
PyInterpreterState *interp = _PyInterpreterState_Get();
349-
bootstrap = PyObject_GetAttrString(interp->importlib,
350-
"_bootstrap_external");
351-
if (bootstrap != NULL) {
352-
loader_type = PyObject_GetAttrString(bootstrap, loader_name);
353-
Py_DECREF(bootstrap);
356+
PyObject *bootstrap = PyObject_GetAttrString(interp->importlib,
357+
"_bootstrap_external");
358+
if (bootstrap == NULL) {
359+
return -1;
354360
}
361+
362+
PyObject *loader_type = PyObject_GetAttrString(bootstrap, loader_name);
363+
Py_DECREF(bootstrap);
355364
if (loader_type == NULL) {
356-
Py_DECREF(filename_obj);
357365
return -1;
358366
}
359-
loader = PyObject_CallFunction(loader_type, "sN", "__main__", filename_obj);
367+
368+
PyObject *loader = PyObject_CallFunction(loader_type,
369+
"sO", "__main__", filename);
360370
Py_DECREF(loader_type);
361371
if (loader == NULL) {
362372
return -1;
363373
}
374+
364375
if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
365-
result = -1;
376+
Py_DECREF(loader);
377+
return -1;
366378
}
367379
Py_DECREF(loader);
368-
return result;
380+
return 0;
369381
}
370382

371-
int
372-
PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
373-
PyCompilerFlags *flags)
383+
384+
static int
385+
pyrun_simple_file(FILE *fp, PyObject *filename, int closeit,
386+
PyCompilerFlags *flags)
374387
{
375388
PyObject *m, *d, *v;
376-
const char *ext;
377389
int set_file_name = 0, ret = -1;
378-
size_t len;
379390

380391
m = PyImport_AddModule("__main__");
381392
if (m == NULL)
382393
return -1;
383394
Py_INCREF(m);
384395
d = PyModule_GetDict(m);
385396
if (PyDict_GetItemString(d, "__file__") == NULL) {
386-
PyObject *f;
387-
f = PyUnicode_DecodeFSDefault(filename);
388-
if (f == NULL)
389-
goto done;
390-
if (PyDict_SetItemString(d, "__file__", f) < 0) {
391-
Py_DECREF(f);
397+
if (PyDict_SetItemString(d, "__file__", filename) < 0) {
392398
goto done;
393399
}
394400
if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) {
395-
Py_DECREF(f);
396401
goto done;
397402
}
398403
set_file_name = 1;
399-
Py_DECREF(f);
400404
}
401-
len = strlen(filename);
402-
ext = filename + len - (len > 4 ? 4 : 0);
403-
if (maybe_pyc_file(fp, filename, ext, closeit)) {
405+
406+
int pyc = maybe_pyc_file(fp, filename, closeit);
407+
if (pyc < 0) {
408+
goto done;
409+
}
410+
411+
if (pyc) {
404412
FILE *pyc_fp;
405413
/* Try to run a pyc file. First, re-open in binary */
406-
if (closeit)
414+
if (closeit) {
407415
fclose(fp);
408-
if ((pyc_fp = _Py_fopen(filename, "rb")) == NULL) {
416+
}
417+
418+
pyc_fp = _Py_fopen_obj(filename, "rb");
419+
if (pyc_fp == NULL) {
409420
fprintf(stderr, "python: Can't reopen .pyc file\n");
410421
goto done;
411422
}
@@ -416,17 +427,17 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
416427
fclose(pyc_fp);
417428
goto done;
418429
}
419-
v = run_pyc_file(pyc_fp, filename, d, d, flags);
430+
v = run_pyc_file(pyc_fp, d, d, flags);
420431
} else {
421432
/* When running from stdin, leave __main__.__loader__ alone */
422-
if (strcmp(filename, "<stdin>") != 0 &&
433+
if (PyUnicode_CompareWithASCIIString(filename, "<stdin>") != 0 &&
423434
set_main_loader(d, filename, "SourceFileLoader") < 0) {
424435
fprintf(stderr, "python: failed to set __main__.__loader__\n");
425436
ret = -1;
426437
goto done;
427438
}
428-
v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
429-
closeit, flags);
439+
v = pyrun_file(fp, filename, Py_file_input, d, d,
440+
closeit, flags);
430441
}
431442
flush_io();
432443
if (v == NULL) {
@@ -449,6 +460,21 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
449460
return ret;
450461
}
451462

463+
464+
int
465+
PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
466+
PyCompilerFlags *flags)
467+
{
468+
PyObject *filename_obj = PyUnicode_DecodeFSDefault(filename);
469+
if (filename_obj == NULL) {
470+
return -1;
471+
}
472+
int res = pyrun_simple_file(fp, filename_obj, closeit, flags);
473+
Py_DECREF(filename_obj);
474+
return res;
475+
}
476+
477+
452478
int
453479
PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
454480
{
@@ -1036,39 +1062,53 @@ PyRun_StringFlags(const char *str, int start, PyObject *globals,
10361062
return ret;
10371063
}
10381064

1039-
PyObject *
1040-
PyRun_FileExFlags(FILE *fp, const char *filename_str, int start, PyObject *globals,
1041-
PyObject *locals, int closeit, PyCompilerFlags *flags)
1042-
{
1043-
PyObject *ret = NULL;
1044-
mod_ty mod;
1045-
PyArena *arena = NULL;
1046-
PyObject *filename;
10471065

1048-
filename = PyUnicode_DecodeFSDefault(filename_str);
1049-
if (filename == NULL)
1050-
goto exit;
1051-
1052-
arena = PyArena_New();
1053-
if (arena == NULL)
1054-
goto exit;
1066+
static PyObject *
1067+
pyrun_file(FILE *fp, PyObject *filename, int start, PyObject *globals,
1068+
PyObject *locals, int closeit, PyCompilerFlags *flags)
1069+
{
1070+
PyArena *arena = PyArena_New();
1071+
if (arena == NULL) {
1072+
return NULL;
1073+
}
10551074

1075+
mod_ty mod;
10561076
mod = PyParser_ASTFromFileObject(fp, filename, NULL, start, 0, 0,
10571077
flags, NULL, arena);
1058-
if (closeit)
1078+
if (closeit) {
10591079
fclose(fp);
1060-
if (mod == NULL) {
1061-
goto exit;
10621080
}
1063-
ret = run_mod(mod, filename, globals, locals, flags, arena);
10641081

1065-
exit:
1066-
Py_XDECREF(filename);
1067-
if (arena != NULL)
1068-
PyArena_Free(arena);
1082+
PyObject *ret;
1083+
if (mod != NULL) {
1084+
ret = run_mod(mod, filename, globals, locals, flags, arena);
1085+
}
1086+
else {
1087+
ret = NULL;
1088+
}
1089+
PyArena_Free(arena);
1090+
10691091
return ret;
10701092
}
10711093

1094+
1095+
PyObject *
1096+
PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
1097+
PyObject *locals, int closeit, PyCompilerFlags *flags)
1098+
{
1099+
PyObject *filename_obj = PyUnicode_DecodeFSDefault(filename);
1100+
if (filename_obj == NULL) {
1101+
return NULL;
1102+
}
1103+
1104+
PyObject *res = pyrun_file(fp, filename_obj, start, globals,
1105+
locals, closeit, flags);
1106+
Py_DECREF(filename_obj);
1107+
return res;
1108+
1109+
}
1110+
1111+
10721112
static void
10731113
flush_io(void)
10741114
{
@@ -1150,8 +1190,8 @@ run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals,
11501190
}
11511191

11521192
static PyObject *
1153-
run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
1154-
PyObject *locals, PyCompilerFlags *flags)
1193+
run_pyc_file(FILE *fp, PyObject *globals, PyObject *locals,
1194+
PyCompilerFlags *flags)
11551195
{
11561196
PyCodeObject *co;
11571197
PyObject *v;

0 commit comments

Comments
 (0)