|
| 1 | +/** |
| 2 | + * PANDA 3D SOFTWARE |
| 3 | + * Copyright (c) Carnegie Mellon University. All rights reserved. |
| 4 | + * |
| 5 | + * All use of this software is subject to the terms of the revised BSD |
| 6 | + * license. You should have received a copy of this license along |
| 7 | + * with this source code in a file named "LICENSE." |
| 8 | + * |
| 9 | + * @file loaderFileTypeRegistry_ext.cxx |
| 10 | + * @author rdb |
| 11 | + * @date 2019-07-30 |
| 12 | + */ |
| 13 | + |
| 14 | +#include "loaderFileTypeRegistry_ext.h" |
| 15 | + |
| 16 | +#ifdef HAVE_PYTHON |
| 17 | + |
| 18 | +#include "pythonLoaderFileType.h" |
| 19 | + |
| 20 | +/** |
| 21 | + * Registers a loader file type that is implemented in Python. |
| 22 | + */ |
| 23 | +void Extension<LoaderFileTypeRegistry>:: |
| 24 | +register_type(PyObject *type) { |
| 25 | + PythonLoaderFileType *loader = new PythonLoaderFileType(); |
| 26 | + |
| 27 | + if (loader->init(type)) { |
| 28 | + _this->register_type(loader); |
| 29 | + } else { |
| 30 | + delete loader; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Registers a loader file type from a pkg_resources.EntryPoint object, which |
| 36 | + * will be loaded when a file with the extension is encountered. |
| 37 | + */ |
| 38 | +void Extension<LoaderFileTypeRegistry>:: |
| 39 | +register_deferred_type(PyObject *entry_point) { |
| 40 | + // The "name" attribute holds the extension. |
| 41 | + PyObject *name = PyObject_GetAttrString(entry_point, "name"); |
| 42 | + if (name == nullptr) { |
| 43 | + Dtool_Raise_TypeError("entry_point argument is missing name attribute"); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + const char *name_str; |
| 48 | + Py_ssize_t name_len; |
| 49 | +#if PY_MAJOR_VERSION >= 3 |
| 50 | + name_str = PyUnicode_AsUTF8AndSize(name, &name_len); |
| 51 | +#else |
| 52 | + if (PyString_AsStringAndSize(name, (char **)&name_str, &name_len) == -1) { |
| 53 | + name_str = nullptr; |
| 54 | + } |
| 55 | +#endif |
| 56 | + Py_DECREF(name); |
| 57 | + |
| 58 | + if (name_str == nullptr) { |
| 59 | + Dtool_Raise_TypeError("entry_point.name is expected to be str"); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + PythonLoaderFileType *loader = new PythonLoaderFileType(std::string(name_str, name_len), entry_point); |
| 64 | + _this->register_type(loader); |
| 65 | +} |
| 66 | + |
| 67 | +#endif |
0 commit comments