00001
00002
00003
00004
00005
00006 #include <boost/filesystem/path.hpp>
00007 #include <boost/filesystem/operations.hpp>
00008 #include <boost/filesystem/exception.hpp>
00009 #include <Base/PyObjectBase.h>
00010 #include <Base/Console.h>
00011 #include <Base/Exception.h>
00012 #include <CXX/Objects.hxx>
00013
00014 #define new DEBUG_CLIENTBLOCK
00015
00016 using Base::streq;
00017 using namespace Base;
00018
00020 PyTypeObject BaseClassPy::Type = {
00021 PyObject_HEAD_INIT(&PyType_Type)
00022 0,
00023 "Base.BaseClass",
00024 sizeof(BaseClassPy),
00025 0,
00026
00027 PyDestructor,
00028 0,
00029 __getattr,
00030 __setattr,
00031 0,
00032 __repr,
00033 0,
00034 0,
00035 0,
00036 0,
00037 0,
00038 0,
00039 0,
00040 0,
00041
00042 0,
00043
00044 Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_CLASS,
00045 "This is the base class",
00046 0,
00047 0,
00048 0,
00049 0,
00050 0,
00051 0,
00052 Base::BaseClassPy::Methods,
00053 0,
00054 Base::BaseClassPy::GetterSetter,
00055 &Base::PyObjectBase::Type,
00056 0,
00057 0,
00058 0,
00059 0,
00060 __PyInit,
00061 0,
00062 Base::BaseClassPy::PyMake,
00063 0,
00064 0,
00065 0,
00066 0,
00067 0,
00068 0,
00069 0,
00070 0
00071 };
00072
00074 PyMethodDef BaseClassPy::Methods[] = {
00075 {"isDerivedFrom",
00076 (PyCFunction) staticCallback_isDerivedFrom,
00077 METH_VARARGS,
00078 "Returns true if given type is a father"
00079 },
00080 {"getAllDerivedFrom",
00081 (PyCFunction) staticCallback_getAllDerivedFrom,
00082 METH_VARARGS,
00083 "Returns all descentences"
00084 },
00085 {NULL, NULL, 0, NULL}
00086 };
00087
00088
00089
00091 PyGetSetDef BaseClassPy::GetterSetter[] = {
00092 {"Type",
00093 (getter) staticCallback_getType,
00094 (setter) staticCallback_setType,
00095 "Is the type of the FreeCAD object with module domain",
00096 NULL
00097 },
00098 {"Module",
00099 (getter) staticCallback_getModule,
00100 (setter) staticCallback_setModule,
00101 "Module in which this class is defined",
00102 NULL
00103 },
00104 {NULL, NULL, NULL, NULL, NULL}
00105 };
00106
00107
00108
00109
00110 PyObject * BaseClassPy::staticCallback_isDerivedFrom (PyObject *self, PyObject *args)
00111 {
00112
00113 if (!((PyObjectBase*) self)->isValid()){
00114 PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!");
00115 return NULL;
00116 }
00117
00118
00119 try {
00120 PyObject* ret = ((BaseClassPy*)self)->isDerivedFrom(args);
00121 return ret;
00122 }
00123 catch(const Base::Exception& e)
00124 {
00125 std::string str;
00126 str += "FreeCAD exception thrown (";
00127 str += e.what();
00128 str += ")";
00129 e.ReportException();
00130 PyErr_SetString(PyExc_Exception,str.c_str());
00131 return NULL;
00132 }
00133 catch(const boost::filesystem::filesystem_error& e)
00134 {
00135 std::string str;
00136 str += "File system exception thrown (";
00137
00138
00139 str += e.what();
00140 str += ")\n";
00141 Base::Console().Error(str.c_str());
00142 PyErr_SetString(PyExc_Exception,str.c_str());
00143 return NULL;
00144 }
00145 catch(const Py::Exception&)
00146 {
00147
00148 return NULL;
00149 }
00150 catch(const char* e)
00151 {
00152 Base::Console().Error(e);
00153 PyErr_SetString(PyExc_Exception,e);
00154 return NULL;
00155 }
00156
00157 #ifndef DONT_CATCH_CXX_EXCEPTIONS
00158 catch(const std::exception& e)
00159 {
00160 std::string str;
00161 str += "FC++ exception thrown (";
00162 str += e.what();
00163 str += ")";
00164 Base::Console().Error(str.c_str());
00165 PyErr_SetString(PyExc_Exception,str.c_str());
00166 return NULL;
00167 }
00168 catch(...)
00169 {
00170 PyErr_SetString(PyExc_Exception,"Unknown C++ exception");
00171 return NULL;
00172 }
00173 #endif
00174 }
00175
00176
00177
00178
00179 PyObject * BaseClassPy::staticCallback_getAllDerivedFrom (PyObject *self, PyObject *args)
00180 {
00181
00182 if (!((PyObjectBase*) self)->isValid()){
00183 PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!");
00184 return NULL;
00185 }
00186
00187
00188 try {
00189 PyObject* ret = ((BaseClassPy*)self)->getAllDerivedFrom(args);
00190 return ret;
00191 }
00192 catch(const Base::Exception& e)
00193 {
00194 std::string str;
00195 str += "FreeCAD exception thrown (";
00196 str += e.what();
00197 str += ")";
00198 e.ReportException();
00199 PyErr_SetString(PyExc_Exception,str.c_str());
00200 return NULL;
00201 }
00202 catch(const boost::filesystem::filesystem_error& e)
00203 {
00204 std::string str;
00205 str += "File system exception thrown (";
00206
00207
00208 str += e.what();
00209 str += ")\n";
00210 Base::Console().Error(str.c_str());
00211 PyErr_SetString(PyExc_Exception,str.c_str());
00212 return NULL;
00213 }
00214 catch(const Py::Exception&)
00215 {
00216
00217 return NULL;
00218 }
00219 catch(const char* e)
00220 {
00221 Base::Console().Error(e);
00222 PyErr_SetString(PyExc_Exception,e);
00223 return NULL;
00224 }
00225
00226 #ifndef DONT_CATCH_CXX_EXCEPTIONS
00227 catch(const std::exception& e)
00228 {
00229 std::string str;
00230 str += "FC++ exception thrown (";
00231 str += e.what();
00232 str += ")";
00233 Base::Console().Error(str.c_str());
00234 PyErr_SetString(PyExc_Exception,str.c_str());
00235 return NULL;
00236 }
00237 catch(...)
00238 {
00239 PyErr_SetString(PyExc_Exception,"Unknown C++ exception");
00240 return NULL;
00241 }
00242 #endif
00243 }
00244
00245
00246
00247
00248 PyObject * BaseClassPy::staticCallback_getType (PyObject *self, void * )
00249 {
00250 if (!((PyObjectBase*) self)->isValid()){
00251 PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!");
00252 return NULL;
00253 }
00254
00255 try {
00256 return Py::new_reference_to(((BaseClassPy*)self)->getType());
00257 } catch (const Py::Exception&) {
00258
00259 return NULL;
00260 } catch (...) {
00261 PyErr_SetString(PyExc_Exception, "Unknown exception while reading attribute 'Type' of object 'BaseClass'");
00262 return NULL;
00263 }
00264 }
00265
00266 int BaseClassPy::staticCallback_setType (PyObject *self, PyObject * , void * )
00267 {
00268 if (!((PyObjectBase*) self)->isValid()){
00269 PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!");
00270 return -1;
00271 }
00272
00273 PyErr_SetString(PyExc_AttributeError, "Attribute 'Type' of object 'BaseClass' is read-only");
00274 return -1;
00275 }
00276
00277
00278
00279
00280 PyObject * BaseClassPy::staticCallback_getModule (PyObject *self, void * )
00281 {
00282 if (!((PyObjectBase*) self)->isValid()){
00283 PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!");
00284 return NULL;
00285 }
00286
00287 try {
00288 return Py::new_reference_to(((BaseClassPy*)self)->getModule());
00289 } catch (const Py::Exception&) {
00290
00291 return NULL;
00292 } catch (...) {
00293 PyErr_SetString(PyExc_Exception, "Unknown exception while reading attribute 'Module' of object 'BaseClass'");
00294 return NULL;
00295 }
00296 }
00297
00298 int BaseClassPy::staticCallback_setModule (PyObject *self, PyObject * , void * )
00299 {
00300 if (!((PyObjectBase*) self)->isValid()){
00301 PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!");
00302 return -1;
00303 }
00304
00305 PyErr_SetString(PyExc_AttributeError, "Attribute 'Module' of object 'BaseClass' is read-only");
00306 return -1;
00307 }
00308
00309
00310
00311
00312
00313
00314 PyParentObject BaseClassPy::Parents[] = { PARENTSBaseBaseClassPy };
00315
00316
00317
00318
00319 BaseClassPy::BaseClassPy(BaseClass *pcObject, PyTypeObject *T)
00320 : PyObjectBase(reinterpret_cast<PyObjectBase::PointerType>(pcObject), T)
00321 {
00322 }
00323
00324 PyObject *BaseClassPy::PyMake(struct _typeobject *, PyObject *, PyObject *)
00325 {
00326
00327 PyErr_SetString(PyExc_RuntimeError, "You cannot create directly an instance of 'BaseClassPy'.");
00328
00329 return 0;
00330 }
00331
00332 int BaseClassPy::PyInit(PyObject* , PyObject* )
00333 {
00334 return 0;
00335 }
00336
00337
00338
00339
00340 BaseClassPy::~BaseClassPy()
00341 {
00342 }
00343
00344
00345
00346
00347 PyObject *BaseClassPy::_repr(void)
00348 {
00349 return Py_BuildValue("s", representation().c_str());
00350 }
00351
00352
00353
00354
00355 PyObject *BaseClassPy::_getattr(char *attr)
00356 {
00357 try {
00358
00359 PyObject *r = getCustomAttributes(attr);
00360 if(r) return r;
00361 }
00362 #ifndef DONT_CATCH_CXX_EXCEPTIONS
00363 catch(const Base::Exception& e)
00364 {
00365 std::string str;
00366 str += "FreeCAD exception thrown (";
00367 str += e.what();
00368 str += ")";
00369 e.ReportException();
00370 PyErr_SetString(PyExc_Exception,str.c_str());
00371 return NULL;
00372 }
00373 catch(const std::exception& e)
00374 {
00375 std::string str;
00376 str += "FC++ exception thrown (";
00377 str += e.what();
00378 str += ")";
00379 Base::Console().Error(str.c_str());
00380 PyErr_SetString(PyExc_Exception,str.c_str());
00381 return NULL;
00382 }
00383 catch(const Py::Exception&)
00384 {
00385
00386 return NULL;
00387 }
00388 catch(...)
00389 {
00390 PyErr_SetString(PyExc_Exception,"Unknown C++ exception");
00391 return NULL;
00392 }
00393 #else // DONT_CATCH_CXX_EXCEPTIONS
00394 catch(const Base::Exception& e)
00395 {
00396 std::string str;
00397 str += "FreeCAD exception thrown (";
00398 str += e.what();
00399 str += ")";
00400 e.ReportException();
00401 PyErr_SetString(PyExc_Exception,str.c_str());
00402 return NULL;
00403 }
00404 catch(const Py::Exception&)
00405 {
00406
00407 return NULL;
00408 }
00409 #endif // DONT_CATCH_CXX_EXCEPTIONS
00410
00411 PyObject *rvalue = Py_FindMethod(Methods, this, attr);
00412 if (rvalue == NULL)
00413 {
00414 PyErr_Clear();
00415 return PyObjectBase::_getattr(attr);
00416 }
00417 else
00418 {
00419 return rvalue;
00420 }
00421 }
00422
00423 int BaseClassPy::_setattr(char *attr, PyObject *value)
00424 {
00425 try {
00426
00427 int r = setCustomAttributes(attr, value);
00428 if(r==1) return 0;
00429 }
00430 #ifndef DONT_CATCH_CXX_EXCEPTIONS
00431 catch(const Base::Exception& e)
00432 {
00433 std::string str;
00434 str += "FreeCAD exception thrown (";
00435 str += e.what();
00436 str += ")";
00437 e.ReportException();
00438 PyErr_SetString(PyExc_Exception,str.c_str());
00439 return -1;
00440 }
00441 catch(const std::exception& e)
00442 {
00443 std::string str;
00444 str += "FC++ exception thrown (";
00445 str += e.what();
00446 str += ")";
00447 Base::Console().Error(str.c_str());
00448 PyErr_SetString(PyExc_Exception,str.c_str());
00449 return -1;
00450 }
00451 catch(const Py::Exception&)
00452 {
00453
00454 return -1;
00455 }
00456 catch(...)
00457 {
00458 PyErr_SetString(PyExc_Exception,"Unknown C++ exception");
00459 return -1;
00460 }
00461 #else // DONT_CATCH_CXX_EXCEPTIONS
00462 catch(const Base::Exception& e)
00463 {
00464 std::string str;
00465 str += "FreeCAD exception thrown (";
00466 str += e.what();
00467 str += ")";
00468 e.ReportException();
00469 PyErr_SetString(PyExc_Exception,str.c_str());
00470 return -1;
00471 }
00472 catch(const Py::Exception&)
00473 {
00474
00475 return -1;
00476 }
00477 #endif // DONT_CATCH_CXX_EXCEPTIONS
00478
00479 return PyObjectBase::_setattr(attr, value);
00480 }
00481
00482 BaseClass *BaseClassPy::getBaseClassPtr(void) const
00483 {
00484 return static_cast<BaseClass *>(_pcTwinPointer);
00485 }
00486
00487 #if 0
00488
00489
00490
00491
00492
00493
00494 std::string BaseClassPy::representation(void) const
00495 {
00496 return std::string("<BaseClass object>");
00497 }
00498
00499 PyObject* BaseClassPy::isDerivedFrom(PyObject *args)
00500 {
00501 PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented");
00502 return 0;
00503 }
00504
00505 PyObject* BaseClassPy::getAllDerivedFrom(PyObject *args)
00506 {
00507 PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented");
00508 return 0;
00509 }
00510
00511
00512
00513 Py::String BaseClassPy::getType(void) const
00514 {
00515
00516 throw Py::AttributeError("Not yet implemented");
00517 }
00518
00519 Py::Int BaseClassPy::getModule(void) const
00520 {
00521
00522 throw Py::AttributeError("Not yet implemented");
00523 }
00524
00525 PyObject *BaseClassPy::getCustomAttributes(const char* attr) const
00526 {
00527 return 0;
00528 }
00529
00530 int BaseClassPy::setCustomAttributes(const char* attr, PyObject *obj)
00531 {
00532 return 0;
00533 }
00534 #endif
00535
00536
00537