Skip to content

Commit

Permalink
python#4747: on Windows, starting a module with a non-ascii filename …
Browse files Browse the repository at this point in the history
…would print a useless "SyntaxError: None"

when the script contains a "# coding:" declaration.

The Python API expects char* to be utf-8 encoded. wcstombs should be avoided here.

Reviewed by Benjamin. Will backport to 3.0
  • Loading branch information
amauryfa committed Jan 1, 2009
1 parent 8ed9a80 commit 374e220
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ What's New in Python 3.1 alpha 0
Core and Builtins
-----------------

- Issue #4747: When the terminal does not use utf-8, executing a script with
non-ascii characters in its name could fail with a "SyntaxError: None" error.

- Issue #4797: IOError.filename was not set when _fileio.FileIO failed to open
file with `bytes' filename on Windows.

Expand Down
11 changes: 7 additions & 4 deletions Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -600,18 +600,21 @@ Py_Main(int argc, wchar_t **argv)
}

if (sts==-1) {
char cfilename[PATH_MAX];
PyObject *filenameObj = NULL;
char *p_cfilename = "<stdin>";
if (filename) {
size_t r = wcstombs(cfilename, filename, PATH_MAX);
p_cfilename = cfilename;
if (r == (size_t)-1 || r >= PATH_MAX)
filenameObj = PyUnicode_FromWideChar(
filename, wcslen(filename));
if (filenameObj != NULL)
p_cfilename = _PyUnicode_AsString(filenameObj);
else
p_cfilename = "<decoding error>";
}
sts = PyRun_AnyFileExFlags(
fp,
p_cfilename,
filename != NULL, &cf) != 0;
Py_XDECREF(filenameObj);
}

}
Expand Down

0 comments on commit 374e220

Please sign in to comment.