bpo-32381: Add _PyRun_AnyFileObject() (GH-23723) · python/cpython@a82f63f · GitHub
Skip to content

Commit a82f63f

Browse files
authored
bpo-32381: Add _PyRun_AnyFileObject() (GH-23723)
pymain_run_file() no longer encodes the filename: pass the filename as an object to the new _PyRun_AnyFileObject() function. Add new private functions: * _PyRun_AnyFileObject() * _PyRun_InteractiveLoopObject() * _Py_FdIsInteractive()
1 parent ca06440 commit a82f63f

5 files changed

Lines changed: 106 additions & 53 deletions

File tree

Include/cpython/pylifecycle.h

Lines changed: 1 addition & 0 deletions

Include/cpython/pythonrun.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ PyAPI_FUNC(int) PyRun_AnyFileExFlags(
1313
const char *filename, /* decoded from the filesystem encoding */
1414
int closeit,
1515
PyCompilerFlags *flags);
16+
PyAPI_FUNC(int) _PyRun_AnyFileObject(
17+
FILE *fp,
18+
PyObject *filename,
19+
int closeit,
20+
PyCompilerFlags *flags);
1621
PyAPI_FUNC(int) PyRun_SimpleFileExFlags(
1722
FILE *fp,
1823
const char *filename, /* decoded from the filesystem encoding */
@@ -30,6 +35,10 @@ PyAPI_FUNC(int) PyRun_InteractiveLoopFlags(
3035
FILE *fp,
3136
const char *filename, /* decoded from the filesystem encoding */
3237
PyCompilerFlags *flags);
38+
PyAPI_FUNC(int) _PyRun_InteractiveLoopObject(
39+
FILE *fp,
40+
PyObject *filename,
41+
PyCompilerFlags *flags);
3342

3443

3544
PyAPI_FUNC(PyObject *) PyRun_StringFlags(const char *, int, PyObject *,

Modules/main.c

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -313,17 +313,8 @@ pymain_run_file(const PyConfig *config, PyCompilerFlags *cf)
313313
}
314314
FILE *fp = _Py_wfopen(filename, L"rb");
315315
if (fp == NULL) {
316-
char *cfilename_buffer;
317-
const char *cfilename;
318-
int err = errno;
319-
cfilename_buffer = _Py_EncodeLocaleRaw(filename, NULL);
320-
if (cfilename_buffer != NULL)
321-
cfilename = cfilename_buffer;
322-
else
323-
cfilename = "<unprintable file name>";
324-
fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n",
325-
config->program_name, cfilename, err, strerror(err));
326-
PyMem_RawFree(cfilename_buffer);
316+
fprintf(stderr, "%ls: can't open file '%ls': [Errno %d] %s\n",
317+
config->program_name, filename, errno, strerror(errno));
327318
return 2;
328319
}
329320

@@ -353,25 +344,15 @@ pymain_run_file(const PyConfig *config, PyCompilerFlags *cf)
353344
return pymain_exit_err_print();
354345
}
355346

356-
PyObject *unicode, *bytes = NULL;
357-
const char *filename_str;
358-
359-
unicode = PyUnicode_FromWideChar(filename, wcslen(filename));
360-
if (unicode != NULL) {
361-
bytes = PyUnicode_EncodeFSDefault(unicode);
362-
Py_DECREF(unicode);
363-
}
364-
if (bytes != NULL) {
365-
filename_str = PyBytes_AsString(bytes);
366-
}
367-
else {
368-
PyErr_Clear();
369-
filename_str = "<filename encoding error>";
347+
PyObject *filename_obj = PyUnicode_FromWideChar(filename, -1);
348+
if (filename_obj == NULL) {
349+
PyErr_Print();
350+
return -1;
370351
}
371352

372353
/* PyRun_AnyFileExFlags(closeit=1) calls fclose(fp) before running code */
373-
int run = PyRun_AnyFileExFlags(fp, filename_str, 1, cf);
374-
Py_XDECREF(bytes);
354+
int run = _PyRun_AnyFileObject(fp, filename_obj, 1, cf);
355+
Py_XDECREF(filename_obj);
375356
return (run != 0);
376357
}
377358

Python/pylifecycle.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2739,6 +2739,21 @@ Py_FdIsInteractive(FILE *fp, const char *filename)
27392739
}
27402740

27412741

2742+
int
2743+
_Py_FdIsInteractive(FILE *fp, PyObject *filename)
2744+
{
2745+
if (isatty((int)fileno(fp))) {
2746+
return 1;
2747+
}
2748+
if (!Py_InteractiveFlag) {
2749+
return 0;
2750+
}
2751+
return (filename == NULL) ||
2752+
(PyUnicode_CompareWithASCIIString(filename, "<stdin>") == 0) ||
2753+
(PyUnicode_CompareWithASCIIString(filename, "???") == 0);
2754+
}
2755+
2756+
27422757
/* Wrappers around sigaction() or signal(). */
27432758

27442759
PyOS_sighandler_t

Python/pythonrun.c

Lines changed: 73 additions & 26 deletions

0 commit comments

Comments
 (0)