prc: Add pickle support to ConfigVariable

The current value is not pickled.  I might change my mind on this, but my thinking is that ConfigVariable doesn't really contain a value, it's just an accessor for a value from the config page, so the current state of the variables should be pickled with the config pages.
This commit is contained in:
rdb 2021-12-19 14:36:17 +01:00
parent 5eb0f04e87
commit a0c2f2ff3b
5 changed files with 81 additions and 0 deletions

View File

@ -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

View File

@ -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;

View File

@ -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<ConfigVariable>::
__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

View File

@ -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<ConfigVariable> : public ExtensionBase<ConfigVariable> {
public:
PyObject *__reduce__(PyObject *self) const;
};
#endif // HAVE_PYTHON
#endif // CONFIGVARIABLE_EXT_H

View File

@ -1,2 +1,3 @@
#include "configVariable_ext.cxx"
#include "streamReader_ext.cxx"
#include "streamWriter_ext.cxx"