App: remove Py2 code from several src/App .cpp files

This commit is contained in:
luz paz
2021-04-14 10:18:27 -04:00
committed by wwmayer
parent ee98ed5baf
commit fc6d1292d1
12 changed files with 0 additions and 268 deletions

View File

@@ -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
}
}
@@ -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;
}
@@ -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);
@@ -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)

View File

@@ -330,13 +330,8 @@ 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;
@@ -344,24 +339,7 @@ PyObject* DocumentObjectPy::setExpression(PyObject * args)
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.");
@@ -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;

View File

@@ -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
}
}
@@ -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;
@@ -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!");

View File

@@ -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));
@@ -490,23 +481,8 @@ 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) {
@@ -514,7 +490,6 @@ App::any pyObjectToAny(Py::Object value, bool check) {
}
return App::any(std::string(value));
}
#endif
else {
return App::any(pyObjectWrap(pyvalue));
}
@@ -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
@@ -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);
@@ -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: {
@@ -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

View File

@@ -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 */
@@ -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
@@ -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
//
@@ -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);
}

View File

@@ -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 << "'";
@@ -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;

View File

@@ -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);

View File

@@ -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)

View File

@@ -253,7 +253,6 @@ PyObject *PropertyFileIncluded::getPyObject(void)
return p;
}
#if PY_MAJOR_VERSION >= 3
namespace App {
const char* getNameFromFile(PyObject* value)
{
@@ -286,11 +285,9 @@ bool isIOFile(PyObject* file)
return isFile;
}
}
#endif
void PropertyFileIncluded::setPyObject(PyObject *value)
{
#if PY_MAJOR_VERSION >= 3
std::string string;
if (PyUnicode_Check(value)) {
string = PyUnicode_AsUTF8(value);
@@ -301,21 +298,6 @@ void PropertyFileIncluded::setPyObject(PyObject *value)
else if (isIOFile(value)){
string = getNameFromFile(value);
}
#else
std::string string;
if (PyUnicode_Check(value)) {
PyObject* unicode = PyUnicode_AsUTF8String(value);
string = PyString_AsString(unicode);
Py_DECREF(unicode);
}
else if (PyString_Check(value)) {
string = PyString_AsString(value);
}
else if (PyFile_Check(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)");
@@ -324,7 +306,6 @@ void PropertyFileIncluded::setPyObject(PyObject *value)
// decoding file
std::string fileStr;
#if PY_MAJOR_VERSION >= 3
if (PyUnicode_Check(file)) {
fileStr = PyUnicode_AsUTF8(file);
}
@@ -334,20 +315,6 @@ void PropertyFileIncluded::setPyObject(PyObject *value)
else if (isIOFile(value)) {
fileStr = getNameFromFile(file);
}
#else
if (PyUnicode_Check(file)) {
PyObject* unicode = PyUnicode_AsUTF8String(file);
fileStr = PyString_AsString(unicode);
Py_DECREF(unicode);
}
else if (PyString_Check(file)) {
fileStr = PyString_AsString(file);
}
else if (PyFile_Check(file)) {
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;
@@ -356,7 +323,6 @@ void PropertyFileIncluded::setPyObject(PyObject *value)
// decoding name
std::string nameStr;
#if PY_MAJOR_VERSION >= 3
if (PyUnicode_Check(name)) {
nameStr = PyUnicode_AsUTF8(name);
}
@@ -366,15 +332,6 @@ void PropertyFileIncluded::setPyObject(PyObject *value)
else if (isIOFile(value)) {
nameStr = getNameFromFile(name);
}
#else
if (PyString_Check(name)) {
nameStr = PyString_AsString(name);
}
else if (PyFile_Check(name)) {
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;

View File

@@ -118,39 +118,24 @@ void PropertyVector::setPyObject(PyObject *value)
item = PyTuple_GetItem(value,0);
if (PyFloat_Check(item))
cVec.x = PyFloat_AsDouble(item);
#if PY_MAJOR_VERSION < 3
else if (PyInt_Check(item))
cVec.x = (double)PyInt_AsLong(item);
#else
else if (PyLong_Check(item))
cVec.x = (double)PyLong_AsLong(item);
#endif
else
throw Base::TypeError("Not allowed type used in tuple (float expected)...");
// y
item = PyTuple_GetItem(value,1);
if (PyFloat_Check(item))
cVec.y = PyFloat_AsDouble(item);
#if PY_MAJOR_VERSION < 3
else if (PyInt_Check(item))
cVec.y = (double)PyInt_AsLong(item);
#else
else if (PyLong_Check(item))
cVec.y = (double)PyLong_AsLong(item);
#endif
else
throw Base::TypeError("Not allowed type used in tuple (float expected)...");
// z
item = PyTuple_GetItem(value,2);
if (PyFloat_Check(item))
cVec.z = PyFloat_AsDouble(item);
#if PY_MAJOR_VERSION < 3
else if (PyInt_Check(item))
cVec.z = (double)PyInt_AsLong(item);
#else
else if (PyLong_Check(item))
cVec.z = (double)PyLong_AsLong(item);
#endif
else
throw Base::TypeError("Not allowed type used in tuple (float expected)...");
setValue( cVec );
@@ -474,13 +459,8 @@ void PropertyMatrix::setPyObject(PyObject *value)
item = PyTuple_GetItem(value,x+y*4);
if (PyFloat_Check(item))
cMatrix[x][y] = PyFloat_AsDouble(item);
#if PY_MAJOR_VERSION < 3
else if (PyInt_Check(item))
cMatrix[x][y] = (double)PyInt_AsLong(item);
#else
else if (PyLong_Check(item))
cMatrix[x][y] = (double)PyLong_AsLong(item);
#endif
else
throw Base::TypeError("Not allowed type used in matrix tuple (a number expected)...");
}

View File

@@ -341,14 +341,7 @@ void PropertyPythonObject::Restore(Base::XMLReader &reader)
<< " has no class " << reader.getAttribute("class");
throw Py::AttributeError(s.str());
}
#if PY_MAJOR_VERSION >= 3
if (PyType_Check(cls)) {
#else
if (PyClass_Check(cls)) {
this->object = PyInstance_NewRaw(cls, 0);
}
else if (PyType_Check(cls)) {
#endif
this->object = PyType_GenericAlloc((PyTypeObject*)cls, 0);
}
else {
@@ -362,11 +355,7 @@ void PropertyPythonObject::Restore(Base::XMLReader &reader)
Py::Module mod(PyImport_ImportModule(nam.c_str()),true);
if (mod.isNull())
throw Py::Exception();
#if PY_MAJOR_VERSION >= 3
this->object = PyObject_CallObject(mod.getAttr(cls).ptr(), NULL);
#else
this->object = PyInstance_NewRaw(mod.getAttr(cls).ptr(), 0);
#endif
load_pickle = true;
buffer = std::string(what[2].second, end);
}

View File

@@ -78,25 +78,10 @@ Base::Quantity PropertyQuantity::createQuantityFromPy(PyObject *value)
Base::Quantity quant;
if (PyUnicode_Check(value)){
#if PY_MAJOR_VERSION >= 3
quant = Quantity::parse(QString::fromUtf8(PyUnicode_AsUTF8(value)));
}
#else
PyObject* unicode = PyUnicode_AsUTF8String(value);
std::string Str;
Str = PyString_AsString(unicode);
quant = Quantity::parse(QString::fromUtf8(Str.c_str()));
Py_DECREF(unicode);
}
else if (PyString_Check(value))
quant = Quantity::parse(QString::fromLatin1(PyString_AsString(value)));
#endif
else if (PyFloat_Check(value))
quant = Quantity(PyFloat_AsDouble(value),_Unit);
#if PY_MAJOR_VERSION < 3
else if (PyInt_Check(value))
quant = Quantity((double)PyInt_AsLong(value),_Unit);
#endif
else if (PyLong_Check(value))
quant = Quantity((double)PyLong_AsLong(value),_Unit);
else if (PyObject_TypeCheck(value, &(QuantityPy::Type))) {