53 ExtendedData() : fObject(nullptr), fSmartClass(nullptr), fDispatchPtr(nullptr), fArraySize(0) {}
55 for (
auto& pc : fDatamemberCache)
56 Py_XDECREF(pc.second);
57 fDatamemberCache.clear();
79#define EXT_OBJECT(pyobj) ((ExtendedData*)((pyobj)->fObject))->fObject
80#define DATA_CACHE(pyobj) ((ExtendedData*)((pyobj)->fObject))->fDatamemberCache
81#define SMART_CLS(pyobj) ((ExtendedData*)((pyobj)->fObject))->fSmartClass
82#define SMART_TYPE(pyobj) SMART_CLS(pyobj)->fCppType
83#define DISPATCHPTR(pyobj) ((ExtendedData*)((pyobj)->fObject))->fDispatchPtr
84#define ARRAY_SIZE(pyobj) ((ExtendedData*)((pyobj)->fObject))->fArraySize
90 fObject = (
void*)
new ExtendedData{};
119 PyObject* cpy = PyObject_GetAttrString(self, (
char*)
"__cpp_copy__");
120 if (cpy && PyCallable_Check(cpy)) {
123 PyTuple_SET_ITEM(args, 0, newinst);
124 PyObject* res = PyObject_CallObject(cpy, args);
143 bool bMergeOk = PyDict_Merge(newdct, selfdct, 1) == 0;
178 Py_INCREF(smart_type);
253 int result = PyObject_IsTrue(pylen);
274 PyObject *mname =
nullptr, *sigarg =
nullptr;
275 if (!PyArg_ParseTuple(args,
const_cast<char*
>(
"O!O!:__dispatch__"),
280 PyObject* pymeth = PyObject_GetAttr(self, mname);
285 PyObject* pydisp = PyObject_GetAttrString(pymeth,
const_cast<char*
>(
"__overload__"));
292 PyObject* oload = PyObject_CallFunctionObjArgs(pydisp, sigarg,
nullptr);
326 if (!PyTuple_Check(shape) || PyTuple_GET_SIZE(shape) != 1) {
327 PyErr_SetString(PyExc_TypeError,
"tuple object of size 1 expected");
331 long sz = PyLong_AsLong(PyTuple_GET_ITEM(shape, 0));
333 PyErr_SetString(PyExc_ValueError,
"array length must be positive");
350 PyErr_Format(PyExc_TypeError,
"%s object does not support indexing",
Py_TYPE(self)->tp_name);
355 if (idx == (
Py_ssize_t)-1 && PyErr_Occurred())
361 PyErr_SetString(PyExc_IndexError,
"negative indices not supported for array of structs");
367 if (0 <= maxidx && maxidx <= idx) {
368 PyErr_SetString(PyExc_IndexError,
"index out of range");
373 unsigned flags = 0;
size_t sz =
sizeof(
void*);
381 void* indexed_obj = (
void*)(address+(uintptr_t)(idx*sz));
388 {(
char*)
"__destruct__", (PyCFunction)
op_destruct, METH_NOARGS,
389 (
char*)
"call the C++ destructor"},
390 {(
char*)
"__dispatch__", (PyCFunction)
op_dispatch, METH_VARARGS,
391 (
char*)
"dispatch to selected overload"},
393 (
char*)
"get associated smart pointer, if any"},
394 {(
char*)
"__getitem__", (PyCFunction)
op_getitem, METH_O,
395 (
char*)
"pointer dereferencing"},
396 {(
char*)
"__reshape__", (PyCFunction)
op_reshape, METH_O,
397 (
char*)
"cast pointer to 1D array type"},
398 {(
char*)
nullptr,
nullptr, 0,
nullptr}
417 PyObject_GC_UnTrack((
PyObject*)pyobj);
436 using namespace Utility;
440 void* rawcpp = ((
CPPInstance*)self)->GetObjectRaw();
459 const char* cppop = op == Py_EQ ?
"==" :
"!=";
460 PyCallable* pyfunc = FindBinaryOperator(self, obj, cppop);
471 if (binop == Py_None) {
473 if (binop && binop != Py_None) flipit =
true;
476 if (!binop || binop == Py_None)
return nullptr;
479 Py_INCREF(obj); PyTuple_SET_ITEM(args, 0, obj);
492 if (!flipit)
return result;
494 int istrue = PyObject_IsTrue(
result);
509 if (clActual && clActual != klass) {
511 clActual, klass, address, -1 ,
true );
512 if (
offset != -1) address = (
void*)((intptr_t)address +
offset);
519#define CPYCPPYY_ORDERED_OPERATOR_STUB(op, ometh, label) \
521 PyCallable* pyfunc = Utility::FindBinaryOperator((PyObject*)self, other, #op);\
523 ometh = (PyObject*)CPPOverload_New(#label, pyfunc); \
540 if (op == Py_EQ || op == Py_NE) {
558 self->GetObject() == ((
CPPInstance*)other)->GetObject())) {
565 bIsEq = addr1 && addr2 && (addr1 == addr2);
568 if ((op == Py_EQ && bIsEq) || (op == Py_NE && !bIsEq))
575 else if (op == Py_LT || op == Py_LE || op == Py_GT || op == Py_GE) {
596 PyErr_SetString(PyExc_NotImplementedError,
"");
600 return PyObject_CallFunctionObjArgs(meth, (
PyObject*)self, other,
nullptr);
603 Py_INCREF(Py_NotImplemented);
604 return Py_NotImplemented;
614 return PyBaseObject_Type.tp_repr((
PyObject*)self);
628 const_cast<char*
>(
"<%s.%s object at %p held by %s at %p>"),
645 if (
sizeof(
unsigned long) >=
sizeof(
size_t))
646 return (
Py_hash_t)PyLong_AsUnsignedLong(obj);
647 return (
Py_hash_t)PyLong_AsUnsignedLongLong(obj);
668 bool isValid = PyMapping_HasKeyString(dct, (
char*)
"__call__");
671 PyObject* hashobj = PyObject_CallObject(hashcls,
nullptr);
677 PyObject* hashval = PyObject_CallFunctionObjArgs(hashobj, (
PyObject*)self,
nullptr);
688 ((PyTypeObject*)
Py_TYPE(self))->tp_hash = PyBaseObject_Type.tp_hash;
689 return PyBaseObject_Type.tp_hash((
PyObject*)self);
696 std::ostringstream s;
699#if PY_VERSION_HEX >= 0x03000000
707 if (isBound) res = PyObject_CallFunctionObjArgs(lshift, pys, NULL);
708 else res = PyObject_CallFunctionObjArgs(lshift, pys, pyobj, NULL);
711#if PY_VERSION_HEX >= 0x03000000
752 else if (pyname == (
PyObject*)0x01) {
765 }
else if (pyname == (
PyObject*)0x02) {
774 PyObject* lshift = PyObject_GetAttr(
793 static PyObject* printValue =
nullptr;
795 PyObject* gbl = PyDict_GetItemString(PySys_GetObject((
char*)
"modules"),
"cppyy.gbl");
796 PyObject* cl = PyObject_GetAttrString(gbl, (
char*)
"cling");
797 printValue = PyObject_GetAttrString(cl, (
char*)
"printValue");
801 Py_DECREF(printValue);
802 if (!PyCallable_Check(printValue))
803 printValue =
nullptr;
823 PyObject* OL = PyObject_GetItem(printValue, clName);
826 PyObject* pretty = OL ? PyObject_CallFunctionObjArgs(OL, byref,
nullptr) :
nullptr;
832 const std::string& pv = *(std::string*)((
CPPInstance*)pretty)->GetObject();
833 if (!pv.empty() && pv.find(
"@0x") == std::string::npos)
860 long shouldown = PyLong_AsLong(
value);
861 if (shouldown == -1 && PyErr_Occurred()) {
862 PyErr_SetString(PyExc_ValueError,
"__python_owns__ should be either True or False");
875 (
char*)
"If true, python manages the life time of this object",
nullptr},
876 {(
char*)
nullptr,
nullptr,
nullptr,
nullptr,
nullptr}
881#define CPYCPPYY_STUB_BODY(name, op) \
882 bool previously_resolved_overload = (bool)meth; \
885 PyCallable* pyfunc = Utility::FindBinaryOperator(left, right, #op); \
886 if (pyfunc) meth = (PyObject*)CPPOverload_New(#name, pyfunc); \
888 PyErr_SetString(PyExc_NotImplementedError, ""); \
892 PyObject* res = PyObject_CallFunctionObjArgs(meth, cppobj, other, nullptr);\
893 if (!res && previously_resolved_overload) { \
896 PyCallable* pyfunc = Utility::FindBinaryOperator(left, right, #op); \
897 if (pyfunc) ((CPPOverload*&)meth)->AdoptMethod(pyfunc); \
899 PyErr_SetString(PyExc_NotImplementedError, ""); \
903 res = PyObject_CallFunctionObjArgs(meth, cppobj, other, nullptr); \
908#define CPYCPPYY_OPERATOR_STUB(name, op, ometh) \
909static PyObject* op_##name##_stub(PyObject* left, PyObject* right) \
912 CPPClass* klass = (CPPClass*)Py_TYPE(left); \
913 if (!klass->fOperators) klass->fOperators = new Utility::PyOperators{}; \
914 PyObject*& meth = ometh; \
915 PyObject *cppobj = left, *other = right; \
916 CPYCPPYY_STUB_BODY(name, op) \
919#define CPYCPPYY_ASSOCIATIVE_OPERATOR_STUB(name, op, lmeth, rmeth) \
920static PyObject* op_##name##_stub(PyObject* left, PyObject* right) \
923 CPPClass* klass; PyObject** pmeth; \
924 PyObject *cppobj, *other; \
925 if (CPPInstance_Check(left)) { \
926 klass = (CPPClass*)Py_TYPE(left); \
927 if (!klass->fOperators) klass->fOperators = new Utility::PyOperators{};\
928 pmeth = &lmeth; cppobj = left; other = right; \
929 } else if (CPPInstance_Check(right)) { \
930 klass = (CPPClass*)Py_TYPE(right); \
931 if (!klass->fOperators) klass->fOperators = new Utility::PyOperators{};\
932 pmeth = &rmeth; cppobj = right; other = left; \
934 PyErr_SetString(PyExc_NotImplementedError, ""); \
937 PyObject*& meth = *pmeth; \
938 CPYCPPYY_STUB_BODY(name, op) \
941#define CPYCPPYY_UNARY_OPERATOR(name, op, label) \
942static PyObject* op_##name##_stub(PyObject* pyobj) \
945 PyCallable* pyfunc = Utility::FindUnaryOperator((PyObject*)Py_TYPE(pyobj), #op);\
946 if (pyfunc && Utility::AddToClass((PyObject*)Py_TYPE(pyobj), #label, pyfunc))\
947 return PyObject_CallMethod(pyobj, (char*)#label, nullptr); \
948 PyErr_SetString(PyExc_NotImplementedError, ""); \
965#
if PY_VERSION_HEX < 0x03000000
981#if PY_VERSION_HEX < 0x03000000
987#if PY_VERSION_HEX < 0x03000000
994#if PY_VERSION_HEX < 0x03000000
1004#if PY_VERSION_HEX >= 0x02020000
1006#if PY_VERSION_HEX < 0x03000000
1014#
if PY_VERSION_HEX >= 0x02050000
1017#
if PY_VERSION_HEX >= 0x03050000
1027 (
char*)
"cppyy.CPPInstance",
1045 Py_TPFLAGS_DEFAULT |
1046 Py_TPFLAGS_BASETYPE |
1047 Py_TPFLAGS_CHECKTYPES
1048#if PY_VERSION_HEX >= 0x03120000
1049 | Py_TPFLAGS_MANAGED_DICT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_MANAGED_WEAKREF
1052 (
char*)
"cppyy object proxy (internal)",
1077#if PY_VERSION_HEX >= 0x02030000
1080#if PY_VERSION_HEX >= 0x02060000
1083#if PY_VERSION_HEX >= 0x03040000
1086#if PY_VERSION_HEX >= 0x03080000
1089#if PY_VERSION_HEX >= 0x030c0000
#define CPYCPPYY_UNARY_OPERATOR(name, op, label)
#define EXT_OBJECT(pyobj)
#define SMART_TYPE(pyobj)
#define CPYCPPYY_OPERATOR_STUB(name, op, ometh)
#define DATA_CACHE(pyobj)
#define DISPATCHPTR(pyobj)
#define CPYCPPYY_ORDERED_OPERATOR_STUB(op, ometh, label)
#define CPYCPPYY_ASSOCIATIVE_OPERATOR_STUB(name, op, lmeth, rmeth)
#define CPyCppyy_PyText_AsString
#define CPyCppyy_PyText_FromFormat
#define CPyCppyy_PyText_Type
#define CPyCppyy_PyText_FromString
#define PyVarObject_HEAD_INIT(type, size)
std::ios_base::fmtflags fFlags
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 offset
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 target
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 result
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
#define ARRAY_SIZE(array)
Cppyy::TCppType_t GetSmartIsA() const
void CastToArray(Py_ssize_t sz)
CPPInstance * Copy(void *cppinst, PyTypeObject *target=nullptr)
CI_DatamemberCache_t & GetDatamemberCache()
void SetSmart(PyObject *smart_type)
PyObject_HEAD void * fObject
void * GetExtendedObject()
Cppyy::TCppType_t ObjectIsA(bool check_smart=true) const
void SetDispatchPtr(void *)
Utility::PyOperators * fOperators
static bool RegisterPyObject(CPPInstance *pyobj, void *cppobj)
static bool UnregisterPyObject(CPPInstance *pyobj, PyObject *pyclass)
std::string extract_namespace(const std::string &name)
PyCallable * FindBinaryOperator(PyObject *left, PyObject *right, const char *op, Cppyy::TCppScope_t scope=0)
bool AddToClass(PyObject *pyclass, const char *label, PyCFunction cfunc, int flags=METH_VARARGS)
std::string ClassName(PyObject *pyobj)
CPPOverload * CPPOverload_New(const std::string &name, std::vector< PyCallable * > &methods)
PyTypeObject CPPInstance_Type
static PyObject * op_str_internal(PyObject *pyobj, PyObject *lshift, bool isBound)
static PyObject * op_div_stub(PyObject *left, PyObject *right)
static void ScopeFlagSet(CPPInstance *self, CPPScope::EFlags flag)
static Py_hash_t CPyCppyy_PyLong_AsHash_t(PyObject *obj)
PyObject * CreateScopeProxy(Cppyy::TCppScope_t, const unsigned flags=0)
static int op_nonzero(CPPInstance *self)
static PyObject * op_mul_stub(PyObject *left, PyObject *right)
static PyMethodDef op_methods[]
static PyObject * op_repr(CPPInstance *self)
PyObject * BindCppObjectNoCast(Cppyy::TCppObject_t object, Cppyy::TCppType_t klass, const unsigned flags=0)
static PyObject * op_richcompare(CPPInstance *self, PyObject *other, int op)
std::vector< std::pair< ptrdiff_t, PyObject * > > CI_DatamemberCache_t
static int op_setownership(CPPInstance *pyobj, PyObject *value, void *)
bool CPPScope_Check(T *object)
static PyObject * eqneq_binop(CPPClass *klass, PyObject *self, PyObject *obj, int op)
static PyObject * op_getownership(CPPInstance *pyobj, void *)
static PyObject * op_neg_stub(PyObject *pyobj)
static int op_clear(CPPInstance *pyobj)
static PyObject * op_sub_stub(PyObject *left, PyObject *right)
void op_dealloc_nofree(CPPInstance *)
bool CPPInstance_Check(T *object)
static PyObject * op_getitem(CPPInstance *self, PyObject *pyidx)
static PyObject * op_reshape(CPPInstance *self, PyObject *shape)
static PyGetSetDef op_getset[]
PyObject * gNullPtrObject
PyTypeObject CPPOverload_Type
static PyNumberMethods op_as_number
static void * cast_actual(void *obj)
static PyObject * op_str(CPPInstance *self)
static void op_dealloc(CPPInstance *pyobj)
static PyObject * op_pos_stub(PyObject *pyobj)
static PyObject * op_add_stub(PyObject *left, PyObject *right)
static Py_hash_t op_hash(CPPInstance *self)
static PyObject * op_dispatch(PyObject *self, PyObject *args, PyObject *)
static PyObject * op_invert_stub(PyObject *pyobj)
PyTypeObject CPPScope_Type
static bool ScopeFlagCheck(CPPInstance *self, CPPScope::EFlags flag)
static PyObject * op_get_smartptr(CPPInstance *self)
static CPPInstance * op_new(PyTypeObject *subtype, PyObject *, PyObject *)
static PyObject * op_destruct(CPPInstance *self)
RPY_EXPORTED ptrdiff_t GetBaseOffset(TCppType_t derived, TCppType_t base, TCppObject_t address, int direction, bool rerror=false)
RPY_EXPORTED size_t SizeOf(TCppType_t klass)
RPY_EXPORTED std::string ToString(TCppType_t klass, TCppObject_t obj)
RPY_EXPORTED void CallDestructor(TCppType_t type, TCppObject_t self)
RPY_EXPORTED void Destruct(TCppType_t type, TCppObject_t instance)
RPY_EXPORTED TCppType_t GetActualClass(TCppType_t klass, TCppObject_t obj)
RPY_EXPORTED std::string GetScopedFinalName(TCppType_t type)
RPY_EXPORTED void Deallocate(TCppType_t type, TCppObject_t instance)
RPY_EXPORTED void * CallR(TCppMethod_t method, TCppObject_t self, size_t nargs, void *args)
RPY_EXPORTED TCppScope_t GetScope(const std::string &scope_name)
RPY_EXPORTED std::string GetFinalName(TCppType_t type)