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

Method fromString

src/App/PropertyPythonObject.cpp:286–332  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

284}
285
286void PropertyPythonObject::fromString(const std::string& repr)
287{
288 Base::PyGILStateLocker lock;
289 try {
290 if (repr.empty()) {
291 return;
292 }
293 Py::Module pickle(PyImport_ImportModule("json"), true);
294 if (pickle.isNull()) {
295 throw Py::Exception();
296 }
297 Py::Callable method(pickle.getAttr(std::string("loads")));
298 Py::Tuple args(1);
299 args.setItem(0, Py::String(repr));
300 Py::Object res = method.apply(args);
301
302 if (this->object.hasAttr("loads")) {
303 Py::Tuple args(1);
304 args.setItem(0, res);
305 Py::Callable state(this->object.getAttr("loads"));
306 state.apply(args);
307 }
308 // support add-ons that use the old method names
309 else if (this->object.hasAttr("__setstate__")
310#if PY_VERSION_HEX >= 0x030b0000
311 && this->object.getAttr("__setstate__").hasAttr("__func__")
312#endif
313 ) {
314 Py::Tuple args(1);
315 args.setItem(0, res);
316 Py::Callable state(this->object.getAttr("__setstate__"));
317 state.apply(args);
318 }
319 else if (this->object.hasAttr("__dict__")) {
320 if (!res.isNone()) {
321 this->object.setAttr("__dict__", res);
322 }
323 }
324 else {
325 this->object = res;
326 }
327 }
328 catch (Py::Exception&) {
329 Base::PyException e; // extract the Python error text
330 e.reportException();
331 }
332}
333
334
335std::string PropertyPythonObject::encodeValue(const std::string& str) const

Callers 2

RestoreMethod · 0.95
RestoreDocFileMethod · 0.95

Calls 7

isNoneMethod · 0.80
ExceptionClass · 0.70
StringClass · 0.70
emptyMethod · 0.45
isNullMethod · 0.45
applyMethod · 0.45
reportExceptionMethod · 0.45

Tested by

no test coverage detected