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
154struct CPyCppyy_tagCDataObject {
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 PyErr_SetString(PyExc_TypeError,
"aap");
451 void* addr = GetCPPInstanceAddress(
"as_cobject", args, kwds);
461 void* addr = GetCPPInstanceAddress(
"as_capsule", args, kwds);
463#if PY_VERSION_HEX < 0x02060000
464 return PyCObject_FromVoidPtr(addr,
nullptr);
466 return PyCapsule_New(addr,
nullptr,
nullptr);
475 void* addr = GetCPPInstanceAddress(
"as_ctypes", args, kwds);
480 static PyTypeObject* ct_cvoidp =
nullptr;
482 PyObject* ctmod = PyImport_ImportModule(
"ctypes");
483 if (!ctmod)
return nullptr;
485 ct_cvoidp = (PyTypeObject*)PyObject_GetAttrString(ctmod,
"c_void_p");
487 if (!ct_cvoidp)
return nullptr;
488 Py_DECREF(ct_cvoidp);
491 PyObject* ref = ct_cvoidp->tp_new(ct_cvoidp,
nullptr,
nullptr);
492 *(
void**)((CPyCppyy_tagCDataObject*)ref)->b_ptr = addr;
493 ((CPyCppyy_tagCDataObject*)ref)->b_needsfree = 0;
503 PyErr_Format(PyExc_TypeError,
509 PyObject* pyaddr = PyTuple_GET_ITEM(args, 0);
511 void* addr =
nullptr;
512 if (pyaddr != &_CPyCppyy_NullPtrStruct) {
514 if (PyErr_Occurred()) {
517 addr = PyLong_AsVoidPtr(pyaddr);
518 if (PyErr_Occurred()) {
523 if (!addr || !buflen) {
524 PyErr_SetString(PyExc_TypeError,
525 "BindObject requires a CObject or long integer as first argument");
548 PyErr_SetString(PyExc_TypeError,
549 "BindObject expects a valid class or class name as an argument");
553 bool do_cast =
false;
555 PyObject* cast = PyDict_GetItemString(kwds,
"cast");
556 do_cast = cast && PyObject_IsTrue(cast);
570 PyErr_SetString(PyExc_TypeError,
"C++ object expected");
584 PyObject* pythonizor =
nullptr;
const char* scope;
585 if (!PyArg_ParseTuple(args,
const_cast<char*
>(
"Os"), &pythonizor, &scope))
588 if (!PyCallable_Check(pythonizor)) {
589 PyObject* pystr = PyObject_Str(pythonizor);
590 PyErr_Format(PyExc_TypeError,
596 Py_INCREF(pythonizor);
607 PyObject* pythonizor =
nullptr;
const char* scope;
608 if (!PyArg_ParseTuple(args,
const_cast<char*
>(
"Os"), &pythonizor, &scope))
613 auto p2 = std::find(p1->second.begin(), p1->second.end(), pythonizor);
614 if (p2 != p1->second.end()) {
615 p1->second.erase(p2);
629 if (!PyArg_ParseTuple(args,
const_cast<char*
>(
"O!"), &PyInt_Type, &policy))
632 long l = PyInt_AS_LONG(policy);
637 PyErr_Format(PyExc_ValueError,
"Unknown policy %ld",
l);
647 if (!PyArg_ParseTuple(args,
const_cast<char*
>(
"O"), &setProtected))
662 if (!PyArg_ParseTuple(args,
const_cast<char*
>(
"O!O!"),
666 (
bool)PyLong_AsLong(pykeep) ? pyobj->
PythonOwns() : pyobj->CppOwns();
675 const char* type_name;
676 if (!PyArg_ParseTuple(args,
const_cast<char*
>(
"s"), &type_name))
690 PyErr_SetString(PyExc_TypeError,
"C++ class expected");
705 if (!PyArg_ParseTuple(args,
const_cast<char*
>(
"O!O!"),
721 METH_VARARGS, (
char*)
"cppyy internal function"},
722 {(
char*)
"MakeCppTemplateClass", (PyCFunction)MakeCppTemplateClass,
723 METH_VARARGS, (
char*)
"cppyy internal function"},
724 {(
char*)
"_set_cpp_lazy_lookup", (PyCFunction)SetCppLazyLookup,
725 METH_VARARGS, (
char*)
"cppyy internal function"},
727 METH_NOARGS, (
char*)
"cppyy internal function"},
728 {(
char*)
"addressof", (PyCFunction)addressof,
729 METH_VARARGS | METH_KEYWORDS, (
char*)
"Retrieve address of proxied object or field as a value."},
730 {(
char*)
"as_cobject", (PyCFunction)AsCObject,
731 METH_VARARGS | METH_KEYWORDS, (
char*)
"Retrieve address of proxied object or field in a CObject."},
732 {(
char*)
"as_capsule", (PyCFunction)AsCapsule,
733 METH_VARARGS | METH_KEYWORDS, (
char*)
"Retrieve address of proxied object or field in a PyCapsule."},
734 {(
char*)
"as_ctypes", (PyCFunction)AsCTypes,
735 METH_VARARGS | METH_KEYWORDS, (
char*)
"Retrieve address of proxied object or field in a ctypes c_void_p."},
736 {(
char*)
"bind_object", (PyCFunction)BindObject,
737 METH_VARARGS | METH_KEYWORDS, (
char*)
"Create an object of given type, from given address."},
738 {(
char*)
"move", (PyCFunction)Move,
739 METH_O, (
char*)
"Cast the C++ object to become movable."},
740 {(
char*)
"add_pythonization", (PyCFunction)AddPythonization,
741 METH_VARARGS, (
char*)
"Add a pythonizor."},
742 {(
char*)
"remove_pythonization", (PyCFunction)RemovePythonization,
743 METH_VARARGS, (
char*)
"Remove a pythonizor."},
744 {(
char*)
"SetMemoryPolicy", (PyCFunction)SetMemoryPolicy,
745 METH_VARARGS, (
char*)
"Determines object ownership model."},
746 {(
char*)
"SetGlobalSignalPolicy", (PyCFunction)SetGlobalSignalPolicy,
747 METH_VARARGS, (
char*)
"Trap signals in safe mode to prevent interpreter abort."},
748 {(
char*)
"SetOwnership", (PyCFunction)SetOwnership,
749 METH_VARARGS, (
char*)
"Modify held C++ object ownership."},
750 {(
char*)
"AddSmartPtrType", (PyCFunction)AddSmartPtrType,
751 METH_VARARGS, (
char*)
"Add a smart pointer to the list of known smart pointer types."},
752 {(
char*)
"_pin_type", (PyCFunction)PinType,
753 METH_O, (
char*)
"Install a type pinning."},
754 {(
char*)
"Cast", (PyCFunction)Cast,
755 METH_VARARGS, (
char*)
"Cast the given object to the given type"},
756 {
nullptr,
nullptr, 0,
nullptr}
759#define QuoteIdent(ident) #ident
760#define QuoteMacro(macro) QuoteIdent(macro)
761#define LIBCPPYY_NAME "libcppyy" QuoteMacro(PY_MAJOR_VERSION) "_" QuoteMacro(PY_MINOR_VERSION)
763#define CONCAT(a, b, c, d) a##b##c##d
764#define LIBCPPYY_INIT_FUNCTION(a, b, c, d) CONCAT(a, b, c, d)
766#if PY_VERSION_HEX >= 0x03000000
771#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
773static int cpycppyymodule_traverse(
PyObject*
m, visitproc visit,
void* arg)
775 Py_VISIT(GETSTATE(
m)->error);
779static int cpycppyymodule_clear(
PyObject*
m)
781 Py_CLEAR(GETSTATE(
m)->error);
785static struct PyModuleDef moduledef = {
786 PyModuleDef_HEAD_INIT,
789 sizeof(
struct module_state),
792 cpycppyymodule_traverse,
793 cpycppyymodule_clear,
799#define CPYCPPYY_INIT_ERROR return nullptr
813#if PY_VERSION_HEX < 0x03090000
814 PyEval_InitThreads();
821 PyDict_SetItem(dict, notstring, notstring);
822 Py_DECREF(notstring);
823#if PY_VERSION_HEX >= 0x03030000
831#if PY_VERSION_HEX >= 0x03000000
847 PyModule_AddObject(
gThisModule,
"UserExceptions", PyDict_New());
909 PyObject* cppfatal = PyErr_NewException((
char*)
"cppyy.ll.FatalError",
nullptr,
nullptr);
910 PyModule_AddObject(
gThisModule, (
char*)
"FatalError", cppfatal);
912 gBusException = PyErr_NewException((
char*)
"cppyy.ll.BusError", cppfatal,
nullptr);
914 gSegvException = PyErr_NewException((
char*)
"cppyy.ll.SegmentationViolation", cppfatal,
nullptr);
916 gIllException = PyErr_NewException((
char*)
"cppyy.ll.IllegalInstruction", cppfatal,
nullptr);
918 gAbrtException = PyErr_NewException((
char*)
"cppyy.ll.AbortSignal", cppfatal,
nullptr);
922 PyModule_AddObject(
gThisModule, (
char*)
"kMemoryHeuristics",
924 PyModule_AddObject(
gThisModule, (
char*)
"kMemoryStrict",
932#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)