Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

py3-fixes #3

Merged
merged 1 commit into from
Jul 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/App/LinkBaseExtensionPyImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,20 @@ static bool getProperty(PropTmpMap &props, const LinkBaseExtension::PropInfoMap
const PropMap &propMap, PyObject *key, PyObject *value)
{
std::ostringstream str;

#if PY_MAJOR_VERSION < 3
if(!PyString_Check(key)) {
PyErr_SetString(PyExc_TypeError, "key must be string");
return false;
}
const char *keyStr = PyString_AsString(key);
#else
if(!PyUnicode_Check(key)) {
PyErr_SetString(PyExc_TypeError, "key must be a unicode string");
return false;
}
const char *keyStr = PyUnicode_AsUTF8(key);
#endif
auto it = infoMap.find(keyStr);
if(it == infoMap.end()){
str << "unknown key '" << keyStr << "'";
Expand All @@ -63,11 +72,19 @@ static bool getProperty(PropTmpMap &props, const LinkBaseExtension::PropInfoMap
if(key == value)
valStr = keyStr;
else {
#if PY_MAJOR_VERSION < 3
if(!PyString_Check(value)) {
PyErr_SetString(PyExc_TypeError, "value must be string");
return false;
}
valStr = PyString_AsString(value);
#else
if(!PyUnicode_Check(value)) {
PyErr_SetString(PyExc_TypeError, "value must be unicode string");
return false;
}
valStr = PyUnicode_AsUTF8(value);
#endif
}

auto pIt = propMap.find(valStr);
Expand Down
4 changes: 4 additions & 0 deletions src/App/Property.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,11 @@ void PropertyListsBase::_setPyObject(PyObject *value) {
for (Py_ssize_t i=0; i<nSize;++i) {
std::string keyStr;
PyObject* key = PyList_GetItem(keyList, i);
#if PY_MAJOR_VERSION < 3
if(!PyInt_Check(key))
#else
if(!PyLong_Check(key))
#endif
throw Base::TypeError("expect key type to be interger");
auto idx = PyLong_AsLong(key);
if(idx<-1 || idx>listSize)
Expand Down
4 changes: 2 additions & 2 deletions src/App/PropertyStandard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1232,7 +1232,7 @@ PyObject *PropertyFloatList::getPyObject(void)
double PropertyFloatList::getPyValue(PyObject *item) const {
if (PyFloat_Check(item)) {
return PyFloat_AsDouble(item);
#if PYTHON_VERSION_MAJOR >= 3
#if PY_MAJOR_VERSION >= 3
} else if (PyLong_Check(item)) {
return static_cast<double>(PyLong_AsLong(item));
#else
Expand Down Expand Up @@ -1719,7 +1719,7 @@ std::string PropertyStringList::getPyValue(PyObject *item) const {
std::string ret;
if (PyUnicode_Check(item)) {
#if PY_MAJOR_VERSION >= 3
values[i] = PyUnicode_AsUTF8(item);
ret = PyUnicode_AsUTF8(item);
#else
PyObject* unicode = PyUnicode_AsUTF8String(item);
ret = PyString_AsString(unicode);
Expand Down