30#if PY_VERSION_HEX >= 0x03030000
31 typedef struct PyDictKeyEntry {
38 typedef struct _dictkeysobject {
43#if PY_VERSION_HEX >= 0x03060000
54 PyDictKeyEntry dk_entries[1];
58#define CPYCPPYY_GET_DICT_LOOKUP(mp) \
59 ((dict_lookup_func&)mp->ma_keys->dk_lookup)
63#define CPYCPPYY_GET_DICT_LOOKUP(mp) \
64 ((dict_lookup_func&)mp->ma_lookup)
76 Py_FatalError(
"deallocating nullptr");
86#if PY_VERSION_HEX < 0x03000000
92#
if PY_VERSION_HEX < 0x03000000
96#
if PY_VERSION_HEX < 0x03000000
100#
if PY_VERSION_HEX < 0x03000000
104#
if PY_VERSION_HEX >= 0x02020000
106#
if PY_VERSION_HEX < 0x03000000
113#
if PY_VERSION_HEX >= 0x02050000
116#
if PY_VERSION_HEX >= 0x03050000
132 (hashfunc)_Py_HashPointer,
133 0, 0, 0, 0, 0, Py_TPFLAGS_DEFAULT, 0, 0, 0, 0, 0, 0, 0,
134 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
135#if PY_VERSION_HEX >= 0x02030000
138#if PY_VERSION_HEX >= 0x02060000
141#if PY_VERSION_HEX >= 0x03040000
186 PyObject* cppyy = PyImport_AddModule((
char*)
"cppyy");
187 fGbl = PyObject_GetAttrString(cppyy, (
char*)
"gbl");
189 ~GblGetter() { Py_DECREF(fGbl); }
199#if PY_VERSION_HEX >= 0x03060000
205#define CPYCPPYY_ORGDICT_LOOKUP(mp, key, hash, value_addr, hashpos) \
206 OrgDictLookup(mp, key, hash, value_addr, hashpos)
211#elif PY_VERSION_HEX >= 0x03030000
212inline PyDictKeyEntry* OrgDictLookup(
218#define CPYCPPYY_ORGDICT_LOOKUP(mp, key, hash, value_addr, hashpos) \
219 OrgDictLookup(mp, key, hash, value_addr)
221PyDictKeyEntry* CPyCppyyLookDictString(
226inline PyDictEntry* OrgDictLookup(PyDictObject* mp,
PyObject* key,
long hash)
231#define CPYCPPYY_ORGDICT_LOOKUP(mp, key, hash, value_addr, hashpos) \
232 OrgDictLookup(mp, key, hash)
234PyDictEntry* CPyCppyyLookDictString(PyDictObject* mp,
PyObject* key,
long hash)
237 static GblGetter gbl;
238#if PY_VERSION_HEX >= 0x03060000
249#if PY_VERSION_HEX >= 0x03060000
252 if (!ep || (ep->me_key && ep->me_value))
257 if (PyDict_GetItem(PyEval_GetBuiltins(), key) != 0)
264 PyObject* val = PyObject_GetAttr(*gbl, key);
274 PyObject* actual_val =
Py_TYPE(val)->tp_descr_get(val,
nullptr,
nullptr);
281 if (PyDict_SetItem((
PyObject*)mp, key, val) == 0) {
284#if PY_VERSION_HEX >= 0x03060000
287 ep->me_key =
nullptr;
288 ep->me_value =
nullptr;
298#if PY_VERSION_HEX >= 0x03030000
299 if (mp->ma_keys->dk_usable <= 0) {
304 const int maxinsert = 5;
306 for (
int varmax = 1; varmax <= maxinsert; ++varmax) {
307 for (
int ivar = 0; ivar < varmax; ++ivar) {
309 PyDict_SetItem((
PyObject*)mp, buf[ivar], Py_None);
311 for (
int ivar = 0; ivar < varmax; ++ivar) {
312 PyDict_DelItem((
PyObject*)mp, buf[ivar]);
313 Py_DECREF(buf[ivar]);
315 if (0 < mp->ma_keys->dk_usable)
341 PyDictObject* dict =
nullptr;
342 if (!PyArg_ParseTuple(args,
const_cast<char*
>(
"O!"), &PyDict_Type, &dict))
358 PyErr_Format(PyExc_TypeError,
"too few arguments for template instantiation");
363 const std::string& tmpl_name =
365 if (!tmpl_name.size())
372static char* GCIA_kwlist[] = {(
char*)
"instance", (
char*)
"field", (
char*)
"byref", NULL};
373static void* GetCPPInstanceAddress(
const char* fname,
PyObject* args,
PyObject* kwds)
377 if (PyArg_ParseTupleAndKeywords(args, kwds,
const_cast<char*
>(
"O|O!b"), GCIA_kwlist,
391 void* addr = (
void*)pyprop->
GetAddress(pyobj);
398 PyErr_Format(PyExc_TypeError,
405 if (!byref)
return ((
CPPInstance*)pyobj)->GetObject();
409 if (!PyErr_Occurred())
410 PyErr_Format(PyExc_ValueError,
"invalid argument for %s", fname);
418 void* addr = GetCPPInstanceAddress(
"addressof", args, kwds);
420 return PyLong_FromLongLong((intptr_t)addr);
421 else if (!PyErr_Occurred()) {
422 return PyLong_FromLong(0);
423 }
else if (PyTuple_CheckExact(args) && PyTuple_GET_SIZE(args) == 1) {
425 PyObject* arg0 = PyTuple_GET_ITEM(args, 0);
426 if (arg0 ==
gNullPtrObject || (PyInt_Check(arg0) && PyInt_AsLong(arg0) == 0))
427 return PyLong_FromLong(0);
429 if (addr)
return PyLong_FromLongLong((intptr_t)addr);
433 if (!PyErr_Occurred()) {
434 if (PyTuple_CheckExact(args) && PyTuple_GET_SIZE(args)) {
435 PyObject* str = PyObject_Str(PyTuple_GET_ITEM(args, 0));
439 PyErr_Format(PyExc_TypeError,
"unknown object at %p", (
void*)PyTuple_GET_ITEM(args, 0));
450 void* addr = GetCPPInstanceAddress(
"as_cobject", args, kwds);
460 void* addr = GetCPPInstanceAddress(
"as_capsule", args, kwds);
462#if PY_VERSION_HEX < 0x02060000
463 return PyCObject_FromVoidPtr(addr,
nullptr);
465 return PyCapsule_New(addr,
nullptr,
nullptr);
474 void* addr = GetCPPInstanceAddress(
"as_ctypes", args, kwds);
479 static PyTypeObject* ct_cvoidp =
nullptr;
481 PyObject* ctmod = PyImport_ImportModule(
"ctypes");
482 if (!ctmod)
return nullptr;
484 ct_cvoidp = (PyTypeObject*)PyObject_GetAttrString(ctmod,
"c_void_p");
486 if (!ct_cvoidp)
return nullptr;
487 Py_DECREF(ct_cvoidp);
490 PyObject* ref = ct_cvoidp->tp_new(ct_cvoidp,
nullptr,
nullptr);
502 PyErr_Format(PyExc_TypeError,
508 PyObject* pyaddr = PyTuple_GET_ITEM(args, 0);
510 void* addr =
nullptr;
511 if (pyaddr != &_CPyCppyy_NullPtrStruct) {
513 if (PyErr_Occurred()) {
516 addr = PyLong_AsVoidPtr(pyaddr);
517 if (PyErr_Occurred()) {
522 if (!addr || !buflen) {
523 PyErr_SetString(PyExc_TypeError,
524 "BindObject requires a CObject or long integer as first argument");
547 PyErr_SetString(PyExc_TypeError,
548 "BindObject expects a valid class or class name as an argument");
552 bool do_cast =
false;
554 PyObject* cast = PyDict_GetItemString(kwds,
"cast");
555 do_cast = cast && PyObject_IsTrue(cast);
569 PyErr_SetString(PyExc_TypeError,
"C++ object expected");
583 PyObject* pythonizor =
nullptr;
const char* scope;
584 if (!PyArg_ParseTuple(args,
const_cast<char*
>(
"Os"), &pythonizor, &scope))
587 if (!PyCallable_Check(pythonizor)) {
588 PyObject* pystr = PyObject_Str(pythonizor);
589 PyErr_Format(PyExc_TypeError,
595 Py_INCREF(pythonizor);
606 PyObject* pythonizor =
nullptr;
const char* scope;
607 if (!PyArg_ParseTuple(args,
const_cast<char*
>(
"Os"), &pythonizor, &scope))
612 auto p2 = std::find(p1->second.begin(), p1->second.end(), pythonizor);
613 if (p2 != p1->second.end()) {
614 p1->second.erase(p2);
628 if (!PyArg_ParseTuple(args,
const_cast<char*
>(
"O!"), &PyInt_Type, &policy))
631 long l = PyInt_AS_LONG(policy);
636 PyErr_Format(PyExc_ValueError,
"Unknown policy %ld",
l);
646 if (!PyArg_ParseTuple(args,
const_cast<char*
>(
"O"), &setProtected))
661 if (!PyArg_ParseTuple(args,
const_cast<char*
>(
"O!O!"),
665 (
bool)PyLong_AsLong(pykeep) ? pyobj->
PythonOwns() : pyobj->CppOwns();
674 const char* type_name;
675 if (!PyArg_ParseTuple(args,
const_cast<char*
>(
"s"), &type_name))
689 PyErr_SetString(PyExc_TypeError,
"C++ class expected");
704 if (!PyArg_ParseTuple(args,
const_cast<char*
>(
"O!O!"),
720 METH_VARARGS, (
char*)
"cppyy internal function"},
721 {(
char*)
"MakeCppTemplateClass", (PyCFunction)MakeCppTemplateClass,
722 METH_VARARGS, (
char*)
"cppyy internal function"},
723 {(
char*)
"_set_cpp_lazy_lookup", (PyCFunction)SetCppLazyLookup,
724 METH_VARARGS, (
char*)
"cppyy internal function"},
726 METH_NOARGS, (
char*)
"cppyy internal function"},
727 {(
char*)
"addressof", (PyCFunction)addressof,
728 METH_VARARGS | METH_KEYWORDS, (
char*)
"Retrieve address of proxied object or field as a value."},
729 {(
char*)
"as_cobject", (PyCFunction)AsCObject,
730 METH_VARARGS | METH_KEYWORDS, (
char*)
"Retrieve address of proxied object or field in a CObject."},
731 {(
char*)
"as_capsule", (PyCFunction)AsCapsule,
732 METH_VARARGS | METH_KEYWORDS, (
char*)
"Retrieve address of proxied object or field in a PyCapsule."},
733 {(
char*)
"as_ctypes", (PyCFunction)AsCTypes,
734 METH_VARARGS | METH_KEYWORDS, (
char*)
"Retrieve address of proxied object or field in a ctypes c_void_p."},
735 {(
char*)
"bind_object", (PyCFunction)BindObject,
736 METH_VARARGS | METH_KEYWORDS, (
char*)
"Create an object of given type, from given address."},
737 {(
char*)
"move", (PyCFunction)Move,
738 METH_O, (
char*)
"Cast the C++ object to become movable."},
739 {(
char*)
"add_pythonization", (PyCFunction)AddPythonization,
740 METH_VARARGS, (
char*)
"Add a pythonizor."},
741 {(
char*)
"remove_pythonization", (PyCFunction)RemovePythonization,
742 METH_VARARGS, (
char*)
"Remove a pythonizor."},
743 {(
char*)
"SetMemoryPolicy", (PyCFunction)SetMemoryPolicy,
744 METH_VARARGS, (
char*)
"Determines object ownership model."},
745 {(
char*)
"SetGlobalSignalPolicy", (PyCFunction)SetGlobalSignalPolicy,
746 METH_VARARGS, (
char*)
"Trap signals in safe mode to prevent interpreter abort."},
747 {(
char*)
"SetOwnership", (PyCFunction)SetOwnership,
748 METH_VARARGS, (
char*)
"Modify held C++ object ownership."},
749 {(
char*)
"AddSmartPtrType", (PyCFunction)AddSmartPtrType,
750 METH_VARARGS, (
char*)
"Add a smart pointer to the list of known smart pointer types."},
751 {(
char*)
"_pin_type", (PyCFunction)PinType,
752 METH_O, (
char*)
"Install a type pinning."},
753 {(
char*)
"Cast", (PyCFunction)Cast,
754 METH_VARARGS, (
char*)
"Cast the given object to the given type"},
755 {
nullptr,
nullptr, 0,
nullptr}
758#define QuoteIdent(ident) #ident
759#define QuoteMacro(macro) QuoteIdent(macro)
760#define LIBCPPYY_NAME "libcppyy" QuoteMacro(PY_MAJOR_VERSION) "_" QuoteMacro(PY_MINOR_VERSION)
762#define CONCAT(a, b, c, d) a##b##c##d
763#define LIBCPPYY_INIT_FUNCTION(a, b, c, d) CONCAT(a, b, c, d)
765#if PY_VERSION_HEX >= 0x03000000
770#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
772static int cpycppyymodule_traverse(
PyObject*
m, visitproc visit,
void* arg)
774 Py_VISIT(GETSTATE(
m)->error);
778static int cpycppyymodule_clear(
PyObject*
m)
780 Py_CLEAR(GETSTATE(
m)->error);
784static struct PyModuleDef moduledef = {
785 PyModuleDef_HEAD_INIT,
788 sizeof(
struct module_state),
791 cpycppyymodule_traverse,
792 cpycppyymodule_clear,
798#define CPYCPPYY_INIT_ERROR return nullptr
812#if PY_VERSION_HEX < 0x03090000
813 PyEval_InitThreads();
820 PyDict_SetItem(dict, notstring, notstring);
821 Py_DECREF(notstring);
822#if PY_VERSION_HEX >= 0x03030000
830#if PY_VERSION_HEX >= 0x03000000
846 PyModule_AddObject(
gThisModule,
"UserExceptions", PyDict_New());
908 PyObject* cppfatal = PyErr_NewException((
char*)
"cppyy.ll.FatalError",
nullptr,
nullptr);
909 PyModule_AddObject(
gThisModule, (
char*)
"FatalError", cppfatal);
911 gBusException = PyErr_NewException((
char*)
"cppyy.ll.BusError", cppfatal,
nullptr);
913 gSegvException = PyErr_NewException((
char*)
"cppyy.ll.SegmentationViolation", cppfatal,
nullptr);
915 gIllException = PyErr_NewException((
char*)
"cppyy.ll.IllegalInstruction", cppfatal,
nullptr);
917 gAbrtException = PyErr_NewException((
char*)
"cppyy.ll.AbortSignal", cppfatal,
nullptr);
921 PyModule_AddObject(
gThisModule, (
char*)
"kMemoryHeuristics",
923 PyModule_AddObject(
gThisModule, (
char*)
"kMemoryStrict",
931#if PY_VERSION_HEX >= 0x03000000
#define CPYCPPYY_ORGDICT_LOOKUP(mp, key, hash, value_addr, hashpos)
static PyObject * nullptr_repr(PyObject *)
static PyNumberMethods nullptr_as_number
#define CPYCPPYY_INIT_ERROR
#define LIBCPPYY_INIT_FUNCTION(a, b, c, d)
static void nullptr_dealloc(PyObject *)
static int nullptr_nonzero(PyObject *)
static PyMethodDef gCPyCppyyMethods[]
static PyTypeObject PyNullPtr_t_Type
#define CPYCPPYY_GET_DICT_LOOKUP(mp)
PyDictEntry *(* dict_lookup_func)(PyDictObject *, PyObject *, long)
#define PY_SSIZE_T_FORMAT
static void * CPyCppyy_PyCapsule_GetPointer(PyObject *capsule, const char *)
#define CPyCppyy_PyText_AsString
static PyObject * CPyCppyy_PyCapsule_New(void *cobj, const char *, void(*destr)(void *))
#define CPyCppyy_PyText_FromFormat
#define CPyCppyy_PyText_Type
#define CPyCppyy_PyText_FromString
#define CPyCppyy_PyText_Check
#define PyVarObject_HEAD_INIT(type, size)
TTime operator*(const TTime &t1, const TTime &t2)
void * GetAddress(CPPInstance *pyobj)
R__EXTERN PyObject * gName
Py_ssize_t GetBuffer(PyObject *pyobject, char tc, int size, void *&buf, bool check=true)
std::string ConstructTemplateArgs(PyObject *pyname, PyObject *tpArgs, PyObject *args=nullptr, ArgPreference=kNone, int argoff=0, int *pcnt=nullptr)
bool InitProxy(PyObject *module, PyTypeObject *pytype, const char *name)
Set of helper functions that are invoked from the pythonizors, on the Python side.
PyTypeObject CPPInstance_Type
PyObject * gAbrtException
R__EXTERN bool gDictLookupActive
PyTypeObject VectorIter_Type
PyTypeObject CPPExcInstance_Type
dict_lookup_func gDictLookupOrg
PyTypeObject CustomInstanceMethod_Type
bool CPPDataMember_CheckExact(T *object)
PyTypeObject RefFloat_Type
Custom "builtins," detectable by type, for pass by ref and improved performance.
PyObject * gSegvException
std::set< Cppyy::TCppType_t > gPinnedTypes
PyObject * DestroyPyStrings()
PyObject * BindCppObjectNoCast(Cppyy::TCppObject_t object, Cppyy::TCppType_t klass, const unsigned flags=0)
std::map< std::string, std::vector< PyObject * > > gPythonizations
bool CPPScope_Check(T *object)
bool CPPInstance_Check(T *object)
PyObject * CreateScopeProxy(Cppyy::TCppScope_t)
PyTypeObject IndexIter_Type
PyObject * gNullPtrObject
PyTypeObject CPPOverload_Type
PyTypeObject TemplateProxy_Type
R__EXTERN PyObject * gThisModule
PyTypeObject InstanceArrayIter_Type
bool CPPDataMember_Check(T *object)
PyObject * BindCppObject(Cppyy::TCppObject_t object, Cppyy::TCppType_t klass, const unsigned flags=0)
PyTypeObject CPPScope_Type
PyTypeObject LowLevelView_Type
PyTypeObject CPPDataMember_Type
PyTypeObject TupleOfInstances_Type
Representation of C-style array of instances.
RPY_EXPORTED void AddSmartPtrType(const std::string &)
RPY_EXPORTED TCppScope_t GetScope(const std::string &scope_name)
static bool SetGlobalSignalPolicy(bool setProtected)
static bool SetMemoryPolicy(ECallFlags e)
PyObject_HEAD char * b_ptr