Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Loading...
Searching...
No Matches
Pythonize.cxx
Go to the documentation of this file.
1// Bindings
2#include "CPyCppyy.h"
3#include "Pythonize.h"
4#include "Converters.h"
5#include "CPPInstance.h"
6#include "CPPFunction.h"
7#include "CPPOverload.h"
8#include "CustomPyTypes.h"
9#include "LowLevelViews.h"
10#include "ProxyWrappers.h"
11#include "PyCallable.h"
12#include "PyStrings.h"
13#include "TypeManip.h"
14#include "Utility.h"
15
16// Standard
17#include <algorithm>
18#include <complex>
19#include <set>
20#include <stdexcept>
21#include <sstream>
22#include <string>
23#include <utility>
24
25
26//- data and local helpers ---------------------------------------------------
27namespace CPyCppyy {
28 extern PyObject* gThisModule;
29 extern std::map<std::string, std::vector<PyObject*>> gPythonizations;
30}
31
32namespace {
33
34// for convenience
35using namespace CPyCppyy;
36
37//-----------------------------------------------------------------------------
39// prevents calls to Py_TYPE(pyclass)->tp_getattr, which is unnecessary for our
40// purposes here and could tickle problems w/ spurious lookups into ROOT meta
41 PyObject* dct = PyObject_GetAttr(pyclass, PyStrings::gDict);
42 if (dct) {
45 if (attr) {
48 return ret;
49 }
50 }
52 return false;
53}
54
56// get an attribute without causing getattr lookups
57 PyObject* dct = PyObject_GetAttr(pyclass, PyStrings::gDict);
58 if (dct) {
61 return attr;
62 }
63 return nullptr;
64}
65
66//-----------------------------------------------------------------------------
67inline bool IsTemplatedSTLClass(const std::string& name, const std::string& klass) {
68// Scan the name of the class and determine whether it is a template instantiation.
69 auto pos = name.find(klass);
70 return (pos == 0 || pos == 5) && name.find("::", name.rfind(">")) == std::string::npos;
71}
72
73// to prevent compiler warnings about const char* -> char*
74inline PyObject* CallPyObjMethod(PyObject* obj, const char* meth)
75{
76// Helper; call method with signature: obj->meth().
77 Py_INCREF(obj);
78 PyObject* result = PyObject_CallMethod(obj, const_cast<char*>(meth), const_cast<char*>(""));
79 Py_DECREF(obj);
80 return result;
81}
82
83//-----------------------------------------------------------------------------
84inline PyObject* CallPyObjMethod(PyObject* obj, const char* meth, PyObject* arg1)
85{
86// Helper; call method with signature: obj->meth(arg1).
87 Py_INCREF(obj);
89 obj, const_cast<char*>(meth), const_cast<char*>("O"), arg1);
90 Py_DECREF(obj);
91 return result;
92}
93
94//-----------------------------------------------------------------------------
96{
97// Helper; converts python index into straight C index.
99 if (idx == (Py_ssize_t)-1 && PyErr_Occurred())
100 return nullptr;
101
103 if (idx >= size || (idx < 0 && idx < -size)) {
104 PyErr_SetString(PyExc_IndexError, "index out of range");
105 return nullptr;
106 }
107
108 PyObject* pyindex = nullptr;
109 if (idx >= 0) {
111 pyindex = index;
112 } else
114
115 return pyindex;
116}
117
118//-----------------------------------------------------------------------------
119inline bool AdjustSlice(const Py_ssize_t nlen, Py_ssize_t& start, Py_ssize_t& stop, Py_ssize_t& step)
120{
121// Helper; modify slice range to match the container.
122 if ((step > 0 && stop <= start) || (step < 0 && start <= stop))
123 return false;
124
125 if (start < 0) start = 0;
126 if (start >= nlen) start = nlen-1;
127 if (step >= nlen) step = nlen;
128
129 stop = step > 0 ? std::min(nlen, stop) : (stop >= 0 ? stop : -1);
130 return true;
131}
132
133//-----------------------------------------------------------------------------
135{
136// Helper; call method with signature: meth(pyindex).
139 if (!pyindex) {
141 return nullptr;
142 }
143
147 return result;
148}
149
150//- "smart pointer" behavior ---------------------------------------------------
152{
153// Follow operator*() if present (available in python as __deref__), so that
154// smart pointers behave as expected.
155 if (name == PyStrings::gTypeCode || name == PyStrings::gCTypesType) {
156 // TODO: these calls come from TemplateProxy and are unlikely to be needed in practice,
157 // whereas as-is, they can accidentally dereference the result of end() on some STL
158 // containers. Obviously, this is a dumb hack that should be resolved more fundamentally.
160 return nullptr;
161 }
162
164 PyErr_SetString(PyExc_TypeError, "getattr(): attribute name must be string");
165
166 PyObject* pyptr = PyObject_CallMethodNoArgs(self, PyStrings::gDeref);
167 if (!pyptr)
168 return nullptr;
169
170// prevent a potential infinite loop
171 if (Py_TYPE(pyptr) == Py_TYPE(self)) {
174 PyErr_Format(PyExc_AttributeError, "%s has no attribute \'%s\'",
178
180 return nullptr;
181 }
182
185 return result;
186}
187
188//-----------------------------------------------------------------------------
190{
191// Follow operator->() if present (available in python as __follow__), so that
192// smart pointers behave as expected.
194 PyErr_SetString(PyExc_TypeError, "getattr(): attribute name must be string");
195
196 PyObject* pyptr = PyObject_CallMethodNoArgs(self, PyStrings::gFollow);
197 if (!pyptr)
198 return nullptr;
199
202 return result;
203}
204
205//- pointer checking bool converter -------------------------------------------
207{
208 if (!CPPInstance_Check(self)) {
209 PyErr_SetString(PyExc_TypeError, "C++ object proxy expected");
210 return nullptr;
211 }
212
213 if (!((CPPInstance*)self)->GetObject())
215
216 return PyObject_CallMethodNoArgs(self, PyStrings::gCppBool);
217}
218
219//- vector behavior as primitives ----------------------------------------------
220#if PY_VERSION_HEX < 0x03040000
221#define PyObject_LengthHint _PyObject_LengthHint
222#endif
223
224// TODO: can probably use the below getters in the InitializerListConverter
225struct ItemGetter {
226 ItemGetter(PyObject* pyobj) : fPyObject(pyobj) { Py_INCREF(fPyObject); }
227 virtual ~ItemGetter() { Py_DECREF(fPyObject); }
228 virtual Py_ssize_t size() = 0;
229 virtual PyObject* get() = 0;
230 PyObject* fPyObject;
231};
232
233struct CountedItemGetter : public ItemGetter {
234 CountedItemGetter(PyObject* pyobj) : ItemGetter(pyobj), fCur(0) {}
235 Py_ssize_t fCur;
236};
237
238struct TupleItemGetter : public CountedItemGetter {
239 using CountedItemGetter::CountedItemGetter;
240 virtual Py_ssize_t size() { return PyTuple_GET_SIZE(fPyObject); }
241 virtual PyObject* get() {
242 if (fCur < PyTuple_GET_SIZE(fPyObject)) {
243 PyObject* item = PyTuple_GET_ITEM(fPyObject, fCur++);
244 Py_INCREF(item);
245 return item;
246 }
247 PyErr_SetString(PyExc_StopIteration, "end of tuple");
248 return nullptr;
249 }
250};
251
252struct ListItemGetter : public CountedItemGetter {
253 using CountedItemGetter::CountedItemGetter;
254 virtual Py_ssize_t size() { return PyList_GET_SIZE(fPyObject); }
255 virtual PyObject* get() {
256 if (fCur < PyList_GET_SIZE(fPyObject)) {
257 PyObject* item = PyList_GET_ITEM(fPyObject, fCur++);
258 Py_INCREF(item);
259 return item;
260 }
261 PyErr_SetString(PyExc_StopIteration, "end of list");
262 return nullptr;
263 }
264};
265
266struct SequenceItemGetter : public CountedItemGetter {
267 using CountedItemGetter::CountedItemGetter;
268 virtual Py_ssize_t size() {
269 Py_ssize_t sz = PySequence_Size(fPyObject);
270 if (sz < 0) {
271 PyErr_Clear();
272 return PyObject_LengthHint(fPyObject, 8);
273 }
274 return sz;
275 }
276 virtual PyObject* get() { return PySequence_GetItem(fPyObject, fCur++); }
277};
278
279struct IterItemGetter : public ItemGetter {
280 using ItemGetter::ItemGetter;
281 virtual Py_ssize_t size() { return PyObject_LengthHint(fPyObject, 8); }
282 virtual PyObject* get() { return (*(Py_TYPE(fPyObject)->tp_iternext))(fPyObject); }
283};
284
285static ItemGetter* GetGetter(PyObject* args)
286{
287// Create an ItemGetter to loop over the iterable argument, if any.
288 ItemGetter* getter = nullptr;
289
290 if (PyTuple_GET_SIZE(args) == 1) {
291 PyObject* fi = PyTuple_GET_ITEM(args, 0);
293 return nullptr; // do not accept string to fill std::vector<char>
294
295 // TODO: this only tests for new-style buffers, which is too strict, but a
296 // generic check for Py_TYPE(fi)->tp_as_buffer is too loose (note that the
297 // main use case is numpy, which offers the new interface)
299 return nullptr;
300
302 getter = new TupleItemGetter(fi);
303 else if (PyList_CheckExact(fi))
304 getter = new ListItemGetter(fi);
305 else if (PySequence_Check(fi))
306 getter = new SequenceItemGetter(fi);
307 else {
309 if (iter) {
310 getter = new IterItemGetter{iter};
311 Py_DECREF(iter);
312 }
313 else PyErr_Clear();
314 }
315 }
316
317 return getter;
318}
319
320static bool FillVector(PyObject* vecin, PyObject* args, ItemGetter* getter)
321{
322 Py_ssize_t sz = getter->size();
323 if (sz < 0)
324 return false;
325
326// reserve memory as applicable
327 if (0 < sz) {
328 PyObject* res = PyObject_CallMethod(vecin, (char*)"reserve", (char*)"n", sz);
329 Py_DECREF(res);
330 } else // i.e. sz == 0, so empty container: done
331 return true;
332
333 bool fill_ok = true;
334
335// two main options: a list of lists (or tuples), or a list of objects; the former
336// are emplace_back'ed, the latter push_back'ed
338 if (!fi) PyErr_Clear();
340 // use emplace_back to construct the vector entries one by one
341 PyObject* eb_call = PyObject_GetAttrString(vecin, (char*)"emplace_back");
342 PyObject* vtype = GetAttrDirect((PyObject*)Py_TYPE(vecin), PyStrings::gValueType);
343 bool value_is_vector = false;
345 // if the value_type is a vector, then allow for initialization from sequences
346 if (std::string(CPyCppyy_PyText_AsString(vtype)).rfind("std::vector", 0) != std::string::npos)
347 value_is_vector = true;
348 } else
349 PyErr_Clear();
351
352 if (eb_call) {
354 for (int i = 0; /* until break */; ++i) {
355 PyObject* item = getter->get();
356 if (item) {
357 if (value_is_vector && PySequence_Check(item)) {
358 eb_args = PyTuple_New(1);
359 PyTuple_SET_ITEM(eb_args, 0, item);
360 } else if (PyTuple_CheckExact(item)) {
361 eb_args = item;
362 } else if (PyList_CheckExact(item)) {
365 for (Py_ssize_t j = 0; j < isz; ++j) {
366 PyObject* iarg = PyList_GET_ITEM(item, j);
369 }
370 Py_DECREF(item);
371 } else {
372 Py_DECREF(item);
373 PyErr_Format(PyExc_TypeError, "argument %d is not a tuple or list", i);
374 fill_ok = false;
375 break;
376 }
379 if (!ebres) {
380 fill_ok = false;
381 break;
382 }
384 } else {
385 if (PyErr_Occurred()) {
388 fill_ok = false;
389 else { PyErr_Clear(); }
390 }
391 break;
392 }
393 }
395 }
396 } else {
397 // use push_back to add the vector entries one by one
398 PyObject* pb_call = PyObject_GetAttrString(vecin, (char*)"push_back");
399 if (pb_call) {
400 for (;;) {
401 PyObject* item = getter->get();
402 if (item) {
404 Py_DECREF(item);
405 if (!pbres) {
406 fill_ok = false;
407 break;
408 }
410 } else {
411 if (PyErr_Occurred()) {
414 fill_ok = false;
415 else { PyErr_Clear(); }
416 }
417 break;
418 }
419 }
421 }
422 }
423 Py_XDECREF(fi);
424
425 return fill_ok;
426}
427
428PyObject* VectorIAdd(PyObject* self, PyObject* args, PyObject* /* kwds */)
429{
430// Implement fast __iadd__ on std::vector (generic __iadd__ is in Python)
431 ItemGetter* getter = GetGetter(args);
432
433 if (getter) {
434 bool fill_ok = FillVector(self, args, getter);
435 delete getter;
436
437 if (!fill_ok)
438 return nullptr;
439
441 return self;
442 }
443
444// if no getter, it could still be b/c we have a buffer (e.g. numpy); looping over
445// a buffer here is slow, so use insert() instead
446 if (PyTuple_GET_SIZE(args) == 1) {
447 PyObject* fi = PyTuple_GET_ITEM(args, 0);
449 PyObject* vend = PyObject_CallMethodNoArgs(self, PyStrings::gEnd);
450 if (vend) {
451 PyObject* result = PyObject_CallMethodObjArgs(self, PyStrings::gInsert, vend, fi, nullptr);
453 return result;
454 }
455 }
456 }
457
458 if (!PyErr_Occurred())
459 PyErr_SetString(PyExc_TypeError, "argument is not iterable");
460 return nullptr; // error already set
461}
462
463
464PyObject* VectorInit(PyObject* self, PyObject* args, PyObject* /* kwds */)
465{
466// Specialized vector constructor to allow construction from containers; allowing
467// such construction from initializer_list instead would possible, but can be
468// error-prone. This use case is common enough for std::vector to implement it
469// directly, except for arrays (which can be passed wholesale) and strings (which
470// won't convert properly as they'll be seen as buffers)
471
472 ItemGetter* getter = GetGetter(args);
473
474 if (getter) {
475 // construct an empty vector, then back-fill it
476 PyObject* result = PyObject_CallMethodNoArgs(self, PyStrings::gRealInit);
477 if (!result) {
478 delete getter;
479 return nullptr;
480 }
481
482 bool fill_ok = FillVector(self, args, getter);
483 delete getter;
484
485 if (!fill_ok) {
487 return nullptr;
488 }
489
490 return result;
491 }
492
493// The given argument wasn't iterable: simply forward to regular constructor
494 PyObject* realInit = PyObject_GetAttr(self, PyStrings::gRealInit);
495 if (realInit) {
496 PyObject* result = PyObject_Call(realInit, args, nullptr);
498 return result;
499 }
500
501 return nullptr;
502}
503
504//---------------------------------------------------------------------------
506{
507 PyObject* pydata = CallPyObjMethod(self, "__real_data");
509 return pydata;
510
511 PyObject* pylen = PyObject_CallMethodNoArgs(self, PyStrings::gSize);
512 if (!pylen) {
513 PyErr_Clear();
514 return pydata;
515 }
516
517 long clen = PyInt_AsLong(pylen);
519
521 ((CPPInstance*)pydata)->CastToArray(clen);
522 return pydata;
523 }
524
525 ((LowLevelView*)pydata)->resize((size_t)clen);
526 return pydata;
527}
528
529
530// This function implements __array__, added to std::vector python proxies and causes
531// a bug (see explanation at Utility::AddToClass(pyclass, "__array__"...) in CPyCppyy::Pythonize)
532// The recursive nature of this function, passes each subarray (pydata) to the next call and only
533// the final buffer is cast to a lowlevel view and resized (in VectorData), resulting in only the
534// first 1D array to be returned. See https://github.com/root-project/root/issues/17729
535// It is temporarily removed to prevent errors due to -Wunused-function, since it is no longer added.
536#if 0
537//---------------------------------------------------------------------------
539{
540 PyObject* pydata = VectorData(self, nullptr);
541 PyObject* view = PyObject_CallMethodNoArgs(pydata, PyStrings::gArray);
543 return view;
544}
545#endif
546
547//-----------------------------------------------------------------------------
548static PyObject* vector_iter(PyObject* v) {
550 if (!vi) return nullptr;
551
552 Py_INCREF(v);
553 vi->ii_container = v;
554
555// tell the iterator code to set a life line if this container is a temporary
556 vi->vi_flags = vectoriterobject::kDefault;
557 if (v->ob_refcnt <= 2 || (((CPPInstance*)v)->fFlags & CPPInstance::kIsValue))
559
560 PyObject* pyvalue_type = PyObject_GetAttr((PyObject*)Py_TYPE(v), PyStrings::gValueType);
561 if (pyvalue_type) {
562 PyObject* pyvalue_size = GetAttrDirect((PyObject*)Py_TYPE(v), PyStrings::gValueSize);
563 if (pyvalue_size) {
564 vi->vi_stride = PyLong_AsLong(pyvalue_size);
566 } else {
567 PyErr_Clear();
568 vi->vi_stride = 0;
569 }
570
572 std::string value_type = CPyCppyy_PyText_AsString(pyvalue_type);
573 value_type = Cppyy::ResolveName(value_type);
574 vi->vi_klass = Cppyy::GetScope(value_type);
575 if (!vi->vi_klass) {
576 // look for a special case of pointer to a class type (which is a builtin, but it
577 // is more useful to treat it polymorphically by allowing auto-downcasts)
578 const std::string& clean_type = TypeManip::clean_type(value_type, false, false);
580 if (c && TypeManip::compound(value_type) == "*") {
581 vi->vi_klass = c;
583 }
584 }
585 if (vi->vi_klass) {
586 vi->vi_converter = nullptr;
587 if (!vi->vi_flags) {
588 if (value_type.back() != '*') // meaning, object stored by-value
590 }
591 } else
592 vi->vi_converter = CPyCppyy::CreateConverter(value_type);
593 if (!vi->vi_stride) vi->vi_stride = Cppyy::SizeOf(value_type);
594
595 } else if (CPPScope_Check(pyvalue_type)) {
596 vi->vi_klass = ((CPPClass*)pyvalue_type)->fCppType;
597 vi->vi_converter = nullptr;
598 if (!vi->vi_stride) vi->vi_stride = Cppyy::SizeOf(vi->vi_klass);
599 if (!vi->vi_flags) vi->vi_flags = vectoriterobject::kNeedLifeLine;
600 }
601
602 PyObject* pydata = CallPyObjMethod(v, "__real_data");
603 if (!pydata || Utility::GetBuffer(pydata, '*', 1, vi->vi_data, false) == 0)
604 vi->vi_data = CPPInstance_Check(pydata) ? ((CPPInstance*)pydata)->GetObjectRaw() : nullptr;
606
607 } else {
608 PyErr_Clear();
609 vi->vi_data = nullptr;
610 vi->vi_stride = 0;
611 vi->vi_converter = nullptr;
612 vi->vi_klass = 0;
613 vi->vi_flags = 0;
614 }
615
617
618 vi->ii_pos = 0;
619 vi->ii_len = PySequence_Size(v);
620
622 return (PyObject*)vi;
623}
624
626{
627// Implement python's __getitem__ for std::vector<>s.
628 if (PySlice_Check(index)) {
629 if (!self->GetObject()) {
630 PyErr_SetString(PyExc_TypeError, "unsubscriptable object");
631 return nullptr;
632 }
633
636
637 Py_ssize_t start, stop, step;
639
641 if (!AdjustSlice(nlen, start, stop, step))
642 return nseq;
643
644 const Py_ssize_t sign = step < 0 ? -1 : 1;
645 for (Py_ssize_t i = start; i*sign < stop*sign; i += step) {
647 PyObject* item = PyObject_CallMethodOneArg((PyObject*)self, PyStrings::gGetNoCheck, pyidx);
648 CallPyObjMethod(nseq, "push_back", item);
649 Py_DECREF(item);
651 }
652
653 return nseq;
654 }
655
656 return CallSelfIndex(self, (PyObject*)index, PyStrings::gGetNoCheck);
657}
658
659
661
663{
664// std::vector<bool> is a special-case in C++, and its return type depends on
665// the compiler: treat it special here as well
666 if (!CPPInstance_Check(self) || self->ObjectIsA() != sVectorBoolTypeID) {
668 "require object of type std::vector<bool>, but %s given",
669 Cppyy::GetScopedFinalName(self->ObjectIsA()).c_str());
670 return nullptr;
671 }
672
673 if (!self->GetObject()) {
674 PyErr_SetString(PyExc_TypeError, "unsubscriptable object");
675 return nullptr;
676 }
677
678 if (PySlice_Check(idx)) {
681
682 Py_ssize_t start, stop, step;
685 if (!AdjustSlice(nlen, start, stop, step))
686 return nseq;
687
688 const Py_ssize_t sign = step < 0 ? -1 : 1;
689 for (Py_ssize_t i = start; i*sign < stop*sign; i += step) {
691 PyObject* item = PyObject_CallMethodOneArg((PyObject*)self, PyStrings::gGetItem, pyidx);
692 CallPyObjMethod(nseq, "push_back", item);
693 Py_DECREF(item);
695 }
696
697 return nseq;
698 }
699
701 if (!pyindex)
702 return nullptr;
703
706
707// get hold of the actual std::vector<bool> (no cast, as vector is never a base)
708 std::vector<bool>* vb = (std::vector<bool>*)self->GetObject();
709
710// finally, return the value
711 if (bool((*vb)[index]))
714}
715
717{
718// std::vector<bool> is a special-case in C++, and its return type depends on
719// the compiler: treat it special here as well
720 if (!CPPInstance_Check(self) || self->ObjectIsA() != sVectorBoolTypeID) {
722 "require object of type std::vector<bool>, but %s given",
723 Cppyy::GetScopedFinalName(self->ObjectIsA()).c_str());
724 return nullptr;
725 }
726
727 if (!self->GetObject()) {
728 PyErr_SetString(PyExc_TypeError, "unsubscriptable object");
729 return nullptr;
730 }
731
732 int bval = 0; PyObject* idx = nullptr;
733 if (!PyArg_ParseTuple(args, const_cast<char*>("Oi:__setitem__"), &idx, &bval))
734 return nullptr;
735
737 if (!pyindex)
738 return nullptr;
739
742
743// get hold of the actual std::vector<bool> (no cast, as vector is never a base)
744 std::vector<bool>* vb = (std::vector<bool>*)self->GetObject();
745
746// finally, set the value
747 (*vb)[index] = (bool)bval;
748
750}
751
752
753//- array behavior as primitives ----------------------------------------------
754PyObject* ArrayInit(PyObject* self, PyObject* args, PyObject* /* kwds */)
755{
756// std::array is normally only constructed using aggregate initialization, which
757// is a concept that does not exist in python, so use this custom constructor to
758// to fill the array using setitem
759
760 if (args && PyTuple_GET_SIZE(args) == 1 && PySequence_Check(PyTuple_GET_ITEM(args, 0))) {
761 // construct the empty array, then fill it
762 PyObject* result = PyObject_CallMethodNoArgs(self, PyStrings::gRealInit);
763 if (!result)
764 return nullptr;
765
766 PyObject* items = PyTuple_GET_ITEM(args, 0);
768 if (PySequence_Size(self) != fillsz) {
769 PyErr_Format(PyExc_ValueError, "received sequence of size %zd where %zd expected",
772 return nullptr;
773 }
774
775 PyObject* si_call = PyObject_GetAttr(self, PyStrings::gSetItem);
776 for (Py_ssize_t i = 0; i < fillsz; ++i) {
781 Py_DECREF(item);
782 if (!sires) {
785 return nullptr;
786 } else
788 }
790
791 return result;
792 } else
793 PyErr_Clear();
794
795// The given argument wasn't iterable: simply forward to regular constructor
796 PyObject* realInit = PyObject_GetAttr(self, PyStrings::gRealInit);
797 if (realInit) {
798 PyObject* result = PyObject_Call(realInit, args, nullptr);
800 return result;
801 }
802
803 return nullptr;
804}
805
806
807//- map behavior as primitives ------------------------------------------------
809{
810// construct an empty map, then fill it with the key, value pairs
811 PyObject* result = PyObject_CallMethodNoArgs(self, PyStrings::gRealInit);
812 if (!result)
813 return nullptr;
814
815 PyObject* si_call = PyObject_GetAttr(self, PyStrings::gSetItem);
816 for (Py_ssize_t i = 0; i < PySequence_Size(pairs); ++i) {
818 PyObject* sires = nullptr;
819 if (pair && PySequence_Check(pair) && PySequence_Size(pair) == 2) {
820 PyObject* key = PySequence_GetItem(pair, 0);
824 Py_DECREF(key);
825 }
826 Py_DECREF(pair);
827 if (!sires) {
830 if (!PyErr_Occurred())
831 PyErr_SetString(PyExc_TypeError, "Failed to fill map (argument not a dict or sequence of pairs)");
832 return nullptr;
833 } else
835 }
837
838 return result;
839}
840
841PyObject* MapInit(PyObject* self, PyObject* args, PyObject* /* kwds */)
842{
843// Specialized map constructor to allow construction from mapping containers and
844// from tuples of pairs ("initializer_list style").
845
846// PyMapping_Check is not very discriminatory, as it basically only checks for the
847// existence of __getitem__, hence the most common cases of tuple and list are
848// dropped straight-of-the-bat (the PyMapping_Items call will fail on them).
849 if (PyTuple_GET_SIZE(args) == 1 && PyMapping_Check(PyTuple_GET_ITEM(args, 0)) && \
851 PyObject* assoc = PyTuple_GET_ITEM(args, 0);
852#if PY_VERSION_HEX < 0x03000000
853 // to prevent warning about literal string, expand macro
854 PyObject* items = PyObject_CallMethod(assoc, (char*)"items", nullptr);
855#else
856 // in p3, PyMapping_Items isn't a macro, but a function that short-circuits dict
858#endif
859 if (items && PySequence_Check(items)) {
862 return result;
863 }
864
866 PyErr_Clear();
867
868 // okay to fall through as long as 'self' has not been created (is done in MapFromPairs)
869 }
870
871// tuple of pairs case (some mapping types are sequences)
872 if (PyTuple_GET_SIZE(args) == 1 && PySequence_Check(PyTuple_GET_ITEM(args, 0)))
873 return MapFromPairs(self, PyTuple_GET_ITEM(args, 0));
874
875// The given argument wasn't a mapping or tuple of pairs: forward to regular constructor
876 PyObject* realInit = PyObject_GetAttr(self, PyStrings::gRealInit);
877 if (realInit) {
878 PyObject* result = PyObject_Call(realInit, args, nullptr);
880 return result;
881 }
882
883 return nullptr;
884}
885
887{
888// Implement python's __contains__ for std::map/std::set
889 PyObject* result = nullptr;
890
891 PyObject* iter = CallPyObjMethod(self, "find", obj);
892 if (CPPInstance_Check(iter)) {
893 PyObject* end = PyObject_CallMethodNoArgs(self, PyStrings::gEnd);
894 if (CPPInstance_Check(end)) {
895 if (!PyObject_RichCompareBool(iter, end, Py_EQ)) {
897 result = Py_True;
898 }
899 }
900 Py_XDECREF(end);
901 }
902 Py_XDECREF(iter);
903
904 if (!result) {
905 PyErr_Clear(); // e.g. wrong argument type, which should always lead to False
908 }
909
910 return result;
911}
912
913
914//- set behavior as primitives ------------------------------------------------
915PyObject* SetInit(PyObject* self, PyObject* args, PyObject* /* kwds */)
916{
917// Specialized set constructor to allow construction from Python sets.
918 if (PyTuple_GET_SIZE(args) == 1 && PySet_Check(PyTuple_GET_ITEM(args, 0))) {
919 PyObject* pyset = PyTuple_GET_ITEM(args, 0);
920
921 // construct an empty set, then fill it
922 PyObject* result = PyObject_CallMethodNoArgs(self, PyStrings::gRealInit);
923 if (!result)
924 return nullptr;
925
927 if (iter) {
928 PyObject* ins_call = PyObject_GetAttrString(self, (char*)"insert");
929
930 IterItemGetter getter{iter};
931 Py_DECREF(iter);
932
933 PyObject* item = getter.get();
934 while (item) {
936 Py_DECREF(item);
937 if (!insres) {
940 return nullptr;
941 } else
943 item = getter.get();
944 }
946 }
947
948 return result;
949 }
950
951// The given argument wasn't iterable: simply forward to regular constructor
952 PyObject* realInit = PyObject_GetAttr(self, PyStrings::gRealInit);
953 if (realInit) {
954 PyObject* result = PyObject_Call(realInit, args, nullptr);
956 return result;
957 }
958
959 return nullptr;
960}
961
962
963//- STL container iterator support --------------------------------------------
964static const ptrdiff_t PS_END_ADDR = 7; // non-aligned address, so no clash
965static const ptrdiff_t PS_FLAG_ADDR = 11; // id.
966static const ptrdiff_t PS_COLL_ADDR = 13; // id.
967
969{
970// Implement python's __iter__ for low level views used through STL-type begin()/end()
971 PyObject* iter = PyObject_CallMethodNoArgs(self, PyStrings::gBegin);
972
973 if (LowLevelView_Check(iter)) {
974 // builtin pointer iteration: can only succeed if a size is available
976 if (sz == -1) {
977 Py_DECREF(iter);
978 return nullptr;
979 }
980 PyObject* lliter = Py_TYPE(iter)->tp_iter(iter);
981 ((indexiterobject*)lliter)->ii_len = sz;
982 Py_DECREF(iter);
983 return lliter;
984 }
985
986 if (iter) {
987 Py_DECREF(iter);
988 PyErr_SetString(PyExc_TypeError, "unrecognized iterator type for low level views");
989 }
990
991 return nullptr;
992}
993
995{
996// Implement python's __iter__ for std::iterator<>s
997 PyObject* iter = PyObject_CallMethodNoArgs(self, PyStrings::gBegin);
998 if (iter) {
999 PyObject* end = PyObject_CallMethodNoArgs(self, PyStrings::gEnd);
1000 if (end) {
1001 if (CPPInstance_Check(iter)) {
1002 // use the data member cache to store extra state on the iterator object,
1003 // without it being visible on the Python side
1004 auto& dmc = ((CPPInstance*)iter)->GetDatamemberCache();
1005 dmc.push_back(std::make_pair(PS_END_ADDR, end));
1006
1007 // set a flag, indicating first iteration (reset in __next__)
1009 dmc.push_back(std::make_pair(PS_FLAG_ADDR, Py_False));
1010
1011 // make sure the iterated over collection remains alive for the duration
1012 Py_INCREF(self);
1013 dmc.push_back(std::make_pair(PS_COLL_ADDR, self));
1014 } else {
1015 // could store "end" on the object's dictionary anyway, but if end() returns
1016 // a user-customized object, then its __next__ is probably custom, too
1017 Py_DECREF(end);
1018 }
1019 }
1020 }
1021 return iter;
1022}
1023
1024//- generic iterator support over a sequence with operator[] and size ---------
1025//-----------------------------------------------------------------------------
1026static PyObject* index_iter(PyObject* c) {
1028 if (!ii) return nullptr;
1029
1030 Py_INCREF(c);
1031 ii->ii_container = c;
1032 ii->ii_pos = 0;
1033 ii->ii_len = PySequence_Size(c);
1034
1036 return (PyObject*)ii;
1037}
1038
1039
1040//- safe indexing for STL-like vector w/o iterator dictionaries ---------------
1041/* replaced by indexiterobject iteration, but may still have some future use ...
1042PyObject* CheckedGetItem(PyObject* self, PyObject* obj)
1043{
1044// Implement a generic python __getitem__ for STL-like classes that are missing the
1045// reflection info for their iterators. This is then used for iteration by means of
1046// consecutive indices, it such index is of integer type.
1047 Py_ssize_t size = PySequence_Size(self);
1048 Py_ssize_t idx = PyInt_AsSsize_t(obj);
1049 if ((size == (Py_ssize_t)-1 || idx == (Py_ssize_t)-1) && PyErr_Occurred()) {
1050 // argument conversion problem: let method itself resolve anew and report
1051 PyErr_Clear();
1052 return PyObject_CallMethodOneArg(self, PyStrings::gGetNoCheck, obj);
1053 }
1054
1055 bool inbounds = false;
1056 if (idx < 0) idx += size;
1057 if (0 <= idx && 0 <= size && idx < size)
1058 inbounds = true;
1059
1060 if (inbounds)
1061 return PyObject_CallMethodOneArg(self, PyStrings::gGetNoCheck, obj);
1062 else
1063 PyErr_SetString( PyExc_IndexError, "index out of range" );
1064
1065 return nullptr;
1066}*/
1067
1068
1069//- pair as sequence to allow tuple unpacking --------------------------------
1071{
1072// For std::map<> iteration, unpack std::pair<>s into tuples for the loop.
1073 long idx = PyLong_AsLong(pyindex);
1074 if (idx == -1 && PyErr_Occurred())
1075 return nullptr;
1076
1077 if (!CPPInstance_Check(self) || !((CPPInstance*)self)->GetObject()) {
1078 PyErr_SetString(PyExc_TypeError, "unsubscriptable object");
1079 return nullptr;
1080 }
1081
1082 if ((int)idx == 0)
1083 return PyObject_GetAttr(self, PyStrings::gFirst);
1084 else if ((int)idx == 1)
1085 return PyObject_GetAttr(self, PyStrings::gSecond);
1086
1087// still here? Trigger stop iteration
1088 PyErr_SetString(PyExc_IndexError, "out of bounds");
1089 return nullptr;
1090}
1091
1092//- simplistic len() functions -----------------------------------------------
1094 return PyInt_FromLong(2);
1095}
1096
1097
1098//- shared/unique_ptr behavior -----------------------------------------------
1099PyObject* SmartPtrInit(PyObject* self, PyObject* args, PyObject* /* kwds */)
1100{
1101// since the shared/unique pointer will take ownership, we need to relinquish it
1102 PyObject* realInit = PyObject_GetAttr(self, PyStrings::gRealInit);
1103 if (realInit) {
1104 PyObject* result = PyObject_Call(realInit, args, nullptr);
1106 if (result && PyTuple_GET_SIZE(args) == 1 && CPPInstance_Check(PyTuple_GET_ITEM(args, 0))) {
1108 if (!(cppinst->fFlags & CPPInstance::kIsSmartPtr)) cppinst->CppOwns();
1109 }
1110 return result;
1111 }
1112 return nullptr;
1113}
1114
1115
1116//- string behavior as primitives --------------------------------------------
1117#if PY_VERSION_HEX >= 0x03000000
1118// TODO: this is wrong, b/c it doesn't order
1121}
1122#endif
1123static inline
1124PyObject* CPyCppyy_PyString_FromCppString(std::string* s, bool native=true) {
1125 if (native)
1126 return PyBytes_FromStringAndSize(s->data(), s->size());
1127 return CPyCppyy_PyText_FromStringAndSize(s->data(), s->size());
1128}
1129
1130static inline
1131PyObject* CPyCppyy_PyString_FromCppString(std::wstring* s, bool native=true) {
1132 PyObject* pyobj = PyUnicode_FromWideChar(s->data(), s->size());
1133 if (pyobj && native) {
1134 PyObject* pybytes = PyUnicode_AsEncodedString(pyobj, "UTF-8", "strict");
1136 pyobj = pybytes;
1137 }
1138 return pyobj;
1139}
1140
1141#define CPPYY_IMPL_STRING_PYTHONIZATION(type, name) \
1142static inline \
1143PyObject* name##StringGetData(PyObject* self, bool native=true) \
1144{ \
1145 if (CPyCppyy::CPPInstance_Check(self)) { \
1146 type* obj = ((type*)((CPPInstance*)self)->GetObject()); \
1147 if (obj) return CPyCppyy_PyString_FromCppString(obj, native); \
1148 } \
1149 PyErr_Format(PyExc_TypeError, "object mismatch (%s expected)", #type); \
1150 return nullptr; \
1151} \
1152 \
1153PyObject* name##StringStr(PyObject* self) \
1154{ \
1155 PyObject* pyobj = name##StringGetData(self, false); \
1156 if (!pyobj) { \
1157 /* do a native conversion to make printing possible (debatable) */ \
1158 PyErr_Clear(); \
1159 PyObject* pybytes = name##StringGetData(self, true); \
1160 if (pybytes) { /* should not fail */ \
1161 pyobj = PyObject_Str(pybytes); \
1162 Py_DECREF(pybytes); \
1163 } \
1164 } \
1165 return pyobj; \
1166} \
1167 \
1168PyObject* name##StringBytes(PyObject* self) \
1169{ \
1170 return name##StringGetData(self, true); \
1171} \
1172 \
1173PyObject* name##StringRepr(PyObject* self) \
1174{ \
1175 PyObject* data = name##StringGetData(self, true); \
1176 if (data) { \
1177 PyObject* repr = PyObject_Repr(data); \
1178 Py_DECREF(data); \
1179 return repr; \
1180 } \
1181 return nullptr; \
1182} \
1183 \
1184PyObject* name##StringIsEqual(PyObject* self, PyObject* obj) \
1185{ \
1186 PyObject* data = name##StringGetData(self, PyBytes_Check(obj)); \
1187 if (data) { \
1188 PyObject* result = PyObject_RichCompare(data, obj, Py_EQ); \
1189 Py_DECREF(data); \
1190 return result; \
1191 } \
1192 return nullptr; \
1193} \
1194 \
1195PyObject* name##StringIsNotEqual(PyObject* self, PyObject* obj) \
1196{ \
1197 PyObject* data = name##StringGetData(self, PyBytes_Check(obj)); \
1198 if (data) { \
1199 PyObject* result = PyObject_RichCompare(data, obj, Py_NE); \
1200 Py_DECREF(data); \
1201 return result; \
1202 } \
1203 return nullptr; \
1204}
1205
1206// Only define STLStringCompare:
1207#define CPPYY_IMPL_STRING_PYTHONIZATION_CMP(type, name) \
1208CPPYY_IMPL_STRING_PYTHONIZATION(type, name) \
1209PyObject* name##StringCompare(PyObject* self, PyObject* obj) \
1210{ \
1211 PyObject* data = name##StringGetData(self, PyBytes_Check(obj)); \
1212 int result = 0; \
1213 if (data) { \
1214 result = PyObject_Compare(data, obj); \
1215 Py_DECREF(data); \
1216 } \
1217 if (PyErr_Occurred()) \
1218 return nullptr; \
1219 return PyInt_FromLong(result); \
1220}
1221
1224
1225static inline std::string* GetSTLString(CPPInstance* self) {
1226 if (!CPPInstance_Check(self)) {
1227 PyErr_SetString(PyExc_TypeError, "std::string object expected");
1228 return nullptr;
1229 }
1230
1231 std::string* obj = (std::string*)self->GetObject();
1232 if (!obj)
1233 PyErr_SetString(PyExc_ReferenceError, "attempt to access a null-pointer");
1234
1235 return obj;
1236}
1237
1239{
1240 std::string* obj = GetSTLString(self);
1241 if (!obj)
1242 return nullptr;
1243
1244 char* keywords[] = {(char*)"encoding", (char*)"errors", (char*)nullptr};
1245 const char* encoding; const char* errors;
1247 const_cast<char*>("s|s"), keywords, &encoding, &errors))
1248 return nullptr;
1249
1250 return PyUnicode_Decode(obj->data(), obj->size(), encoding, errors);
1251}
1252
1254{
1255 std::string* obj = GetSTLString(self);
1256 if (!obj)
1257 return nullptr;
1258
1259 const char* needle = CPyCppyy_PyText_AsString(pyobj);
1260 if (!needle)
1261 return nullptr;
1262
1263 if (obj->find(needle) != std::string::npos) {
1265 }
1266
1268}
1269
1271{
1272 std::string* obj = GetSTLString(self);
1273 if (!obj)
1274 return nullptr;
1275
1276// both str and std::string have a method "replace", but the Python version only
1277// accepts strings and takes no keyword arguments, whereas the C++ version has no
1278// overload that takes a string
1279
1280 if (2 <= PyTuple_GET_SIZE(args) && CPyCppyy_PyText_Check(PyTuple_GET_ITEM(args, 0))) {
1281 PyObject* pystr = CPyCppyy_PyText_FromStringAndSize(obj->data(), obj->size());
1282 PyObject* meth = PyObject_GetAttrString(pystr, (char*)"replace");
1285 Py_DECREF(meth);
1286 return result;
1287 }
1288
1289 PyObject* cppreplace = PyObject_GetAttrString((PyObject*)self, (char*)"__cpp_replace");
1290 if (cppreplace) {
1291 PyObject* result = PyObject_Call(cppreplace, args, nullptr);
1293 return result;
1294 }
1295
1296 PyErr_SetString(PyExc_AttributeError, "\'std::string\' object has no attribute \'replace\'");
1297 return nullptr;
1298}
1299
1300#define CPYCPPYY_STRING_FINDMETHOD(name, cppname, pyname) \
1301PyObject* STLString##name(CPPInstance* self, PyObject* args, PyObject* /*kwds*/) \
1302{ \
1303 std::string* obj = GetSTLString(self); \
1304 if (!obj) \
1305 return nullptr; \
1306 \
1307 PyObject* cppmeth = PyObject_GetAttrString((PyObject*)self, (char*)#cppname);\
1308 if (cppmeth) { \
1309 PyObject* result = PyObject_Call(cppmeth, args, nullptr); \
1310 Py_DECREF(cppmeth); \
1311 if (result) { \
1312 if (PyLongOrInt_AsULong64(result) == (PY_ULONG_LONG)std::string::npos) {\
1313 Py_DECREF(result); \
1314 return PyInt_FromLong(-1); \
1315 } \
1316 return result; \
1317 } \
1318 PyErr_Clear(); \
1319 } \
1320 \
1321 PyObject* pystr = CPyCppyy_PyText_FromStringAndSize(obj->data(), obj->size());\
1322 PyObject* pymeth = PyObject_GetAttrString(pystr, (char*)#pyname); \
1323 Py_DECREF(pystr); \
1324 PyObject* result = PyObject_CallObject(pymeth, args); \
1325 Py_DECREF(pymeth); \
1326 return result; \
1327}
1328
1329// both str and std::string have method "find" and "rfin"; try the C++ version first
1330// and fall back on the Python one in case of failure
1333
1335{
1336 std::string* obj = GetSTLString(self);
1337 if (!obj)
1338 return nullptr;
1339
1340 PyObject* pystr = CPyCppyy_PyText_FromStringAndSize(obj->data(), obj->size());
1343 return attr;
1344}
1345
1346
1347#if 0
1349{
1350// force C++ string types conversion to Python str per Python __repr__ requirements
1351 PyObject* res = PyObject_CallMethodNoArgs(self, PyStrings::gCppRepr);
1352 if (!res || CPyCppyy_PyText_Check(res))
1353 return res;
1355 Py_DECREF(res);
1356 return str_res;
1357}
1358
1360{
1361// force C++ string types conversion to Python str per Python __str__ requirements
1362 PyObject* res = PyObject_CallMethodNoArgs(self, PyStrings::gCppStr);
1363 if (!res || CPyCppyy_PyText_Check(res))
1364 return res;
1366 Py_DECREF(res);
1367 return str_res;
1368}
1369#endif
1370
1372{
1373// std::string objects hash to the same values as Python strings to allow
1374// matches in dictionaries etc.
1377 Py_DECREF(data);
1378 return h;
1379}
1380
1381
1382//- string_view behavior as primitive ----------------------------------------
1384{
1385// if constructed from a Python unicode object, the constructor will convert it
1386// to a temporary byte string, which is likely to go out of scope too soon; so
1387// buffer it as needed
1388 PyObject* realInit = PyObject_GetAttr(self, PyStrings::gRealInit);
1389 if (realInit) {
1390 PyObject *strbuf = nullptr, *newArgs = nullptr;
1391 if (PyTuple_GET_SIZE(args) == 1) {
1392 PyObject* arg0 = PyTuple_GET_ITEM(args, 0);
1393 if (PyUnicode_Check(arg0)) {
1394 // convert to the expected bytes array to control the temporary
1395 strbuf = PyUnicode_AsEncodedString(arg0, "UTF-8", "strict");
1396 newArgs = PyTuple_New(1);
1399 } else if (PyBytes_Check(arg0)) {
1400 // tie the life time of the provided string to the string_view
1401 Py_INCREF(arg0);
1402 strbuf = arg0;
1403 }
1404 }
1405
1406 PyObject* result = PyObject_Call(realInit, newArgs ? newArgs : args, nullptr);
1407
1410
1411 // if construction was successful and a string buffer was used, add a
1412 // life line to it from the string_view bound object
1413 if (result && self && strbuf)
1414 PyObject_SetAttr(self, PyStrings::gLifeLine, strbuf);
1416
1417 return result;
1418 }
1419 return nullptr;
1420}
1421
1422
1423//- STL iterator behavior ----------------------------------------------------
1425{
1426// Python iterator protocol __next__ for STL forward iterators.
1427 bool mustIncrement = true;
1428 PyObject* last = nullptr;
1429 if (CPPInstance_Check(self)) {
1430 auto& dmc = ((CPPInstance*)self)->GetDatamemberCache();
1431 for (auto& p: dmc) {
1432 if (p.first == PS_END_ADDR) {
1433 last = p.second;
1434 Py_INCREF(last);
1435 } else if (p.first == PS_FLAG_ADDR) {
1436 mustIncrement = p.second == Py_True;
1437 if (!mustIncrement) {
1438 Py_DECREF(p.second);
1440 p.second = Py_True;
1441 }
1442 }
1443 }
1444 }
1445
1446 PyObject* next = nullptr;
1447 if (last) {
1448 // handle special case of empty container (i.e. self is end)
1449 if (!PyObject_RichCompareBool(last, self, Py_EQ)) {
1450 bool iter_valid = true;
1451 if (mustIncrement) {
1452 // prefer preinc, but allow post-inc; in both cases, it is "self" that has
1453 // the updated state to dereference
1454 PyObject* iter = PyObject_CallMethodNoArgs(self, PyStrings::gPreInc);
1455 if (!iter) {
1456 PyErr_Clear();
1457 static PyObject* dummy = PyInt_FromLong(1l);
1458 iter = PyObject_CallMethodOneArg(self, PyStrings::gPostInc, dummy);
1459 }
1461 Py_XDECREF(iter);
1462 }
1463
1464 if (iter_valid) {
1465 next = PyObject_CallMethodNoArgs(self, PyStrings::gDeref);
1466 if (!next) PyErr_Clear();
1467 }
1468 }
1469 Py_DECREF(last);
1470 }
1471
1472 if (!next) PyErr_SetString(PyExc_StopIteration, "");
1473 return next;
1474}
1475
1476
1477//- STL complex<T> behavior --------------------------------------------------
1478#define COMPLEX_METH_GETSET(name, cppname) \
1479static PyObject* name##ComplexGet(PyObject* self, void*) { \
1480 return PyObject_CallMethodNoArgs(self, cppname); \
1481} \
1482static int name##ComplexSet(PyObject* self, PyObject* value, void*) { \
1483 PyObject* result = PyObject_CallMethodOneArg(self, cppname, value); \
1484 if (result) { \
1485 Py_DECREF(result); \
1486 return 0; \
1487 } \
1488 return -1; \
1489} \
1490PyGetSetDef name##Complex{(char*)#name, (getter)name##ComplexGet, (setter)name##ComplexSet, nullptr, nullptr};
1491
1492COMPLEX_METH_GETSET(real, PyStrings::gCppReal)
1493COMPLEX_METH_GETSET(imag, PyStrings::gCppImag)
1494
1496 PyObject* real = PyObject_CallMethodNoArgs(self, PyStrings::gCppReal);
1497 if (!real) return nullptr;
1498 double r = PyFloat_AsDouble(real);
1499 Py_DECREF(real);
1500 if (r == -1. && PyErr_Occurred())
1501 return nullptr;
1502
1503 PyObject* imag = PyObject_CallMethodNoArgs(self, PyStrings::gCppImag);
1504 if (!imag) return nullptr;
1505 double i = PyFloat_AsDouble(imag);
1506 Py_DECREF(imag);
1507 if (i == -1. && PyErr_Occurred())
1508 return nullptr;
1509
1510 return PyComplex_FromDoubles(r, i);
1511}
1512
1514 PyObject* real = PyObject_CallMethodNoArgs(self, PyStrings::gCppReal);
1515 if (!real) return nullptr;
1516 double r = PyFloat_AsDouble(real);
1517 Py_DECREF(real);
1518 if (r == -1. && PyErr_Occurred())
1519 return nullptr;
1520
1521 PyObject* imag = PyObject_CallMethodNoArgs(self, PyStrings::gCppImag);
1522 if (!imag) return nullptr;
1523 double i = PyFloat_AsDouble(imag);
1524 Py_DECREF(imag);
1525 if (i == -1. && PyErr_Occurred())
1526 return nullptr;
1527
1528 std::ostringstream s;
1529 s << '(' << r << '+' << i << "j)";
1530 return CPyCppyy_PyText_FromString(s.str().c_str());
1531}
1532
1534{
1535 return PyFloat_FromDouble(((std::complex<double>*)self->GetObject())->real());
1536}
1537
1538static int ComplexDRealSet(CPPInstance* self, PyObject* value, void*)
1539{
1540 double d = PyFloat_AsDouble(value);
1541 if (d == -1.0 && PyErr_Occurred())
1542 return -1;
1543 ((std::complex<double>*)self->GetObject())->real(d);
1544 return 0;
1545}
1546
1547PyGetSetDef ComplexDReal{(char*)"real", (getter)ComplexDRealGet, (setter)ComplexDRealSet, nullptr, nullptr};
1548
1549
1551{
1552 return PyFloat_FromDouble(((std::complex<double>*)self->GetObject())->imag());
1553}
1554
1555static int ComplexDImagSet(CPPInstance* self, PyObject* value, void*)
1556{
1557 double d = PyFloat_AsDouble(value);
1558 if (d == -1.0 && PyErr_Occurred())
1559 return -1;
1560 ((std::complex<double>*)self->GetObject())->imag(d);
1561 return 0;
1562}
1563
1564PyGetSetDef ComplexDImag{(char*)"imag", (getter)ComplexDImagGet, (setter)ComplexDImagSet, nullptr, nullptr};
1565
1567{
1568 double r = ((std::complex<double>*)self->GetObject())->real();
1569 double i = ((std::complex<double>*)self->GetObject())->imag();
1570 return PyComplex_FromDoubles(r, i);
1571}
1572
1573
1574} // unnamed namespace
1575
1576
1577//- public functions ---------------------------------------------------------
1578namespace CPyCppyy {
1579 std::set<std::string> gIteratorTypes;
1580}
1581
1582static inline
1583bool run_pythonizors(PyObject* pyclass, PyObject* pyname, const std::vector<PyObject*>& v)
1584{
1585 PyObject* args = PyTuple_New(2);
1588
1589 bool pstatus = true;
1590 for (auto pythonizor : v) {
1592 if (!result) {
1593 pstatus = false; // TODO: detail the error handling
1594 break;
1595 }
1597 }
1598 Py_DECREF(args);
1599
1600 return pstatus;
1601}
1602
1603bool CPyCppyy::Pythonize(PyObject* pyclass, const std::string& name)
1604{
1605// Add pre-defined pythonizations (for STL and ROOT) to classes based on their
1606// signature and/or class name.
1607 if (!pyclass)
1608 return false;
1609
1611
1612//- method name based pythonization ------------------------------------------
1613
1614// for smart pointer style classes that are otherwise not known as such; would
1615// prefer operator-> as that returns a pointer (which is simpler since it never
1616// has to deal with ref-assignment), but operator* plays better with STL iters
1617// and algorithms
1618 if (HasAttrDirect(pyclass, PyStrings::gDeref) && !Cppyy::IsSmartPtr(klass->fCppType))
1620 else if (HasAttrDirect(pyclass, PyStrings::gFollow) && !Cppyy::IsSmartPtr(klass->fCppType))
1622
1623// for pre-check of nullptr for boolean types
1624 if (HasAttrDirect(pyclass, PyStrings::gCppBool)) {
1625#if PY_VERSION_HEX >= 0x03000000
1626 const char* pybool_name = "__bool__";
1627#else
1628 const char* pybool_name = "__nonzero__";
1629#endif
1631 }
1632
1633// for STL containers, and user classes modeled after them
1634 if (HasAttrDirect(pyclass, PyStrings::gSize))
1635 Utility::AddToClass(pyclass, "__len__", "size");
1636
1637 if (!IsTemplatedSTLClass(name, "vector") && // vector is dealt with below
1639 if (HasAttrDirect(pyclass, PyStrings::gBegin) && HasAttrDirect(pyclass, PyStrings::gEnd)) {
1640 // obtain the name of the return type
1641 const auto& v = Cppyy::GetMethodIndicesFromName(klass->fCppType, "begin");
1642 if (!v.empty()) {
1643 // check return type; if not explicitly an iterator, add it to the "known" return
1644 // types to add the "next" method on use
1646 const std::string& resname = Cppyy::GetMethodResultType(meth);
1647 bool isIterator = gIteratorTypes.find(resname) != gIteratorTypes.end();
1649 if (resname.find("iterator") == std::string::npos)
1650 gIteratorTypes.insert(resname);
1651 isIterator = true;
1652 }
1653
1654 if (isIterator) {
1655 // install iterator protocol a la STL
1658 } else {
1659 // still okay if this is some pointer type of builtin persuasion (general class
1660 // won't work: the return type needs to understand the iterator protocol)
1661 std::string resolved = Cppyy::ResolveName(resname);
1662 if (resolved.back() == '*' && Cppyy::IsBuiltin(resolved.substr(0, resolved.size()-1))) {
1665 }
1666 }
1667 }
1668 }
1669 if (!((PyTypeObject*)pyclass)->tp_iter && // no iterator resolved
1670 HasAttrDirect(pyclass, PyStrings::gGetItem) && PyObject_HasAttr(pyclass, PyStrings::gLen)) {
1671 // Python will iterate over __getitem__ using integers, but C++ operator[] will never raise
1672 // a StopIteration. A checked getitem (raising IndexError if beyond size()) works in some
1673 // cases but would mess up if operator[] is meant to implement an associative container. So,
1674 // this has to be implemented as an iterator protocol.
1677 }
1678 }
1679
1680// operator==/!= are used in op_richcompare of CPPInstance, which subsequently allows
1681// comparisons to None; if no operator is available, a hook is installed for lazy
1682// lookups in the global and/or class namespace
1683 if (HasAttrDirect(pyclass, PyStrings::gEq, true) && \
1684 Cppyy::GetMethodIndicesFromName(klass->fCppType, "__eq__").empty()) {
1685 PyObject* cppol = PyObject_GetAttr(pyclass, PyStrings::gEq);
1686 if (!klass->fOperators) klass->fOperators = new Utility::PyOperators();
1687 klass->fOperators->fEq = cppol;
1688 // re-insert the forwarding __eq__ from the CPPInstance in case there was a Python-side
1689 // override in the base class
1690 static PyObject* top_eq = nullptr;
1691 if (!top_eq) {
1693 top_eq = PyObject_GetAttr(top_cls, PyStrings::gEq);
1694 Py_DECREF(top_eq); // make it borrowed
1696 }
1697 PyObject_SetAttr(pyclass, PyStrings::gEq, top_eq);
1698 }
1699
1700 if (HasAttrDirect(pyclass, PyStrings::gNe, true) && \
1701 Cppyy::GetMethodIndicesFromName(klass->fCppType, "__ne__").empty()) {
1702 PyObject* cppol = PyObject_GetAttr(pyclass, PyStrings::gNe);
1703 if (!klass->fOperators) klass->fOperators = new Utility::PyOperators();
1704 klass->fOperators->fNe = cppol;
1705 // re-insert the forwarding __ne__ (same reason as above for __eq__)
1706 static PyObject* top_ne = nullptr;
1707 if (!top_ne) {
1709 top_ne = PyObject_GetAttr(top_cls, PyStrings::gNe);
1710 Py_DECREF(top_ne); // make it borrowed
1712 }
1713 PyObject_SetAttr(pyclass, PyStrings::gNe, top_ne);
1714 }
1715
1716#if 0
1717 if (HasAttrDirect(pyclass, PyStrings::gRepr, true)) {
1718 // guarantee that the result of __repr__ is a Python string
1719 Utility::AddToClass(pyclass, "__cpp_repr", "__repr__");
1721 }
1722
1723 if (HasAttrDirect(pyclass, PyStrings::gStr, true)) {
1724 // guarantee that the result of __str__ is a Python string
1725 Utility::AddToClass(pyclass, "__cpp_str", "__str__");
1727 }
1728#endif
1729
1730 // This pythonization is disabled for ROOT because it is a bit buggy
1731#if 0
1732 if (Cppyy::IsAggregate(((CPPClass*)pyclass)->fCppType) && name.compare(0, 5, "std::", 5) != 0) {
1733 // create a pseudo-constructor to allow initializer-style object creation
1734 Cppyy::TCppType_t kls = ((CPPClass*)pyclass)->fCppType;
1736 if (ndata) {
1737 std::string rname = name;
1739
1740 std::ostringstream initdef;
1741 initdef << "namespace __cppyy_internal {\n"
1742 << "void init_" << rname << "(" << name << "*& self";
1743 bool codegen_ok = true;
1744 std::vector<std::string> arg_types, arg_names, arg_defaults;
1745 arg_types.reserve(ndata); arg_names.reserve(ndata); arg_defaults.reserve(ndata);
1746 for (Cppyy::TCppIndex_t i = 0; i < ndata; ++i) {
1748 continue;
1749
1750 const std::string& txt = Cppyy::GetDatamemberType(kls, i);
1751 const std::string& res = Cppyy::IsEnum(txt) ? txt : Cppyy::ResolveName(txt);
1752 const std::string& cpd = TypeManip::compound(res);
1753 std::string res_clean = TypeManip::clean_type(res, false, true);
1754
1755 if (res_clean == "internal_enum_type_t")
1756 res_clean = txt; // restore (properly scoped name)
1757
1758 if (res.rfind(']') == std::string::npos && res.rfind(')') == std::string::npos) {
1759 if (!cpd.empty()) arg_types.push_back(res_clean+cpd);
1760 else arg_types.push_back("const "+res_clean+"&");
1761 arg_names.push_back(Cppyy::GetDatamemberName(kls, i));
1762 if ((!cpd.empty() && cpd.back() == '*') || Cppyy::IsBuiltin(res_clean))
1763 arg_defaults.push_back("0");
1764 else {
1767 }
1768 } else {
1769 codegen_ok = false; // TODO: how to support arrays, anonymous enums, etc?
1770 break;
1771 }
1772 }
1773
1774 if (codegen_ok && !arg_types.empty()) {
1775 bool defaults_ok = arg_defaults.size() == arg_types.size();
1776 for (std::vector<std::string>::size_type i = 0; i < arg_types.size(); ++i) {
1777 initdef << ", " << arg_types[i] << " " << arg_names[i];
1778 if (defaults_ok) initdef << " = " << arg_defaults[i];
1779 }
1780 initdef << ") {\n self = new " << name << "{";
1781 for (std::vector<std::string>::size_type i = 0; i < arg_names.size(); ++i) {
1782 if (i != 0) initdef << ", ";
1783 initdef << arg_names[i];
1784 }
1785 initdef << "};\n} }";
1786
1787 if (Cppyy::Compile(initdef.str(), true /* silent */)) {
1788 Cppyy::TCppScope_t cis = Cppyy::GetScope("__cppyy_internal");
1789 const auto& mix = Cppyy::GetMethodIndicesFromName(cis, "init_"+rname);
1790 if (mix.size()) {
1791 if (!Utility::AddToClass(pyclass, "__init__",
1793 PyErr_Clear();
1794 }
1795 }
1796 }
1797 }
1798 }
1799#endif
1800
1801
1802//- class name based pythonization -------------------------------------------
1803
1804 if (IsTemplatedSTLClass(name, "vector")) {
1805
1806 // std::vector<bool> is a special case in C++
1808 if (klass->fCppType == sVectorBoolTypeID) {
1811 } else {
1812 // constructor that takes python collections
1813 Utility::AddToClass(pyclass, "__real_init", "__init__");
1815
1816 // data with size
1817 Utility::AddToClass(pyclass, "__real_data", "data");
1819
1820 // The addition of the __array__ utility to std::vector Python proxies causes a
1821 // bug where the resulting array is a single dimension, causing loss of data when
1822 // converting to numpy arrays, for >1dim vectors. Since this C++ pythonization
1823 // was added with the upgrade in 6.32, and is only defined and used recursively,
1824 // the safe option is to disable this function and no longer add it.
1825#if 0
1826 // numpy array conversion
1828#endif
1829 // checked getitem
1830 if (HasAttrDirect(pyclass, PyStrings::gLen)) {
1831 Utility::AddToClass(pyclass, "_getitem__unchecked", "__getitem__");
1833 }
1834
1835 // vector-optimized iterator protocol
1837
1838 // optimized __iadd__
1840
1841 // helpers for iteration
1842 const std::string& vtype = Cppyy::ResolveName(name+"::value_type");
1843 if (vtype.rfind("value_type") == std::string::npos) { // actually resolved?
1845 PyObject_SetAttr(pyclass, PyStrings::gValueType, pyvalue_type);
1847 }
1848
1849 size_t typesz = Cppyy::SizeOf(name+"::value_type");
1850 if (typesz) {
1852 PyObject_SetAttr(pyclass, PyStrings::gValueSize, pyvalue_size);
1854 }
1855 }
1856 }
1857
1858 else if (IsTemplatedSTLClass(name, "array")) {
1859 // constructor that takes python associative collections
1860 Utility::AddToClass(pyclass, "__real_init", "__init__");
1862 }
1863
1864 else if (IsTemplatedSTLClass(name, "map") || IsTemplatedSTLClass(name, "unordered_map")) {
1865 // constructor that takes python associative collections
1866 Utility::AddToClass(pyclass, "__real_init", "__init__");
1868
1870 }
1871
1872 else if (IsTemplatedSTLClass(name, "set")) {
1873 // constructor that takes python associative collections
1874 Utility::AddToClass(pyclass, "__real_init", "__init__");
1876
1878 }
1879
1880 else if (IsTemplatedSTLClass(name, "pair")) {
1883 }
1884
1885 if (IsTemplatedSTLClass(name, "shared_ptr") || IsTemplatedSTLClass(name, "unique_ptr")) {
1886 Utility::AddToClass(pyclass, "__real_init", "__init__");
1888 }
1889
1890 else if (!((PyTypeObject*)pyclass)->tp_iter && \
1891 (name.find("iterator") != std::string::npos || gIteratorTypes.find(name) != gIteratorTypes.end())) {
1892 ((PyTypeObject*)pyclass)->tp_iternext = (iternextfunc)STLIterNext;
1896 }
1897
1898 else if (name == "string" || name == "std::string") { // TODO: ask backend as well
1907 Utility::AddToClass(pyclass, "__cpp_find", "find");
1909 Utility::AddToClass(pyclass, "__cpp_rfind", "rfind");
1911 Utility::AddToClass(pyclass, "__cpp_replace", "replace");
1914
1915 // to allow use of std::string in dictionaries and findable with str
1917 }
1918
1919 else if (name == "basic_string_view<char>" || name == "std::basic_string_view<char>") {
1920 Utility::AddToClass(pyclass, "__real_init", "__init__");
1922 }
1923
1924 else if (name == "basic_string<wchar_t,char_traits<wchar_t>,allocator<wchar_t> >" || name == "std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >") {
1931 }
1932
1933 else if (name == "complex<double>" || name == "std::complex<double>") {
1934 Utility::AddToClass(pyclass, "__cpp_real", "real");
1936 Utility::AddToClass(pyclass, "__cpp_imag", "imag");
1940 }
1941
1942 else if (IsTemplatedSTLClass(name, "complex")) {
1943 Utility::AddToClass(pyclass, "__cpp_real", "real");
1945 Utility::AddToClass(pyclass, "__cpp_imag", "imag");
1949 }
1950
1951// direct user access; there are two calls here:
1952// - explicit pythonization: won't fall through to the base classes and is preferred if present
1953// - normal pythonization: only called if explicit isn't present, falls through to base classes
1954 bool bUserOk = true; PyObject* res = nullptr;
1956 if (HasAttrDirect(pyclass, PyStrings::gExPythonize)) {
1957 res = PyObject_CallMethodObjArgs(pyclass, PyStrings::gExPythonize, pyclass, pyname, nullptr);
1958 bUserOk = (bool)res;
1959 } else {
1960 PyObject* func = PyObject_GetAttr(pyclass, PyStrings::gPythonize);
1961 if (func) {
1962 res = PyObject_CallFunctionObjArgs(func, pyclass, pyname, nullptr);
1963 Py_DECREF(func);
1964 bUserOk = (bool)res;
1965 } else
1966 PyErr_Clear();
1967 }
1968 if (!bUserOk) {
1970 return false;
1971 } else {
1972 Py_XDECREF(res);
1973 // pyname handed to args tuple below
1974 }
1975
1976// call registered pythonizors, if any: first run the namespace-specific pythonizors, then
1977// the global ones (the idea is to allow writing a pythonizor that see all classes)
1978 bool pstatus = true;
1980 if (!outer_scope.empty()) {
1981 auto p = gPythonizations.find(outer_scope);
1982 if (p != gPythonizations.end()) {
1984 name.substr(outer_scope.size()+2, std::string::npos).c_str());
1987 }
1988 }
1989
1990 if (pstatus) {
1991 auto p = gPythonizations.find("");
1992 if (p != gPythonizations.end())
1993 pstatus = run_pythonizors(pyclass, pyname, p->second);
1994 }
1995
1997
1998// phew! all done ...
1999 return pstatus;
2000}
#define Py_TYPE(ob)
Definition CPyCppyy.h:196
#define Py_RETURN_TRUE
Definition CPyCppyy.h:272
#define Py_RETURN_FALSE
Definition CPyCppyy.h:276
#define PyInt_FromSsize_t
Definition CPyCppyy.h:217
#define CPyCppyy_PyText_FromStringAndSize
Definition CPyCppyy.h:85
#define PyBytes_Check
Definition CPyCppyy.h:61
#define PyInt_AsSsize_t
Definition CPyCppyy.h:216
#define CPyCppyy_PySliceCast
Definition CPyCppyy.h:189
#define CPyCppyy_PyText_AsString
Definition CPyCppyy.h:76
long Py_hash_t
Definition CPyCppyy.h:114
static PyObject * PyObject_CallMethodOneArg(PyObject *obj, PyObject *name, PyObject *arg)
Definition CPyCppyy.h:367
#define PyBytes_FromStringAndSize
Definition CPyCppyy.h:70
#define Py_RETURN_NONE
Definition CPyCppyy.h:268
#define CPyCppyy_PyText_Type
Definition CPyCppyy.h:94
static PyObject * PyObject_CallMethodNoArgs(PyObject *obj, PyObject *name)
Definition CPyCppyy.h:363
#define CPPYY__next__
Definition CPyCppyy.h:112
#define CPyCppyy_PyText_FromString
Definition CPyCppyy.h:81
#define CPyCppyy_PyText_Check
Definition CPyCppyy.h:74
_object PyObject
#define CPPYY_IMPL_STRING_PYTHONIZATION_CMP(type, name)
static bool run_pythonizors(PyObject *pyclass, PyObject *pyname, const std::vector< PyObject * > &v)
#define COMPLEX_METH_GETSET(name, cppname)
#define CPYCPPYY_STRING_FINDMETHOD(name, cppname, pyname)
#define PyObject_LengthHint
void FillVector(std::vector< double > &v, int size, T *a)
#define d(i)
Definition RSha256.hxx:102
#define c(i)
Definition RSha256.hxx:101
#define h(i)
Definition RSha256.hxx:106
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
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 r
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 char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t attr
char name[80]
Definition TGX11.cxx:110
const_iterator end() const
void cppscope_to_legalname(std::string &cppscope)
std::string clean_type(const std::string &cppname, bool template_strip=true, bool const_strip=true)
std::string compound(const std::string &name)
std::string extract_namespace(const std::string &name)
Py_ssize_t GetBuffer(PyObject *pyobject, char tc, int size, void *&buf, bool check=true)
Definition Utility.cxx:808
bool AddToClass(PyObject *pyclass, const char *label, PyCFunction cfunc, int flags=METH_VARARGS)
Definition Utility.cxx:182
PyTypeObject VectorIter_Type
static PyObject * GetAttrDirect(PyObject *pyclass, PyObject *pyname)
bool Pythonize(PyObject *pyclass, const std::string &name)
bool CPPOverload_Check(T *object)
Definition CPPOverload.h:90
std::map< std::string, std::vector< PyObject * > > gPythonizations
bool CPPScope_Check(T *object)
Definition CPPScope.h:81
bool LowLevelView_Check(T *object)
bool CPPInstance_Check(T *object)
PyTypeObject IndexIter_Type
PyObject * gThisModule
Definition CPPMethod.cxx:30
CPYCPPYY_EXTERN Converter * CreateConverter(const std::string &name, cdims_t=0)
std::set< std::string > gIteratorTypes
size_t TCppIndex_t
Definition cpp_cppyy.h:24
RPY_EXPORTED size_t SizeOf(TCppType_t klass)
intptr_t TCppMethod_t
Definition cpp_cppyy.h:22
RPY_EXPORTED bool IsDefaultConstructable(TCppType_t type)
RPY_EXPORTED bool IsEnum(const std::string &type_name)
RPY_EXPORTED std::vector< TCppIndex_t > GetMethodIndicesFromName(TCppScope_t scope, const std::string &name)
RPY_EXPORTED TCppIndex_t GetNumDatamembers(TCppScope_t scope, bool accept_namespace=false)
RPY_EXPORTED bool Compile(const std::string &code, bool silent=false)
RPY_EXPORTED std::string ResolveName(const std::string &cppitem_name)
TCppScope_t TCppType_t
Definition cpp_cppyy.h:19
RPY_EXPORTED bool IsAggregate(TCppType_t type)
RPY_EXPORTED std::string GetScopedFinalName(TCppType_t type)
RPY_EXPORTED bool IsPublicData(TCppScope_t scope, TCppIndex_t idata)
RPY_EXPORTED bool IsBuiltin(const std::string &type_name)
RPY_EXPORTED bool IsStaticData(TCppScope_t scope, TCppIndex_t idata)
RPY_EXPORTED std::string GetDatamemberType(TCppScope_t scope, TCppIndex_t idata)
RPY_EXPORTED TCppMethod_t GetMethod(TCppScope_t scope, TCppIndex_t imeth)
RPY_EXPORTED bool IsSmartPtr(TCppType_t type)
RPY_EXPORTED TCppScope_t GetScope(const std::string &scope_name)
size_t TCppScope_t
Definition cpp_cppyy.h:18
RPY_EXPORTED std::string GetMethodResultType(TCppMethod_t)
RPY_EXPORTED std::string GetDatamemberName(TCppScope_t scope, TCppIndex_t idata)