2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-21 17:12:22 +00:00

Added module name retrieval for pickling support

[SVN r8246]
This commit is contained in:
Dave Abrahams
2000-11-17 20:22:30 +00:00
parent 12cdb1d2c7
commit a3fa57d58f
2 changed files with 23 additions and 6 deletions

View File

@@ -10,9 +10,23 @@
namespace py {
namespace {
Ptr name_holder;
}
String Module::name()
{
// If this fails, you haven't created a Module object
assert(name_holder.get() != 0);
return String(name_holder);
}
Module::Module(const char* name)
: m_module(Py_InitModule(const_cast<char*>(name), initial_methods))
{
// If this fails, you've created more than 1 Module object in your module
assert(name_holder.get() == 0);
name_holder = Ptr(PyObject_GetAttrString(m_module, "__name__"));
}
void
@@ -24,14 +38,13 @@ Module::add(Function* x, const char* name)
void Module::add(Ptr x, const char* name)
{
PyObject* dictionary = PyModule_GetDict( m_module );
PyObject* dictionary = PyModule_GetDict(m_module);
PyDict_SetItemString(dictionary, const_cast<char*>(name), x.get());
}
void Module::add(PyTypeObject* x, const char* name /*= 0*/)
{
this->add(Ptr(as_object(x)),
name ? name : x->tp_name);
this->add(Ptr(as_object(x)), name ? name : x->tp_name);
}
PyMethodDef Module::initial_methods[] = { { 0, 0, 0, 0 } };

View File

@@ -11,6 +11,7 @@
# include "pyconfig.h"
# include "pyptr.h"
# include "objects.h"
# include "functions.h"
namespace py {
@@ -19,13 +20,13 @@ class Module
{
typedef PyObject * (*RawFunctionPtr)(py::Tuple const &, py::Dict const &);
public:
public:
// Create a module. REQUIRES: only one Module is created per module.
Module(const char* name);
// Add elements to the module
void add(Function* x, const char* name);
void add(PyTypeObject* x, const char* name = 0);
void add(Ptr x, const char*name);
template <class Fn>
@@ -39,6 +40,9 @@ public:
{
add(new_wrapped_function(fn), name);
}
static String name();
private:
PyObject* m_module;
static PyMethodDef initial_methods[1];