diff options
author | Victor Stinner <vstinner@python.org> | 2020-12-09 22:37:27 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-09 22:37:27 +0100 |
commit | a82f63f5af027a0eab0f0812d750b804368cbd25 (patch) | |
tree | aa8703c8745915d904d05ef132c1aa96fb1fd911 /Modules | |
parent | bpo-32381: Remove unused _Py_fopen() function (GH-23711) (diff) | |
download | cpython-a82f63f5af027a0eab0f0812d750b804368cbd25.tar.gz cpython-a82f63f5af027a0eab0f0812d750b804368cbd25.tar.bz2 cpython-a82f63f5af027a0eab0f0812d750b804368cbd25.zip |
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()
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/main.c | 35 |
1 files changed, 8 insertions, 27 deletions
diff --git a/Modules/main.c b/Modules/main.c index 3aa4d91c9a3..7ffcb07a7fd 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -313,17 +313,8 @@ pymain_run_file(const PyConfig *config, PyCompilerFlags *cf) } FILE *fp = _Py_wfopen(filename, L"rb"); if (fp == NULL) { - char *cfilename_buffer; - const char *cfilename; - int err = errno; - cfilename_buffer = _Py_EncodeLocaleRaw(filename, NULL); - if (cfilename_buffer != NULL) - cfilename = cfilename_buffer; - else - cfilename = "<unprintable file name>"; - fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n", - config->program_name, cfilename, err, strerror(err)); - PyMem_RawFree(cfilename_buffer); + fprintf(stderr, "%ls: can't open file '%ls': [Errno %d] %s\n", + config->program_name, filename, errno, strerror(errno)); return 2; } @@ -353,25 +344,15 @@ pymain_run_file(const PyConfig *config, PyCompilerFlags *cf) return pymain_exit_err_print(); } - PyObject *unicode, *bytes = NULL; - const char *filename_str; - - unicode = PyUnicode_FromWideChar(filename, wcslen(filename)); - if (unicode != NULL) { - bytes = PyUnicode_EncodeFSDefault(unicode); - Py_DECREF(unicode); - } - if (bytes != NULL) { - filename_str = PyBytes_AsString(bytes); - } - else { - PyErr_Clear(); - filename_str = "<filename encoding error>"; + PyObject *filename_obj = PyUnicode_FromWideChar(filename, -1); + if (filename_obj == NULL) { + PyErr_Print(); + return -1; } /* PyRun_AnyFileExFlags(closeit=1) calls fclose(fp) before running code */ - int run = PyRun_AnyFileExFlags(fp, filename_str, 1, cf); - Py_XDECREF(bytes); + int run = _PyRun_AnyFileObject(fp, filename_obj, 1, cf); + Py_XDECREF(filename_obj); return (run != 0); } |