Skip to content

Commit

Permalink
+ replace old C casts with static_cast in generated C++ code, remove …
Browse files Browse the repository at this point in the history
…unused parent structure of Python classes, support of -1 as return value of setCustomAttributes
  • Loading branch information
wwmayer committed Sep 27, 2015
1 parent 6fea753 commit ee28daa
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 51 deletions.
5 changes: 0 additions & 5 deletions src/Base/ParameterPy.cpp
Expand Up @@ -223,11 +223,6 @@ PyMethodDef ParameterGrpPy::Methods[] = {
{NULL, NULL, 0, NULL} /* Sentinel */
};

//--------------------------------------------------------------------------
// Parents structure
//--------------------------------------------------------------------------
PyParentObject ParameterGrpPy::Parents[] = {&PyObjectBase::Type,&ParameterGrpPy::Type, NULL};

//--------------------------------------------------------------------------
// constructor
//--------------------------------------------------------------------------
Expand Down
5 changes: 0 additions & 5 deletions src/Base/PyObjectBase.cpp
Expand Up @@ -132,11 +132,6 @@ PyMethodDef PyObjectBase::Methods[] = {
{NULL, NULL, 0, NULL} /* Sentinel */
};

/*------------------------------
* PyObjectBase Parents -- Every class, even the abstract one should have parents
------------------------------*/
PyParentObject PyObjectBase::Parents[] = {&PyObjectBase::Type, NULL};

PyObject* PyObjectBase::__getattr(PyObject * obj, char *attr)
{
// This should be the entry in Type
Expand Down
7 changes: 1 addition & 6 deletions src/Base/PyObjectBase.h
Expand Up @@ -143,9 +143,7 @@ typedef PyTypeObject * PyParentObject;
public: \
static PyTypeObject Type; \
static PyMethodDef Methods[]; \
static PyParentObject Parents[]; \
virtual PyTypeObject *GetType(void) {return &Type;} \
virtual PyParentObject *GetParents(void) {return Parents;}
virtual PyTypeObject *GetType(void) {return &Type;}

/** This defines the _getattr_up macro
* which allows attribute and method calls
Expand Down Expand Up @@ -485,9 +483,6 @@ BaseExport extern PyObject* BaseExceptionFreeCADError;

#endif // DONT_CATCH_CXX_EXCEPTIONS

/// Root definition of the inheritance tree of the FreeCAD python objects
#define PARENTSBasePyObjectBase &Base::PyObjectBase::Type,NULL

/** Python helper class
* This class encapsulate the Decoding of UTF8 to a python object.
* Including exception handling.
Expand Down
5 changes: 0 additions & 5 deletions src/Gui/WidgetFactory.cpp
Expand Up @@ -915,11 +915,6 @@ PyMethodDef PyResource::Methods[] = {
{NULL, NULL} /* Sentinel */
};

//--------------------------------------------------------------------------
// Parents structure
//--------------------------------------------------------------------------
PyParentObject PyResource::Parents[] = {&PyObjectBase::Type,&PyResource::Type, NULL};

//--------------------------------------------------------------------------
// constructor
//--------------------------------------------------------------------------
Expand Down
5 changes: 0 additions & 5 deletions src/Mod/Test/Gui/UnitTestPy.cpp
Expand Up @@ -271,11 +271,6 @@ PyMethodDef TestGui::UnitTestPy::Methods[] = {
{NULL, NULL} /* Sentinel */
};

//--------------------------------------------------------------------------
// Parents structure
//--------------------------------------------------------------------------
PyParentObject TestGui::UnitTestPy::Parents[] = {&PyObjectBase::Type, NULL};

//--------------------------------------------------------------------------
// Constructor
//--------------------------------------------------------------------------
Expand Down
49 changes: 24 additions & 25 deletions src/Tools/generateTemplates/templateClassPyExport.py
Expand Up @@ -60,9 +60,7 @@ class @self.export.Namespace@Export @self.export.Name@ : public @self.export.Fat
static PyObject * richCompare(PyObject *v, PyObject *w, int op);
-
static PyGetSetDef GetterSetter[];
static PyParentObject Parents[];
virtual PyTypeObject *GetType(void) {return &Type;};
virtual PyParentObject *GetParents(void) {return Parents;}
public:
@self.export.Name@(@self.export.TwinPointer@ *pcObject, PyTypeObject *T = &Type);
Expand Down Expand Up @@ -201,6 +199,7 @@ class @self.export.Namespace@Export @self.export.Name@ : public @self.export.Fat
/// getter method for special attributes (e.g. dynamic ones)
PyObject *getCustomAttributes(const char* attr) const;
/// setter for special attributes (e.g. dynamic ones)
/// Output: Success=1, Failure=-1, Ignore=0
int setCustomAttributes(const char* attr, PyObject *obj);
PyObject *_getattr(char *attr); // __getattr__ function
int _setattr(char *attr, PyObject *value); // __setattr__ function
Expand All @@ -217,8 +216,6 @@ class @self.export.Namespace@Export @self.export.Name@ : public @self.export.Fat
-
};
#define PARENTS@self.export.Namespace@@self.export.Name@ &@self.export.Name@::Type,PARENTS@self.export.FatherNamespace@@self.export.Father@
} //namespace @self.export.Namespace@
#endif // @self.export.Namespace.upper()@_@self.export.Name.upper()@_H
Expand Down Expand Up @@ -439,28 +436,28 @@ class @self.export.Namespace@Export @self.export.Name@ : public @self.export.Fat
-
{
// test if twin object not allready deleted
if (!((PyObjectBase*) self)->isValid()){
if (!static_cast<PyObjectBase*>(self)->isValid()) {
PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!");
return NULL;
}
+ if (not i.Const):
// test if object is set Const
if (((PyObjectBase*) self)->isConst()){
if (static_cast<PyObjectBase*>(self)->isConst()) {
PyErr_SetString(PyExc_ReferenceError, "This object is immutable, you can not set any attribute or call a non const method");
return NULL;
}
-
try { // catches all exceptions coming up from c++ and generate a python exception
+ if i.Keyword:
PyObject* ret = ((@self.export.Name@*)self)->@i.Name@(args, kwd);
PyObject* ret = static_cast<@self.export.Name@*>(self)->@i.Name@(args, kwd);
= else:
PyObject* ret = ((@self.export.Name@*)self)->@i.Name@(args);
PyObject* ret = static_cast<@self.export.Name@*>(self)->@i.Name@(args);
-
+ if (not i.Const):
if (ret != 0)
((@self.export.Name@*)self)->startNotify();
static_cast<@self.export.Name@*>(self)->startNotify();
-
return ret;
}
Expand Down Expand Up @@ -524,13 +521,13 @@ class @self.export.Namespace@Export @self.export.Name@ : public @self.export.Fat
// has to be implemented in @self.export.Name@Imp.cpp
PyObject * @self.export.Name@::staticCallback_get@i.Name@ (PyObject *self, void * /*closure*/)
{
if (!((PyObjectBase*) self)->isValid()){
if (!static_cast<PyObjectBase*>(self)->isValid()){
PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!");
return NULL;
}
try {
return Py::new_reference_to(((@self.export.Name@*)self)->get@i.Name@());
return Py::new_reference_to(static_cast<@self.export.Name@*>(self)->get@i.Name@());
} catch (const Py::Exception&) {
// The exception text is already set
return NULL;
Expand All @@ -543,7 +540,7 @@ class @self.export.Namespace@Export @self.export.Name@ : public @self.export.Fat
+ if (i.ReadOnly):
int @self.export.Name@::staticCallback_set@i.Name@ (PyObject *self, PyObject * /*value*/, void * /*closure*/)
{
if (!((PyObjectBase*) self)->isValid()){
if (!static_cast<PyObjectBase*>(self)->isValid()){
PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!");
return -1;
}
Expand All @@ -553,21 +550,21 @@ class @self.export.Namespace@Export @self.export.Name@ : public @self.export.Fat
}
= else:
int @self.export.Name@::staticCallback_set@i.Name@ (PyObject *self, PyObject *value, void * /*closure*/)
{
if (!((PyObjectBase*) self)->isValid()){
{
if (!static_cast<PyObjectBase*>(self)->isValid()){
PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!");
return -1;
}
if (((PyObjectBase*) self)->isConst()){
if (static_cast<PyObjectBase*>(self)->isConst()){
PyErr_SetString(PyExc_ReferenceError, "This object is immutable, you can not set any attribute or call a method");
return -1;
}
try {
+ if (i.Parameter.Type == "Float"):
((@self.export.Name@*)self)->set@i.Name@(Py::@i.Parameter.Type@(PyNumber_Float(value),true));
static_cast<@self.export.Name@*>(self)->set@i.Name@(Py::@i.Parameter.Type@(PyNumber_Float(value),true));
= else:
((@self.export.Name@*)self)->set@i.Name@(Py::@i.Parameter.Type@(value,false));
static_cast<@self.export.Name@*>(self)->set@i.Name@(Py::@i.Parameter.Type@(value,false));
-
return 0;
} catch (const Py::Exception&) {
Expand All @@ -583,16 +580,12 @@ class @self.export.Namespace@Export @self.export.Name@ : public @self.export.Fat
-
//--------------------------------------------------------------------------
// Parents structure
//--------------------------------------------------------------------------
PyParentObject @self.export.Name@::Parents[] = { PARENTS@self.export.Namespace@@self.export.Name@ };
//--------------------------------------------------------------------------
// Constructor
//--------------------------------------------------------------------------
@self.export.Name@::@self.export.Name@(@self.export.TwinPointer@ *pcObject, PyTypeObject *T)
: @self.export.Father@(reinterpret_cast<@self.export.Father@::PointerType>(pcObject), T)
: @self.export.Father@(static_cast<@self.export.Father@::PointerType>(pcObject), T)
{
+ if (self.export.Reference):
pcObject->ref();
Expand Down Expand Up @@ -624,7 +617,7 @@ class @self.export.Namespace@Export @self.export.Name@ : public @self.export.Fat
-
+ if (self.export.Delete):
// delete the handled object when the PyObject dies
@self.export.Name@::PointerType ptr = reinterpret_cast<@self.export.Name@::PointerType>(_pcTwinPointer);
@self.export.Name@::PointerType ptr = static_cast<@self.export.Name@::PointerType>(_pcTwinPointer);
delete ptr;
-
}
Expand Down Expand Up @@ -709,12 +702,18 @@ class @self.export.Namespace@Export @self.export.Name@ : public @self.export.Fat
}
}
int @self.export.Name@::_setattr(char *attr, PyObject *value) // __setattr__ function: note only need to handle new state
int @self.export.Name@::_setattr(char *attr, PyObject *value) // __setattr__ function: note only need to handle new state
{
try {
// setter for special Attributes (e.g. dynamic ones)
int r = setCustomAttributes(attr, value);
if(r==1) return 0;
// r = 1: handled
// r = -1: error
// r = 0: ignore
if (r == 1)
return 0;
else if (r == -1)
return -1;
}
#ifndef DONT_CATCH_CXX_EXCEPTIONS
catch(const Base::Exception& e) // catch the FreeCAD exceptions
Expand Down

0 comments on commit ee28daa

Please sign in to comment.