[Python-checkins] r83779 - python/branches/py3k/Modules/main.c

victor.stinner python-checkins at python.org
Sat Aug 7 12:57:17 CEST 2010


Author: victor.stinner
Date: Sat Aug  7 12:57:17 2010
New Revision: 83779

Log:
Issue #9425: Create run_command() subfunction

Use PyUnicode_AsUTF8String() instead of _PyUnicode_AsString()

Modified:
   python/branches/py3k/Modules/main.c

Modified: python/branches/py3k/Modules/main.c
==============================================================================
--- python/branches/py3k/Modules/main.c	(original)
+++ python/branches/py3k/Modules/main.c	Sat Aug  7 12:57:17 2010
@@ -253,6 +253,28 @@
     }
 }
 
+static int
+run_command(wchar_t *command, PyCompilerFlags *cf)
+{
+    PyObject *unicode, *bytes;
+    int ret;
+
+    unicode = PyUnicode_FromWideChar(command, -1);
+    if (unicode == NULL)
+        goto error;
+    bytes = PyUnicode_AsUTF8String(unicode);
+    Py_DECREF(unicode);
+    if (bytes == NULL)
+        goto error;
+    ret = PyRun_SimpleStringFlags(PyBytes_AsString(bytes), cf);
+    Py_DECREF(bytes);
+    return ret != 0;
+
+error:
+    PyErr_Print();
+    return 1;
+}
+
 
 /* Main program */
 
@@ -564,22 +586,8 @@
     }
 
     if (command) {
-        char *commandStr;
-        PyObject *commandObj = PyUnicode_FromWideChar(
-            command, wcslen(command));
+        sts = run_command(command, &cf);
         free(command);
-        if (commandObj != NULL)
-            commandStr = _PyUnicode_AsString(commandObj);
-        else
-            commandStr = NULL;
-        if (commandStr != NULL) {
-            sts = PyRun_SimpleStringFlags(commandStr, &cf) != 0;
-            Py_DECREF(commandObj);
-        }
-        else {
-            PyErr_Print();
-            sts = 1;
-        }
     } else if (module) {
         sts = RunModule(module, 1);
     }


More information about the Python-checkins mailing list