Pārlūkot izejas kodu

Compile with Python 3.3

rdb 13 gadi atpakaļ
vecāks
revīzija
441666fb79

+ 17 - 5
dtool/src/dtoolutil/executionEnvironment.cxx

@@ -274,12 +274,24 @@ ns_get_environment_variable(const string &var) const {
       PyObject *obj = PySys_GetObject((char*) "argv");  // borrowed reference
       PyObject *obj = PySys_GetObject((char*) "argv");  // borrowed reference
       if (obj != NULL && PyList_Check(obj)) {
       if (obj != NULL && PyList_Check(obj)) {
         PyObject *item = PyList_GetItem(obj, 0);  // borrowed reference
         PyObject *item = PyList_GetItem(obj, 0);  // borrowed reference
-        // Hmm, could this ever be a Unicode object?
-        if (item != NULL && PyString_Check(item)) {
-          char *str = PyString_AsString(item);
-          if (str != (char *)NULL) {
-            main_dir = Filename::from_os_specific(str);
+        if (item != NULL) {
+          if (PyUnicode_Check(item)) {
+            Py_ssize_t size = PyUnicode_GetSize(item);
+            wchar_t *data = new wchar_t[size];
+            if (PyUnicode_AsWideChar(item, data, size) != -1) {
+              wstring wstr (data, size);
+              main_dir = Filename::from_os_specific_w(wstr);
+            }
+            delete data;
           }
           }
+#if PY_MAJOR_VERSION < 3
+          else if (PyString_Check(item)) {
+            char *str = PyString_AsString(item);
+            if (str != (char *)NULL) {
+              main_dir = Filename::from_os_specific(str);
+            }
+          }
+#endif
         }
         }
       }
       }
       
       

+ 9 - 0
dtool/src/dtoolutil/filename.cxx

@@ -2005,8 +2005,17 @@ scan_directory() const {
   PyObject *result = PyList_New(contents.size());
   PyObject *result = PyList_New(contents.size());
   for (size_t i = 0; i < contents.size(); ++i) {
   for (size_t i = 0; i < contents.size(); ++i) {
     const string &filename = contents[i];
     const string &filename = contents[i];
+#if PY_MAJOR_VERSION >= 3
+    // This function expects UTF-8.
+    PyObject *str = PyUnicode_FromStringAndSize(filename.data(), filename.size());
+#else
     PyObject *str = PyString_FromStringAndSize(filename.data(), filename.size());
     PyObject *str = PyString_FromStringAndSize(filename.data(), filename.size());
+#endif
+#ifdef Py_LIMITED_API
+    PyList_SetItem(result, i, str);
+#else
     PyList_SET_ITEM(result, i, str);
     PyList_SET_ITEM(result, i, str);
+#endif
   }
   }
 
 
   return result;
   return result;

+ 9 - 0
dtool/src/dtoolutil/globPattern.cxx

@@ -134,8 +134,17 @@ match_files(const Filename &cwd) const {
   PyObject *result = PyList_New(contents.size());
   PyObject *result = PyList_New(contents.size());
   for (size_t i = 0; i < contents.size(); ++i) {
   for (size_t i = 0; i < contents.size(); ++i) {
     const string &filename = contents[i];
     const string &filename = contents[i];
+#if PY_MAJOR_VERSION >= 3
+    // This function expects UTF-8.
+    PyObject *str = PyUnicode_FromStringAndSize(filename.data(), filename.size());
+#else
     PyObject *str = PyString_FromStringAndSize(filename.data(), filename.size());
     PyObject *str = PyString_FromStringAndSize(filename.data(), filename.size());
+#endif
+#ifdef Py_LIMITED_API
+    PyList_SetItem(result, i, str);
+#else
     PyList_SET_ITEM(result, i, str);
     PyList_SET_ITEM(result, i, str);
+#endif
   }
   }
 
 
   return result;
   return result;