py3: App: files A-C ported to python3

This commit is contained in:
Yorik van Havre
2017-05-10 00:07:47 +02:00
committed by wmayer
parent 36960df73e
commit 0d033757ae
2 changed files with 50 additions and 10 deletions

View File

@@ -203,6 +203,11 @@ PyDoc_STRVAR(FreeCAD_doc,
PyDoc_STRVAR(Console_doc,
"FreeCAD Console\n"
);
PyDoc_STRVAR(Base_doc,
"The Base module contains the classes for the geometric basics\n"
"like vector, matrix, bounding box, placement, rotation, axis, ...\n"
);
Application::Application(std::map<std::string,std::string> &mConfig)
: _mConfig(mConfig), _pActiveDoc(0)
@@ -214,10 +219,21 @@ Application::Application(std::map<std::string,std::string> &mConfig)
// setting up Python binding
Base::PyGILStateLocker lock;
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef FreeCADModuleDef = {PyModuleDef_HEAD_INIT,"FreeCAD", FreeCAD_doc, -1, Application::Methods};
PyObject* pAppModule = PyModule_Create(&FreeCADModuleDef);
_PyImport_FixupBuiltin(pAppModule, "FreeCAD");
#else
PyObject* pAppModule = Py_InitModule3("FreeCAD", Application::Methods, FreeCAD_doc);
#endif
Py::Module(pAppModule).setAttr(std::string("ActiveDocument"),Py::None());
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef ConsoleModuleDef = {PyModuleDef_HEAD_INIT, "__FreeCADConsole__", Console_doc, -1, ConsoleSingleton::Methods};
PyObject* pConsoleModule = PyModule_Create(&ConsoleModuleDef);
#else
PyObject* pConsoleModule = Py_InitModule3("__FreeCADConsole__", ConsoleSingleton::Methods, Console_doc);
#endif
// introducing additional classes
@@ -234,11 +250,13 @@ Application::Application(std::map<std::string,std::string> &mConfig)
// Note: Create an own module 'Base' which should provide the python
// binding classes from the base module. At a later stage we should
// remove these types from the FreeCAD module.
PyObject* pBaseModule = Py_InitModule3("__FreeCADBase__", NULL,
"The Base module contains the classes for the geometric basics\n"
"like vector, matrix, bounding box, placement, rotation, axis, ...");
// Python exceptions
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef BaseModuleDef = {PyModuleDef_HEAD_INIT, "__FreeCADBase__", Base_doc, -1, NULL};
PyObject* pBaseModule = PyModule_Create(&BaseModuleDef);
#else
PyObject* pBaseModule = Py_InitModule3("__FreeCADBase__", NULL, Base_doc);
#endif
Base::BaseExceptionFreeCADError = PyErr_NewException("Base.FreeCADError", PyExc_RuntimeError, NULL);
Py_INCREF(Base::BaseExceptionFreeCADError);
PyModule_AddObject(pBaseModule, "FreeCADError", Base::BaseExceptionFreeCADError);
@@ -260,11 +278,17 @@ Application::Application(std::map<std::string,std::string> &mConfig)
PyModule_AddObject(pAppModule, "Console", pConsoleModule);
//insert Units module
PyObject* pUnitsModule = Py_InitModule3("Units", Base::UnitsApi::Methods,
"The Unit API");
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef UnitsModuleDef = {PyModuleDef_HEAD_INIT, "Units", "The Unit API", -1, Base::UnitsApi::Methods};
PyObject* pUnitsModule = PyModule_Create(&UnitsModuleDef);
#else
PyObject* pUnitsModule = Py_InitModule3("Units", Base::UnitsApi::Methods,"The Unit API");
#endif
Base::Interpreter().addType(&Base::QuantityPy ::Type,pUnitsModule,"Quantity");
// make sure to set the 'nb_true_divide' slot
#if PY_MAJOR_VERSION < 3
Base::QuantityPy::Type.tp_as_number->nb_true_divide = Base::QuantityPy::Type.tp_as_number->nb_divide;
#endif
Base::Interpreter().addType(&Base::UnitPy ::Type,pUnitsModule,"Unit");
Py_INCREF(pUnitsModule);
@@ -1427,10 +1451,14 @@ void Application::initApplication(void)
// starting the init script
Console().Log("Run App init script\n");
Interpreter().runString(Base::ScriptFactory().ProduceScript("CMakeVariables"));
Interpreter().runString(Base::ScriptFactory().ProduceScript("FreeCADInit"));
ObjectLabelObserver::instance();
try {
Interpreter().runString(Base::ScriptFactory().ProduceScript("CMakeVariables"));
Interpreter().runString(Base::ScriptFactory().ProduceScript("FreeCADInit"));
ObjectLabelObserver::instance();
}
catch (const Base::Exception& e) {
Base::Console().Error("%s\n", e.what());
}
}
std::list<std::string> Application::getCmdLineFiles()

View File

@@ -352,7 +352,11 @@ PyObject* Application::sGetConfig(PyObject * /*self*/, PyObject *args,PyObject *
}
else {
// do not set an error because this may break existing python code
#if PY_MAJOR_VERSION >= 3
return PyUnicode_FromString("");
#else
return PyString_FromString("");
#endif
}
}
@@ -364,7 +368,11 @@ PyObject* Application::sDumpConfig(PyObject * /*self*/, PyObject *args,PyObject
PyObject *dict = PyDict_New();
for (std::map<std::string,std::string>::iterator It= GetApplication()._mConfig.begin();
It!=GetApplication()._mConfig.end();++It) {
#if PY_MAJOR_VERSION >= 3
PyDict_SetItemString(dict,It->first.c_str(), PyUnicode_FromString(It->second.c_str()));
#else
PyDict_SetItemString(dict,It->first.c_str(), PyString_FromString(It->second.c_str()));
#endif
}
return dict;
}
@@ -559,7 +567,11 @@ PyObject* Application::sListDocuments(PyObject * /*self*/, PyObject *args,PyObje
for (std::map<std::string,Document*>::const_iterator It = GetApplication().DocMap.begin();
It != GetApplication().DocMap.end();++It) {
#if PY_MAJOR_VERSION >= 3
pKey = PyUnicode_FromString(It->first.c_str());
#else
pKey = PyString_FromString(It->first.c_str());
#endif
// GetPyObject() increments
pValue = static_cast<Base::PyObjectBase*>(It->second->getPyObject());
PyDict_SetItem(pDict, pKey, pValue);