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);
260 int result = PyObject_IsTrue(pylen);
281 PyObject *mname =
nullptr, *sigarg =
nullptr;
282 if (!PyArg_ParseTuple(args,
const_cast<char*
>(
"O!O!:__dispatch__"),
287 PyObject* pymeth = PyObject_GetAttr(self, mname);
292 PyObject* pydisp = PyObject_GetAttrString(pymeth,
const_cast<char*
>(
"__overload__"));
299 PyObject* oload = PyObject_CallFunctionObjArgs(pydisp, sigarg,
nullptr);
333 if (!PyTuple_Check(shape) || PyTuple_GET_SIZE(shape) != 1) {
334 PyErr_SetString(PyExc_TypeError,
"tuple object of size 1 expected");
338 long sz = PyLong_AsLong(PyTuple_GET_ITEM(shape, 0));
340 PyErr_SetString(PyExc_ValueError,
"array length must be positive");
357 PyErr_Format(PyExc_TypeError,
"%s object does not support indexing",
Py_TYPE(self)->tp_name);
364 PyErr_SetString(PyExc_IndexError,
"negative indices not supported for array of structs");
370 if (0 <= maxidx && maxidx <= idx) {
371 PyErr_SetString(PyExc_IndexError,
"index out of range");
376 unsigned flags = 0;
size_t sz =
sizeof(
void*);
384 void* indexed_obj = (
void*)(address+(uintptr_t)(idx*sz));
406 {(
char*)
"__destruct__", (PyCFunction)
op_destruct, METH_NOARGS,
407 (
char*)
"call the C++ destructor"},
408 {(
char*)
"__dispatch__", (PyCFunction)
op_dispatch, METH_VARARGS,
409 (
char*)
"dispatch to selected overload"},
411 (
char*)
"get associated smart pointer, if any"},
412 {(
char*)
"__reshape__", (PyCFunction)
op_reshape, METH_O,
413 (
char*)
"cast pointer to 1D array type"},
414 {(
char*)
nullptr,
nullptr, 0,
nullptr}
433 PyObject_GC_UnTrack((
PyObject*)pyobj);
452 using namespace Utility;
456 void* rawcpp = ((
CPPInstance*)self)->GetObjectRaw();
475 const char* cppop = op == Py_EQ ?
"==" :
"!=";
476 PyCallable* pyfunc = FindBinaryOperator(self, obj, cppop);
487 if (binop == Py_None) {
489 if (binop && binop != Py_None) flipit =
true;
492 if (!binop || binop == Py_None)
return nullptr;
495 Py_INCREF(obj); PyTuple_SET_ITEM(args, 0, obj);
508 if (!flipit)
return result;
510 int istrue = PyObject_IsTrue(
result);
525 if (clActual && clActual != klass) {
527 clActual, klass, address, -1 ,
true );
528 if (
offset != -1) address = (
void*)((intptr_t)address +
offset);
535#define CPYCPPYY_ORDERED_OPERATOR_STUB(op, ometh, label) \
537 PyCallable* pyfunc = Utility::FindBinaryOperator((PyObject*)self, other, #op);\
539 ometh = (PyObject*)CPPOverload_New(#label, pyfunc); \
556 if (op == Py_EQ || op == Py_NE) {
574 self->GetObject() == ((
CPPInstance*)other)->GetObject())) {
581 bIsEq = addr1 && addr2 && (addr1 == addr2);
584 if ((op == Py_EQ && bIsEq) || (op == Py_NE && !bIsEq))
591 else if (op == Py_LT || op == Py_LE || op == Py_GT || op == Py_GE) {
612 PyErr_SetString(PyExc_NotImplementedError,
"");
616 return PyObject_CallFunctionObjArgs(meth, (
PyObject*)self, other,
nullptr);
619 Py_INCREF(Py_NotImplemented);
620 return Py_NotImplemented;
630 return PyBaseObject_Type.tp_repr((
PyObject*)self);
644 const_cast<char*
>(
"<%s.%s object at %p held by %s at %p>"),
661 if (
sizeof(
unsigned long) >=
sizeof(
size_t))
662 return (
Py_hash_t)PyLong_AsUnsignedLong(obj);
663 return (
Py_hash_t)PyLong_AsUnsignedLongLong(obj);
684 bool isValid = PyMapping_HasKeyString(dct, (
char*)
"__call__");
687 PyObject* hashobj = PyObject_CallObject(hashcls,
nullptr);
693 PyObject* hashval = PyObject_CallFunctionObjArgs(hashobj, (
PyObject*)self,
nullptr);
704 ((PyTypeObject*)
Py_TYPE(self))->tp_hash = PyBaseObject_Type.tp_hash;
705 return PyBaseObject_Type.tp_hash((
PyObject*)self);
712 std::ostringstream s;
715#if PY_VERSION_HEX >= 0x03000000
723 if (isBound) res = PyObject_CallFunctionObjArgs(lshift, pys, NULL);
724 else res = PyObject_CallFunctionObjArgs(lshift, pys, pyobj, NULL);
727#if PY_VERSION_HEX >= 0x03000000
768 else if (pyname == (
PyObject*)0x01) {
781 }
else if (pyname == (
PyObject*)0x02) {
790 PyObject* lshift = PyObject_GetAttr(
809 static PyObject* printValue =
nullptr;
811 PyObject* gbl = PyDict_GetItemString(PySys_GetObject((
char*)
"modules"),
"cppyy.gbl");
812 PyObject* cl = PyObject_GetAttrString(gbl, (
char*)
"cling");
813 printValue = PyObject_GetAttrString(cl, (
char*)
"printValue");
817 Py_DECREF(printValue);
818 if (!PyCallable_Check(printValue))
819 printValue =
nullptr;
839 PyObject* OL = PyObject_GetItem(printValue, clName);
842 PyObject* pretty = OL ? PyObject_CallFunctionObjArgs(OL, byref,
nullptr) :
nullptr;
848 const std::string& pv = *(std::string*)((
CPPInstance*)pretty)->GetObject();
849 if (!pv.empty() && pv.find(
"@0x") == std::string::npos)
876 long shouldown = PyLong_AsLong(
value);
877 if (shouldown == -1 && PyErr_Occurred()) {
878 PyErr_SetString(PyExc_ValueError,
"__python_owns__ should be either True or False");
891 (
char*)
"If true, python manages the life time of this object",
nullptr},
892 {(
char*)
nullptr,
nullptr,
nullptr,
nullptr,
nullptr}
897#define CPYCPPYY_STUB_BODY(name, op) \
898 bool previously_resolved_overload = (bool)meth; \
901 PyCallable* pyfunc = Utility::FindBinaryOperator(left, right, #op); \
902 if (pyfunc) meth = (PyObject*)CPPOverload_New(#name, pyfunc); \
904 PyErr_SetString(PyExc_NotImplementedError, ""); \
908 PyObject* res = PyObject_CallFunctionObjArgs(meth, cppobj, other, nullptr);\
909 if (!res && previously_resolved_overload) { \
912 PyCallable* pyfunc = Utility::FindBinaryOperator(left, right, #op); \
913 if (pyfunc) ((CPPOverload*&)meth)->AdoptMethod(pyfunc); \
915 PyErr_SetString(PyExc_NotImplementedError, ""); \
919 res = PyObject_CallFunctionObjArgs(meth, cppobj, other, nullptr); \
924#define CPYCPPYY_OPERATOR_STUB(name, op, ometh) \
925static PyObject* op_##name##_stub(PyObject* left, PyObject* right) \
928 CPPClass* klass = (CPPClass*)Py_TYPE(left); \
929 if (!klass->fOperators) klass->fOperators = new Utility::PyOperators{}; \
930 PyObject*& meth = ometh; \
931 PyObject *cppobj = left, *other = right; \
932 CPYCPPYY_STUB_BODY(name, op) \
935#define CPYCPPYY_ASSOCIATIVE_OPERATOR_STUB(name, op, lmeth, rmeth) \
936static PyObject* op_##name##_stub(PyObject* left, PyObject* right) \
939 CPPClass* klass; PyObject** pmeth; \
940 PyObject *cppobj, *other; \
941 if (CPPInstance_Check(left)) { \
942 klass = (CPPClass*)Py_TYPE(left); \
943 if (!klass->fOperators) klass->fOperators = new Utility::PyOperators{};\
944 pmeth = &lmeth; cppobj = left; other = right; \
945 } else if (CPPInstance_Check(right)) { \
946 klass = (CPPClass*)Py_TYPE(right); \
947 if (!klass->fOperators) klass->fOperators = new Utility::PyOperators{};\
948 pmeth = &rmeth; cppobj = right; other = left; \
950 PyErr_SetString(PyExc_NotImplementedError, ""); \
953 PyObject*& meth = *pmeth; \
954 CPYCPPYY_STUB_BODY(name, op) \
957#define CPYCPPYY_UNARY_OPERATOR(name, op, label) \
958static PyObject* op_##name##_stub(PyObject* pyobj) \
961 PyCallable* pyfunc = Utility::FindUnaryOperator((PyObject*)Py_TYPE(pyobj), #op);\
962 if (pyfunc && Utility::AddToClass((PyObject*)Py_TYPE(pyobj), #label, pyfunc))\
963 return PyObject_CallMethod(pyobj, (char*)#label, nullptr); \
964 PyErr_SetString(PyExc_NotImplementedError, ""); \
981#
if PY_VERSION_HEX < 0x03000000
997#if PY_VERSION_HEX < 0x03000000
1003#if PY_VERSION_HEX < 0x03000000
1010#if PY_VERSION_HEX < 0x03000000
1020#if PY_VERSION_HEX >= 0x02020000
1022#if PY_VERSION_HEX < 0x03000000
1030#
if PY_VERSION_HEX >= 0x02050000
1033#
if PY_VERSION_HEX >= 0x03050000
1043 (
char*)
"cppyy.CPPInstance",
1061 Py_TPFLAGS_DEFAULT |
1062 Py_TPFLAGS_BASETYPE |
1063 Py_TPFLAGS_CHECKTYPES |
1065 (
char*)
"cppyy object proxy (internal)",
1090#
if PY_VERSION_HEX >= 0x02030000
1093#
if PY_VERSION_HEX >= 0x02060000
1096#
if PY_VERSION_HEX >= 0x03040000
1099#
if PY_VERSION_HEX >= 0x03080000
1102#
if PY_VERSION_HEX >= 0x030c0000
1105#
if PY_VERSION_HEX >= 0x030d0000
#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 PySequenceMethods op_as_sequence
static PyMethodDef op_methods[]
static PyObject * op_repr(CPPInstance *self)
static PyObject * op_item(CPPInstance *self, Py_ssize_t idx)
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_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 int op_traverse(CPPInstance *, visitproc, void *)
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)