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

Skip to content

Commit 374e220

Browse files
committed
#4747: on Windows, starting a module with a non-ascii filename 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
1 parent 8ed9a80 commit 374e220

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 3.1 alpha 0
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #4747: When the terminal does not use utf-8, executing a script with
16+
non-ascii characters in its name could fail with a "SyntaxError: None" error.
17+
1518
- Issue #4797: IOError.filename was not set when _fileio.FileIO failed to open
1619
file with `bytes' filename on Windows.
1720

Modules/main.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -600,18 +600,21 @@ Py_Main(int argc, wchar_t **argv)
600600
}
601601

602602
if (sts==-1) {
603-
char cfilename[PATH_MAX];
603+
PyObject *filenameObj = NULL;
604604
char *p_cfilename = "<stdin>";
605605
if (filename) {
606-
size_t r = wcstombs(cfilename, filename, PATH_MAX);
607-
p_cfilename = cfilename;
608-
if (r == (size_t)-1 || r >= PATH_MAX)
606+
filenameObj = PyUnicode_FromWideChar(
607+
filename, wcslen(filename));
608+
if (filenameObj != NULL)
609+
p_cfilename = _PyUnicode_AsString(filenameObj);
610+
else
609611
p_cfilename = "<decoding error>";
610612
}
611613
sts = PyRun_AnyFileExFlags(
612614
fp,
613615
p_cfilename,
614616
filename != NULL, &cf) != 0;
617+
Py_XDECREF(filenameObj);
615618
}
616619

617620
}

0 commit comments

Comments
 (0)