py3: support of Python 3.4

This commit is contained in:
wmayer
2017-06-06 23:49:42 +02:00
parent 2b323670c7
commit 9513792292
4 changed files with 66 additions and 3 deletions

View File

@@ -543,9 +543,9 @@ endif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
ELSE(NOT PYTHONLIBS_FOUND)
# prevent python3 lower than 3.3 (not enough utf8<->unicode tools)
IF(PYTHON_VERSION_MAJOR EQUAL 3)
IF(PYTHON_VERSION_MINOR LESS 5)
MESSAGE(FATAL_ERROR "To build FreeCAD with Python3, you need at least version 3.5\n")
ENDIF(PYTHON_VERSION_MINOR LESS 5)
IF(PYTHON_VERSION_MINOR LESS 4)
MESSAGE(FATAL_ERROR "To build FreeCAD with Python3, you need at least version 3.4\n")
ENDIF(PYTHON_VERSION_MINOR LESS 4)
ENDIF(PYTHON_VERSION_MAJOR EQUAL 3)
ENDIF(NOT PYTHONLIBS_FOUND)

View File

@@ -476,7 +476,11 @@ const char* InterpreterSingleton::init(int argc,char *argv[])
{
if (!Py_IsInitialized()) {
#if PY_MAJOR_VERSION >= 3
#if PY_MINOR_VERSION >= 5
Py_SetProgramName(Py_DecodeLocale(argv[0],NULL));
#else
Py_SetProgramName(_Py_char2wchar(argv[0],NULL));
#endif
#else
Py_SetProgramName(argv[0]);
#endif
@@ -486,7 +490,11 @@ const char* InterpreterSingleton::init(int argc,char *argv[])
size_t size = argc;
wchar_t **_argv = new wchar_t*[size];
for (int i = 0; i < argc; i++) {
#if PY_MINOR_VERSION >= 5
_argv[i] = Py_DecodeLocale(argv[i],NULL);
#else
_argv[i] = _Py_char2wchar(argv[i],NULL);
#endif
}
PySys_SetArgv(argc, _argv);
#else
@@ -496,7 +504,11 @@ const char* InterpreterSingleton::init(int argc,char *argv[])
this->_global = PyEval_SaveThread();
}
#if PY_MAJOR_VERSION >= 3
#if PY_MINOR_VERSION >= 5
return Py_EncodeLocale(Py_GetPath(),NULL);
#else
return _Py_wchar2char(Py_GetPath(),NULL);
#endif
#else
return Py_GetPath();
#endif

View File

@@ -219,6 +219,53 @@ FreeCADGui_getSoDBVersion(PyObject * /*self*/, PyObject *args)
#endif
}
// Copied from https://github.com/python/cpython/blob/master/Objects/moduleobject.c
#if PY_MAJOR_VERSION >= 3
#if PY_MINOR_VERSION <= 4
static int
_add_methods_to_object(PyObject *module, PyObject *name, PyMethodDef *functions)
{
PyObject *func;
PyMethodDef *fdef;
for (fdef = functions; fdef->ml_name != NULL; fdef++) {
if ((fdef->ml_flags & METH_CLASS) ||
(fdef->ml_flags & METH_STATIC)) {
PyErr_SetString(PyExc_ValueError,
"module functions cannot set"
" METH_CLASS or METH_STATIC");
return -1;
}
func = PyCFunction_NewEx(fdef, (PyObject*)module, name);
if (func == NULL) {
return -1;
}
if (PyObject_SetAttrString(module, fdef->ml_name, func) != 0) {
Py_DECREF(func);
return -1;
}
Py_DECREF(func);
}
return 0;
}
int
PyModule_AddFunctions(PyObject *m, PyMethodDef *functions)
{
int res;
PyObject *name = PyModule_GetNameObject(m);
if (name == NULL) {
return -1;
}
res = _add_methods_to_object(m, name, functions);
Py_DECREF(name);
return res;
}
#endif
#endif
struct PyMethodDef FreeCADGui_methods[] = {
{"subgraphFromObject",FreeCADGui_subgraphFromObject,METH_VARARGS,
"subgraphFromObject(object) -> Node\n\n"

View File

@@ -202,7 +202,11 @@ int main( int argc, char ** argv )
"Python version information:\n%4\n")
.arg(appName).arg(QString::fromUtf8(e.what()))
#if PY_MAJOR_VERSION >= 3
#if PY_MINOR_VERSION >= 5
.arg(QString::fromUtf8(Py_EncodeLocale(Py_GetPath(),NULL))).arg(QString::fromLatin1(Py_GetVersion()));
#else
.arg(QString::fromUtf8(_Py_wchar2char(Py_GetPath(),NULL))).arg(QString::fromLatin1(Py_GetVersion()));
#endif
#else
.arg(QString::fromUtf8(Py_GetPath())).arg(QString::fromLatin1(Py_GetVersion()));
#endif