Skip to content

Commit

Permalink
App: remove Py2 code from several src/App .cpp files
Browse files Browse the repository at this point in the history
  • Loading branch information
luzpaz authored and wwmayer committed Apr 19, 2021
1 parent ee98ed5 commit fc6d129
Show file tree
Hide file tree
Showing 12 changed files with 0 additions and 268 deletions.
17 changes: 0 additions & 17 deletions src/App/ApplicationPy.cpp
Expand Up @@ -449,11 +449,7 @@ PyObject* Application::sGetConfig(PyObject * /*self*/, PyObject *args)
}
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
}
}

Expand All @@ -465,11 +461,7 @@ PyObject* Application::sDumpConfig(PyObject * /*self*/, PyObject *args)
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;
}
Expand Down Expand Up @@ -718,11 +710,7 @@ PyObject* Application::sListDocuments(PyObject * /*self*/, PyObject *args)
docs = Document::getDependentDocuments(docs,true);

for (auto doc : docs) {
#if PY_MAJOR_VERSION >= 3
pKey = PyUnicode_FromString(doc->getName());
#else
pKey = PyString_FromString(doc->getName());
#endif
// GetPyObject() increments
pValue = static_cast<Base::PyObjectBase*>(doc->getPyObject());
PyDict_SetItem(pDict, pKey, pValue);
Expand Down Expand Up @@ -764,13 +752,8 @@ PyObject *Application::sSetLogLevel(PyObject * /*self*/, PyObject *args)
return NULL;
PY_TRY{
int l;
#if PY_MAJOR_VERSION < 3
if (PyString_Check(pcObj)) {
const char *pstr = PyString_AsString(pcObj);
#else
if (PyUnicode_Check(pcObj)) {
const char *pstr = PyUnicode_AsUTF8(pcObj);
#endif
if(strcmp(pstr,"Log") == 0)
l = FC_LOGLEVEL_LOG;
else if(strcmp(pstr,"Warning") == 0)
Expand Down
40 changes: 0 additions & 40 deletions src/App/DocumentObjectPyImp.cpp
Expand Up @@ -330,38 +330,16 @@ PyObject* DocumentObjectPy::setExpression(PyObject * args)

if (Py::Object(expr).isNone())
getDocumentObjectPtr()->setExpression(p, std::shared_ptr<Expression>());
#if PY_MAJOR_VERSION >= 3
else if (PyUnicode_Check(expr)) {
const char * exprStr = PyUnicode_AsUTF8(expr);
#else
else if (PyString_Check(expr)) {
const char * exprStr = PyString_AsString(expr);
#endif
std::shared_ptr<Expression> shared_expr(Expression::parse(getDocumentObjectPtr(), exprStr));
if(shared_expr && comment)
shared_expr->comment = comment;

getDocumentObjectPtr()->setExpression(p, shared_expr);
}
else if (PyUnicode_Check(expr)) {
#if PY_MAJOR_VERSION >= 3
std::string exprStr = PyUnicode_AsUTF8(expr);
#else
PyObject* unicode = PyUnicode_AsEncodedString(expr, "utf-8", 0);
if (unicode) {
std::string exprStr = PyString_AsString(unicode);
Py_DECREF(unicode);
std::shared_ptr<Expression> shared_expr(ExpressionParser::parse(getDocumentObjectPtr(), exprStr.c_str()));

if(shared_expr && comment)
shared_expr->comment = comment;
getDocumentObjectPtr()->setExpression(p, shared_expr);
}
else {
// utf-8 encoding failed
return 0;
}
#endif
}
else
throw Py::TypeError("String or None expected.");
Expand Down Expand Up @@ -445,32 +423,14 @@ PyObject* DocumentObjectPy::getSubObject(PyObject *args, PyObject *keywds)
std::vector<std::string> subs;
bool single=true;
if (PyUnicode_Check(obj)) {
#if PY_MAJOR_VERSION >= 3
subs.push_back(PyUnicode_AsUTF8(obj));
#else
PyObject* unicode = PyUnicode_AsUTF8String(obj);
subs.push_back(PyString_AsString(unicode));
Py_DECREF(unicode);
}
else if (PyString_Check(obj)) {
subs.push_back(PyString_AsString(obj));
#endif
} else if (PySequence_Check(obj)) {
single=false;
Py::Sequence shapeSeq(obj);
for (Py::Sequence::iterator it = shapeSeq.begin(); it != shapeSeq.end(); ++it) {
PyObject* item = (*it).ptr();
if (PyUnicode_Check(item)) {
#if PY_MAJOR_VERSION >= 3
subs.push_back(PyUnicode_AsUTF8(item));
#else
PyObject* unicode = PyUnicode_AsUTF8String(item);
subs.push_back(PyString_AsString(unicode));
Py_DECREF(unicode);
}
else if (PyString_Check(item)) {
subs.push_back(PyString_AsString(item));
#endif
}else{
PyErr_SetString(PyExc_TypeError, "non-string object in sequence");
return 0;
Expand Down
24 changes: 0 additions & 24 deletions src/App/DocumentPyImp.cpp
Expand Up @@ -179,11 +179,7 @@ PyObject* DocumentPy::exportGraphviz(PyObject * args)
else {
std::stringstream str;
getDocumentPtr()->exportGraphviz(str);
#if PY_MAJOR_VERSION >= 3
return PyUnicode_FromString(str.str().c_str());
#else
return PyString_FromString(str.str().c_str());
#endif
}
}

Expand Down Expand Up @@ -400,20 +396,9 @@ PyObject* DocumentPy::openTransaction(PyObject *args)
if (!value) {
cmd = "<empty>";
}
#if PY_MAJOR_VERSION >= 3
else if (PyUnicode_Check(value)) {
cmd = PyUnicode_AsUTF8(value);
}
#else
else if (PyUnicode_Check(value)) {
PyObject* unicode = PyUnicode_AsUTF8String(value);
cmd = PyString_AsString(unicode);
Py_DECREF(unicode);
}
else if (PyString_Check(value)) {
cmd = PyString_AsString(value);
}
#endif
else {
PyErr_SetString(PyExc_TypeError, "string or unicode expected");
return NULL;
Expand Down Expand Up @@ -716,16 +701,7 @@ PyObject* DocumentPy::getTempFileName(PyObject *args)

std::string string;
if (PyUnicode_Check(value)) {
#if PY_MAJOR_VERSION >= 3
string = PyUnicode_AsUTF8(value);
#else
PyObject* unicode = PyUnicode_AsUTF8String(value);
string = PyString_AsString(unicode);
Py_DECREF(unicode);
}
else if (PyString_Check(value)) {
string = PyString_AsString(value);
#endif
}
else {
std::string error = std::string("type must be a string!");
Expand Down
60 changes: 0 additions & 60 deletions src/App/Expression.cpp
Expand Up @@ -445,17 +445,8 @@ static Py::Object _pyObjectFromAny(const App::any &value, const Expression *e) {
else if (is_type(value,typeid(float)))
return Py::Float(cast<float>(value));
else if (is_type(value,typeid(int)))
#if PY_MAJOR_VERSION < 3
return Py::Int(cast<int>(value));
#else
return Py::Long(cast<int>(value));
#endif
else if (is_type(value,typeid(long))) {
#if PY_MAJOR_VERSION < 3
long l = cast<long>(value);
if(std::abs(l)<=INT_MAX)
return Py::Int(int(l));
#endif
return Py::Long(cast<long>(value));
} else if (is_type(value,typeid(bool)))
return Py::Boolean(cast<bool>(value));
Expand Down Expand Up @@ -490,31 +481,15 @@ App::any pyObjectToAny(Py::Object value, bool check) {
}
if (PyFloat_Check(pyvalue))
return App::any(PyFloat_AsDouble(pyvalue));
#if PY_MAJOR_VERSION < 3
if (PyInt_Check(pyvalue))
return App::any(PyInt_AsLong(pyvalue));
#endif
if (PyLong_Check(pyvalue))
return App::any(PyLong_AsLong(pyvalue));
#if PY_MAJOR_VERSION < 3
else if (PyString_Check(pyvalue))
return App::any(std::string(PyString_AsString(pyvalue)));
else if (PyUnicode_Check(pyvalue)) {
PyObject * s = PyUnicode_AsUTF8String(pyvalue);
if(!s)
FC_THROWM(Base::ValueError,"Invalid unicode string");
Py::Object o(s,true);
return App::any(std::string(PyString_AsString(s)));
}
#else
else if (PyUnicode_Check(pyvalue)) {
const char* value = PyUnicode_AsUTF8(pyvalue);
if (!value) {
FC_THROWM(Base::ValueError, "Invalid unicode string");
}
return App::any(std::string(value));
}
#endif
else {
return App::any(pyObjectWrap(pyvalue));
}
Expand All @@ -525,10 +500,6 @@ bool pyToQuantity(Quantity &q, const Py::Object &pyobj) {
q = *static_cast<Base::QuantityPy*>(*pyobj)->getQuantityPtr();
else if (PyFloat_Check(*pyobj))
q = Quantity(PyFloat_AsDouble(*pyobj));
#if PY_MAJOR_VERSION < 3
else if (PyInt_Check(*pyobj))
q = Quantity(PyInt_AsLong(*pyobj));
#endif
else if (PyLong_Check(*pyobj))
q = Quantity(PyLong_AsLong(*pyobj));
else
Expand Down Expand Up @@ -1479,30 +1450,6 @@ static Py::Object calc(const Expression *expr, int op,
BINARY_OP(DIV,TrueDivide)
case OperatorExpression::ADD: {
PyObject *res;
#if PY_MAJOR_VERSION < 3
if (PyString_CheckExact(*l) && PyString_CheckExact(*r)) {
Py_ssize_t v_len = PyString_GET_SIZE(*l);
Py_ssize_t w_len = PyString_GET_SIZE(*r);
Py_ssize_t new_len = v_len + w_len;
if (new_len < 0)
__EXPR_THROW(OverflowError, "strings are too large to concat", expr);

if (l.ptr()->ob_refcnt==1 && !PyString_CHECK_INTERNED(l.ptr())) {
res = Py::new_reference_to(l);
// Must make sure ob_refcnt is still 1
l = Py::Object();
if (_PyString_Resize(&res, new_len) != 0)
EXPR_PY_THROW(expr);
memcpy(PyString_AS_STRING(res) + v_len, PyString_AS_STRING(*r), w_len);
}else{
res = Py::new_reference_to(l);
l = Py::Object();
PyString_Concat(&res,*r);
if(!res) EXPR_PY_THROW(expr);
}
return Py::asObject(res);
}
#else
if (PyUnicode_CheckExact(*l) && PyUnicode_CheckExact(*r)) {
if(inplace) {
res = Py::new_reference_to(l);
Expand All @@ -1516,7 +1463,6 @@ static Py::Object calc(const Expression *expr, int op,
if(!res) EXPR_PY_THROW(expr);
return Py::asObject(res);
}
#endif
_BINARY_OP(Add);
}
case OperatorExpression::POW: {
Expand All @@ -1530,15 +1476,9 @@ static Py::Object calc(const Expression *expr, int op,
}
case OperatorExpression::MOD: {
PyObject *res;
#if PY_MAJOR_VERSION < 3
if (PyString_CheckExact(l.ptr()) &&
(!PyString_Check(r.ptr()) || PyString_CheckExact(r.ptr())))
res = PyString_Format(l.ptr(), r.ptr());
#else
if (PyUnicode_CheckExact(l.ptr()) &&
(!PyUnicode_Check(r.ptr()) || PyUnicode_CheckExact(r.ptr())))
res = PyUnicode_Format(l.ptr(), r.ptr());
#endif
else if(inplace)
res = PyNumber_InPlaceRemainder(l.ptr(),r.ptr());
else
Expand Down
14 changes: 0 additions & 14 deletions src/App/FeaturePythonPyImp.inl
Expand Up @@ -49,11 +49,7 @@ PyTypeObject FeaturePythonPyT<FeaturePyT>::Type = {
/* --- Functions to access object as input/output buffer ---------*/
0, /* tp_as_buffer */
/* --- Flags to define presence of optional/expanded features */
#if PY_MAJOR_VERSION >= 3
Py_TPFLAGS_BASETYPE|Py_TPFLAGS_DEFAULT, /*tp_flags */
#else
Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_CLASS, /*tp_flags */
#endif
"This is the father of all Feature classes", /*tp_doc */
0, /*tp_traverse */
0, /*tp_clear */
Expand Down Expand Up @@ -81,9 +77,7 @@ PyTypeObject FeaturePythonPyT<FeaturePyT>::Type = {
0, /*tp_weaklist */
0, /*tp_del */
0 /*tp_version_tag */
#if PY_MAJOR_VERSION >= 3
,0 /*tp_finalize */
#endif
#if PY_VERSION_HEX >= 0x03080000
,0 /*tp_vectorcall */
#endif
Expand All @@ -108,11 +102,7 @@ template<class FeaturePyT>
int FeaturePythonPyT<FeaturePyT>::__setattro(PyObject *obj, PyObject *attro, PyObject *value)
{
const char *attr;
#if PY_MAJOR_VERSION >= 3
attr = PyUnicode_AsUTF8(attro);
#else
attr = PyString_AsString(attro);
#endif
// This overwrites PyObjectBase::__setattr because this actively disallows to delete an attribute
//

Expand Down Expand Up @@ -144,11 +134,7 @@ int FeaturePythonPyT<FeaturePyT>::_setattr(const char *attr, PyObject *value)
if (value) {
if (PyFunction_Check(value)) {
PyErr_Clear();
#if PY_MAJOR_VERSION < 3
dict_item = PyMethod_New(value, this, 0);
#else
dict_item = PyMethod_New(value, this);
#endif
returnValue = PyDict_SetItemString(dict_methods, attr, dict_item);
Py_XDECREF(dict_item);
}
Expand Down
16 changes: 0 additions & 16 deletions src/App/LinkBaseExtensionPyImp.cpp
Expand Up @@ -48,19 +48,11 @@ static bool getProperty(PropTmpMap &props, const LinkBaseExtension::PropInfoMap
{
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 @@ -72,19 +64,11 @@ static bool getProperty(PropTmpMap &props, const LinkBaseExtension::PropInfoMap
if(key == value)
valStr = keyStr;
else if (value!=Py_None) {
#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
}

App::Property *prop = 0;
Expand Down
4 changes: 0 additions & 4 deletions src/App/ObjectIdentifier.cpp
Expand Up @@ -1548,11 +1548,7 @@ Py::Object ObjectIdentifier::access(const ResolveResults &result, Py::Object *va
GET_MODULE(re);
break;
case PseudoBuiltins:
#if PY_MAJOR_VERSION < 3
GET_MODULE(__builtin__);
#else
GET_MODULE(builtins);
#endif
break;
case PseudoMath:
GET_MODULE(math);
Expand Down
4 changes: 0 additions & 4 deletions src/App/Property.cpp
Expand Up @@ -286,11 +286,7 @@ void PropertyListsBase::_setPyObject(PyObject *value) {
for(auto it=dict.begin();it!=dict.end();++it) {
const auto &item = *it;
PyObject *key = item.first.ptr();
#if PY_MAJOR_VERSION < 3
if(!PyInt_Check(key))
#else
if(!PyLong_Check(key))
#endif
throw Base::TypeError("expect key type to be integer");
long idx = PyLong_AsLong(key);
if(idx<-1 || idx>listSize)
Expand Down

0 comments on commit fc6d129

Please sign in to comment.