py3: App: files D-Z ported to python3

issue 0000995
diff to py3-branch will remain in the following files in src/App:
- ExtensionContainer.cpp
- FeaturePythonPyImp.h +.inl
most likely these files depend on Tools and Base
This commit is contained in:
looooo
2017-05-11 09:55:40 +02:00
committed by wmayer
parent f9bfd77555
commit ca3e58e2f3
10 changed files with 389 additions and 20 deletions

View File

@@ -253,8 +253,55 @@ PyObject *PropertyFileIncluded::getPyObject(void)
return p;
}
#if PY_MAJOR_VERSION >= 3
namespace App {
const char* getNameFromFile(PyObject* value)
{
const char* string = 0;
PyObject *oname = PyObject_GetAttrString (value, "name");
if (oname) {
if (PyUnicode_Check (oname)) {
string = PyUnicode_AsUTF8 (oname);
}
else if (PyBytes_Check (oname)) {
string = PyBytes_AsString (oname);
}
Py_DECREF (oname);
}
if (!string)
throw Base::TypeError("Unable to get filename");
return string;
}
bool isIOFile(PyObject* file)
{
PyObject* io = PyImport_ImportModule("io");
PyObject* IOBase_Class = PyObject_GetAttrString(io, "IOBase");
bool isFile = PyObject_IsInstance(file, IOBase_Class);
Py_DECREF(IOBase_Class);
Py_DECREF(io);
return isFile;
}
}
#endif
void PropertyFileIncluded::setPyObject(PyObject *value)
{
#if PY_MAJOR_VERSION >= 3
std::string string;
if (PyUnicode_Check(value)) {
string = PyUnicode_AsUTF8(value);
}
else if (PyBytes_Check(value)) {
string = PyBytes_AsString(value);
}
else if (isIOFile(value)){
string = getNameFromFile(value);
}
#else
std::string string;
if (PyUnicode_Check(value)) {
PyObject* unicode = PyUnicode_AsUTF8String(value);
@@ -268,6 +315,7 @@ void PropertyFileIncluded::setPyObject(PyObject *value)
PyObject* FileName = PyFile_Name(value);
string = PyString_AsString(FileName);
}
#endif
else if (PyTuple_Check(value)) {
if (PyTuple_Size(value) != 2)
throw Base::TypeError("Tuple needs size of (filePath,newFileName)");
@@ -276,6 +324,17 @@ void PropertyFileIncluded::setPyObject(PyObject *value)
// decoding file
std::string fileStr;
#if PY_MAJOR_VERSION >= 3
if (PyUnicode_Check(file)) {
fileStr = PyUnicode_AsUTF8(file);
}
else if (PyBytes_Check(file)) {
fileStr = PyBytes_AsString(file);
}
else if (isIOFile(value)) {
fileStr = getNameFromFile(file);
}
#else
if (PyUnicode_Check(file)) {
PyObject* unicode = PyUnicode_AsUTF8String(file);
fileStr = PyString_AsString(unicode);
@@ -288,6 +347,7 @@ void PropertyFileIncluded::setPyObject(PyObject *value)
PyObject* FileName = PyFile_Name(file);
fileStr = PyString_AsString(FileName);
}
#endif
else {
std::string error = std::string("First item in tuple must be a file or string, not ");
error += file->ob_type->tp_name;
@@ -296,6 +356,17 @@ void PropertyFileIncluded::setPyObject(PyObject *value)
// decoding name
std::string nameStr;
#if PY_MAJOR_VERSION >= 3
if (PyUnicode_Check(name)) {
nameStr = PyUnicode_AsUTF8(name);
}
else if (PyBytes_Check(name)) {
nameStr = PyBytes_AsString(name);
}
else if (isIOFile(value)) {
nameStr = getNameFromFile(name);
}
#else
if (PyString_Check(name)) {
nameStr = PyString_AsString(name);
}
@@ -303,6 +374,7 @@ void PropertyFileIncluded::setPyObject(PyObject *value)
PyObject* FileName = PyFile_Name(name);
nameStr = PyString_AsString(FileName);
}
#endif
else {
std::string error = std::string("Second item in tuple must be a string, not ");
error += name->ob_type->tp_name;