Skip to content

Commit 49b72fb

Browse files
committed
Move Python support code from libp3interrogatedb to generated module
This prevents libp3interrogatedb from having a dependency on the Python library. See panda3d#387
1 parent e6f870e commit 49b72fb

16 files changed

+977
-972
lines changed

dtool/src/dtoolbase/typeHandle_ext.cxx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
*/
2323
TypeHandle Extension<TypeHandle>::
2424
make(PyTypeObject *tp) {
25-
if (!PyType_IsSubtype(tp, &Dtool_DTOOL_SUPER_BASE._PyType)) {
25+
Dtool_PyTypedObject *super_base = Dtool_GetSuperBase();
26+
if (!PyType_IsSubtype(tp, (PyTypeObject *)super_base)) {
2627
PyErr_SetString(PyExc_TypeError, "a Panda type is required");
2728
return TypeHandle::none();
2829
}

dtool/src/interrogate/interfaceMakerPythonNative.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3097,7 +3097,7 @@ write_module_class(ostream &out, Object *obj) {
30973097

30983098
out << " Dtool_" << ClassName << "._PyType.tp_bases = PyTuple_Pack(" << bases.size() << baseargs << ");\n";
30993099
} else {
3100-
out << " Dtool_" << ClassName << "._PyType.tp_base = (PyTypeObject *)&Dtool_DTOOL_SUPER_BASE;\n";
3100+
out << " Dtool_" << ClassName << "._PyType.tp_base = (PyTypeObject *)Dtool_GetSuperBase();\n";
31013101
}
31023102

31033103
int num_nested = obj->_itype.number_of_nested_types();

dtool/src/interrogate/interrogate_module.cxx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
using std::cerr;
3131
using std::string;
3232

33+
// This contains a big source string determined at compile time.
34+
extern const char interrogate_preamble_python_native[];
35+
3336
Filename output_code_filename;
3437
string module_name;
3538
string library_name;
@@ -635,8 +638,10 @@ int main(int argc, char *argv[]) {
635638

636639
if (build_python_native_wrappers) {
637640
write_python_table_native(output_code);
638-
}
639641

642+
// Output the support code.
643+
output_code << interrogate_preamble_python_native << "\n";
644+
}
640645
}
641646
}
642647

dtool/src/interrogatedb/dtool_super_base.cxx

Lines changed: 103 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -15,120 +15,132 @@
1515

1616
#ifdef HAVE_PYTHON
1717

18-
class EmptyClass {
19-
};
20-
Define_Module_Class_Private(dtoolconfig, DTOOL_SUPER_BASE, EmptyClass, DTOOL_SUPER_BASE111);
21-
2218
static PyObject *GetSuperBase(PyObject *self) {
23-
Py_INCREF((PyTypeObject *)&Dtool_DTOOL_SUPER_BASE); // order is important .. this is used for static functions
24-
return (PyObject *) &Dtool_DTOOL_SUPER_BASE;
25-
};
26-
27-
PyMethodDef Dtool_Methods_DTOOL_SUPER_BASE[] = {
28-
{ "DtoolGetSuperBase", (PyCFunction) &GetSuperBase, METH_NOARGS, "Will Return SUPERbase Class"},
29-
{ nullptr, nullptr, 0, nullptr }
19+
Dtool_PyTypedObject *super_base = Dtool_GetSuperBase();
20+
Py_XINCREF((PyTypeObject *)super_base); // order is important .. this is used for static functions
21+
return (PyObject *)super_base;
3022
};
3123

32-
EXPCL_INTERROGATEDB void Dtool_PyModuleClassInit_DTOOL_SUPER_BASE(PyObject *module) {
33-
static bool initdone = false;
34-
if (!initdone) {
35-
36-
initdone = true;
37-
Dtool_DTOOL_SUPER_BASE._PyType.tp_dict = PyDict_New();
38-
PyDict_SetItemString(Dtool_DTOOL_SUPER_BASE._PyType.tp_dict, "DtoolClassDict", Dtool_DTOOL_SUPER_BASE._PyType.tp_dict);
39-
40-
if (PyType_Ready((PyTypeObject *)&Dtool_DTOOL_SUPER_BASE) < 0) {
41-
PyErr_SetString(PyExc_TypeError, "PyType_Ready(Dtool_DTOOL_SUPER_BASE)");
42-
return;
43-
}
44-
Py_INCREF((PyTypeObject *)&Dtool_DTOOL_SUPER_BASE);
45-
46-
PyDict_SetItemString(Dtool_DTOOL_SUPER_BASE._PyType.tp_dict, "DtoolGetSuperBase", PyCFunction_New(&Dtool_Methods_DTOOL_SUPER_BASE[0], (PyObject *)&Dtool_DTOOL_SUPER_BASE));
47-
}
48-
24+
static void Dtool_PyModuleClassInit_DTOOL_SUPER_BASE(PyObject *module) {
4925
if (module != nullptr) {
50-
Py_INCREF((PyTypeObject *)&Dtool_DTOOL_SUPER_BASE);
51-
PyModule_AddObject(module, "DTOOL_SUPER_BASE", (PyObject *)&Dtool_DTOOL_SUPER_BASE);
26+
Dtool_PyTypedObject *super_base = Dtool_GetSuperBase();
27+
Py_INCREF((PyTypeObject *)&super_base);
28+
PyModule_AddObject(module, "DTOOL_SUPER_BASE", (PyObject *)&super_base);
5229
}
5330
}
5431

55-
inline void *Dtool_DowncastInterface_DTOOL_SUPER_BASE(void *from_this, Dtool_PyTypedObject *from_type) {
32+
static void *Dtool_DowncastInterface_DTOOL_SUPER_BASE(void *from_this, Dtool_PyTypedObject *from_type) {
5633
return nullptr;
5734
}
5835

59-
inline void *Dtool_UpcastInterface_DTOOL_SUPER_BASE(PyObject *self, Dtool_PyTypedObject *requested_type) {
36+
static void *Dtool_UpcastInterface_DTOOL_SUPER_BASE(PyObject *self, Dtool_PyTypedObject *requested_type) {
6037
return nullptr;
6138
}
6239

63-
int Dtool_Init_DTOOL_SUPER_BASE(PyObject *self, PyObject *args, PyObject *kwds) {
40+
static int Dtool_Init_DTOOL_SUPER_BASE(PyObject *self, PyObject *args, PyObject *kwds) {
6441
assert(self != nullptr);
6542
PyErr_Format(PyExc_TypeError, "cannot init constant class %s", Py_TYPE(self)->tp_name);
6643
return -1;
6744
}
6845

69-
EXPORT_THIS Dtool_PyTypedObject Dtool_DTOOL_SUPER_BASE = {
70-
{
71-
PyVarObject_HEAD_INIT(nullptr, 0)
72-
"dtoolconfig.DTOOL_SUPER_BASE",
73-
sizeof(Dtool_PyInstDef),
74-
0, // tp_itemsize
75-
&Dtool_FreeInstance_DTOOL_SUPER_BASE,
76-
nullptr, // tp_print
77-
nullptr, // tp_getattr
78-
nullptr, // tp_setattr
46+
static void Dtool_FreeInstance_DTOOL_SUPER_BASE(PyObject *self) {
47+
Py_TYPE(self)->tp_free(self);
48+
}
49+
50+
/**
51+
* Returns a pointer to the DTOOL_SUPER_BASE class that is the base class of
52+
* all Panda types. This pointer is shared by all modules.
53+
*/
54+
Dtool_PyTypedObject *Dtool_GetSuperBase() {
55+
Dtool_TypeMap *type_map = Dtool_GetGlobalTypeMap();
56+
auto it = type_map->find("DTOOL_SUPER_BASE");
57+
if (it != type_map->end()) {
58+
return it->second;
59+
}
60+
61+
static PyMethodDef methods[] = {
62+
{ "DtoolGetSuperBase", (PyCFunction)&GetSuperBase, METH_NOARGS, "Will Return SUPERbase Class"},
63+
{ nullptr, nullptr, 0, nullptr }
64+
};
65+
66+
static Dtool_PyTypedObject super_base_type = {
67+
{
68+
PyVarObject_HEAD_INIT(nullptr, 0)
69+
"dtoolconfig.DTOOL_SUPER_BASE",
70+
sizeof(Dtool_PyInstDef),
71+
0, // tp_itemsize
72+
&Dtool_FreeInstance_DTOOL_SUPER_BASE,
73+
nullptr, // tp_print
74+
nullptr, // tp_getattr
75+
nullptr, // tp_setattr
7976
#if PY_MAJOR_VERSION >= 3
80-
nullptr, // tp_compare
77+
nullptr, // tp_compare
8178
#else
82-
&DtoolInstance_ComparePointers,
79+
&DtoolInstance_ComparePointers,
8380
#endif
84-
nullptr, // tp_repr
85-
nullptr, // tp_as_number
86-
nullptr, // tp_as_sequence
87-
nullptr, // tp_as_mapping
88-
&DtoolInstance_HashPointer,
89-
nullptr, // tp_call
90-
nullptr, // tp_str
91-
PyObject_GenericGetAttr,
92-
PyObject_GenericSetAttr,
93-
nullptr, // tp_as_buffer
94-
(Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_CHECKTYPES),
95-
nullptr, // tp_doc
96-
nullptr, // tp_traverse
97-
nullptr, // tp_clear
81+
nullptr, // tp_repr
82+
nullptr, // tp_as_number
83+
nullptr, // tp_as_sequence
84+
nullptr, // tp_as_mapping
85+
&DtoolInstance_HashPointer,
86+
nullptr, // tp_call
87+
nullptr, // tp_str
88+
PyObject_GenericGetAttr,
89+
PyObject_GenericSetAttr,
90+
nullptr, // tp_as_buffer
91+
(Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_CHECKTYPES),
92+
nullptr, // tp_doc
93+
nullptr, // tp_traverse
94+
nullptr, // tp_clear
9895
#if PY_MAJOR_VERSION >= 3
99-
&DtoolInstance_RichComparePointers,
96+
&DtoolInstance_RichComparePointers,
10097
#else
101-
nullptr, // tp_richcompare
98+
nullptr, // tp_richcompare
10299
#endif
103-
0, // tp_weaklistoffset
104-
nullptr, // tp_iter
105-
nullptr, // tp_iternext
106-
Dtool_Methods_DTOOL_SUPER_BASE,
107-
standard_type_members,
108-
nullptr, // tp_getset
109-
nullptr, // tp_base
110-
nullptr, // tp_dict
111-
nullptr, // tp_descr_get
112-
nullptr, // tp_descr_set
113-
0, // tp_dictoffset
114-
Dtool_Init_DTOOL_SUPER_BASE,
115-
PyType_GenericAlloc,
116-
Dtool_new_DTOOL_SUPER_BASE,
117-
PyObject_Del,
118-
nullptr, // tp_is_gc
119-
nullptr, // tp_bases
120-
nullptr, // tp_mro
121-
nullptr, // tp_cache
122-
nullptr, // tp_subclasses
123-
nullptr, // tp_weaklist
124-
nullptr, // tp_del
125-
},
126-
TypeHandle::none(),
127-
Dtool_PyModuleClassInit_DTOOL_SUPER_BASE,
128-
Dtool_UpcastInterface_DTOOL_SUPER_BASE,
129-
Dtool_DowncastInterface_DTOOL_SUPER_BASE,
130-
nullptr,
131-
nullptr,
132-
};
100+
0, // tp_weaklistoffset
101+
nullptr, // tp_iter
102+
nullptr, // tp_iternext
103+
methods,
104+
standard_type_members,
105+
nullptr, // tp_getset
106+
nullptr, // tp_base
107+
nullptr, // tp_dict
108+
nullptr, // tp_descr_get
109+
nullptr, // tp_descr_set
110+
0, // tp_dictoffset
111+
Dtool_Init_DTOOL_SUPER_BASE,
112+
PyType_GenericAlloc,
113+
nullptr, // tp_new
114+
PyObject_Del,
115+
nullptr, // tp_is_gc
116+
nullptr, // tp_bases
117+
nullptr, // tp_mro
118+
nullptr, // tp_cache
119+
nullptr, // tp_subclasses
120+
nullptr, // tp_weaklist
121+
nullptr, // tp_del
122+
},
123+
TypeHandle::none(),
124+
Dtool_PyModuleClassInit_DTOOL_SUPER_BASE,
125+
Dtool_UpcastInterface_DTOOL_SUPER_BASE,
126+
Dtool_DowncastInterface_DTOOL_SUPER_BASE,
127+
nullptr,
128+
nullptr,
129+
};
130+
131+
super_base_type._PyType.tp_dict = PyDict_New();
132+
PyDict_SetItemString(super_base_type._PyType.tp_dict, "DtoolClassDict", super_base_type._PyType.tp_dict);
133+
134+
if (PyType_Ready((PyTypeObject *)&super_base_type) < 0) {
135+
PyErr_SetString(PyExc_TypeError, "PyType_Ready(Dtool_DTOOL_SUPER_BASE)");
136+
return nullptr;
137+
}
138+
Py_INCREF((PyTypeObject *)&super_base_type);
139+
140+
PyDict_SetItemString(super_base_type._PyType.tp_dict, "DtoolGetSuperBase", PyCFunction_New(&methods[0], (PyObject *)&super_base_type));
141+
142+
(*type_map)["DTOOL_SUPER_BASE"] = &super_base_type;
143+
return &super_base_type;
144+
}
133145

134146
#endif // HAVE_PYTHON

dtool/src/interrogatedb/p3interrogatedb_composite1.cxx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "config_interrogatedb.cxx"
2-
#include "dtool_super_base.cxx"
32
#include "indexRemapper.cxx"
43
#include "interrogateComponent.cxx"
54
#include "interrogateDatabase.cxx"

dtool/src/interrogatedb/p3interrogatedb_composite2.cxx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,3 @@
44
#include "interrogate_datafile.cxx"
55
#include "interrogate_interface.cxx"
66
#include "interrogate_request.cxx"
7-
#include "py_panda.cxx"
8-
#include "py_compat.cxx"
9-
#include "py_wrappers.cxx"

dtool/src/interrogatedb/py_compat.cxx

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
/**
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-
*
92
* @file py_compat.cxx
103
* @author rdb
114
* @date 2017-12-03

dtool/src/interrogatedb/py_compat.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
/**
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-
*
92
* @file py_compat.h
103
* @author rdb
114
* @date 2017-12-02
@@ -106,7 +99,7 @@ typedef int Py_ssize_t;
10699
// PyInt_FromSize_t automatically picks the right type.
107100
# define PyLongOrInt_AS_LONG PyInt_AsLong
108101

109-
EXPCL_INTERROGATEDB size_t PyLongOrInt_AsSize_t(PyObject *);
102+
size_t PyLongOrInt_AsSize_t(PyObject *);
110103
#endif
111104

112105
// Which character to use in PyArg_ParseTuple et al for a byte string.

dtool/src/interrogatedb/py_panda.I

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
/**
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-
*
92
* @file py_panda.I
103
* @author rdb
114
* @date 2016-06-06
@@ -142,6 +135,18 @@ DTool_CreatePyInstanceTyped(T *obj, bool memory_rules) {
142135
return DTool_CreatePyInstanceTyped((void*) obj, *known_class, memory_rules, false, obj->get_type().get_index());
143136
}
144137

138+
/**
139+
* Finishes initializing the Dtool_PyInstDef.
140+
*/
141+
INLINE int
142+
DTool_PyInit_Finalize(PyObject *self, void *local_this, Dtool_PyTypedObject *type, bool memory_rules, bool is_const) {
143+
((Dtool_PyInstDef *)self)->_My_Type = type;
144+
((Dtool_PyInstDef *)self)->_ptr_to_object = local_this;
145+
((Dtool_PyInstDef *)self)->_memory_rules = memory_rules;
146+
((Dtool_PyInstDef *)self)->_is_const = is_const;
147+
return 0;
148+
}
149+
145150
/**
146151
* Checks that the tuple is empty.
147152
*/

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy