forked from boostorg/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.cpp
More file actions
78 lines (64 loc) · 2.08 KB
/
module.cpp
File metadata and controls
78 lines (64 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// (C) Copyright David Abrahams 2000. Permission to copy, use, modify, sell and
// distribute this software is granted provided this copyright notice appears
// in all copies. This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
//
// The author gratefully acknowleges the support of Dragon Systems, Inc., in
// producing this work.
#include <boost/python/module.hpp>
#include <boost/python/errors.hpp>
namespace boost { namespace python {
namespace {
ref name_holder;
}
bool module_base::initializing()
{
return name_holder.get() != 0;
}
string module_base::name()
{
// If this fails, you haven't created a module object
assert(initializing());
return string(name_holder);
}
module_base::module_base(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 = ref(PyObject_GetAttrString(m_module, const_cast<char*>("__name__")));
}
module_base::~module_base()
{
name_holder.reset();
}
void module_base::add(PyObject* x, const char* name)
{
add(ref(x), name);
}
void module_base::add(ref x, const char* name)
{
ref f(x); // First take possession of the object.
if (PyObject_SetAttrString(m_module, const_cast<char*>(name), x.get()) < 0)
throw error_already_set();
}
void module_base::add(PyTypeObject* x, const char* name /*= 0*/)
{
this->add((PyObject*)x, name ? name : x->tp_name);
}
void module_base::add_overload(objects::function* x, const char* name)
{
PyObject* existing = PyObject_HasAttrString(m_module, const_cast<char*>(name))
? PyObject_GetAttrString(m_module, const_cast<char*>(name))
: 0;
if (existing != 0 && existing->ob_type == &objects::function_type)
{
static_cast<objects::function*>(existing)->add_overload(x);
}
else
{
add(x, name);
}
}
PyMethodDef module_base::initial_methods[] = { { 0, 0, 0, 0 } };
}} // namespace boost::python