From 40cddd628027243677c08d13fe77125bae4125b4 Mon Sep 17 00:00:00 2001 From: rdb Date: Wed, 27 Mar 2013 15:48:24 +0000 Subject: [PATCH] Fixes for Python 3.2 --- direct/src/ffi/panda3d.py | 26 +++++++----- dtool/src/interrogate/interrogate_module.cxx | 42 +++++++++++++------- panda/src/event/pythonTask.cxx | 5 +++ panda/src/pipeline/thread.cxx | 5 +++ panda/src/pipeline/threadSimpleManager.cxx | 5 +++ panda/src/putil/pythonCallbackObject.cxx | 5 +++ 6 files changed, 62 insertions(+), 26 deletions(-) diff --git a/direct/src/ffi/panda3d.py b/direct/src/ffi/panda3d.py index 89f68a7aab..07f9c9e436 100644 --- a/direct/src/ffi/panda3d.py +++ b/direct/src/ffi/panda3d.py @@ -68,7 +68,7 @@ class panda3d_import_manager: target = dir break if target == None: - raise ImportError, "Cannot find %s" % (filename) + raise ImportError("Cannot find %s" % (filename)) target = cls.os.path.abspath(target) # And add that directory to the system library path. @@ -110,8 +110,10 @@ class panda3d_import_manager: # Try to import it normally first. try: return __import__(name) - except ImportError, err: - if str(err) != "No module named " + name: + except ImportError: + _, err, _ = cls.sys.exc_info() + if str(err) != "No module named " + name and \ + str(err) != "No module named '%s'" % name: raise # Hm, importing normally didn't work. Let's try imp.load_dynamic. @@ -130,7 +132,7 @@ class panda3d_import_manager: break if target == None: message = "DLL loader cannot find %s." % name - raise ImportError, message + raise ImportError(message) target = cls.os.path.abspath(target) # Now import the file explicitly. @@ -173,7 +175,7 @@ class panda3d_submodule(type(sys)): return value # Not found? Raise the error that Python would normally raise. - raise AttributeError, "'module' object has no attribute '%s'" % name + raise AttributeError("'module' object has no attribute '%s'" % name) class panda3d_multisubmodule(type(sys)): """ Represents a submodule of 'panda3d' that represents multiple @@ -192,10 +194,11 @@ class panda3d_multisubmodule(type(sys)): for lib in self.__libraries__: try: self.__manager__.libimport(lib) - except ImportError, msg: + except ImportError: + _, msg, _ = self.__manager__.sys.exc_info() err.append(str(msg).rstrip('.')) if len(err) > 0: - raise ImportError, ', '.join(err) + raise ImportError(', '.join(err)) def __getattr__(self, name): if name == "__all__": @@ -217,7 +220,7 @@ class panda3d_multisubmodule(type(sys)): return value # Not found? Raise the error that Python would normally raise. - raise AttributeError, "'module' object has no attribute '%s'" % name + raise AttributeError("'module' object has no attribute '%s'" % name) class panda3d_module(type(sys)): """ Represents the main 'panda3d' module. """ @@ -232,10 +235,11 @@ class panda3d_module(type(sys)): for module in self.modules: try: self.__manager__.sys.modules["panda3d.%s" % module].__load__() - except ImportError, msg: + except ImportError: + _, msg, _ = self.__manager__.sys.exc_info() err.append(str(msg).rstrip('.')) if len(err) > 0: - raise ImportError, ', '.join(err) + raise ImportError(', '.join(err)) def __getattr__(self, name): @@ -250,7 +254,7 @@ class panda3d_module(type(sys)): return value # Not found? Raise the error that Python would normally raise. - raise AttributeError, "'module' object has no attribute '%s'" % name + raise AttributeError("'module' object has no attribute '%s'" % name) # Create the fake module objects and insert them into sys.modules. this = panda3d_module("panda3d") diff --git a/dtool/src/interrogate/interrogate_module.cxx b/dtool/src/interrogate/interrogate_module.cxx index 28a240ba3e..724904b891 100644 --- a/dtool/src/interrogate/interrogate_module.cxx +++ b/dtool/src/interrogate/interrogate_module.cxx @@ -109,30 +109,42 @@ int write_python_table_native(ostream &out) { pset::iterator ii; for(ii = Libraries.begin(); ii != Libraries.end(); ii++) { - printf("Referencing Library %s\n",(*ii).c_str()); - out << "extern LibraryDef "<< *ii << "_moddef ;\n"; + printf("Referencing Library %s\n",(*ii).c_str()); + out << "extern LibraryDef "<< *ii << "_moddef;\n"; } - out << "#ifdef _WIN32\n" - << "extern \"C\" __declspec(dllexport) void init" << library_name << "();\n" + out << "\n" + << "#if PY_MAJOR_VERSION >= 3\n" + << "#define INIT_FUNC PyObject *PyInit_" << library_name << "\n" << "#else\n" - << "extern \"C\" void init" << library_name << "();\n" + << "#define INIT_FUNC void init" << library_name << "\n" << "#endif\n\n" - << "void init" << library_name << "() \n{\n"; - + + << "#ifdef _WIN32\n" + << "extern \"C\" __declspec(dllexport) INIT_FUNC();\n" + << "#else\n" + << "extern \"C\" INIT_FUNC();\n" + << "#endif\n\n" + + << "INIT_FUNC() {\n"; + if (track_interpreter) { - out << " in_interpreter = 1;\n"; + out << " in_interpreter = 1;\n"; } - out << " LibraryDef *defs[] = {"; - for(ii = Libraries.begin(); ii != Libraries.end(); ii++) - out << "&"<< *ii << "_moddef,"; + out << " LibraryDef *defs[] = {"; + for(ii = Libraries.begin(); ii != Libraries.end(); ii++) { + out << "&"<< *ii << "_moddef, "; + } - out << " NULL };\n "; + out << "NULL};\n\n"; - - out << " Dtool_PyModuleInitHelper( defs, \"" << library_name << "\");\n"; - out << "\n\n\n}\n"; + out << "#if PY_MAJOR_VERSION >= 3\n"; + out << " return Dtool_PyModuleInitHelper(defs, \"" << library_name << "\");\n"; + out << "#else\n"; + out << " Dtool_PyModuleInitHelper(defs, \"" << library_name << "\");\n"; + out << "#endif\n"; + out << "}\n"; return count; } diff --git a/panda/src/event/pythonTask.cxx b/panda/src/event/pythonTask.cxx index 1cbb8a389d..66d3d6bfa6 100644 --- a/panda/src/event/pythonTask.cxx +++ b/panda/src/event/pythonTask.cxx @@ -51,6 +51,11 @@ PythonTask(PyObject *function, const string &name) : // Ensure that the Python threading system is initialized and ready // to go. #ifdef WITH_THREAD // This symbol defined within Python.h + +#if PY_VERSION_HEX >= 0x03020000 + Py_Initialize(); +#endif + PyEval_InitThreads(); #endif #endif diff --git a/panda/src/pipeline/thread.cxx b/panda/src/pipeline/thread.cxx index b33f083890..8d1d3a9a7d 100644 --- a/panda/src/pipeline/thread.cxx +++ b/panda/src/pipeline/thread.cxx @@ -70,6 +70,11 @@ Thread(const string &name, const string &sync_name) : // Ensure that the Python threading system is initialized and ready // to go. #ifdef WITH_THREAD // This symbol defined within Python.h + +#if PY_VERSION_HEX >= 0x03020000 + Py_Initialize(); +#endif + PyEval_InitThreads(); #endif #endif diff --git a/panda/src/pipeline/threadSimpleManager.cxx b/panda/src/pipeline/threadSimpleManager.cxx index 5865479695..eafa2f93eb 100644 --- a/panda/src/pipeline/threadSimpleManager.cxx +++ b/panda/src/pipeline/threadSimpleManager.cxx @@ -523,6 +523,11 @@ init_pointers() { #ifdef HAVE_PYTHON // Ensure that the Python threading system is initialized and ready // to go. + +#if PY_VERSION_HEX >= 0x03020000 + Py_Initialize(); +#endif + PyEval_InitThreads(); #endif } diff --git a/panda/src/putil/pythonCallbackObject.cxx b/panda/src/putil/pythonCallbackObject.cxx index 1ed3f2aa6c..2b0b26baf0 100644 --- a/panda/src/putil/pythonCallbackObject.cxx +++ b/panda/src/putil/pythonCallbackObject.cxx @@ -41,6 +41,11 @@ PythonCallbackObject(PyObject *function) { // Ensure that the Python threading system is initialized and ready // to go. #ifdef WITH_THREAD // This symbol defined within Python.h + +#if PY_VERSION_HEX >= 0x03020000 + Py_Initialize(); +#endif + PyEval_InitThreads(); #endif #endif