Logo ROOT  
Reference Guide
 
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//-----------------------------------------------------------------------------
38bool HasAttrDirect(PyObject* pyclass, PyObject* pyname, bool mustBeCPyCppyy = false) {
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) {
43 PyObject* attr = PyObject_GetItem(dct, pyname);
44 Py_DECREF(dct);
45 if (attr) {
46 bool ret = !mustBeCPyCppyy || CPPOverload_Check(attr);
47 Py_DECREF(attr);
48 return ret;
49 }
50 }
51 PyErr_Clear();
52 return false;
53}
54
55PyObject* GetAttrDirect(PyObject* pyclass, PyObject* pyname) {
56// get an attribute without causing getattr lookups
57 PyObject* dct = PyObject_GetAttr(pyclass, PyStrings::gDict);
58 if (dct) {
59 PyObject* attr = PyObject_GetItem(dct, pyname);
60 Py_DECREF(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);
88 PyObject* result = PyObject_CallMethod(
89 obj, const_cast<char*>(meth), const_cast<char*>("O"), arg1);
90 Py_DECREF(obj);
91 return result;
92}
93
94//-----------------------------------------------------------------------------
95PyObject* PyStyleIndex(PyObject* self, PyObject* index)
96{
97// Helper; converts python index into straight C index.
99 if (idx == (Py_ssize_t)-1 && PyErr_Occurred())
100 return nullptr;
101
102 Py_ssize_t size = PySequence_Size(self);
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) {
110 Py_INCREF(index);
111 pyindex = index;
112 } else
113 pyindex = PyLong_FromSsize_t(size+idx);
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//-----------------------------------------------------------------------------
134inline PyObject* CallSelfIndex(CPPInstance* self, PyObject* idx, PyObject* pymeth)
135{
136// Helper; call method with signature: meth(pyindex).
137 Py_INCREF((PyObject*)self);
138 PyObject* pyindex = PyStyleIndex((PyObject*)self, idx);
139 if (!pyindex) {
140 Py_DECREF((PyObject*)self);
141 return nullptr;
142 }
143
144 PyObject* result = PyObject_CallMethodOneArg((PyObject*)self, pymeth, pyindex);
145 Py_DECREF(pyindex);
146 Py_DECREF((PyObject*)self);
147 return result;
148}
149
150//- "smart pointer" behavior ---------------------------------------------------
151PyObject* DeRefGetAttr(PyObject* self, PyObject* name)
152{
153// Follow operator*() if present (available in python as __deref__), so that
154// smart pointers behave as expected.
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.
159 PyErr_SetString(PyExc_AttributeError, CPyCppyy_PyText_AsString(name));
160 return nullptr;
161 }
162
164 PyErr_SetString(PyExc_TypeError, "getattr(): attribute name must be string");
165
167 if (!pyptr)
168 return nullptr;
169
170// prevent a potential infinite loop
171 if (Py_TYPE(pyptr) == Py_TYPE(self)) {
172 PyObject* val1 = PyObject_Str(self);
173 PyObject* val2 = PyObject_Str(name);
174 PyErr_Format(PyExc_AttributeError, "%s has no attribute \'%s\'",
176 Py_DECREF(val2);
177 Py_DECREF(val1);
178
179 Py_DECREF(pyptr);
180 return nullptr;
181 }
182
183 PyObject* result = PyObject_GetAttr(pyptr, name);
184 Py_DECREF(pyptr);
185 return result;
186}
187
188//-----------------------------------------------------------------------------
189PyObject* FollowGetAttr(PyObject* self, PyObject* name)
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
197 if (!pyptr)
198 return nullptr;
199
200 PyObject* result = PyObject_GetAttr(pyptr, name);
201 Py_DECREF(pyptr);
202 return result;
203}
204
205//- pointer checking bool converter -------------------------------------------
206PyObject* NullCheckBool(PyObject* self)
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
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)
298 if (PyObject_CheckBuffer(fi))
299 return nullptr;
300
301 if (PyTuple_CheckExact(fi))
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 {
308 PyObject* iter = PyObject_GetIter(fi);
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
337 PyObject* fi = PySequence_GetItem(PyTuple_GET_ITEM(args, 0), 0);
338 if (!fi) PyErr_Clear();
339 if (fi && (PyTuple_CheckExact(fi) || PyList_CheckExact(fi))) {
340 // use emplace_back to construct the vector entries one by one
341 PyObject* eb_call = PyObject_GetAttrString(vecin, (char*)"emplace_back");
343 bool value_is_vector = false;
344 if (vtype && CPyCppyy_PyText_Check(vtype)) {
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();
350 Py_XDECREF(vtype);
351
352 if (eb_call) {
353 PyObject* eb_args;
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)) {
363 Py_ssize_t isz = PyList_GET_SIZE(item);
364 eb_args = PyTuple_New(isz);
365 for (Py_ssize_t j = 0; j < isz; ++j) {
366 PyObject* iarg = PyList_GET_ITEM(item, j);
367 Py_INCREF(iarg);
368 PyTuple_SET_ITEM(eb_args, j, iarg);
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 }
377 PyObject* ebres = PyObject_CallObject(eb_call, eb_args);
378 Py_DECREF(eb_args);
379 if (!ebres) {
380 fill_ok = false;
381 break;
382 }
383 Py_DECREF(ebres);
384 } else {
385 if (PyErr_Occurred()) {
386 if (!(PyErr_ExceptionMatches(PyExc_IndexError) ||
387 PyErr_ExceptionMatches(PyExc_StopIteration)))
388 fill_ok = false;
389 else { PyErr_Clear(); }
390 }
391 break;
392 }
393 }
394 Py_DECREF(eb_call);
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) {
403 PyObject* pbres = PyObject_CallFunctionObjArgs(pb_call, item, nullptr);
404 Py_DECREF(item);
405 if (!pbres) {
406 fill_ok = false;
407 break;
408 }
409 Py_DECREF(pbres);
410 } else {
411 if (PyErr_Occurred()) {
412 if (!(PyErr_ExceptionMatches(PyExc_IndexError) ||
413 PyErr_ExceptionMatches(PyExc_StopIteration)))
414 fill_ok = false;
415 else { PyErr_Clear(); }
416 }
417 break;
418 }
419 }
420 Py_DECREF(pb_call);
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
440 Py_INCREF(self);
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);
448 if (PyObject_CheckBuffer(fi) && !(CPyCppyy_PyText_Check(fi) || PyBytes_Check(fi))) {
450 if (vend) {
451 PyObject* result = PyObject_CallMethodObjArgs(self, PyStrings::gInsert, vend, fi, nullptr);
452 Py_DECREF(vend);
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
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) {
486 Py_DECREF(result);
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);
497 Py_DECREF(realInit);
498 return result;
499 }
500
501 return nullptr;
502}
503
504//---------------------------------------------------------------------------
505PyObject* VectorData(PyObject* self, PyObject*)
506{
507 PyObject* pydata = CallPyObjMethod(self, "__real_data");
508 if (!LowLevelView_Check(pydata) && !CPPInstance_Check(pydata))
509 return pydata;
510
512 if (!pylen) {
513 PyErr_Clear();
514 return pydata;
515 }
516
517 long clen = PyInt_AsLong(pylen);
518 Py_DECREF(pylen);
519
520 if (CPPInstance_Check(pydata)) {
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//---------------------------------------------------------------------------
538PyObject* VectorArray(PyObject* self, PyObject* /* args */)
539{
540 PyObject* pydata = VectorData(self, nullptr);
542 Py_DECREF(pydata);
543 return view;
544}
545#endif
546
547//-----------------------------------------------------------------------------
548static PyObject* vector_iter(PyObject* v) {
549 vectoriterobject* vi = PyObject_GC_New(vectoriterobject, &VectorIter_Type);
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
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) {
563 if (pyvalue_size) {
564 vi->vi_stride = PyLong_AsLong(pyvalue_size);
565 Py_DECREF(pyvalue_size);
566 } else {
567 PyErr_Clear();
568 vi->vi_stride = 0;
569 }
570
571 if (CPyCppyy_PyText_Check(pyvalue_type)) {
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);
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;
605 Py_XDECREF(pydata);
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
616 Py_XDECREF(pyvalue_type);
617
618 vi->ii_pos = 0;
619 vi->ii_len = PySequence_Size(v);
620
621 PyObject_GC_Track(vi);
622 return (PyObject*)vi;
623}
624
625PyObject* VectorGetItem(CPPInstance* self, PySliceObject* index)
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
634 PyObject* pyclass = (PyObject*)Py_TYPE((PyObject*)self);
635 PyObject* nseq = PyObject_CallObject(pyclass, nullptr);
636
637 Py_ssize_t start, stop, step;
638 PySlice_GetIndices((CPyCppyy_PySliceCast)index, PyObject_Length((PyObject*)self), &start, &stop, &step);
639
640 const Py_ssize_t nlen = PySequence_Size((PyObject*)self);
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) {
646 PyObject* pyidx = PyInt_FromSsize_t(i);
648 CallPyObjMethod(nseq, "push_back", item);
649 Py_DECREF(item);
650 Py_DECREF(pyidx);
651 }
652
653 return nseq;
654 }
655
656 return CallSelfIndex(self, (PyObject*)index, PyStrings::gGetNoCheck);
657}
658
659
660static Cppyy::TCppType_t sVectorBoolTypeID = (Cppyy::TCppType_t)0;
661
662PyObject* VectorBoolGetItem(CPPInstance* self, PyObject* idx)
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) {
667 PyErr_Format(PyExc_TypeError,
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)) {
679 PyObject* pyclass = (PyObject*)Py_TYPE((PyObject*)self);
680 PyObject* nseq = PyObject_CallObject(pyclass, nullptr);
681
682 Py_ssize_t start, stop, step;
683 PySlice_GetIndices((CPyCppyy_PySliceCast)idx, PyObject_Length((PyObject*)self), &start, &stop, &step);
684 const Py_ssize_t nlen = PySequence_Size((PyObject*)self);
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) {
690 PyObject* pyidx = PyInt_FromSsize_t(i);
692 CallPyObjMethod(nseq, "push_back", item);
693 Py_DECREF(item);
694 Py_DECREF(pyidx);
695 }
696
697 return nseq;
698 }
699
700 PyObject* pyindex = PyStyleIndex((PyObject*)self, idx);
701 if (!pyindex)
702 return nullptr;
703
704 int index = (int)PyLong_AsLong(pyindex);
705 Py_DECREF(pyindex);
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
716PyObject* VectorBoolSetItem(CPPInstance* self, PyObject* args)
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) {
721 PyErr_Format(PyExc_TypeError,
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
736 PyObject* pyindex = PyStyleIndex((PyObject*)self, idx);
737 if (!pyindex)
738 return nullptr;
739
740 int index = (int)PyLong_AsLong(pyindex);
741 Py_DECREF(pyindex);
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
763 if (!result)
764 return nullptr;
765
766 PyObject* items = PyTuple_GET_ITEM(args, 0);
767 Py_ssize_t fillsz = PySequence_Size(items);
768 if (PySequence_Size(self) != fillsz) {
769 PyErr_Format(PyExc_ValueError, "received sequence of size %zd where %zd expected",
770 fillsz, PySequence_Size(self));
771 Py_DECREF(result);
772 return nullptr;
773 }
774
775 PyObject* si_call = PyObject_GetAttr(self, PyStrings::gSetItem);
776 for (Py_ssize_t i = 0; i < fillsz; ++i) {
777 PyObject* item = PySequence_GetItem(items, i);
779 PyObject* sires = PyObject_CallFunctionObjArgs(si_call, index, item, nullptr);
780 Py_DECREF(index);
781 Py_DECREF(item);
782 if (!sires) {
783 Py_DECREF(si_call);
784 Py_DECREF(result);
785 return nullptr;
786 } else
787 Py_DECREF(sires);
788 }
789 Py_DECREF(si_call);
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);
799 Py_DECREF(realInit);
800 return result;
801 }
802
803 return nullptr;
804}
805
806
807//- map behavior as primitives ------------------------------------------------
808static PyObject* MapFromPairs(PyObject* self, PyObject* pairs)
809{
810// construct an empty map, then fill it with the key, value pairs
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) {
817 PyObject* pair = PySequence_GetItem(pairs, i);
818 PyObject* sires = nullptr;
819 if (pair && PySequence_Check(pair) && PySequence_Size(pair) == 2) {
820 PyObject* key = PySequence_GetItem(pair, 0);
821 PyObject* value = PySequence_GetItem(pair, 1);
822 sires = PyObject_CallFunctionObjArgs(si_call, key, value, nullptr);
823 Py_DECREF(value);
824 Py_DECREF(key);
825 }
826 Py_DECREF(pair);
827 if (!sires) {
828 Py_DECREF(si_call);
829 Py_DECREF(result);
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
834 Py_DECREF(sires);
835 }
836 Py_DECREF(si_call);
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)) && \
850 !(PyTuple_Check(PyTuple_GET_ITEM(args, 0)) || PyList_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
857 PyObject* items = PyMapping_Items(assoc);
858#endif
859 if (items && PySequence_Check(items)) {
860 PyObject* result = MapFromPairs(self, items);
861 Py_DECREF(items);
862 return result;
863 }
864
865 Py_XDECREF(items);
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);
879 Py_DECREF(realInit);
880 return result;
881 }
882
883 return nullptr;
884}
885
886PyObject* STLContainsWithFind(PyObject* self, PyObject* obj)
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)) {
894 if (CPPInstance_Check(end)) {
895 if (!PyObject_RichCompareBool(iter, end, Py_EQ)) {
896 Py_INCREF(Py_True);
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
906 Py_INCREF(Py_False);
907 result = Py_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
923 if (!result)
924 return nullptr;
925
926 PyObject* iter = PyObject_GetIter(pyset);
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) {
935 PyObject* insres = PyObject_CallFunctionObjArgs(ins_call, item, nullptr);
936 Py_DECREF(item);
937 if (!insres) {
938 Py_DECREF(ins_call);
939 Py_DECREF(result);
940 return nullptr;
941 } else
942 Py_DECREF(insres);
943 item = getter.get();
944 }
945 Py_DECREF(ins_call);
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);
955 Py_DECREF(realInit);
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
968PyObject* LLSequenceIter(PyObject* self)
969{
970// Implement python's __iter__ for low level views used through STL-type begin()/end()
972
973 if (LowLevelView_Check(iter)) {
974 // builtin pointer iteration: can only succeed if a size is available
975 Py_ssize_t sz = PySequence_Size(self);
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
994PyObject* STLSequenceIter(PyObject* self)
995{
996// Implement python's __iter__ for std::iterator<>s
998 if (iter) {
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__)
1008 Py_INCREF(Py_False);
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) {
1027 indexiterobject* ii = PyObject_GC_New(indexiterobject, &IndexIter_Type);
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
1035 PyObject_GC_Track(ii);
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 --------------------------------
1070PyObject* PairUnpack(PyObject* self, PyObject* pyindex)
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 -----------------------------------------------
1093PyObject* ReturnTwo(CPPInstance*, PyObject*) {
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);
1105 Py_DECREF(realInit);
1106 if (result && PyTuple_GET_SIZE(args) == 1 && CPPInstance_Check(PyTuple_GET_ITEM(args, 0))) {
1107 CPPInstance* cppinst = (CPPInstance*)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
1119static int PyObject_Compare(PyObject* one, PyObject* other) {
1120 return !PyObject_RichCompareBool(one, other, Py_EQ);
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");
1135 Py_DECREF(pyobj);
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
1223CPPYY_IMPL_STRING_PYTHONIZATION_CMP(std::wstring, STLW)
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
1238PyObject* STLStringDecode(CPPInstance* self, PyObject* args, PyObject* kwds)
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;
1246 if (!PyArg_ParseTupleAndKeywords(args, kwds,
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
1253PyObject* STLStringContains(CPPInstance* self, PyObject* pyobj)
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
1270PyObject* STLStringReplace(CPPInstance* self, PyObject* args, PyObject* /*kwds*/)
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");
1283 Py_DECREF(pystr);
1284 PyObject* result = PyObject_CallObject(meth, args);
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);
1292 Py_DECREF(cppreplace);
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
1331CPYCPPYY_STRING_FINDMETHOD( Find, __cpp_find, find)
1332CPYCPPYY_STRING_FINDMETHOD(RFind, __cpp_rfind, rfind)
1333
1334PyObject* STLStringGetAttr(CPPInstance* self, PyObject* attr_name)
1335{
1336 std::string* obj = GetSTLString(self);
1337 if (!obj)
1338 return nullptr;
1339
1340 PyObject* pystr = CPyCppyy_PyText_FromStringAndSize(obj->data(), obj->size());
1341 PyObject* attr = PyObject_GetAttr(pystr, attr_name);
1342 Py_DECREF(pystr);
1343 return attr;
1344}
1345
1346
1347#if 0
1348PyObject* UTF8Repr(PyObject* self)
1349{
1350// force C++ string types conversion to Python str per Python __repr__ requirements
1352 if (!res || CPyCppyy_PyText_Check(res))
1353 return res;
1354 PyObject* str_res = PyObject_Str(res);
1355 Py_DECREF(res);
1356 return str_res;
1357}
1358
1359PyObject* UTF8Str(PyObject* self)
1360{
1361// force C++ string types conversion to Python str per Python __str__ requirements
1363 if (!res || CPyCppyy_PyText_Check(res))
1364 return res;
1365 PyObject* str_res = PyObject_Str(res);
1366 Py_DECREF(res);
1367 return str_res;
1368}
1369#endif
1370
1371Py_hash_t STLStringHash(PyObject* self)
1372{
1373// std::string objects hash to the same values as Python strings to allow
1374// matches in dictionaries etc.
1375 PyObject* data = STLStringGetData(self, false);
1377 Py_DECREF(data);
1378 return h;
1379}
1380
1381
1382//- string_view behavior as primitive ----------------------------------------
1383PyObject* StringViewInit(PyObject* self, PyObject* args, PyObject* /* kwds */)
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);
1397 Py_INCREF(strbuf);
1398 PyTuple_SET_ITEM(newArgs, 0, strbuf);
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
1408 Py_XDECREF(newArgs);
1409 Py_DECREF(realInit);
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);
1415 Py_XDECREF(strbuf);
1416
1417 return result;
1418 }
1419 return nullptr;
1420}
1421
1422
1423//- STL iterator behavior ----------------------------------------------------
1424PyObject* STLIterNext(PyObject* self)
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);
1439 Py_INCREF(Py_True);
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
1455 if (!iter) {
1456 PyErr_Clear();
1457 static PyObject* dummy = PyInt_FromLong(1l);
1459 }
1460 iter_valid = iter && PyObject_RichCompareBool(last, self, Py_NE);
1461 Py_XDECREF(iter);
1462 }
1463
1464 if (iter_valid) {
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
1494
1495static PyObject* ComplexComplex(PyObject* self) {
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
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
1513static PyObject* ComplexRepr(PyObject* self) {
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
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
1533static PyObject* ComplexDRealGet(CPPInstance* self, void*)
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
1550static PyObject* ComplexDImagGet(CPPInstance* self, void*)
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
1566static PyObject* ComplexDComplex(CPPInstance* self)
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);
1586 Py_INCREF(pyclass); PyTuple_SET_ITEM(args, 0, pyclass);
1587 Py_INCREF(pyname); PyTuple_SET_ITEM(args, 1, pyname);
1588
1589 bool pstatus = true;
1590 for (auto pythonizor : v) {
1591 PyObject* result = PyObject_CallObject(pythonizor, args);
1592 if (!result) {
1593 pstatus = false; // TODO: detail the error handling
1594 break;
1595 }
1596 Py_DECREF(result);
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
1610 CPPScope* klass = (CPPScope*)pyclass;
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))
1619 Utility::AddToClass(pyclass, "__getattr__", (PyCFunction)DeRefGetAttr, METH_O);
1620 else if (HasAttrDirect(pyclass, PyStrings::gFollow) && !Cppyy::IsSmartPtr(klass->fCppType))
1621 Utility::AddToClass(pyclass, "__getattr__", (PyCFunction)FollowGetAttr, METH_O);
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
1630 Utility::AddToClass(pyclass, pybool_name, (PyCFunction)NullCheckBool, METH_NOARGS);
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
1638 !((PyTypeObject*)pyclass)->tp_iter) {
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();
1648 if (!isIterator && Cppyy::GetScope(resname)) {
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
1656 ((PyTypeObject*)pyclass)->tp_iter = (getiterfunc)STLSequenceIter;
1657 Utility::AddToClass(pyclass, "__iter__", (PyCFunction)STLSequenceIter, METH_NOARGS);
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))) {
1663 ((PyTypeObject*)pyclass)->tp_iter = (getiterfunc)LLSequenceIter;
1664 Utility::AddToClass(pyclass, "__iter__", (PyCFunction)LLSequenceIter, METH_NOARGS);
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.
1675 ((PyTypeObject*)pyclass)->tp_iter = (getiterfunc)index_iter;
1676 Utility::AddToClass(pyclass, "__iter__", (PyCFunction)index_iter, METH_NOARGS);
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) {
1692 PyObject* top_cls = PyObject_GetAttrString(gThisModule, "CPPInstance");
1693 top_eq = PyObject_GetAttr(top_cls, PyStrings::gEq);
1694 Py_DECREF(top_eq); // make it borrowed
1695 Py_DECREF(top_cls);
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) {
1708 PyObject* top_cls = PyObject_GetAttrString(gThisModule, "CPPInstance");
1709 top_ne = PyObject_GetAttr(top_cls, PyStrings::gNe);
1710 Py_DECREF(top_ne); // make it borrowed
1711 Py_DECREF(top_cls);
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__");
1720 Utility::AddToClass(pyclass, "__repr__", (PyCFunction)UTF8Repr, METH_NOARGS);
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__");
1726 Utility::AddToClass(pyclass, "__str__", (PyCFunction)UTF8Str, METH_NOARGS);
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) {
1747 if (Cppyy::IsStaticData(kls, i) || !Cppyy::IsPublicData(kls, 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 {
1765 Cppyy::TCppScope_t klsid = Cppyy::GetScope(res_clean);
1766 if (Cppyy::IsDefaultConstructable(klsid)) arg_defaults.push_back(res_clean+"{}");
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__",
1792 new CPPFunction(cis, Cppyy::GetMethod(cis, mix[0]))))
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++
1807 if (!sVectorBoolTypeID) sVectorBoolTypeID = (Cppyy::TCppType_t)Cppyy::GetScope("std::vector<bool>");
1808 if (klass->fCppType == sVectorBoolTypeID) {
1809 Utility::AddToClass(pyclass, "__getitem__", (PyCFunction)VectorBoolGetItem, METH_O);
1810 Utility::AddToClass(pyclass, "__setitem__", (PyCFunction)VectorBoolSetItem);
1811 } else {
1812 // constructor that takes python collections
1813 Utility::AddToClass(pyclass, "__real_init", "__init__");
1814 Utility::AddToClass(pyclass, "__init__", (PyCFunction)VectorInit, METH_VARARGS | METH_KEYWORDS);
1815
1816 // data with size
1817 Utility::AddToClass(pyclass, "__real_data", "data");
1818 Utility::AddToClass(pyclass, "data", (PyCFunction)VectorData);
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
1827 Utility::AddToClass(pyclass, "__array__", (PyCFunction)VectorArray);
1828#endif
1829 // checked getitem
1830 if (HasAttrDirect(pyclass, PyStrings::gLen)) {
1831 Utility::AddToClass(pyclass, "_getitem__unchecked", "__getitem__");
1832 Utility::AddToClass(pyclass, "__getitem__", (PyCFunction)VectorGetItem, METH_O);
1833 }
1834
1835 // vector-optimized iterator protocol
1836 ((PyTypeObject*)pyclass)->tp_iter = (getiterfunc)vector_iter;
1837
1838 // optimized __iadd__
1839 Utility::AddToClass(pyclass, "__iadd__", (PyCFunction)VectorIAdd, METH_VARARGS | METH_KEYWORDS);
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?
1844 PyObject* pyvalue_type = CPyCppyy_PyText_FromString(vtype.c_str());
1845 PyObject_SetAttr(pyclass, PyStrings::gValueType, pyvalue_type);
1846 Py_DECREF(pyvalue_type);
1847 }
1848
1849 size_t typesz = Cppyy::SizeOf(name+"::value_type");
1850 if (typesz) {
1851 PyObject* pyvalue_size = PyLong_FromSsize_t(typesz);
1852 PyObject_SetAttr(pyclass, PyStrings::gValueSize, pyvalue_size);
1853 Py_DECREF(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__");
1861 Utility::AddToClass(pyclass, "__init__", (PyCFunction)ArrayInit, METH_VARARGS | METH_KEYWORDS);
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__");
1867 Utility::AddToClass(pyclass, "__init__", (PyCFunction)MapInit, METH_VARARGS | METH_KEYWORDS);
1868
1869 Utility::AddToClass(pyclass, "__contains__", (PyCFunction)STLContainsWithFind, METH_O);
1870 }
1871
1872 else if (IsTemplatedSTLClass(name, "set")) {
1873 // constructor that takes python associative collections
1874 Utility::AddToClass(pyclass, "__real_init", "__init__");
1875 Utility::AddToClass(pyclass, "__init__", (PyCFunction)SetInit, METH_VARARGS | METH_KEYWORDS);
1876
1877 Utility::AddToClass(pyclass, "__contains__", (PyCFunction)STLContainsWithFind, METH_O);
1878 }
1879
1880 else if (IsTemplatedSTLClass(name, "pair")) {
1881 Utility::AddToClass(pyclass, "__getitem__", (PyCFunction)PairUnpack, METH_O);
1882 Utility::AddToClass(pyclass, "__len__", (PyCFunction)ReturnTwo, METH_NOARGS);
1883 }
1884
1885 if (IsTemplatedSTLClass(name, "shared_ptr") || IsTemplatedSTLClass(name, "unique_ptr")) {
1886 Utility::AddToClass(pyclass, "__real_init", "__init__");
1887 Utility::AddToClass(pyclass, "__init__", (PyCFunction)SmartPtrInit, METH_VARARGS | METH_KEYWORDS);
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;
1893 Utility::AddToClass(pyclass, CPPYY__next__, (PyCFunction)STLIterNext, METH_NOARGS);
1894 ((PyTypeObject*)pyclass)->tp_iter = (getiterfunc)PyObject_SelfIter;
1895 Utility::AddToClass(pyclass, "__iter__", (PyCFunction)PyObject_SelfIter, METH_NOARGS);
1896 }
1897
1898 else if (name == "string" || name == "std::string") { // TODO: ask backend as well
1899 Utility::AddToClass(pyclass, "__repr__", (PyCFunction)STLStringRepr, METH_NOARGS);
1900 Utility::AddToClass(pyclass, "__str__", (PyCFunction)STLStringStr, METH_NOARGS);
1901 Utility::AddToClass(pyclass, "__bytes__", (PyCFunction)STLStringBytes, METH_NOARGS);
1902 Utility::AddToClass(pyclass, "__cmp__", (PyCFunction)STLStringCompare, METH_O);
1903 Utility::AddToClass(pyclass, "__eq__", (PyCFunction)STLStringIsEqual, METH_O);
1904 Utility::AddToClass(pyclass, "__ne__", (PyCFunction)STLStringIsNotEqual, METH_O);
1905 Utility::AddToClass(pyclass, "__contains__", (PyCFunction)STLStringContains, METH_O);
1906 Utility::AddToClass(pyclass, "decode", (PyCFunction)STLStringDecode, METH_VARARGS | METH_KEYWORDS);
1907 Utility::AddToClass(pyclass, "__cpp_find", "find");
1908 Utility::AddToClass(pyclass, "find", (PyCFunction)STLStringFind, METH_VARARGS | METH_KEYWORDS);
1909 Utility::AddToClass(pyclass, "__cpp_rfind", "rfind");
1910 Utility::AddToClass(pyclass, "rfind", (PyCFunction)STLStringRFind, METH_VARARGS | METH_KEYWORDS);
1911 Utility::AddToClass(pyclass, "__cpp_replace", "replace");
1912 Utility::AddToClass(pyclass, "replace", (PyCFunction)STLStringReplace, METH_VARARGS | METH_KEYWORDS);
1913 Utility::AddToClass(pyclass, "__getattr__", (PyCFunction)STLStringGetAttr, METH_O);
1914
1915 // to allow use of std::string in dictionaries and findable with str
1916 ((PyTypeObject*)pyclass)->tp_hash = (hashfunc)STLStringHash;
1917 }
1918
1919 else if (name == "basic_string_view<char>" || name == "std::basic_string_view<char>") {
1920 Utility::AddToClass(pyclass, "__real_init", "__init__");
1921 Utility::AddToClass(pyclass, "__init__", (PyCFunction)StringViewInit, METH_VARARGS | METH_KEYWORDS);
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> >") {
1925 Utility::AddToClass(pyclass, "__repr__", (PyCFunction)STLWStringRepr, METH_NOARGS);
1926 Utility::AddToClass(pyclass, "__str__", (PyCFunction)STLWStringStr, METH_NOARGS);
1927 Utility::AddToClass(pyclass, "__bytes__", (PyCFunction)STLWStringBytes, METH_NOARGS);
1928 Utility::AddToClass(pyclass, "__cmp__", (PyCFunction)STLWStringCompare, METH_O);
1929 Utility::AddToClass(pyclass, "__eq__", (PyCFunction)STLWStringIsEqual, METH_O);
1930 Utility::AddToClass(pyclass, "__ne__", (PyCFunction)STLWStringIsNotEqual, METH_O);
1931 }
1932
1933 else if (name == "complex<double>" || name == "std::complex<double>") {
1934 Utility::AddToClass(pyclass, "__cpp_real", "real");
1935 PyObject_SetAttrString(pyclass, "real", PyDescr_NewGetSet((PyTypeObject*)pyclass, &ComplexDReal));
1936 Utility::AddToClass(pyclass, "__cpp_imag", "imag");
1937 PyObject_SetAttrString(pyclass, "imag", PyDescr_NewGetSet((PyTypeObject*)pyclass, &ComplexDImag));
1938 Utility::AddToClass(pyclass, "__complex__", (PyCFunction)ComplexDComplex, METH_NOARGS);
1939 Utility::AddToClass(pyclass, "__repr__", (PyCFunction)ComplexRepr, METH_NOARGS);
1940 }
1941
1942 else if (IsTemplatedSTLClass(name, "complex")) {
1943 Utility::AddToClass(pyclass, "__cpp_real", "real");
1944 PyObject_SetAttrString(pyclass, "real", PyDescr_NewGetSet((PyTypeObject*)pyclass, &realComplex));
1945 Utility::AddToClass(pyclass, "__cpp_imag", "imag");
1946 PyObject_SetAttrString(pyclass, "imag", PyDescr_NewGetSet((PyTypeObject*)pyclass, &imagComplex));
1947 Utility::AddToClass(pyclass, "__complex__", (PyCFunction)ComplexComplex, METH_NOARGS);
1948 Utility::AddToClass(pyclass, "__repr__", (PyCFunction)ComplexRepr, METH_NOARGS);
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;
1955 PyObject* pyname = CPyCppyy_PyText_FromString(name.c_str());
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) {
1969 Py_DECREF(pyname);
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;
1979 std::string outer_scope = TypeManip::extract_namespace(name);
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());
1985 pstatus = run_pythonizors(pyclass, subname, p->second);
1986 Py_DECREF(subname);
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
1996 Py_DECREF(pyname);
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
int Py_ssize_t
Definition CPyCppyy.h:215
#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
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
Int_t i
Cppyy::TCppType_t ObjectIsA(bool check_smart=true) const
Utility::PyOperators * fOperators
Definition CPPScope.h:61
Cppyy::TCppType_t fCppType
Definition CPPScope.h:55
STL class.
PyObject * gCTypesType
Definition PyStrings.cxx:38
PyObject * gRealInit
Definition PyStrings.cxx:41
PyObject * gExPythonize
Definition PyStrings.cxx:71
PyObject * gLifeLine
Definition PyStrings.cxx:28
PyObject * gGetItem
Definition PyStrings.cxx:22
PyObject * gCppBool
Definition PyStrings.cxx:10
PyObject * gCppReal
Definition PyStrings.cxx:63
PyObject * gPythonize
Definition PyStrings.cxx:72
PyObject * gTypeCode
Definition PyStrings.cxx:37
PyObject * gPostInc
Definition PyStrings.cxx:17
PyObject * gCppImag
Definition PyStrings.cxx:64
PyObject * gValueSize
Definition PyStrings.cxx:61
PyObject * gSetItem
Definition PyStrings.cxx:24
PyObject * gGetNoCheck
Definition PyStrings.cxx:23
PyObject * gCppRepr
Definition PyStrings.cxx:34
PyObject * gValueType
Definition PyStrings.cxx:60
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
CPPScope CPPClass
Definition CPPScope.h:68
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)
PyObject_HEAD PyObject * ii_container
Cppyy::TCppType_t vi_klass
CPyCppyy::Converter * vi_converter