Base: Do not use else before return

This commit is contained in:
Ladislav Michl
2023-11-15 10:12:42 +01:00
committed by 3x380V
parent 915bfd3dde
commit 95b37fa806
13 changed files with 115 additions and 176 deletions

View File

@@ -475,14 +475,11 @@ void InterpreterSingleton::runFile(const char* pxFileName, bool local)
if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
throw SystemExitException();
}
throw PyException();
}
Py_DECREF(result);
}
else {
throw FileException("Unknown file", pxFileName);
}
throw FileException("Unknown file", pxFileName);
}
bool InterpreterSingleton::loadModule(const char* psModName)

View File

@@ -256,11 +256,9 @@ PyObject* MatrixPy::richCompare(PyObject* v, PyObject* w, int op)
Py_INCREF(res);
return res;
}
else {
// This always returns False
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
// This always returns False
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
PyObject* MatrixPy::move(PyObject* args)

View File

@@ -501,11 +501,7 @@ std::vector<Base::Reference<ParameterGrp>> ParameterGrp::GetGroups()
/// test if this group is empty
bool ParameterGrp::IsEmpty() const
{
if (_pGroupNode && _pGroupNode->getFirstChild()) {
return false;
}
return true;
return !(_pGroupNode && _pGroupNode->getFirstChild());
}
/// test if a special sub group is in this group

View File

@@ -148,22 +148,18 @@ PyObject* PlacementPy::richCompare(PyObject* v, PyObject* w, int op)
PyErr_SetString(PyExc_TypeError, "no ordering relation is defined for Placement");
return nullptr;
}
else if (op == Py_EQ) {
if (op == Py_EQ) {
res = (p1 == p2) ? Py_True : Py_False;
Py_INCREF(res);
return res;
}
else {
res = (p1 != p2) ? Py_True : Py_False;
Py_INCREF(res);
return res;
}
}
else {
// This always returns False
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
res = (p1 != p2) ? Py_True : Py_False;
Py_INCREF(res);
return res;
}
// This always returns False
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
PyObject* PlacementPy::move(PyObject* args)

View File

@@ -365,7 +365,7 @@ int PyObjectBase::__setattro(PyObject *obj, PyObject *attro, PyObject *value)
PyErr_Format(PyExc_AttributeError, "Cannot delete attribute: '%s'", attr);
return -1;
}
else if (!static_cast<PyObjectBase*>(obj)->isValid()){
if (!static_cast<PyObjectBase*>(obj)->isValid()){
PyErr_Format(PyExc_ReferenceError, "Cannot access attribute '%s' of deleted object", attr);
return -1;
}
@@ -404,36 +404,32 @@ PyObject *PyObjectBase::_getattr(const char *attr)
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
return reinterpret_cast<PyObject *>(Py_TYPE(this));
}
else if (streq(attr, "__members__")) {
if (streq(attr, "__members__")) {
// Use __dict__ instead as __members__ is deprecated
return nullptr;
}
else if (streq(attr,"__dict__")) {
if (streq(attr,"__dict__")) {
// Return the default dict
PyTypeObject *tp = Py_TYPE(this);
Py_XINCREF(tp->tp_dict);
return tp->tp_dict;
}
else if (streq(attr,"softspace")) {
if (streq(attr,"softspace")) {
// Internal Python stuff
return nullptr;
}
else {
// As fallback solution use Python's default method to get generic attributes
PyObject *w{};
PyObject *res{};
w = PyUnicode_InternFromString(attr);
if (w) {
res = PyObject_GenericGetAttr(this, w);
Py_XDECREF(w);
return res;
} else {
// Throw an exception for unknown attributes
PyTypeObject *tp = Py_TYPE(this);
PyErr_Format(PyExc_AttributeError, "%.50s instance has no attribute '%.400s'", tp->tp_name, attr);
return nullptr;
}
// As fallback solution use Python's default method to get generic attributes
PyObject *w{}, *res{};
w = PyUnicode_InternFromString(attr);
if (w) {
res = PyObject_GenericGetAttr(this, w);
Py_XDECREF(w);
return res;
}
// Throw an exception for unknown attributes
PyTypeObject *tp = Py_TYPE(this);
PyErr_Format(PyExc_AttributeError, "%.50s instance has no attribute '%.400s'", tp->tp_name, attr);
return nullptr;
}
int PyObjectBase::_setattr(const char *attr, PyObject *value)
@@ -449,12 +445,11 @@ int PyObjectBase::_setattr(const char *attr, PyObject *value)
int res = PyObject_GenericSetAttr(this, w, value);
Py_DECREF(w);
return res;
} else {
// Throw an exception for unknown attributes
PyTypeObject *tp = Py_TYPE(this);
PyErr_Format(PyExc_AttributeError, "%.50s instance has no attribute '%.400s'", tp->tp_name, attr);
return -1;
}
// Throw an exception for unknown attributes
PyTypeObject *tp = Py_TYPE(this);
PyErr_Format(PyExc_AttributeError, "%.50s instance has no attribute '%.400s'", tp->tp_name, attr);
return -1;
}
/*------------------------------

View File

@@ -359,27 +359,25 @@ PP_Convert_Result(PyObject *presult, const char *resFormat, void *resTarget)
Py_DECREF(presult); /* procedures and stmts return None */
return 0;
}
else
if (! PyArg_Parse(presult, resFormat, resTarget)) { /* convert Python->C */
Py_DECREF(presult); /* need not be tuple */
return -1; /* error in convert */
}
else {
if (strcmp(resFormat, "O") != 0) { /* free object unless exported */
if (strcmp(resFormat, "s") == 0) { /* copy string: caller owns it */
char **target = (char**) resTarget;
if (strcmp(resFormat, "O") != 0) { /* free object unless exported */
if (strcmp(resFormat, "s") == 0) { /* copy string: caller owns it */
char **target = (char**) resTarget;
#if defined (__GNUC__)
*target = strdup(*target);
*target = strdup(*target);
#else
*target = _strdup(*target);
*target = _strdup(*target);
#endif
}
Py_DECREF(presult);
}
return 0; /* returns 0=success, -1=failure */
} /* if 0: C result in *resTarget */
} /* caller must decref if fmt="O" */
/* caller must free() if fmt="s" */
Py_DECREF(presult);
}
return 0; /* returns 0=success, -1=failure */
/* if 0: C result in *resTarget */
} /* caller must decref if fmt="O" */
/* caller must free() if fmt="s" */
int
PP_Get_Global(const char *modname, const char *varname, const char *resfmt, void *cresult)
@@ -430,27 +428,26 @@ int PP_DEBUG = 0; /* debug embedded code with pdb? */
const char *PP_Init(const char *modname) {
Py_Initialize(); /* init python if needed */
if (modname!=NULL) return modname;
{ /* we assume here that the caller frees allocated memory */
return "__main__";
}
if (modname)
return modname;
/* we assume here that the caller frees allocated memory */
return "__main__";
}
int
PP_Make_Dummy_Module(const char *modname) /* namespace for strings, if no file */
{ /* instead of sharing __main__ for all */
PyObject *module = NULL, *dict = NULL; /* note: __main__ is created in py_init */
PP_Make_Dummy_Module(const char *modname) /* namespace for strings, if no file */
{ /* instead of sharing __main__ for all */
PyObject *module = NULL, *dict = NULL; /* note: __main__ is created in py_init */
Py_Initialize();
module = PyImport_AddModule(modname); /* fetch or make, no load */
if (module == NULL) /* module not incref'd */
return -1;
else { /* module.__dict__ */
dict = PyModule_GetDict(module); /* ['__dummy__'] = None */
PyDict_SetItemString(dict, "__dummy__", Py_None);
PyDict_SetItemString(dict, "__builtins__", PyEval_GetBuiltins());
return 0;
}
/* module.__dict__ */
dict = PyModule_GetDict(module); /* ['__dummy__'] = None */
PyDict_SetItemString(dict, "__dummy__", Py_None);
PyDict_SetItemString(dict, "__builtins__", PyEval_GetBuiltins());
return 0;
}
@@ -479,17 +476,14 @@ PP_Load_Module(const char *modname) /* modname can be "package.module" for
PyDict_GetItemString(PyModule_GetDict(module), "__dummy__")) {
return module; /* not increfd */
}
else
if (PP_RELOAD && module != NULL && PyModule_Check(module)) {
module = PyImport_ReloadModule(module); /* reload file,run code */
Py_XDECREF(module); /* still on sys.modules */
return module; /* not increfd */
}
else {
module = PyImport_ImportModule(modname); /* fetch or load module */
Py_XDECREF(module); /* still on sys.modules */
return module; /* not increfd */
}
module = PyImport_ImportModule(modname); /* fetch or load module */
Py_XDECREF(module); /* still on sys.modules */
return module; /* not increfd */
}

View File

@@ -508,20 +508,18 @@ PyObject* QuantityPy::number_power_handler(PyObject* self, PyObject* other, PyOb
return new QuantityPy(new Quantity(q));
}
else if (PyFloat_Check(other)) {
if (PyFloat_Check(other)) {
Base::Quantity* a = static_cast<QuantityPy*>(self)->getQuantityPtr();
double b = PyFloat_AsDouble(other);
return new QuantityPy(new Quantity(a->pow(b)));
}
else if (PyLong_Check(other)) {
if (PyLong_Check(other)) {
Base::Quantity* a = static_cast<QuantityPy*>(self)->getQuantityPtr();
double b = (double)PyLong_AsLong(other);
return new QuantityPy(new Quantity(a->pow(b)));
}
else {
PyErr_SetString(PyExc_TypeError, "Expected quantity or number");
return nullptr;
}
PyErr_SetString(PyExc_TypeError, "Expected quantity or number");
return nullptr;
}
PY_CATCH
}

View File

@@ -116,12 +116,10 @@ long Base::XMLReader::getAttributeAsInteger(const char* AttrName) const
if (pos != AttrMap.end()) {
return atol(pos->second.c_str());
}
else {
// wrong name, use hasAttribute if not sure!
std::ostringstream msg;
msg << "XML Attribute: \"" << AttrName << "\" not found";
throw Base::XMLAttributeError(msg.str());
}
// wrong name, use hasAttribute if not sure!
std::ostringstream msg;
msg << "XML Attribute: \"" << AttrName << "\" not found";
throw Base::XMLAttributeError(msg.str());
}
unsigned long Base::XMLReader::getAttributeAsUnsigned(const char* AttrName) const
@@ -131,12 +129,10 @@ unsigned long Base::XMLReader::getAttributeAsUnsigned(const char* AttrName) cons
if (pos != AttrMap.end()) {
return strtoul(pos->second.c_str(), nullptr, 10);
}
else {
// wrong name, use hasAttribute if not sure!
std::ostringstream msg;
msg << "XML Attribute: \"" << AttrName << "\" not found";
throw Base::XMLAttributeError(msg.str());
}
// wrong name, use hasAttribute if not sure!
std::ostringstream msg;
msg << "XML Attribute: \"" << AttrName << "\" not found";
throw Base::XMLAttributeError(msg.str());
}
double Base::XMLReader::getAttributeAsFloat(const char* AttrName) const
@@ -146,12 +142,10 @@ double Base::XMLReader::getAttributeAsFloat(const char* AttrName) const
if (pos != AttrMap.end()) {
return atof(pos->second.c_str());
}
else {
// wrong name, use hasAttribute if not sure!
std::ostringstream msg;
msg << "XML Attribute: \"" << AttrName << "\" not found";
throw Base::XMLAttributeError(msg.str());
}
// wrong name, use hasAttribute if not sure!
std::ostringstream msg;
msg << "XML Attribute: \"" << AttrName << "\" not found";
throw Base::XMLAttributeError(msg.str());
}
const char* Base::XMLReader::getAttribute(const char* AttrName) const
@@ -161,12 +155,10 @@ const char* Base::XMLReader::getAttribute(const char* AttrName) const
if (pos != AttrMap.end()) {
return pos->second.c_str();
}
else {
// wrong name, use hasAttribute if not sure!
std::ostringstream msg;
msg << "XML Attribute: \"" << AttrName << "\" not found";
throw Base::XMLAttributeError(msg.str());
}
// wrong name, use hasAttribute if not sure!
std::ostringstream msg;
msg << "XML Attribute: \"" << AttrName << "\" not found";
throw Base::XMLAttributeError(msg.str());
}
bool Base::XMLReader::hasAttribute(const char* AttrName) const
@@ -216,7 +208,7 @@ void Base::XMLReader::readElement(const char* ElementName)
// thus we must stop reading on.
break;
}
else if (ReadType == EndDocument) {
if (ReadType == EndDocument) {
// the end of the document has been reached but we still try to continue on reading
throw Base::XMLParseException("End of document reached");
}
@@ -276,7 +268,7 @@ void Base::XMLReader::readEndElement(const char* ElementName, int level)
&& (level < 0 || level == Level)) {
return;
}
else if (ReadType == EndDocument) {
if (ReadType == EndDocument) {
// the end of the document has been reached but we still try to continue on reading
throw Base::XMLParseException("End of document reached");
}

View File

@@ -271,22 +271,18 @@ PyObject* RotationPy::richCompare(PyObject* v, PyObject* w, int op)
PyErr_SetString(PyExc_TypeError, "no ordering relation is defined for Rotation");
return nullptr;
}
else if (op == Py_EQ) {
if (op == Py_EQ) {
res = (r1 == r2) ? Py_True : Py_False;
Py_INCREF(res);
return res;
}
else {
res = (r1 != r2) ? Py_True : Py_False;
Py_INCREF(res);
return res;
}
}
else {
// This always returns False
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
res = (r1 != r2) ? Py_True : Py_False;
Py_INCREF(res);
return res;
}
// This always returns False
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
PyObject* RotationPy::invert(PyObject* args)

View File

@@ -1118,10 +1118,7 @@ BOOL __stdcall StackWalker::myReadProcMem(
//printf("ReadMemory: hProcess: %p, baseAddr: %p, buffer: %p, size: %d, read: %d, result: %d\n", hProcess, (LPVOID) qwBaseAddress, lpBuffer, nSize, (DWORD) st, (DWORD) bRet);
return bRet;
}
else
{
return s_readMemoryFunction(hProcess, qwBaseAddress, lpBuffer, nSize, lpNumberOfBytesRead, s_readMemoryFunction_UserData);
}
return s_readMemoryFunction(hProcess, qwBaseAddress, lpBuffer, nSize, lpNumberOfBytesRead, s_readMemoryFunction_UserData);
}
void StackWalker::OnLoadModule(LPCSTR img, LPCSTR mod, DWORD64 baseAddr, DWORD size, DWORD result, LPCSTR symType, LPCSTR pdbName, ULONGLONG fileVersion)

View File

@@ -175,10 +175,8 @@ PyObject* UnitPy::number_multiply_handler(PyObject* self, PyObject* other)
return new UnitPy(new Unit((*a) * (*b)));
}
else {
PyErr_SetString(PyExc_TypeError, "A Unit can only be multiplied by a Unit");
return nullptr;
}
PyErr_SetString(PyExc_TypeError, "A Unit can only be multiplied by a Unit");
return nullptr;
}
PyObject* UnitPy::richCompare(PyObject* v, PyObject* w, int op)
@@ -192,22 +190,18 @@ PyObject* UnitPy::richCompare(PyObject* v, PyObject* w, int op)
PyErr_SetString(PyExc_TypeError, "no ordering relation is defined for Units");
return nullptr;
}
else if (op == Py_EQ) {
if (op == Py_EQ) {
res = (*u1 == *u2) ? Py_True : Py_False; // NOLINT
Py_INCREF(res);
return res;
}
else {
res = (*u1 != *u2) ? Py_True : Py_False; // NOLINT
Py_INCREF(res);
return res;
}
}
else {
// This always returns False
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
res = (*u1 != *u2) ? Py_True : Py_False; // NOLINT
Py_INCREF(res);
return res;
}
// This always returns False
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
Py::String UnitPy::getType() const

View File

@@ -193,6 +193,7 @@ double UnitsApi::toDouble(PyObject* args, const Base::Unit& u)
}
throw Base::UnitsMismatchError("Wrong unit type!");
}
if (PyFloat_Check(args)) {
return PyFloat_AsDouble(args);
}

View File

@@ -150,30 +150,24 @@ PyObject* VectorPy::number_multiply_handler(PyObject* self, PyObject* other)
Py::Float mult(a * b);
return Py::new_reference_to(mult);
}
else if (PyNumber_Check(other)) {
if (PyNumber_Check(other)) {
double b = PyFloat_AsDouble(other);
return new VectorPy(a * b);
}
else {
PyErr_SetString(PyExc_TypeError, "A Vector can only be multiplied by Vector or number");
return nullptr;
}
PyErr_SetString(PyExc_TypeError, "A Vector can only be multiplied by Vector or number");
return nullptr;
}
else if (PyObject_TypeCheck(other, &(VectorPy::Type))) {
if (PyObject_TypeCheck(other, &(VectorPy::Type))) {
Base::Vector3d a = static_cast<VectorPy*>(other)->value();
if (PyNumber_Check(self)) {
double b = PyFloat_AsDouble(self);
return new VectorPy(a * b);
}
else {
PyErr_SetString(PyExc_TypeError, "A Vector can only be multiplied by Vector or number");
return nullptr;
}
}
else {
PyErr_SetString(PyExc_TypeError, "First or second arg must be Vector");
PyErr_SetString(PyExc_TypeError, "A Vector can only be multiplied by Vector or number");
return nullptr;
}
PyErr_SetString(PyExc_TypeError, "First or second arg must be Vector");
return nullptr;
}
Py_ssize_t VectorPy::sequence_length(PyObject* /*unused*/)
@@ -243,13 +237,8 @@ PyObject* VectorPy::mapping_subscript(PyObject* self, PyObject* item)
}
return sequence_item(self, i);
}
else if (PySlice_Check(item)) {
Py_ssize_t start = 0;
Py_ssize_t stop = 0;
Py_ssize_t step = 0;
Py_ssize_t slicelength = 0;
Py_ssize_t cur = 0;
Py_ssize_t i = 0;
if (PySlice_Check(item)) {
Py_ssize_t start = 0, stop = 0, step = 0, slicelength = 0, cur = 0, i = 0;
PyObject* slice = item;
if (PySlice_GetIndicesEx(slice, sequence_length(self), &start, &stop, &step, &slicelength)
@@ -260,7 +249,7 @@ PyObject* VectorPy::mapping_subscript(PyObject* self, PyObject* item)
if (slicelength <= 0) {
return PyTuple_New(0);
}
else if (start == 0 && step == 1 && slicelength == sequence_length(self)
if (start == 0 && step == 1 && slicelength == sequence_length(self)
&& PyObject_TypeCheck(self, &(VectorPy::Type))) {
Base::Vector3d v = static_cast<VectorPy*>(self)->value();
Py::Tuple xyz(3);
@@ -269,7 +258,7 @@ PyObject* VectorPy::mapping_subscript(PyObject* self, PyObject* item)
xyz.setItem(2, Py::Float(v.z));
return Py::new_reference_to(xyz);
}
else if (PyObject_TypeCheck(self, &(VectorPy::Type))) {
if (PyObject_TypeCheck(self, &(VectorPy::Type))) {
Base::Vector3d v = static_cast<VectorPy*>(self)->value();
Py::Tuple xyz(static_cast<size_t>(slicelength));
@@ -342,22 +331,18 @@ PyObject* VectorPy::richCompare(PyObject* v, PyObject* w, int op)
PyErr_SetString(PyExc_TypeError, "no ordering relation is defined for Vector");
return nullptr;
}
else if (op == Py_EQ) {
if (op == Py_EQ) {
res = (v1 == v2) ? Py_True : Py_False; // NOLINT
Py_INCREF(res);
return res;
}
else {
res = (v1 != v2) ? Py_True : Py_False; // NOLINT
Py_INCREF(res);
return res;
}
}
else {
// This always returns False
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
res = (v1 != v2) ? Py_True : Py_False; // NOLINT
Py_INCREF(res);
return res;
}
// This always returns False
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
PyObject* VectorPy::isEqual(PyObject* args)