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

Skip to content

Commit 8530e85

Browse files
committed
Revert r33661, which broke all buildbots.
1 parent 14b7851 commit 8530e85

4 files changed

Lines changed: 27 additions & 33 deletions

File tree

Lib/test/test_cmd_line.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,6 @@ def test_run_code(self):
135135
self.exit_code('-c', 'pass'),
136136
0)
137137

138-
# Test handling of non-ascii data
139-
command = "assert(ord('\xe9') == 0xe9)"
140-
self.assertEqual(
141-
self.exit_code('-c', command),
142-
0)
143-
144138

145139
def test_main():
146140
test.support.run_unittest(CmdLineTest)

Misc/NEWS

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ Core and Builtins
1313
-----------------
1414

1515
- Issue #3705: fix crash when given a non-ascii value on the command line for
16-
the "-c" and "-m" parameters.
16+
the "-c" and "-m" parameters. Now the behaviour is as expected under Linux,
17+
although under Windows it fails at a later point.
1718

1819
- Issue #3279: Importing site at interpreter was failing silently because the
1920
site module uses the open builtin which was not initialized at the time.

Modules/main.c

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ Py_Main(int argc, wchar_t **argv)
287287
{
288288
int c;
289289
int sts;
290-
wchar_t *command = NULL;
290+
char *command = NULL;
291291
wchar_t *filename = NULL;
292292
wchar_t *module = NULL;
293293
FILE *fp = stdin;
@@ -299,6 +299,7 @@ Py_Main(int argc, wchar_t **argv)
299299
int version = 0;
300300
int saw_unbuffered_flag = 0;
301301
PyCompilerFlags cf;
302+
char *oldloc;
302303

303304
cf.cf_flags = 0;
304305

@@ -309,19 +310,30 @@ Py_Main(int argc, wchar_t **argv)
309310

310311
while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
311312
if (c == 'c') {
312-
size_t len;
313+
size_t r1, r2;
314+
oldloc = setlocale(LC_ALL, NULL);
315+
setlocale(LC_ALL, "");
316+
r1 = wcslen(_PyOS_optarg);
317+
r2 = wcstombs(NULL, _PyOS_optarg, r1);
318+
if (r2 == (size_t) -1)
319+
Py_FatalError(
320+
"cannot convert character encoding of -c argument");
321+
if (r2 > r1)
322+
r1 = r2;
323+
r1 += 2;
313324
/* -c is the last option; following arguments
314325
that look like options are left for the
315326
command to interpret. */
316-
317-
len = wcslen(_PyOS_optarg) + 1 + 1;
318-
command = (wchar_t *)malloc(sizeof(wchar_t) * len);
327+
command = (char *)malloc(r1);
319328
if (command == NULL)
320329
Py_FatalError(
321330
"not enough memory to copy -c argument");
322-
wcscpy(command, _PyOS_optarg);
323-
command[len - 2] = '\n';
324-
command[len - 1] = 0;
331+
r2 = wcstombs(command, _PyOS_optarg, r1);
332+
if (r2 > r1-1)
333+
Py_FatalError(
334+
"not enough memory to copy -c argument");
335+
strcat(command, "\n");
336+
setlocale(LC_ALL, oldloc);
325337
break;
326338
}
327339

@@ -531,18 +543,8 @@ Py_Main(int argc, wchar_t **argv)
531543
}
532544

533545
if (command) {
534-
PyObject *commandObj = PyUnicode_FromUnicode(
535-
command, wcslen(command));
546+
sts = PyRun_SimpleStringFlags(command, &cf) != 0;
536547
free(command);
537-
if (commandObj != NULL) {
538-
sts = PyRun_SimpleStringFlags(
539-
_PyUnicode_AsString(commandObj), &cf) != 0;
540-
}
541-
else {
542-
PyErr_Print();
543-
sts = 1;
544-
}
545-
Py_DECREF(commandObj);
546548
} else if (module) {
547549
sts = RunModule(module, 1);
548550
}

Python/import.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2804,7 +2804,6 @@ call_find_module(char *name, PyObject *path)
28042804
{
28052805
extern int fclose(FILE *);
28062806
PyObject *fob, *ret;
2807-
PyObject *pathobj;
28082807
struct filedescr *fdp;
28092808
char pathname[MAXPATHLEN+1];
28102809
FILE *fp = NULL;
@@ -2848,9 +2847,9 @@ call_find_module(char *name, PyObject *path)
28482847
fob = Py_None;
28492848
Py_INCREF(fob);
28502849
}
2851-
pathobj = PyUnicode_DecodeFSDefault(pathname);
2852-
ret = Py_BuildValue("NN(ssi)",
2853-
fob, pathobj, fdp->suffix, fdp->mode, fdp->type);
2850+
ret = Py_BuildValue("Os(ssi)",
2851+
fob, pathname, fdp->suffix, fdp->mode, fdp->type);
2852+
Py_DECREF(fob);
28542853
PyMem_FREE(found_encoding);
28552854

28562855
return ret;
@@ -2861,9 +2860,7 @@ imp_find_module(PyObject *self, PyObject *args)
28612860
{
28622861
char *name;
28632862
PyObject *path = NULL;
2864-
if (!PyArg_ParseTuple(args, "es|O:find_module",
2865-
Py_FileSystemDefaultEncoding, &name,
2866-
&path))
2863+
if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
28672864
return NULL;
28682865
return call_find_module(name, path);
28692866
}

0 commit comments

Comments
 (0)