MCPcopy Create free account
hub / github.com/FreeCAD/FreeCAD / Restore

Method Restore

src/App/PropertyPythonObject.cpp:461–533  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

459}
460
461void PropertyPythonObject::Restore(Base::XMLReader& reader)
462{
463 reader.readElement("Python");
464 if (reader.hasAttribute("file")) {
465 std::string file(reader.getAttribute<const char*>("file"));
466 reader.addFile(file.c_str(), this);
467 }
468 else {
469 bool load_json = false;
470 bool load_failed = false;
471 std::string buffer = reader.getAttribute<const char*>("value");
472 if (reader.hasAttribute("encoded") && strcmp(reader.getAttribute<const char*>("encoded"), "yes") == 0) {
473 buffer = Base::base64_decode(buffer);
474 }
475 else {
476 buffer = decodeValue(buffer);
477 }
478
479 Base::PyGILStateLocker lock;
480 try {
481 if (reader.hasAttribute("module") && reader.hasAttribute("class")) {
482 std::string moduleName = reader.getAttribute<const char*>("module");
483 if (!isAllowedModule(moduleName)) {
484 Base::Console().warning(
485 "PropertyPythonObject::Restore: blocked import of module '%s' during"
486 " document restore. Only modules from FreeCAD or installed addons"
487 " are permitted.\n",
488 moduleName.c_str());
489 throw Py::ImportError("module not permitted: " + moduleName);
490 }
491 Py::Module mod(PyImport_ImportModule(moduleName.c_str()), true);
492 if (mod.isNull()) {
493 throw Py::Exception();
494 }
495 std::string className = reader.getAttribute<const char*>("class");
496 PyObject* cls = mod.getAttr(className).ptr();
497 if (!cls) {
498 std::stringstream s;
499 s << "Module " << moduleName << " has no class " << className;
500 throw Py::AttributeError(s.str());
501 }
502 if (PyType_Check(cls)) {
503 this->object = PyType_GenericAlloc((PyTypeObject*)cls, 0);
504 }
505 else {
506 throw Py::TypeError("neither class nor type object");
507 }
508 load_json = true;
509 }
510 else if (reader.hasAttribute("json")) {
511 load_json = true;
512 }
513 }
514 catch (Py::Exception&) {
515 Base::PyException e; // extract the Python error text
516 e.reportException();
517 this->object = Py::None();
518 load_failed = true;

Callers

nothing calls this directly

Calls 14

fromStringMethod · 0.95
base64_decodeFunction · 0.85
isAllowedModuleFunction · 0.85
ImportErrorFunction · 0.85
readElementMethod · 0.80
hasAttributeMethod · 0.80
ExceptionClass · 0.70
addFileMethod · 0.45
c_strMethod · 0.45
warningMethod · 0.45
isNullMethod · 0.45
ptrMethod · 0.45

Tested by

no test coverage detected