summaryrefslogtreecommitdiffstats
path: root/Modules/main.c
diff options
context:
space:
mode:
authorAmaury Forgeot d'Arc <amauryfa@gmail.com>2008-11-11 23:04:59 (GMT)
committerAmaury Forgeot d'Arc <amauryfa@gmail.com>2008-11-11 23:04:59 (GMT)
commit9a5499b4e54f1d74dfe41772d780511c8ad1120c (patch)
tree4aac685d27ffba930b8c36f4dc356aed6728a0b3 /Modules/main.c
parentd3013ffa49e889ff96ed967f574aa01b91b09f12 (diff)
downloadcpython-9a5499b4e54f1d74dfe41772d780511c8ad1120c.zip
cpython-9a5499b4e54f1d74dfe41772d780511c8ad1120c.tar.gz
cpython-9a5499b4e54f1d74dfe41772d780511c8ad1120c.tar.bz2
#3705: Command-line arguments were not correctly decoded when the
terminal does not use UTF8. Now the code propagates the unicode string as far as possible, and avoids the conversion to char* which implicitely uses utf-8. Reviewed by Benjamin.
Diffstat (limited to 'Modules/main.c')
-rw-r--r--Modules/main.c40
1 files changed, 19 insertions, 21 deletions
diff --git a/Modules/main.c b/Modules/main.c
index 6fdc33a..78913ee 100644
--- a/Modules/main.c
+++ b/Modules/main.c
@@ -287,7 +287,7 @@ Py_Main(int argc, wchar_t **argv)
{
int c;
int sts;
- char *command = NULL;
+ wchar_t *command = NULL;
wchar_t *filename = NULL;
wchar_t *module = NULL;
FILE *fp = stdin;
@@ -299,7 +299,6 @@ Py_Main(int argc, wchar_t **argv)
int version = 0;
int saw_unbuffered_flag = 0;
PyCompilerFlags cf;
- char *oldloc;
cf.cf_flags = 0;
@@ -310,30 +309,19 @@ Py_Main(int argc, wchar_t **argv)
while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
if (c == 'c') {
- size_t r1, r2;
- oldloc = setlocale(LC_ALL, NULL);
- setlocale(LC_ALL, "");
- r1 = wcslen(_PyOS_optarg);
- r2 = wcstombs(NULL, _PyOS_optarg, r1);
- if (r2 == (size_t) -1)
- Py_FatalError(
- "cannot convert character encoding of -c argument");
- if (r2 > r1)
- r1 = r2;
- r1 += 2;
+ size_t len;
/* -c is the last option; following arguments
that look like options are left for the
command to interpret. */
- command = (char *)malloc(r1);
+
+ len = wcslen(_PyOS_optarg) + 1 + 1;
+ command = (wchar_t *)malloc(sizeof(wchar_t) * len);
if (command == NULL)
Py_FatalError(
"not enough memory to copy -c argument");
- r2 = wcstombs(command, _PyOS_optarg, r1);
- if (r2 > r1-1)
- Py_FatalError(
- "not enough memory to copy -c argument");
- strcat(command, "\n");
- setlocale(LC_ALL, oldloc);
+ wcscpy(command, _PyOS_optarg);
+ command[len - 2] = '\n';
+ command[len - 1] = 0;
break;
}
@@ -543,8 +531,18 @@ Py_Main(int argc, wchar_t **argv)
}
if (command) {
- sts = PyRun_SimpleStringFlags(command, &cf) != 0;
+ PyObject *commandObj = PyUnicode_FromWideChar(
+ command, wcslen(command));
free(command);
+ if (commandObj != NULL) {
+ sts = PyRun_SimpleStringFlags(
+ _PyUnicode_AsString(commandObj), &cf) != 0;
+ }
+ else {
+ PyErr_Print();
+ sts = 1;
+ }
+ Py_DECREF(commandObj);
} else if (module) {
sts = RunModule(module, 1);
}