constructor method
| 60 | |
| 61 | // constructor method |
| 62 | int MetadataPy::PyInit(PyObject* args, PyObject* /*kwd*/) |
| 63 | { |
| 64 | if (PyArg_ParseTuple(args, "")) { |
| 65 | setTwinPointer(new Metadata()); |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | // Data may be passed directly in as a bytes-like object buffer |
| 70 | PyErr_Clear(); |
| 71 | Py_buffer dataBuffer; |
| 72 | if (PyArg_ParseTuple(args, "y*", &dataBuffer)) { |
| 73 | try { |
| 74 | // NB: This is making a copy of the buffer for simplicity, but that shouldn't be |
| 75 | // necessary. Use either a string_view or a span to avoid the copy in the future. |
| 76 | auto md = |
| 77 | new Metadata(std::string(static_cast<const char*>(dataBuffer.buf), dataBuffer.len)); |
| 78 | setTwinPointer(md); |
| 79 | return 0; |
| 80 | } |
| 81 | catch (const Base::XMLBaseException&) { |
| 82 | // If the XML read failed, this might have been a path to a file, rather than a |
| 83 | // bytes-like object. Fall through to the next case. |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // Main class constructor -- takes a file path, loads the metadata from it |
| 88 | PyErr_Clear(); |
| 89 | char* filename; |
| 90 | if (PyArg_ParseTuple(args, "et", "utf-8", &filename)) { |
| 91 | try { |
| 92 | std::string utf8Name = std::string(filename); |
| 93 | PyMem_Free(filename); |
| 94 | |
| 95 | auto md = new Metadata(Base::FileInfo::stringToPath(utf8Name)); |
| 96 | setTwinPointer(md); |
| 97 | return 0; |
| 98 | } |
| 99 | catch (const Base::XMLBaseException& e) { |
| 100 | e.setPyException(); |
| 101 | return -1; |
| 102 | } |
| 103 | catch (const XMLException& toCatch) { |
| 104 | char* message = XMLString::transcode(toCatch.getMessage()); |
| 105 | std::string what = message; |
| 106 | XMLString::release(&message); |
| 107 | PyErr_SetString(Base::PyExc_FC_XMLBaseException, what.c_str()); |
| 108 | return -1; |
| 109 | } |
| 110 | catch (const DOMException& toCatch) { |
| 111 | char* message = XMLString::transcode(toCatch.getMessage()); |
| 112 | std::string what = message; |
| 113 | XMLString::release(&message); |
| 114 | PyErr_SetString(Base::PyExc_FC_XMLBaseException, what.c_str()); |
| 115 | return -1; |
| 116 | } |
| 117 | catch (...) { |
| 118 | PyErr_SetString(Base::PyExc_FC_GeneralError, "Failed to create Metadata object"); |
| 119 | return -1; |
nothing calls this directly
no test coverage detected