19#define CPYCPPYY_INTERNAL 1
23 void* addr,
const std::string& classname,
bool python_owns =
false);
25#undef CPYCPPYY_INTERNAL
38#if PY_VERSION_HEX >= 0x03030000
39 typedef struct PyDictKeyEntry {
46 typedef struct _dictkeysobject {
51#if PY_VERSION_HEX >= 0x03060000
62 PyDictKeyEntry dk_entries[1];
66#define CPYCPPYY_GET_DICT_LOOKUP(mp) \
67 ((dict_lookup_func&)mp->ma_keys->dk_lookup)
71#define CPYCPPYY_GET_DICT_LOOKUP(mp) \
72 ((dict_lookup_func&)mp->ma_lookup)
84 Py_FatalError(
"deallocating nullptr");
94#if PY_VERSION_HEX < 0x03000000
100#
if PY_VERSION_HEX < 0x03000000
104#
if PY_VERSION_HEX < 0x03000000
108#
if PY_VERSION_HEX < 0x03000000
112#
if PY_VERSION_HEX >= 0x02020000
114#
if PY_VERSION_HEX < 0x03000000
121#
if PY_VERSION_HEX >= 0x02050000
124#
if PY_VERSION_HEX >= 0x03050000
140 (hashfunc)_Py_HashPointer,
141 0, 0, 0, 0, 0, Py_TPFLAGS_DEFAULT, 0, 0, 0, 0, 0, 0, 0,
142 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
143#if PY_VERSION_HEX >= 0x02030000
146#if PY_VERSION_HEX >= 0x02060000
149#if PY_VERSION_HEX >= 0x03040000
194 PyObject* cppyy = PyImport_AddModule((
char*)
"cppyy");
195 fGbl = PyObject_GetAttrString(cppyy, (
char*)
"gbl");
197 ~GblGetter() { Py_DECREF(fGbl); }
207#if PY_VERSION_HEX >= 0x03060000
213#define CPYCPPYY_ORGDICT_LOOKUP(mp, key, hash, value_addr, hashpos) \
214 OrgDictLookup(mp, key, hash, value_addr, hashpos)
219#elif PY_VERSION_HEX >= 0x03030000
220inline PyDictKeyEntry* OrgDictLookup(
226#define CPYCPPYY_ORGDICT_LOOKUP(mp, key, hash, value_addr, hashpos) \
227 OrgDictLookup(mp, key, hash, value_addr)
229PyDictKeyEntry* CPyCppyyLookDictString(
234inline PyDictEntry* OrgDictLookup(PyDictObject* mp,
PyObject* key,
long hash)
239#define CPYCPPYY_ORGDICT_LOOKUP(mp, key, hash, value_addr, hashpos) \
240 OrgDictLookup(mp, key, hash)
242PyDictEntry* CPyCppyyLookDictString(PyDictObject* mp,
PyObject* key,
long hash)
245 static GblGetter gbl;
246#if PY_VERSION_HEX >= 0x03060000
257#if PY_VERSION_HEX >= 0x03060000
260 if (!ep || (ep->me_key && ep->me_value))
265 if (PyDict_GetItem(PyEval_GetBuiltins(), key) != 0)
272 PyObject* val = PyObject_GetAttr(*gbl, key);
282 PyObject* actual_val =
Py_TYPE(val)->tp_descr_get(val,
nullptr,
nullptr);
289 if (PyDict_SetItem((
PyObject*)mp, key, val) == 0) {
292#if PY_VERSION_HEX >= 0x03060000
295 ep->me_key =
nullptr;
296 ep->me_value =
nullptr;
306#if PY_VERSION_HEX >= 0x03030000
307 if (mp->ma_keys->dk_usable <= 0) {
312 const int maxinsert = 5;
314 for (
int varmax = 1; varmax <= maxinsert; ++varmax) {
315 for (
int ivar = 0; ivar < varmax; ++ivar) {
317 PyDict_SetItem((
PyObject*)mp, buf[ivar], Py_None);
319 for (
int ivar = 0; ivar < varmax; ++ivar) {
320 PyDict_DelItem((
PyObject*)mp, buf[ivar]);
321 Py_DECREF(buf[ivar]);
323 if (0 < mp->ma_keys->dk_usable)
349 PyDictObject* dict =
nullptr;
350 if (!PyArg_ParseTuple(args,
const_cast<char*
>(
"O!"), &PyDict_Type, &dict))
366 PyErr_Format(PyExc_TypeError,
"too few arguments for template instantiation");
371 const std::string& tmpl_name =
373 if (!tmpl_name.size())
380static char* GCIA_kwlist[] = {(
char*)
"instance", (
char*)
"field", (
char*)
"byref", NULL};
381static void* GetCPPInstanceAddress(
const char* fname,
PyObject* args,
PyObject* kwds)
385 if (PyArg_ParseTupleAndKeywords(args, kwds,
const_cast<char*
>(
"O|O!b"), GCIA_kwlist,
400 void* addr = (
void*)pyprop->
GetAddress(pyobj);
407 PyErr_Format(PyExc_TypeError,
414 if (!byref)
return ((
CPPInstance*)pyobj)->GetObject();
420 if (req ==
"Instance_AsVoidPtr")
422 else if (req ==
"Instance_FromVoidPtr")
427 if (!PyErr_Occurred())
428 PyErr_Format(PyExc_ValueError,
"invalid argument for %s", fname);
436 void* addr = GetCPPInstanceAddress(
"addressof", args, kwds);
438 return PyLong_FromLongLong((intptr_t)addr);
439 else if (!PyErr_Occurred()) {
440 return PyLong_FromLong(0);
441 }
else if (PyTuple_CheckExact(args) && PyTuple_GET_SIZE(args) == 1) {
443 PyObject* arg0 = PyTuple_GET_ITEM(args, 0);
446 if (arg0 ==
gNullPtrObject || (PyInt_Check(arg0) && PyInt_AsLong(arg0) == 0))
447 return PyLong_FromLong(0);
451 const auto& methods = ((
CPPOverload*)arg0)->fMethodInfo->fMethods;
452 if (methods.size() != 1) {
453 PyErr_SetString(PyExc_TypeError,
"overload is not unambiguous");
458 return PyLong_FromLongLong((intptr_t)addr);
462 if (PyCFunction_Check(arg0)) {
463 void* caddr = (
void*)PyCFunction_GetFunction(arg0);
464 return PyLong_FromLongLong((intptr_t)caddr);
469 if (addr)
return PyLong_FromLongLong((intptr_t)addr);
473 if (!PyErr_Occurred()) {
474 if (PyTuple_CheckExact(args) && PyTuple_GET_SIZE(args)) {
475 PyObject* str = PyObject_Str(PyTuple_GET_ITEM(args, 0));
479 PyErr_Format(PyExc_TypeError,
"unknown object at %p", (
void*)PyTuple_GET_ITEM(args, 0));
490 void* addr = GetCPPInstanceAddress(
"as_cobject", args, kwds);
500 void* addr = GetCPPInstanceAddress(
"as_capsule", args, kwds);
502#if PY_VERSION_HEX < 0x02060000
503 return PyCObject_FromVoidPtr(addr,
nullptr);
505 return PyCapsule_New(addr,
nullptr,
nullptr);
514 void* addr = GetCPPInstanceAddress(
"as_ctypes", args, kwds);
519 static PyTypeObject* ct_cvoidp =
nullptr;
521 PyObject* ctmod = PyImport_ImportModule(
"ctypes");
522 if (!ctmod)
return nullptr;
524 ct_cvoidp = (PyTypeObject*)PyObject_GetAttrString(ctmod,
"c_void_p");
526 if (!ct_cvoidp)
return nullptr;
527 Py_DECREF(ct_cvoidp);
530 PyObject* ref = ct_cvoidp->tp_new(ct_cvoidp,
nullptr,
nullptr);
542 PyErr_Format(PyExc_TypeError,
548 PyObject* pyaddr = PyTuple_GET_ITEM(args, 0);
550 void* addr =
nullptr;
551 if (pyaddr != &_CPyCppyy_NullPtrStruct) {
553 if (PyErr_Occurred()) {
556 addr = PyLong_AsVoidPtr(pyaddr);
557 if (PyErr_Occurred()) {
562 if (!addr || !buflen) {
563 PyErr_SetString(PyExc_TypeError,
564 "BindObject requires a CObject or long integer as first argument");
587 PyErr_SetString(PyExc_TypeError,
588 "BindObject expects a valid class or class name as an argument");
592 bool do_cast =
false;
594 PyObject* cast = PyDict_GetItemString(kwds,
"cast");
595 do_cast = cast && PyObject_IsTrue(cast);
609 PyErr_SetString(PyExc_TypeError,
"C++ object expected");
623 PyObject* pythonizor =
nullptr;
const char* scope;
624 if (!PyArg_ParseTuple(args,
const_cast<char*
>(
"Os"), &pythonizor, &scope))
627 if (!PyCallable_Check(pythonizor)) {
628 PyObject* pystr = PyObject_Str(pythonizor);
629 PyErr_Format(PyExc_TypeError,
635 Py_INCREF(pythonizor);
646 PyObject* pythonizor =
nullptr;
const char* scope;
647 if (!PyArg_ParseTuple(args,
const_cast<char*
>(
"Os"), &pythonizor, &scope))
652 auto p2 = std::find(p1->second.begin(), p1->second.end(), pythonizor);
653 if (p2 != p1->second.end()) {
654 p1->second.erase(p2);
668 if (!PyArg_ParseTuple(args,
const_cast<char*
>(
"O!"), &PyInt_Type, &policy))
671 long l = PyInt_AS_LONG(policy);
676 PyErr_Format(PyExc_ValueError,
"Unknown policy %ld",
l);
686 if (!PyArg_ParseTuple(args,
const_cast<char*
>(
"O"), &setProtected))
701 if (!PyArg_ParseTuple(args,
const_cast<char*
>(
"O!O!"),
705 (
bool)PyLong_AsLong(pykeep) ? pyobj->
PythonOwns() : pyobj->CppOwns();
714 const char* type_name;
715 if (!PyArg_ParseTuple(args,
const_cast<char*
>(
"s"), &type_name))
729 PyErr_SetString(PyExc_TypeError,
"C++ class expected");
744 if (!PyArg_ParseTuple(args,
const_cast<char*
>(
"O!O!"),
760 METH_VARARGS, (
char*)
"cppyy internal function"},
761 {(
char*)
"MakeCppTemplateClass", (PyCFunction)MakeCppTemplateClass,
762 METH_VARARGS, (
char*)
"cppyy internal function"},
763 {(
char*)
"_set_cpp_lazy_lookup", (PyCFunction)SetCppLazyLookup,
764 METH_VARARGS, (
char*)
"cppyy internal function"},
766 METH_NOARGS, (
char*)
"cppyy internal function"},
767 {(
char*)
"addressof", (PyCFunction)addressof,
768 METH_VARARGS | METH_KEYWORDS, (
char*)
"Retrieve address of proxied object or field as a value."},
769 {(
char*)
"as_cobject", (PyCFunction)AsCObject,
770 METH_VARARGS | METH_KEYWORDS, (
char*)
"Retrieve address of proxied object or field in a CObject."},
771 {(
char*)
"as_capsule", (PyCFunction)AsCapsule,
772 METH_VARARGS | METH_KEYWORDS, (
char*)
"Retrieve address of proxied object or field in a PyCapsule."},
773 {(
char*)
"as_ctypes", (PyCFunction)AsCTypes,
774 METH_VARARGS | METH_KEYWORDS, (
char*)
"Retrieve address of proxied object or field in a ctypes c_void_p."},
775 {(
char*)
"bind_object", (PyCFunction)BindObject,
776 METH_VARARGS | METH_KEYWORDS, (
char*)
"Create an object of given type, from given address."},
777 {(
char*)
"move", (PyCFunction)Move,
778 METH_O, (
char*)
"Cast the C++ object to become movable."},
779 {(
char*)
"add_pythonization", (PyCFunction)AddPythonization,
780 METH_VARARGS, (
char*)
"Add a pythonizor."},
781 {(
char*)
"remove_pythonization", (PyCFunction)RemovePythonization,
782 METH_VARARGS, (
char*)
"Remove a pythonizor."},
783 {(
char*)
"SetMemoryPolicy", (PyCFunction)SetMemoryPolicy,
784 METH_VARARGS, (
char*)
"Determines object ownership model."},
785 {(
char*)
"SetGlobalSignalPolicy", (PyCFunction)SetGlobalSignalPolicy,
786 METH_VARARGS, (
char*)
"Trap signals in safe mode to prevent interpreter abort."},
787 {(
char*)
"SetOwnership", (PyCFunction)SetOwnership,
788 METH_VARARGS, (
char*)
"Modify held C++ object ownership."},
789 {(
char*)
"AddSmartPtrType", (PyCFunction)AddSmartPtrType,
790 METH_VARARGS, (
char*)
"Add a smart pointer to the list of known smart pointer types."},
791 {(
char*)
"_pin_type", (PyCFunction)PinType,
792 METH_O, (
char*)
"Install a type pinning."},
793 {(
char*)
"Cast", (PyCFunction)Cast,
794 METH_VARARGS, (
char*)
"Cast the given object to the given type"},
795 {
nullptr,
nullptr, 0,
nullptr}
798#define QuoteIdent(ident) #ident
799#define QuoteMacro(macro) QuoteIdent(macro)
800#define LIBCPPYY_NAME "libcppyy" QuoteMacro(PY_MAJOR_VERSION) "_" QuoteMacro(PY_MINOR_VERSION)
802#define CONCAT(a, b, c, d) a##b##c##d
803#define LIBCPPYY_INIT_FUNCTION(a, b, c, d) CONCAT(a, b, c, d)
805#if PY_VERSION_HEX >= 0x03000000
810#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
812static int cpycppyymodule_traverse(
PyObject*
m, visitproc visit,
void* arg)
814 Py_VISIT(GETSTATE(
m)->error);
818static int cpycppyymodule_clear(
PyObject*
m)
820 Py_CLEAR(GETSTATE(
m)->error);
824static struct PyModuleDef moduledef = {
825 PyModuleDef_HEAD_INIT,
828 sizeof(
struct module_state),
831 cpycppyymodule_traverse,
832 cpycppyymodule_clear,
838#define CPYCPPYY_INIT_ERROR return nullptr
852#if PY_VERSION_HEX < 0x03090000
853 PyEval_InitThreads();
859 PyObject* notstring = PyInt_FromLong(5);
860 PyDict_SetItem(dict, notstring, notstring);
861 Py_DECREF(notstring);
862#if PY_VERSION_HEX >= 0x03030000
870#if PY_VERSION_HEX >= 0x03000000
886 PyModule_AddObject(
gThisModule,
"UserExceptions", PyDict_New());
948 PyObject* cppfatal = PyErr_NewException((
char*)
"cppyy.ll.FatalError",
nullptr,
nullptr);
949 PyModule_AddObject(
gThisModule, (
char*)
"FatalError", cppfatal);
951 gBusException = PyErr_NewException((
char*)
"cppyy.ll.BusError", cppfatal,
nullptr);
953 gSegvException = PyErr_NewException((
char*)
"cppyy.ll.SegmentationViolation", cppfatal,
nullptr);
955 gIllException = PyErr_NewException((
char*)
"cppyy.ll.IllegalInstruction", cppfatal,
nullptr);
957 gAbrtException = PyErr_NewException((
char*)
"cppyy.ll.AbortSignal", cppfatal,
nullptr);
961 PyModule_AddObject(
gThisModule, (
char*)
"kMemoryHeuristics",
963 PyModule_AddObject(
gThisModule, (
char*)
"kMemoryStrict",
971#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)
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
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)
CPYCPPYY_EXTERN PyObject * Instance_FromVoidPtr(void *addr, const std::string &classname, bool python_owns=false)
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)
CPYCPPYY_EXTERN void * Instance_AsVoidPtr(PyObject *pyobject)
PyTypeObject CPPScope_Type
PyTypeObject LowLevelView_Type
bool CPPOverload_CheckExact(T *object)
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