Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
CPyCppyyModule.cxx
Go to the documentation of this file.
1// Bindings
2#include "CPyCppyy.h"
3#include "CallContext.h"
4#include "Converters.h"
5#include "CPPDataMember.h"
6#include "CPPExcInstance.h"
7#include "CPPInstance.h"
8#include "CPPOverload.h"
9#include "CPPScope.h"
10#include "CustomPyTypes.h"
11#include "LowLevelViews.h"
12#include "MemoryRegulator.h"
13#include "ProxyWrappers.h"
14#include "PyStrings.h"
15#include "TemplateProxy.h"
16#include "TupleOfInstances.h"
17#include "Utility.h"
18
19#define CPYCPPYY_INTERNAL 1
21namespace CPyCppyy {
22void* Instance_AsVoidPtr(PyObject* pyobject);
24 void* addr, const std::string& classname, bool python_owns = false);
25} // namespace CPyCppyy
26#undef CPYCPPYY_INTERNAL
27
28// Standard
29#include <algorithm>
30#include <map>
31#include <set>
32#include <string>
33#include <iostream>
34#include <sstream>
35#include <utility>
36#include <vector>
37
38
39// Note: as of py3.11, dictionary objects no longer carry a function pointer for
40// the lookup, so it can no longer be shimmed and "from cppyy.interactive import *"
41// thus no longer works.
42#if PY_VERSION_HEX < 0x030b0000
43
44//- from Python's dictobject.c -------------------------------------------------
45#if PY_VERSION_HEX >= 0x03030000
46 typedef struct PyDictKeyEntry {
47 /* Cached hash code of me_key. */
48 Py_hash_t me_hash;
49 PyObject *me_key;
50 PyObject *me_value; /* This field is only meaningful for combined tables */
51 } PyDictEntry;
52
53 typedef struct _dictkeysobject {
54 Py_ssize_t dk_refcnt;
55 Py_ssize_t dk_size;
56 dict_lookup_func dk_lookup;
57 Py_ssize_t dk_usable;
58#if PY_VERSION_HEX >= 0x03060000
59 Py_ssize_t dk_nentries;
60 union {
61 int8_t as_1[8];
62 int16_t as_2[4];
63 int32_t as_4[2];
64#if SIZEOF_VOID_P > 4
65 int64_t as_8[1];
66#endif
67 } dk_indices;
68#else
69 PyDictKeyEntry dk_entries[1];
70#endif
71 } PyDictKeysObject;
72
73#define CPYCPPYY_GET_DICT_LOOKUP(mp) \
74 ((dict_lookup_func&)mp->ma_keys->dk_lookup)
75
76#else
77
78#define CPYCPPYY_GET_DICT_LOOKUP(mp) \
79 ((dict_lookup_func&)mp->ma_lookup)
80
81#endif
82
83#endif // PY_VERSION_HEX < 0x030b0000
84
85//- data -----------------------------------------------------------------------
87{
88 return CPyCppyy_PyText_FromString("nullptr");
89}
90
92{
93 Py_FatalError("deallocating nullptr");
94}
95
97{
98 return 0;
99}
100
101static PyNumberMethods nullptr_as_number = {
102 0, 0, 0,
103#if PY_VERSION_HEX < 0x03000000
104 0,
105#endif
106 0, 0, 0, 0, 0, 0,
107 (inquiry)nullptr_nonzero, // tp_nonzero (nb_bool in p3)
108 0, 0, 0, 0, 0, 0,
109#if PY_VERSION_HEX < 0x03000000
110 0, // nb_coerce
111#endif
112 0, 0, 0,
113#if PY_VERSION_HEX < 0x03000000
114 0, 0,
115#endif
116 0, 0, 0,
117#if PY_VERSION_HEX < 0x03000000
118 0, // nb_inplace_divide
119#endif
120 0, 0, 0, 0, 0, 0, 0
121#if PY_VERSION_HEX >= 0x02020000
122 , 0 // nb_floor_divide
123#if PY_VERSION_HEX < 0x03000000
124 , 0 // nb_true_divide
125#else
126 , 0 // nb_true_divide
127#endif
128 , 0, 0
129#endif
130#if PY_VERSION_HEX >= 0x02050000
131 , 0 // nb_index
132#endif
133#if PY_VERSION_HEX >= 0x03050000
134 , 0 // nb_matrix_multiply
135 , 0 // nb_inplace_matrix_multiply
136#endif
137};
138
139static PyTypeObject PyNullPtr_t_Type = {
140 PyVarObject_HEAD_INIT(&PyType_Type, 0)
141 "nullptr_t", // tp_name
142 sizeof(PyObject), // tp_basicsize
143 0, // tp_itemsize
144 nullptr_dealloc, // tp_dealloc (never called)
145 0, 0, 0, 0,
146 nullptr_repr, // tp_repr
147 &nullptr_as_number, // tp_as_number
148 0, 0,
149 (hashfunc)_Py_HashPointer, // tp_hash
150 0, 0, 0, 0, 0, Py_TPFLAGS_DEFAULT, 0, 0, 0, 0, 0, 0, 0,
151 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
152#if PY_VERSION_HEX >= 0x02030000
153 , 0 // tp_del
154#endif
155#if PY_VERSION_HEX >= 0x02060000
156 , 0 // tp_version_tag
157#endif
158#if PY_VERSION_HEX >= 0x03040000
159 , 0 // tp_finalize
160#endif
161#if PY_VERSION_HEX >= 0x03080000
162 , 0 // tp_vectorcall
163#endif
164#if PY_VERSION_HEX >= 0x030c0000
165 , 0 // tp_watched
166#endif
167};
168
169
171{
172 return CPyCppyy_PyText_FromString("type default");
173}
174
176{
177 Py_FatalError("deallocating default");
178}
179
180static PyTypeObject PyDefault_t_Type = {
181 PyVarObject_HEAD_INIT(&PyType_Type, 0)
182 "default_t", // tp_name
183 sizeof(PyObject), // tp_basicsize
184 0, // tp_itemsize
185 default_dealloc, // tp_dealloc (never called)
186 0, 0, 0, 0,
187 default_repr, // tp_repr
188 0, 0, 0,
189 (hashfunc)_Py_HashPointer, // tp_hash
190 0, 0, 0, 0, 0, Py_TPFLAGS_DEFAULT, 0, 0, 0, 0, 0, 0, 0,
191 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
192#if PY_VERSION_HEX >= 0x02030000
193 , 0 // tp_del
194#endif
195#if PY_VERSION_HEX >= 0x02060000
196 , 0 // tp_version_tag
197#endif
198#if PY_VERSION_HEX >= 0x03040000
199 , 0 // tp_finalize
200#endif
201#if PY_VERSION_HEX >= 0x03080000
202 , 0 // tp_vectorcall
203#endif
204#if PY_VERSION_HEX >= 0x030c0000
205 , 0 // tp_watched
206#endif
207};
208
209namespace {
210
211PyObject _CPyCppyy_NullPtrStruct = {
212 _PyObject_EXTRA_INIT
214};
215
216PyObject _CPyCppyy_DefaultStruct = {
217 _PyObject_EXTRA_INIT
219};
220
221// TODO: refactor with Converters.cxx
222struct CPyCppyy_tagCDataObject { // non-public (but stable)
223 PyObject_HEAD
224 char* b_ptr;
225 int b_needsfree;
226};
227
228} // unnamed namespace
229
230namespace CPyCppyy {
231 PyObject* gThisModule = nullptr;
233 PyObject* gNullPtrObject = nullptr;
239 std::map<std::string, std::vector<PyObject*>> gPythonizations;
240 std::set<Cppyy::TCppType_t> gPinnedTypes;
241 std::ostringstream gCapturedError;
242 std::streambuf* gOldErrorBuffer = nullptr;
243}
244
245
246//- private helpers ------------------------------------------------------------
247namespace {
248
249using namespace CPyCppyy;
250
251
252//----------------------------------------------------------------------------
253#if PY_VERSION_HEX < 0x030b0000
254namespace {
255
256class GblGetter {
257public:
258 GblGetter() {
259 PyObject* cppyy = PyImport_AddModule((char*)"cppyy");
260 fGbl = PyObject_GetAttrString(cppyy, (char*)"gbl");
261 }
262 ~GblGetter() { Py_DECREF(fGbl); }
263
264 PyObject* operator*() { return fGbl; }
265
266private:
267 PyObject* fGbl;
268};
269
270} // unnamed namespace
271
272#if PY_VERSION_HEX >= 0x03060000
273inline Py_ssize_t OrgDictLookup(PyDictObject* mp, PyObject* key,
274 Py_hash_t hash, PyObject*** value_addr, Py_ssize_t* hashpos)
275{
276 return (*gDictLookupOrg)(mp, key, hash, value_addr, hashpos);
277}
278#define CPYCPPYY_ORGDICT_LOOKUP(mp, key, hash, value_addr, hashpos) \
279 OrgDictLookup(mp, key, hash, value_addr, hashpos)
280
281Py_ssize_t CPyCppyyLookDictString(PyDictObject* mp, PyObject* key,
282 Py_hash_t hash, PyObject*** value_addr, Py_ssize_t* hashpos)
283
284#elif PY_VERSION_HEX >= 0x03030000
285inline PyDictKeyEntry* OrgDictLookup(
286 PyDictObject* mp, PyObject* key, Py_hash_t hash, PyObject*** value_addr)
287{
288 return (*gDictLookupOrg)(mp, key, hash, value_addr);
289}
290
291#define CPYCPPYY_ORGDICT_LOOKUP(mp, key, hash, value_addr, hashpos) \
292 OrgDictLookup(mp, key, hash, value_addr)
293
294PyDictKeyEntry* CPyCppyyLookDictString(
295 PyDictObject* mp, PyObject* key, Py_hash_t hash, PyObject*** value_addr)
296
297#else /* < 3.3 */
298
299inline PyDictEntry* OrgDictLookup(PyDictObject* mp, PyObject* key, long hash)
300{
301 return (*gDictLookupOrg)(mp, key, hash);
302}
303
304#define CPYCPPYY_ORGDICT_LOOKUP(mp, key, hash, value_addr, hashpos) \
305 OrgDictLookup(mp, key, hash)
306
307PyDictEntry* CPyCppyyLookDictString(PyDictObject* mp, PyObject* key, long hash)
308#endif
309{
310 static GblGetter gbl;
311#if PY_VERSION_HEX >= 0x03060000
312 Py_ssize_t ep;
313#else
314 PyDictEntry* ep;
315#endif
316
317// first search dictionary itself
318 ep = CPYCPPYY_ORGDICT_LOOKUP(mp, key, hash, value_addr, hashpos);
320 return ep;
321
322#if PY_VERSION_HEX >= 0x03060000
323 if (ep >= 0)
324#else
325 if (!ep || (ep->me_key && ep->me_value))
326#endif
327 return ep;
328
329// filter for builtins
330 if (PyDict_GetItem(PyEval_GetBuiltins(), key) != 0)
331 return ep;
332
333// normal lookup failed, attempt to get C++ enum/global/class from top-level
334 gDictLookupActive = true;
335
336// attempt to get C++ enum/global/class from top-level
337 PyObject* val = PyObject_GetAttr(*gbl, key);
338
339 if (val) {
340 // success ...
341
342 if (CPPDataMember_CheckExact(val)) {
343 // don't want to add to dictionary (the proper place would be the
344 // dictionary of the (meta)class), but modifying ep will be noticed no
345 // matter what; just return the actual value and live with the copy in
346 // the dictionary (mostly, this is correct)
347 PyObject* actual_val = Py_TYPE(val)->tp_descr_get(val, nullptr, nullptr);
348 Py_DECREF(val);
349 val = actual_val;
350 }
351
352 // add reference to C++ entity in the given dictionary
353 CPYCPPYY_GET_DICT_LOOKUP(mp) = gDictLookupOrg; // prevent recursion
354 if (PyDict_SetItem((PyObject*)mp, key, val) == 0) {
355 ep = CPYCPPYY_ORGDICT_LOOKUP(mp, key, hash, value_addr, hashpos);
356 } else {
357#if PY_VERSION_HEX >= 0x03060000
358 ep = -1;
359#else
360 ep->me_key = nullptr;
361 ep->me_value = nullptr;
362#endif
363 }
364 CPYCPPYY_GET_DICT_LOOKUP(mp) = CPyCppyyLookDictString; // restore
365
366 // done with val
367 Py_DECREF(val);
368 } else
369 PyErr_Clear();
370
371#if PY_VERSION_HEX >= 0x03030000
372 if (mp->ma_keys->dk_usable <= 0) {
373 // big risk that this lookup will result in a resize, so force it here
374 // to be able to reset the lookup function; of course, this is nowhere
375 // near fool-proof, but should cover interactive usage ...
377 const int maxinsert = 5;
378 PyObject* buf[maxinsert];
379 for (int varmax = 1; varmax <= maxinsert; ++varmax) {
380 for (int ivar = 0; ivar < varmax; ++ivar) {
381 buf[ivar] = CPyCppyy_PyText_FromFormat("__CPYCPPYY_FORCE_RESIZE_%d", ivar);
382 PyDict_SetItem((PyObject*)mp, buf[ivar], Py_None);
383 }
384 for (int ivar = 0; ivar < varmax; ++ivar) {
385 PyDict_DelItem((PyObject*)mp, buf[ivar]);
386 Py_DECREF(buf[ivar]);
387 }
388 if (0 < mp->ma_keys->dk_usable)
389 break;
390 }
391
392 // make sure the entry pointer is still valid by re-doing the lookup
393 ep = CPYCPPYY_ORGDICT_LOOKUP(mp, key, hash, value_addr, hashpos);
394
395 // full reset of all lookup functions
397 CPYCPPYY_GET_DICT_LOOKUP(mp) = CPyCppyyLookDictString; // restore
398 }
399#endif
400
401// stopped calling into the reflection system
402 gDictLookupActive = false;
403 return ep;
404}
405
406#endif // PY_VERSION_HEX < 0x030b0000
407
408//----------------------------------------------------------------------------
409static PyObject* SetCppLazyLookup(PyObject*, PyObject* args)
410{
411#if PY_VERSION_HEX < 0x030b0000
412// Modify the given dictionary to install the lookup function that also
413// tries the global C++ namespace before failing. Called on a module's dictionary,
414// this allows for lazy lookups. This works fine for p3.2 and earlier, but should
415// not be used beyond interactive code for p3.3 and later b/c resizing causes the
416// lookup function to revert to the default (lookdict_unicode_nodummy).
417 PyDictObject* dict = nullptr;
418 if (!PyArg_ParseTuple(args, const_cast<char*>("O!"), &PyDict_Type, &dict))
419 return nullptr;
420
421 CPYCPPYY_GET_DICT_LOOKUP(dict) = CPyCppyyLookDictString;
422#else
423// As of py3.11, there is no longer a lookup function pointer in the dict object
424// to replace. Since this feature is not widely advertised, it's simply dropped
425 PyErr_Warn(PyExc_RuntimeWarning, (char*)"lazy lookup is no longer supported");
426 (void)args; // avoid warning about unused parameter
427#endif
428
430}
431
432//----------------------------------------------------------------------------
433static PyObject* MakeCppTemplateClass(PyObject*, PyObject* args)
434{
435// Create a binding for a templated class instantiation.
436
437// args is class name + template arguments; build full instantiation
438 Py_ssize_t nArgs = PyTuple_GET_SIZE(args);
439 if (nArgs < 2) {
440 PyErr_Format(PyExc_TypeError, "too few arguments for template instantiation");
441 return nullptr;
442 }
443
444// build "< type, type, ... >" part of class name (modifies pyname)
445 const std::string& tmpl_name =
446 Utility::ConstructTemplateArgs(PyTuple_GET_ITEM(args, 0), args, nullptr, Utility::kNone, 1);
447 if (!tmpl_name.size())
448 return nullptr;
449
450 return CreateScopeProxy(tmpl_name);
451}
452
453//----------------------------------------------------------------------------
454static char* GCIA_kwlist[] = {(char*)"instance", (char*)"field", (char*)"byref", NULL};
455static void* GetCPPInstanceAddress(const char* fname, PyObject* args, PyObject* kwds)
456{
457// Helper to get the address (address-of-address) of various object proxy types.
458 CPPInstance* pyobj = 0; PyObject* pyname = 0; int byref = 0;
459 if (PyArg_ParseTupleAndKeywords(args, kwds, const_cast<char*>("O|O!b"), GCIA_kwlist,
460 &pyobj, &CPyCppyy_PyText_Type, &pyname, &byref)) {
461
462 if (CPPInstance_Check(pyobj)) {
463 if (pyname != 0) {
464 // locate property proxy for offset info
465 CPPDataMember* pyprop = nullptr;
466
467 PyObject* pyclass = (PyObject*)Py_TYPE((PyObject*)pyobj);
468 PyObject* dict = PyObject_GetAttr(pyclass, PyStrings::gDict);
469 pyprop = (CPPDataMember*)PyObject_GetItem(dict, pyname);
470 Py_DECREF(dict);
471
472 if (CPPDataMember_Check(pyprop)) {
473 // this is an address of a value (i.e. &myobj->prop)
474 void* addr = (void*)pyprop->GetAddress(pyobj);
475 Py_DECREF(pyprop);
476 return addr;
477 }
478
479 Py_XDECREF(pyprop);
480
481 PyErr_Format(PyExc_TypeError,
482 "%s is not a valid data member", CPyCppyy_PyText_AsString(pyname));
483 return nullptr;
484 }
485
486 // this is an address of an address (i.e. &myobj, with myobj of type MyObj*)
487 // note that the return result may be null
488 if (!byref) return ((CPPInstance*)pyobj)->GetObject();
489 return &((CPPInstance*)pyobj)->GetObjectRaw();
490
491 } else if (CPyCppyy_PyText_Check(pyobj)) {
492 // special cases for access to the CPyCppyy API
493 std::string req = CPyCppyy_PyText_AsString((PyObject*)pyobj);
494 if (req == "Instance_AsVoidPtr")
495 return (void*)&Instance_AsVoidPtr;
496 else if (req == "Instance_FromVoidPtr")
497 return (void*)&Instance_FromVoidPtr;
498 }
499 }
500
501 if (!PyErr_Occurred())
502 PyErr_Format(PyExc_ValueError, "invalid argument for %s", fname);
503 return nullptr;
504}
505
506//----------------------------------------------------------------------------
507static PyObject* addressof(PyObject* /* dummy */, PyObject* args, PyObject* kwds)
508{
509// Return object proxy address as a value (cppyy-style), or the same for an array.
510 void* addr = GetCPPInstanceAddress("addressof", args, kwds);
511 if (addr)
512 return PyLong_FromLongLong((intptr_t)addr);
513 else if (!PyErr_Occurred()) {
514 return PyLong_FromLong(0);
515 } else if (PyTuple_CheckExact(args) && PyTuple_GET_SIZE(args) == 1) {
516 PyErr_Clear();
517 PyObject* arg0 = PyTuple_GET_ITEM(args, 0);
518
519 // nullptr special case
520 if (arg0 == gNullPtrObject || (PyInt_Check(arg0) && PyInt_AsLong(arg0) == 0))
521 return PyLong_FromLong(0);
522
523 // overload if unambiguous
524 if (CPPOverload_CheckExact(arg0)) {
525 const auto& methods = ((CPPOverload*)arg0)->fMethodInfo->fMethods;
526 if (methods.size() != 1) {
527 PyErr_SetString(PyExc_TypeError, "overload is not unambiguous");
528 return nullptr;
529 }
530
531 Cppyy::TCppFuncAddr_t caddr = methods[0]->GetFunctionAddress();
532 return PyLong_FromLongLong((intptr_t)caddr);
533 }
534
535 // C functions (incl. ourselves)
536 if (PyCFunction_Check(arg0)) {
537 void* caddr = (void*)PyCFunction_GetFunction(arg0);
538 return PyLong_FromLongLong((intptr_t)caddr);
539 }
540
541 // final attempt: any type of buffer
542 Utility::GetBuffer(arg0, '*', 1, addr, false);
543 if (addr) return PyLong_FromLongLong((intptr_t)addr);
544 }
545
546// error message if not already set
547 if (!PyErr_Occurred()) {
548 if (PyTuple_CheckExact(args) && PyTuple_GET_SIZE(args)) {
549 PyObject* str = PyObject_Str(PyTuple_GET_ITEM(args, 0));
550 if (str && CPyCppyy_PyText_Check(str))
551 PyErr_Format(PyExc_TypeError, "unknown object %s", CPyCppyy_PyText_AsString(str));
552 else
553 PyErr_Format(PyExc_TypeError, "unknown object at %p", (void*)PyTuple_GET_ITEM(args, 0));
554 Py_XDECREF(str);
555 }
556 }
557 return nullptr;
558}
559
560//----------------------------------------------------------------------------
561static PyObject* AsCObject(PyObject* /* unused */, PyObject* args, PyObject* kwds)
562{
563// Return object proxy as an opaque CObject.
564 void* addr = GetCPPInstanceAddress("as_cobject", args, kwds);
565 if (addr)
566 return CPyCppyy_PyCapsule_New((void*)addr, nullptr, nullptr);
567 return nullptr;
568}
569
570//----------------------------------------------------------------------------
571static PyObject* AsCapsule(PyObject* /* unused */, PyObject* args, PyObject* kwds)
572{
573// Return object proxy as an opaque PyCapsule.
574 void* addr = GetCPPInstanceAddress("as_capsule", args, kwds);
575 if (addr)
576#if PY_VERSION_HEX < 0x02060000
577 return PyCObject_FromVoidPtr(addr, nullptr);
578#else
579 return PyCapsule_New(addr, nullptr, nullptr);
580#endif
581 return nullptr;
582}
583
584//----------------------------------------------------------------------------
585static PyObject* AsCTypes(PyObject* /* unused */, PyObject* args, PyObject* kwds)
586{
587// Return object proxy as a ctypes c_void_p
588 void* addr = GetCPPInstanceAddress("as_ctypes", args, kwds);
589 if (!addr)
590 return nullptr;
591
592// TODO: refactor code below with converters code
593 static PyTypeObject* ct_cvoidp = nullptr;
594 if (!ct_cvoidp) {
595 PyObject* ctmod = PyImport_ImportModule("ctypes"); // ref-count kept
596 if (!ctmod) return nullptr;
597
598 ct_cvoidp = (PyTypeObject*)PyObject_GetAttrString(ctmod, "c_void_p");
599 Py_DECREF(ctmod);
600 if (!ct_cvoidp) return nullptr;
601 Py_DECREF(ct_cvoidp); // module keeps a reference
602 }
603
604 PyObject* ref = ct_cvoidp->tp_new(ct_cvoidp, nullptr, nullptr);
605 *(void**)((CPyCppyy_tagCDataObject*)ref)->b_ptr = addr;
606 ((CPyCppyy_tagCDataObject*)ref)->b_needsfree = 0;
607 return ref;
608}
609
610//----------------------------------------------------------------------------
611static PyObject* AsMemoryView(PyObject* /* unused */, PyObject* pyobject)
612{
613// Return a raw memory view on arrays of PODs.
614 if (!CPPInstance_Check(pyobject)) {
615 PyErr_SetString(PyExc_TypeError, "C++ object proxy expected");
616 return nullptr;
617 }
618
619 CPPInstance* pyobj = (CPPInstance*)pyobject;
620 Cppyy::TCppType_t klass = ((CPPClass*)Py_TYPE(pyobject))->fCppType;
621
622 Py_ssize_t array_len = pyobj->ArrayLength();
623
624 if (array_len < 0 || !Cppyy::IsAggregate(klass)) {
625 PyErr_SetString(
626 PyExc_TypeError, "object is not a proxy to an array of PODs of known size");
627 return nullptr;
628 }
629
630 Py_buffer view;
631
632 view.obj = pyobject;
633 view.buf = pyobj->GetObject();
634 view.itemsize = Cppyy::SizeOf(klass);
635 view.len = view.itemsize * array_len;
636 view.readonly = 0;
637 view.format = NULL; // i.e. "B" assumed
638 view.ndim = 1;
639 view.shape = NULL;
640 view.strides = NULL;
641 view.suboffsets = NULL;
642 view.internal = NULL;
643
644 return PyMemoryView_FromBuffer(&view);
645}
646
647//----------------------------------------------------------------------------
648static PyObject* BindObject(PyObject*, PyObject* args, PyObject* kwds)
649{
650// From a long representing an address or a PyCapsule/CObject, bind to a class.
651 Py_ssize_t argc = PyTuple_GET_SIZE(args);
652 if (argc != 2) {
653 PyErr_Format(PyExc_TypeError,
654 "bind_object takes 2 positional arguments but (" PY_SSIZE_T_FORMAT " were given)", argc);
655 return nullptr;
656 }
657
658// convert 2nd argument first (used for both pointer value and instance cases)
659 Cppyy::TCppType_t cast_type = 0;
660 PyObject* arg1 = PyTuple_GET_ITEM(args, 1);
661 if (!CPyCppyy_PyText_Check(arg1)) { // not string, then class
662 if (CPPScope_Check(arg1))
663 cast_type = ((CPPClass*)arg1)->fCppType;
664 else
665 arg1 = PyObject_GetAttr(arg1, PyStrings::gName);
666 } else
667 Py_INCREF(arg1);
668
669 if (!cast_type && arg1) {
671 Py_DECREF(arg1);
672 }
673
674 if (!cast_type) {
675 PyErr_SetString(PyExc_TypeError,
676 "bind_object expects a valid class or class name as an argument");
677 return nullptr;
678 }
679
680// next, convert the first argument, some pointer value or a pre-existing instance
681 PyObject* arg0 = PyTuple_GET_ITEM(args, 0);
682
683 if (CPPInstance_Check(arg0)) {
684 // if this instance's class has a relation to the requested one, calculate the
685 // offset, erase if from any caches, and update the pointer and type
686 CPPInstance* arg0_pyobj = (CPPInstance*)arg0;
687 Cppyy::TCppType_t cur_type = arg0_pyobj->ObjectIsA(false /* check_smart */);
688
689 bool isPython = CPPScope_Check(arg1) && \
690 (((CPPClass*)arg1)->fFlags & CPPScope::kIsPython);
691
692 if (cur_type == cast_type && !isPython) {
693 Py_INCREF(arg0); // nothing to do
694 return arg0;
695 }
696
697 int direction = 0;
698 Cppyy::TCppType_t base = 0, derived = 0;
699 if (Cppyy::IsSubtype(cast_type, cur_type)) {
700 derived = cast_type;
701 base = cur_type;
702 direction = -1; // down-cast
703 } else if (Cppyy::IsSubtype(cur_type, cast_type)) {
704 base = cast_type;
705 derived = cur_type;
706 direction = 1; // up-cast
707 } else {
708 PyErr_SetString(PyExc_TypeError,
709 "provided instance and provided target type are unrelated");
710 return nullptr;
711 }
712
713 Cppyy::TCppObject_t address = (Cppyy::TCppObject_t)arg0_pyobj->GetObject();
714 ptrdiff_t offset = Cppyy::GetBaseOffset(derived, base, address, direction);
715
716 // it's debatable whether a new proxy should be created rather than updating
717 // the old, but changing the old object would be changing the behavior of all
718 // code that has a reference to it, which may not be the intention if the cast
719 // is on a C++ data member; this probably is the "least surprise" option
720
721 // ownership is taken over as needed, again following the principle of "least
722 // surprise" as most likely only the cast object will be retained
723 bool owns = arg0_pyobj->fFlags & CPPInstance::kIsOwner;
724
725 if (!isPython) {
726 // ordinary C++ class
728 (void*)((intptr_t)address + offset), cast_type, owns ? CPPInstance::kIsOwner : 0);
729 if (owns && pyobj) arg0_pyobj->CppOwns();
730 return pyobj;
731
732 } else {
733 // rebinding to a Python-side class, create a fresh instance first to be able to
734 // perform a lookup of the original dispatch object and if found, return original
735 void* cast_address = (void*)((intptr_t)address + offset);
736 PyObject* pyobj = ((PyTypeObject*)arg1)->tp_new((PyTypeObject*)arg1, nullptr, nullptr);
737 ((CPPInstance*)pyobj)->GetObjectRaw() = cast_address;
738
739 PyObject* dispproxy = CPyCppyy::GetScopeProxy(cast_type);
740 PyObject* res = PyObject_CallMethodOneArg(dispproxy, PyStrings::gDispGet, pyobj);
741 /* Note: the resultant object is borrowed */
742 if (CPPInstance_Check(res) && ((CPPInstance*)res)->GetObject() == cast_address) {
743 ((CPPInstance*)pyobj)->CppOwns(); // make sure C++ object isn't deleted
744 Py_DECREF(pyobj); // on DECREF (is default, but still)
745 pyobj = res;
746 } else {
747 if (res) Py_DECREF(res); // most likely Py_None
748 else PyErr_Clear(); // should not happen
749 }
750 Py_DECREF(dispproxy);
751
752 if (pyobj && owns) {
753 arg0_pyobj->CppOwns();
754 ((CPPInstance*)pyobj)->PythonOwns();
755 }
756
757 return pyobj;
758 }
759 }
760
761// not a pre-existing object; get the address and bind
762 void* addr = nullptr;
763 if (arg0 != &_CPyCppyy_NullPtrStruct) {
764 addr = CPyCppyy_PyCapsule_GetPointer(arg0, nullptr);
765 if (PyErr_Occurred()) {
766 PyErr_Clear();
767
768 addr = PyLong_AsVoidPtr(arg0);
769 if (PyErr_Occurred()) {
770 PyErr_Clear();
771
772 // last chance, perhaps it's a buffer/array (return from void*)
773 Py_ssize_t buflen = Utility::GetBuffer(PyTuple_GetItem(args, 0), '*', 1, addr, false);
774 if (!addr || !buflen) {
775 PyErr_SetString(PyExc_TypeError,
776 "bind_object requires a CObject/Capsule, long integer, buffer, or instance as first argument");
777 return nullptr;
778 }
779 }
780 }
781 }
782
783 bool do_cast = false;
784 if (kwds) {
785 PyObject* cast = PyDict_GetItemString(kwds, "cast");
786 do_cast = cast && PyObject_IsTrue(cast);
787 }
788
789 if (do_cast)
790 return BindCppObject(addr, cast_type);
791
792 return BindCppObjectNoCast(addr, cast_type);
793}
794
795//----------------------------------------------------------------------------
796static PyObject* Move(PyObject*, PyObject* pyobject)
797{
798// Prepare the given C++ object for moving.
799 if (!CPPInstance_Check(pyobject)) {
800 PyErr_SetString(PyExc_TypeError, "C++ object expected");
801 return nullptr;
802 }
803
804 ((CPPInstance*)pyobject)->fFlags |= CPPInstance::kIsRValue;
805 Py_INCREF(pyobject);
806 return pyobject;
807}
808
809
810//----------------------------------------------------------------------------
811static PyObject* AddPythonization(PyObject*, PyObject* args)
812{
813// Remove a previously registered pythonizor from the given scope.
814 PyObject* pythonizor = nullptr; const char* scope;
815 if (!PyArg_ParseTuple(args, const_cast<char*>("Os"), &pythonizor, &scope))
816 return nullptr;
817
818 if (!PyCallable_Check(pythonizor)) {
819 PyObject* pystr = PyObject_Str(pythonizor);
820 PyErr_Format(PyExc_TypeError,
821 "given \'%s\' object is not callable", CPyCppyy_PyText_AsString(pystr));
822 Py_DECREF(pystr);
823 return nullptr;
824 }
825
826 Py_INCREF(pythonizor);
827 gPythonizations[scope].push_back(pythonizor);
828
830}
831
832
833//----------------------------------------------------------------------------
834static PyObject* RemovePythonization(PyObject*, PyObject* args)
835{
836// Remove a previously registered pythonizor from the given scope.
837 PyObject* pythonizor = nullptr; const char* scope;
838 if (!PyArg_ParseTuple(args, const_cast<char*>("Os"), &pythonizor, &scope))
839 return nullptr;
840
841 auto p1 = gPythonizations.find(scope);
842 if (p1 != gPythonizations.end()) {
843 auto p2 = std::find(p1->second.begin(), p1->second.end(), pythonizor);
844 if (p2 != p1->second.end()) {
845 p1->second.erase(p2);
847 }
848 }
849
851}
852
853//----------------------------------------------------------------------------
854static PyObject* PinType(PyObject*, PyObject* pyclass)
855{
856// Add a pinning so that objects of type `derived' are interpreted as
857// objects of type `base'.
858 if (!CPPScope_Check(pyclass)) {
859 PyErr_SetString(PyExc_TypeError, "C++ class expected");
860 return nullptr;
861 }
862
863 gPinnedTypes.insert(((CPPClass*)pyclass)->fCppType);
864
866}
867
868//----------------------------------------------------------------------------
870{
871// Add a type reducer to map type2 to type2 on function returns.
872 const char *reducable, *reduced;
873 if (!PyArg_ParseTuple(args, const_cast<char*>("ss"), &reducable, &reduced))
874 return nullptr;
875
876 Cppyy::AddTypeReducer(reducable, reduced);
877
879}
880
881//----------------------------------------------------------------------------
882static PyObject* SetMemoryPolicy(PyObject*, PyObject* args)
883{
884// Set the global memory policy, which affects object ownership when objects
885// are passed as function arguments.
886 PyObject* policy = nullptr;
887 if (!PyArg_ParseTuple(args, const_cast<char*>("O!"), &PyInt_Type, &policy))
888 return nullptr;
889
890 long old = (long)CallContext::sMemoryPolicy;
891
892 long l = PyInt_AS_LONG(policy);
894 return PyInt_FromLong(old);
895 }
896
897 PyErr_Format(PyExc_ValueError, "Unknown policy %ld", l);
898 return nullptr;
899}
900
901//----------------------------------------------------------------------------
902static PyObject* SetGlobalSignalPolicy(PyObject*, PyObject* args)
903{
904// Set the global signal policy, which determines whether a jmp address
905// should be saved to return to after a C++ segfault.
906 PyObject* setProtected = 0;
907 if (!PyArg_ParseTuple(args, const_cast<char*>("O"), &setProtected))
908 return nullptr;
909
910 if (CallContext::SetGlobalSignalPolicy(PyObject_IsTrue(setProtected))) {
912 }
913
915}
916
917//----------------------------------------------------------------------------
918static PyObject* SetOwnership(PyObject*, PyObject* args)
919{
920// Set the ownership (True is python-owns) for the given object.
921 CPPInstance* pyobj = nullptr; PyObject* pykeep = nullptr;
922 if (!PyArg_ParseTuple(args, const_cast<char*>("O!O!"),
923 &CPPInstance_Type, (void*)&pyobj, &PyInt_Type, &pykeep))
924 return nullptr;
925
926 (bool)PyLong_AsLong(pykeep) ? pyobj->PythonOwns() : pyobj->CppOwns();
927
929}
930
931//----------------------------------------------------------------------------
933{
934// Add a smart pointer to the list of known smart pointer types.
935 const char* type_name;
936 if (!PyArg_ParseTuple(args, const_cast<char*>("s"), &type_name))
937 return nullptr;
938
939 Cppyy::AddSmartPtrType(type_name);
940
942}
943
944//----------------------------------------------------------------------------
945static PyObject* BeginCaptureStderr(PyObject*, PyObject*)
946{
947 gOldErrorBuffer = std::cerr.rdbuf();
948 std::cerr.rdbuf(gCapturedError.rdbuf());
949
951}
952
953//----------------------------------------------------------------------------
954static PyObject* EndCaptureStderr(PyObject*, PyObject*)
955{
956// restore old rdbuf and return captured result
957 std::cerr.rdbuf(gOldErrorBuffer);
958 gOldErrorBuffer = nullptr;
959
960 std::string capturedError = std::move(gCapturedError).str();
961
962 gCapturedError.str("");
963 gCapturedError.clear();
964
965 return Py_BuildValue("s", capturedError.c_str());
966}
967} // unnamed namespace
968
969
970//- data -----------------------------------------------------------------------
971static PyMethodDef gCPyCppyyMethods[] = {
972 {(char*) "CreateScopeProxy", (PyCFunction)CPyCppyy::CreateScopeProxy,
973 METH_VARARGS, (char*)"cppyy internal function"},
974 {(char*) "MakeCppTemplateClass", (PyCFunction)MakeCppTemplateClass,
975 METH_VARARGS, (char*)"cppyy internal function"},
976 {(char*) "_set_cpp_lazy_lookup", (PyCFunction)SetCppLazyLookup,
977 METH_VARARGS, (char*)"cppyy internal function"},
978 {(char*) "_DestroyPyStrings", (PyCFunction)CPyCppyy::DestroyPyStrings,
979 METH_NOARGS, (char*)"cppyy internal function"},
980 {(char*) "addressof", (PyCFunction)addressof,
981 METH_VARARGS | METH_KEYWORDS, (char*)"Retrieve address of proxied object or field as a value."},
982 {(char*) "as_cobject", (PyCFunction)AsCObject,
983 METH_VARARGS | METH_KEYWORDS, (char*)"Retrieve address of proxied object or field in a CObject."},
984 {(char*) "as_capsule", (PyCFunction)AsCapsule,
985 METH_VARARGS | METH_KEYWORDS, (char*)"Retrieve address of proxied object or field in a PyCapsule."},
986 {(char*) "as_ctypes", (PyCFunction)AsCTypes,
987 METH_VARARGS | METH_KEYWORDS, (char*)"Retrieve address of proxied object or field in a ctypes c_void_p."},
988 {(char*) "as_memoryview", (PyCFunction)AsMemoryView,
989 METH_O, (char*)"Represent an array of objects as raw memory."},
990 {(char*)"bind_object", (PyCFunction)BindObject,
991 METH_VARARGS | METH_KEYWORDS, (char*) "Create an object of given type, from given address."},
992 {(char*) "move", (PyCFunction)Move,
993 METH_O, (char*)"Cast the C++ object to become movable."},
994 {(char*) "add_pythonization", (PyCFunction)AddPythonization,
995 METH_VARARGS, (char*)"Add a pythonizor."},
996 {(char*) "remove_pythonization", (PyCFunction)RemovePythonization,
997 METH_VARARGS, (char*)"Remove a pythonizor."},
998 {(char*) "_pin_type", (PyCFunction)PinType,
999 METH_O, (char*)"Install a type pinning."},
1000 {(char*) "_add_type_reducer", (PyCFunction)AddTypeReducer,
1001 METH_VARARGS, (char*)"Add a type reducer."},
1002 {(char*) "SetMemoryPolicy", (PyCFunction)SetMemoryPolicy,
1003 METH_VARARGS, (char*)"Determines object ownership model."},
1004 {(char*) "SetGlobalSignalPolicy", (PyCFunction)SetGlobalSignalPolicy,
1005 METH_VARARGS, (char*)"Trap signals in safe mode to prevent interpreter abort."},
1006 {(char*) "SetOwnership", (PyCFunction)SetOwnership,
1007 METH_VARARGS, (char*)"Modify held C++ object ownership."},
1008 {(char*) "AddSmartPtrType", (PyCFunction)AddSmartPtrType,
1009 METH_VARARGS, (char*) "Add a smart pointer to the list of known smart pointer types."},
1010 {(char*) "_begin_capture_stderr", (PyCFunction)BeginCaptureStderr,
1011 METH_NOARGS, (char*) "Begin capturing stderr to a in memory buffer."},
1012 {(char*) "_end_capture_stderr", (PyCFunction)EndCaptureStderr,
1013 METH_NOARGS, (char*) "End capturing stderr and returns the captured buffer."},
1014 {nullptr, nullptr, 0, nullptr}
1015};
1016
1017
1018#if PY_VERSION_HEX >= 0x03000000
1019struct module_state {
1020 PyObject *error;
1021};
1022
1023#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
1024
1025static int cpycppyymodule_traverse(PyObject* m, visitproc visit, void* arg)
1026{
1027 Py_VISIT(GETSTATE(m)->error);
1028 return 0;
1029}
1030
1031static int cpycppyymodule_clear(PyObject* m)
1032{
1033 Py_CLEAR(GETSTATE(m)->error);
1034 return 0;
1035}
1036
1037
1038static struct PyModuleDef moduledef = {
1039 PyModuleDef_HEAD_INIT,
1040 "libcppyy",
1041 nullptr,
1042 sizeof(struct module_state),
1044 nullptr,
1045 cpycppyymodule_traverse,
1046 cpycppyymodule_clear,
1047 nullptr
1048};
1049
1050
1051//----------------------------------------------------------------------------
1052#define CPYCPPYY_INIT_ERROR return nullptr
1053extern "C" PyObject* PyInit_libcppyy()
1054#else
1055#define CPYCPPYY_INIT_ERROR return
1056extern "C" void initlibcppyy()
1057#endif
1058{
1059// Initialization of extension module libcppyy.
1060
1061// load commonly used python strings
1064
1065// setup interpreter
1066#if PY_VERSION_HEX < 0x03090000
1067 PyEval_InitThreads();
1068#endif
1069
1070#if PY_VERSION_HEX < 0x030b0000
1071// prepare for laziness (the insert is needed to capture the most generic lookup
1072// function, just in case ...)
1073 PyObject* dict = PyDict_New();
1074 PyObject* notstring = PyInt_FromLong(5);
1075 PyDict_SetItem(dict, notstring, notstring);
1076 Py_DECREF(notstring);
1077#if PY_VERSION_HEX >= 0x03030000
1078 gDictLookupOrg = (dict_lookup_func)((PyDictObject*)dict)->ma_keys->dk_lookup;
1079#else
1080 gDictLookupOrg = (dict_lookup_func)((PyDictObject*)dict)->ma_lookup;
1081#endif
1082 Py_DECREF(dict);
1083#endif // PY_VERSION_HEX < 0x030b0000
1084
1085// setup this module
1086#if PY_VERSION_HEX >= 0x03000000
1087 gThisModule = PyModule_Create(&moduledef);
1088#else
1089 gThisModule = Py_InitModule(const_cast<char*>("libcppyy"), gCPyCppyyMethods);
1090#endif
1091 if (!gThisModule)
1093
1094// keep gThisModule, but do not increase its reference count even as it is borrowed,
1095// or a self-referencing cycle would be created
1096
1097// external types
1098 gPyTypeMap = PyDict_New();
1099 PyModule_AddObject(gThisModule, "type_map", gPyTypeMap); // steals reference
1100
1101// Pythonizations ...
1102 PyModule_AddObject(gThisModule, "UserExceptions", PyDict_New());
1103
1104// inject meta type
1105 if (!Utility::InitProxy(gThisModule, &CPPScope_Type, "CPPScope"))
1107
1108// inject object proxy type
1109 if (!Utility::InitProxy(gThisModule, &CPPInstance_Type, "CPPInstance"))
1111
1112// inject exception object proxy type
1113 if (!Utility::InitProxy(gThisModule, &CPPExcInstance_Type, "CPPExcInstance"))
1115
1116// inject method proxy type
1117 if (!Utility::InitProxy(gThisModule, &CPPOverload_Type, "CPPOverload"))
1119
1120// inject template proxy type
1121 if (!Utility::InitProxy(gThisModule, &TemplateProxy_Type, "TemplateProxy"))
1123
1124// inject property proxy type
1125 if (!Utility::InitProxy(gThisModule, &CPPDataMember_Type, "CPPDataMember"))
1127
1128// inject custom data types
1129#if PY_VERSION_HEX < 0x03000000
1132
1135#endif
1136
1139
1140 if (!Utility::InitProxy(gThisModule, &TupleOfInstances_Type, "InstanceArray"))
1142
1143 if (!Utility::InitProxy(gThisModule, &LowLevelView_Type, "LowLevelView"))
1145
1146 if (!Utility::InitProxy(gThisModule, &PyNullPtr_t_Type, "nullptr_t"))
1148
1149// custom iterators
1150 if (PyType_Ready(&InstanceArrayIter_Type) < 0)
1152
1153 if (PyType_Ready(&IndexIter_Type) < 0)
1155
1156 if (PyType_Ready(&VectorIter_Type) < 0)
1158
1159// inject identifiable nullptr and default
1160 gNullPtrObject = (PyObject*)&_CPyCppyy_NullPtrStruct;
1161 Py_INCREF(gNullPtrObject);
1162 PyModule_AddObject(gThisModule, (char*)"nullptr", gNullPtrObject);
1163
1164 gDefaultObject = (PyObject*)&_CPyCppyy_DefaultStruct;
1165 Py_INCREF(gDefaultObject);
1166 PyModule_AddObject(gThisModule, (char*)"default", gDefaultObject);
1167
1168// C++-specific exceptions
1169 PyObject* cppfatal = PyErr_NewException((char*)"cppyy.ll.FatalError", nullptr, nullptr);
1170 PyModule_AddObject(gThisModule, (char*)"FatalError", cppfatal);
1171
1172 gBusException = PyErr_NewException((char*)"cppyy.ll.BusError", cppfatal, nullptr);
1173 PyModule_AddObject(gThisModule, (char*)"BusError", gBusException);
1174 gSegvException = PyErr_NewException((char*)"cppyy.ll.SegmentationViolation", cppfatal, nullptr);
1175 PyModule_AddObject(gThisModule, (char*)"SegmentationViolation", gSegvException);
1176 gIllException = PyErr_NewException((char*)"cppyy.ll.IllegalInstruction", cppfatal, nullptr);
1177 PyModule_AddObject(gThisModule, (char*)"IllegalInstruction", gIllException);
1178 gAbrtException = PyErr_NewException((char*)"cppyy.ll.AbortSignal", cppfatal, nullptr);
1179 PyModule_AddObject(gThisModule, (char*)"AbortSignal", gAbrtException);
1180
1181// policy labels
1182 PyModule_AddObject(gThisModule, (char*)"kMemoryHeuristics",
1183 PyInt_FromLong((int)CallContext::kUseHeuristics));
1184 PyModule_AddObject(gThisModule, (char*)"kMemoryStrict",
1185 PyInt_FromLong((int)CallContext::kUseStrict));
1186
1187// gbl namespace is injected in cppyy.py
1188
1189// create the memory regulator
1190 static MemoryRegulator s_memory_regulator;
1191
1192#if PY_VERSION_HEX >= 0x03000000
1193 Py_INCREF(gThisModule);
1194 return gThisModule;
1195#endif
1196}
static PyObject * default_repr(PyObject *)
#define CPYCPPYY_ORGDICT_LOOKUP(mp, key, hash, value_addr, hashpos)
static PyObject * nullptr_repr(PyObject *)
static PyNumberMethods nullptr_as_number
#define CPYCPPYY_INIT_ERROR
static void default_dealloc(PyObject *)
void initlibcppyy()
static PyTypeObject PyDefault_t_Type
static void nullptr_dealloc(PyObject *)
static int nullptr_nonzero(PyObject *)
static PyMethodDef gCPyCppyyMethods[]
static PyTypeObject PyNullPtr_t_Type
#define CPYCPPYY_GET_DICT_LOOKUP(mp)
#define Py_TYPE(ob)
Definition CPyCppyy.h:196
PyDictEntry *(* dict_lookup_func)(PyDictObject *, PyObject *, long)
Definition CPyCppyy.h:44
#define Py_RETURN_TRUE
Definition CPyCppyy.h:272
#define Py_RETURN_FALSE
Definition CPyCppyy.h:276
#define PY_SSIZE_T_FORMAT
Definition CPyCppyy.h:218
static void * CPyCppyy_PyCapsule_GetPointer(PyObject *capsule, const char *)
Definition CPyCppyy.h:104
#define CPyCppyy_PyText_AsString
Definition CPyCppyy.h:76
static PyObject * CPyCppyy_PyCapsule_New(void *cobj, const char *, void(*destr)(void *))
Definition CPyCppyy.h:98
long Py_hash_t
Definition CPyCppyy.h:114
static PyObject * PyObject_CallMethodOneArg(PyObject *obj, PyObject *name, PyObject *arg)
Definition CPyCppyy.h:367
#define CPyCppyy_PyText_FromFormat
Definition CPyCppyy.h:80
#define Py_RETURN_NONE
Definition CPyCppyy.h:268
#define CPyCppyy_PyText_Type
Definition CPyCppyy.h:94
#define CPyCppyy_PyText_FromString
Definition CPyCppyy.h:81
#define CPyCppyy_PyText_Check
Definition CPyCppyy.h:74
#define PyVarObject_HEAD_INIT(type, size)
Definition CPyCppyy.h:194
_object PyObject
static struct PyModuleDef moduledef
#define GETSTATE(m)
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
TTime operator*(const TTime &t1, const TTime &t2)
Definition TTime.h:85
void * GetAddress(CPPInstance *pyobj)
Cppyy::TCppType_t ObjectIsA(bool check_smart=true) const
Py_ssize_t GetBuffer(PyObject *pyobject, char tc, int size, void *&buf, bool check=true)
Definition Utility.cxx:808
std::string ConstructTemplateArgs(PyObject *pyname, PyObject *tpArgs, PyObject *args=nullptr, ArgPreference=kNone, int argoff=0, int *pcnt=nullptr)
Definition Utility.cxx:580
bool InitProxy(PyObject *module, PyTypeObject *pytype, const char *name)
Definition Utility.cxx:788
PyTypeObject CPPInstance_Type
PyObject * gAbrtException
PyObject * gDefaultObject
bool gDictLookupActive
Definition Utility.cxx:28
PyTypeObject VectorIter_Type
PyTypeObject CPPExcInstance_Type
PyObject * GetScopeProxy(Cppyy::TCppScope_t)
std::ostringstream gCapturedError
dict_lookup_func gDictLookupOrg
Definition Utility.cxx:27
PyTypeObject CustomInstanceMethod_Type
PyObject * CreateScopeProxy(Cppyy::TCppScope_t, const unsigned flags=0)
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)
Definition CPPScope.h:81
bool CreatePyStrings()
Definition PyStrings.cxx:84
CPYCPPYY_EXTERN PyObject * Instance_FromVoidPtr(void *addr, const std::string &classname, bool python_owns=false)
Definition API.cxx:121
bool CPPInstance_Check(T *object)
PyTypeObject IndexIter_Type
PyObject * gNullPtrObject
PyTypeObject CPPOverload_Type
PyTypeObject TemplateProxy_Type
PyObject * gThisModule
Definition CPPMethod.cxx:30
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)
Definition API.cxx:106
PyTypeObject CPPScope_Type
Definition CPPScope.cxx:645
PyObject * gIllException
PyTypeObject LowLevelView_Type
PyObject * gPyTypeMap
bool CPPOverload_CheckExact(T *object)
Definition CPPOverload.h:96
PyTypeObject CPPDataMember_Type
PyObject * gBusException
std::streambuf * gOldErrorBuffer
PyTypeObject TupleOfInstances_Type
Representation of C-style array of instances.
PyTypeObject RefInt_Type
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 void AddSmartPtrType(const std::string &)
RPY_EXPORTED bool IsSubtype(TCppType_t derived, TCppType_t base)
void * TCppObject_t
Definition cpp_cppyy.h:21
TCppScope_t TCppType_t
Definition cpp_cppyy.h:19
RPY_EXPORTED void AddTypeReducer(const std::string &reducable, const std::string &reduced)
RPY_EXPORTED bool IsAggregate(TCppType_t type)
RPY_EXPORTED TCppScope_t GetScope(const std::string &scope_name)
void * TCppFuncAddr_t
Definition cpp_cppyy.h:25
void(off) SmallVectorTemplateBase< T
static ECallFlags sMemoryPolicy
Definition CallContext.h:78
static bool SetGlobalSignalPolicy(bool setProtected)
static bool SetMemoryPolicy(ECallFlags e)
PyObject_HEAD char * b_ptr
PyObject * error
TMarker m
Definition textangle.C:8
TLine l
Definition textangle.C:4