diff --git a/dtool/src/prc/CMakeLists.txt b/dtool/src/prc/CMakeLists.txt index f2985662bd..2a97c4665a 100644 --- a/dtool/src/prc/CMakeLists.txt +++ b/dtool/src/prc/CMakeLists.txt @@ -74,6 +74,8 @@ if(HAVE_OPENSSL) endif() set(P3PRC_IGATEEXT + configVariable_ext.cxx + configVariable_ext.h streamReader_ext.cxx streamReader_ext.h streamWriter_ext.cxx diff --git a/dtool/src/prc/configVariable.h b/dtool/src/prc/configVariable.h index c07f12e453..5c242907d1 100644 --- a/dtool/src/prc/configVariable.h +++ b/dtool/src/prc/configVariable.h @@ -44,6 +44,8 @@ PUBLISHED: INLINE size_t get_num_words() const; + EXTENSION(PyObject *__reduce__(PyObject *self) const); + protected: INLINE const ConfigDeclaration *get_default_value() const; diff --git a/dtool/src/prc/configVariable_ext.cxx b/dtool/src/prc/configVariable_ext.cxx new file mode 100644 index 0000000000..d2d3dc8f47 --- /dev/null +++ b/dtool/src/prc/configVariable_ext.cxx @@ -0,0 +1,39 @@ +/** + * PANDA 3D SOFTWAREitiueuiitgyrsrtfu + * Copyright (c) Carnegie Mellon University. All rights reserved. + * + * All use of this software is subject to the terms of the revised BSD + * license. You should have received a copy of this license along + * with this source code in a file named "LICENSE." + * + * @file configVariable_ext.cxx + * @author rdb + * @date 2021-01-01 + */ + +#include "configVariable_ext.h" + +#ifdef HAVE_PYTHON + +/** + * Implements pickle support. + */ +PyObject *Extension:: +__reduce__(PyObject *self) const { + const std::string &name = _this->get_name(); + const std::string &descr = _this->get_description(); + int flags = _this->get_flags(); + + // If the subclass defines a get_default_value method, we assume it takes a + // default value in the constructor. + PyObject *get_default_value = PyObject_GetAttrString((PyObject *)Py_TYPE(self), "get_default_value"); + if (get_default_value != nullptr) { + PyObject *default_value = PyObject_CallOneArg(get_default_value, self); + return Py_BuildValue("O(s#Ns#i)", Py_TYPE(self), name.data(), (Py_ssize_t)name.length(), default_value, descr.data(), (Py_ssize_t)descr.length(), flags); + } + else { + return Py_BuildValue("O(s#s#i)", Py_TYPE(self), name.data(), (Py_ssize_t)name.length(), descr.data(), (Py_ssize_t)descr.length(), flags); + } +} + +#endif diff --git a/dtool/src/prc/configVariable_ext.h b/dtool/src/prc/configVariable_ext.h new file mode 100644 index 0000000000..35e6f2f192 --- /dev/null +++ b/dtool/src/prc/configVariable_ext.h @@ -0,0 +1,37 @@ +/** + * PANDA 3D SOFTWARE + * Copyright (c) Carnegie Mellon University. All rights reserved. + * + * All use of this software is subject to the terms of the revised BSD + * license. You should have received a copy of this license along + * with this source code in a file named "LICENSE." + * + * @file configVariable_ext.h + * @author rdb + * @date 2021-12-10 + */ + +#ifndef CONFIGVARIABLE_EXT_H +#define CONFIGVARIABLE_EXT_H + +#include "dtoolbase.h" + +#ifdef HAVE_PYTHON + +#include "extension.h" +#include "configVariable.h" +#include "py_panda.h" + +/** + * This class defines the extension methods for ConfigVariable, which are called + * instead of any C++ methods with the same prototype. + */ +template<> +class Extension : public ExtensionBase { +public: + PyObject *__reduce__(PyObject *self) const; +}; + +#endif // HAVE_PYTHON + +#endif // CONFIGVARIABLE_EXT_H diff --git a/dtool/src/prc/p3prc_ext_composite.cxx b/dtool/src/prc/p3prc_ext_composite.cxx index 223ef504cd..22a59486b1 100644 --- a/dtool/src/prc/p3prc_ext_composite.cxx +++ b/dtool/src/prc/p3prc_ext_composite.cxx @@ -1,2 +1,3 @@ +#include "configVariable_ext.cxx" #include "streamReader_ext.cxx" #include "streamWriter_ext.cxx"