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
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.
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.
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
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)) {
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
196 PyObject* pyptr = PyObject_CallMethodNoArgs(self, PyStrings::gFollow);
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
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)
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");
342 PyObject* vtype = GetAttrDirect((PyObject*)Py_TYPE(vecin), PyStrings::gValueType);
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))) {
449 PyObject* vend = PyObject_CallMethodNoArgs(self, PyStrings::gEnd);
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
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) {
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
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);
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//---------------------------------------------------------------------------
531PyObject* VectorArray(PyObject* self, PyObject* /* args */)
532{
533 PyObject* pydata = VectorData(self, nullptr);
534 PyObject* view = PyObject_CallMethodNoArgs(pydata, PyStrings::gArray);
535 Py_DECREF(pydata);
536 return view;
537}
538
539
540//-----------------------------------------------------------------------------
541static PyObject* vector_iter(PyObject* v) {
542 vectoriterobject* vi = PyObject_GC_New(vectoriterobject, &VectorIter_Type);
543 if (!vi) return nullptr;
544
545 Py_INCREF(v);
546 vi->ii_container = v;
547
548// tell the iterator code to set a life line if this container is a temporary
550 if (v->ob_refcnt <= 2 || (((CPPInstance*)v)->fFlags & CPPInstance::kIsValue))
552
553 PyObject* pyvalue_type = PyObject_GetAttr((PyObject*)Py_TYPE(v), PyStrings::gValueType);
554 if (pyvalue_type) {
555 PyObject* pyvalue_size = GetAttrDirect((PyObject*)Py_TYPE(v), PyStrings::gValueSize);
556 if (pyvalue_size) {
557 vi->vi_stride = PyLong_AsLong(pyvalue_size);
558 Py_DECREF(pyvalue_size);
559 } else {
560 PyErr_Clear();
561 vi->vi_stride = 0;
562 }
563
564 if (CPyCppyy_PyText_Check(pyvalue_type)) {
565 std::string value_type = CPyCppyy_PyText_AsString(pyvalue_type);
566 vi->vi_klass = Cppyy::GetScope(value_type);
567 if (vi->vi_klass) {
568 vi->vi_converter = nullptr;
569 if (!vi->vi_flags) {
570 value_type = Cppyy::ResolveName(value_type);
571 if (value_type.back() != '*') // meaning, object stored by-value
573 }
574 } else
575 vi->vi_converter = CPyCppyy::CreateConverter(value_type);
576 if (!vi->vi_stride) vi->vi_stride = Cppyy::SizeOf(value_type);
577
578 } else if (CPPScope_Check(pyvalue_type)) {
579 vi->vi_klass = ((CPPClass*)pyvalue_type)->fCppType;
580 vi->vi_converter = nullptr;
581 if (!vi->vi_stride) vi->vi_stride = Cppyy::SizeOf(vi->vi_klass);
583 }
584
585 PyObject* pydata = CallPyObjMethod(v, "__real_data");
586 if (!pydata || Utility::GetBuffer(pydata, '*', 1, vi->vi_data, false) == 0)
587 vi->vi_data = CPPInstance_Check(pydata) ? ((CPPInstance*)pydata)->GetObjectRaw() : nullptr;
588 Py_XDECREF(pydata);
589
590 } else {
591 PyErr_Clear();
592 vi->vi_data = nullptr;
593 vi->vi_stride = 0;
594 vi->vi_converter = nullptr;
595 vi->vi_klass = 0;
596 vi->vi_flags = 0;
597 }
598
599 Py_XDECREF(pyvalue_type);
600
601 vi->ii_pos = 0;
602 vi->ii_len = PySequence_Size(v);
603
604 PyObject_GC_Track(vi);
605 return (PyObject*)vi;
606}
607
608PyObject* VectorGetItem(CPPInstance* self, PySliceObject* index)
609{
610// Implement python's __getitem__ for std::vector<>s.
611 if (PySlice_Check(index)) {
612 if (!self->GetObject()) {
613 PyErr_SetString(PyExc_TypeError, "unsubscriptable object");
614 return nullptr;
615 }
616
617 PyObject* pyclass = (PyObject*)Py_TYPE((PyObject*)self);
618 PyObject* nseq = PyObject_CallObject(pyclass, nullptr);
619
620 Py_ssize_t start, stop, step;
621 PySlice_GetIndices((CPyCppyy_PySliceCast)index, PyObject_Length((PyObject*)self), &start, &stop, &step);
622
623 const Py_ssize_t nlen = PySequence_Size((PyObject*)self);
624 if (!AdjustSlice(nlen, start, stop, step))
625 return nseq;
626
627 const Py_ssize_t sign = step < 0 ? -1 : 1;
628 for (Py_ssize_t i = start; i*sign < stop*sign; i += step) {
629 PyObject* pyidx = PyInt_FromSsize_t(i);
630 PyObject* item = PyObject_CallMethodOneArg((PyObject*)self, PyStrings::gGetNoCheck, pyidx);
631 CallPyObjMethod(nseq, "push_back", item);
632 Py_DECREF(item);
633 Py_DECREF(pyidx);
634 }
635
636 return nseq;
637 }
638
639 return CallSelfIndex(self, (PyObject*)index, PyStrings::gGetNoCheck);
640}
641
642
643static Cppyy::TCppType_t sVectorBoolTypeID = (Cppyy::TCppType_t)0;
644
645PyObject* VectorBoolGetItem(CPPInstance* self, PyObject* idx)
646{
647// std::vector<bool> is a special-case in C++, and its return type depends on
648// the compiler: treat it special here as well
649 if (!CPPInstance_Check(self) || self->ObjectIsA() != sVectorBoolTypeID) {
650 PyErr_Format(PyExc_TypeError,
651 "require object of type std::vector<bool>, but %s given",
652 Cppyy::GetScopedFinalName(self->ObjectIsA()).c_str());
653 return nullptr;
654 }
655
656 if (!self->GetObject()) {
657 PyErr_SetString(PyExc_TypeError, "unsubscriptable object");
658 return nullptr;
659 }
660
661 if (PySlice_Check(idx)) {
662 PyObject* pyclass = (PyObject*)Py_TYPE((PyObject*)self);
663 PyObject* nseq = PyObject_CallObject(pyclass, nullptr);
664
665 Py_ssize_t start, stop, step;
666 PySlice_GetIndices((CPyCppyy_PySliceCast)idx, PyObject_Length((PyObject*)self), &start, &stop, &step);
667 const Py_ssize_t nlen = PySequence_Size((PyObject*)self);
668 if (!AdjustSlice(nlen, start, stop, step))
669 return nseq;
670
671 const Py_ssize_t sign = step < 0 ? -1 : 1;
672 for (Py_ssize_t i = start; i*sign < stop*sign; i += step) {
673 PyObject* pyidx = PyInt_FromSsize_t(i);
674 PyObject* item = PyObject_CallMethodOneArg((PyObject*)self, PyStrings::gGetItem, pyidx);
675 CallPyObjMethod(nseq, "push_back", item);
676 Py_DECREF(item);
677 Py_DECREF(pyidx);
678 }
679
680 return nseq;
681 }
682
683 PyObject* pyindex = PyStyleIndex((PyObject*)self, idx);
684 if (!pyindex)
685 return nullptr;
686
687 int index = (int)PyLong_AsLong(pyindex);
688 Py_DECREF(pyindex);
689
690// get hold of the actual std::vector<bool> (no cast, as vector is never a base)
691 std::vector<bool>* vb = (std::vector<bool>*)self->GetObject();
692
693// finally, return the value
694 if (bool((*vb)[index]))
697}
698
699PyObject* VectorBoolSetItem(CPPInstance* self, PyObject* args)
700{
701// std::vector<bool> is a special-case in C++, and its return type depends on
702// the compiler: treat it special here as well
703 if (!CPPInstance_Check(self) || self->ObjectIsA() != sVectorBoolTypeID) {
704 PyErr_Format(PyExc_TypeError,
705 "require object of type std::vector<bool>, but %s given",
706 Cppyy::GetScopedFinalName(self->ObjectIsA()).c_str());
707 return nullptr;
708 }
709
710 if (!self->GetObject()) {
711 PyErr_SetString(PyExc_TypeError, "unsubscriptable object");
712 return nullptr;
713 }
714
715 int bval = 0; PyObject* idx = nullptr;
716 if (!PyArg_ParseTuple(args, const_cast<char*>("Oi:__setitem__"), &idx, &bval))
717 return nullptr;
718
719 PyObject* pyindex = PyStyleIndex((PyObject*)self, idx);
720 if (!pyindex)
721 return nullptr;
722
723 int index = (int)PyLong_AsLong(pyindex);
724 Py_DECREF(pyindex);
725
726// get hold of the actual std::vector<bool> (no cast, as vector is never a base)
727 std::vector<bool>* vb = (std::vector<bool>*)self->GetObject();
728
729// finally, set the value
730 (*vb)[index] = (bool)bval;
731
733}
734
735
736//- array behavior as primitives ----------------------------------------------
737PyObject* ArrayInit(PyObject* self, PyObject* args, PyObject* /* kwds */)
738{
739// std::array is normally only constructed using aggregate initialization, which
740// is a concept that does not exist in python, so use this custom constructor to
741// to fill the array using setitem
742
743 if (args && PyTuple_GET_SIZE(args) == 1 && PySequence_Check(PyTuple_GET_ITEM(args, 0))) {
744 // construct the empty array, then fill it
745 PyObject* result = PyObject_CallMethodNoArgs(self, PyStrings::gRealInit);
746 if (!result)
747 return nullptr;
748
749 PyObject* items = PyTuple_GET_ITEM(args, 0);
750 Py_ssize_t fillsz = PySequence_Size(items);
751 if (PySequence_Size(self) != fillsz) {
752 PyErr_Format(PyExc_ValueError, "received sequence of size %zd where %zd expected",
753 fillsz, PySequence_Size(self));
754 Py_DECREF(result);
755 return nullptr;
756 }
757
758 PyObject* si_call = PyObject_GetAttr(self, PyStrings::gSetItem);
759 for (Py_ssize_t i = 0; i < fillsz; ++i) {
760 PyObject* item = PySequence_GetItem(items, i);
762 PyObject* sires = PyObject_CallFunctionObjArgs(si_call, index, item, nullptr);
763 Py_DECREF(index);
764 Py_DECREF(item);
765 if (!sires) {
766 Py_DECREF(si_call);
767 Py_DECREF(result);
768 return nullptr;
769 } else
770 Py_DECREF(sires);
771 }
772 Py_DECREF(si_call);
773
774 return result;
775 } else
776 PyErr_Clear();
777
778// The given argument wasn't iterable: simply forward to regular constructor
779 PyObject* realInit = PyObject_GetAttr(self, PyStrings::gRealInit);
780 if (realInit) {
781 PyObject* result = PyObject_Call(realInit, args, nullptr);
782 Py_DECREF(realInit);
783 return result;
784 }
785
786 return nullptr;
787}
788
789
790//- map behavior as primitives ------------------------------------------------
791static PyObject* MapFromPairs(PyObject* self, PyObject* pairs)
792{
793// construct an empty map, then fill it with the key, value pairs
794 PyObject* result = PyObject_CallMethodNoArgs(self, PyStrings::gRealInit);
795 if (!result)
796 return nullptr;
797
798 PyObject* si_call = PyObject_GetAttr(self, PyStrings::gSetItem);
799 for (Py_ssize_t i = 0; i < PySequence_Size(pairs); ++i) {
800 PyObject* pair = PySequence_GetItem(pairs, i);
801 PyObject* sires = nullptr;
802 if (pair && PySequence_Check(pair) && PySequence_Size(pair) == 2) {
803 PyObject* key = PySequence_GetItem(pair, 0);
804 PyObject* value = PySequence_GetItem(pair, 1);
805 sires = PyObject_CallFunctionObjArgs(si_call, key, value, nullptr);
806 Py_DECREF(value);
807 Py_DECREF(key);
808 }
809 Py_DECREF(pair);
810 if (!sires) {
811 Py_DECREF(si_call);
812 Py_DECREF(result);
813 if (!PyErr_Occurred())
814 PyErr_SetString(PyExc_TypeError, "Failed to fill map (argument not a dict or sequence of pairs)");
815 return nullptr;
816 } else
817 Py_DECREF(sires);
818 }
819 Py_DECREF(si_call);
820
821 return result;
822}
823
824PyObject* MapInit(PyObject* self, PyObject* args, PyObject* /* kwds */)
825{
826// Specialized map constructor to allow construction from mapping containers and
827// from tuples of pairs ("initializer_list style").
828
829// PyMapping_Check is not very discriminatory, as it basically only checks for the
830// existence of __getitem__, hence the most common cases of tuple and list are
831// dropped straight-of-the-bat (the PyMapping_Items call will fail on them).
832 if (PyTuple_GET_SIZE(args) == 1 && PyMapping_Check(PyTuple_GET_ITEM(args, 0)) && \
833 !(PyTuple_Check(PyTuple_GET_ITEM(args, 0)) || PyList_Check(PyTuple_GET_ITEM(args, 0)))) {
834 PyObject* assoc = PyTuple_GET_ITEM(args, 0);
835#if PY_VERSION_HEX < 0x03000000
836 // to prevent warning about literal string, expand macro
837 PyObject* items = PyObject_CallMethod(assoc, (char*)"items", nullptr);
838#else
839 // in p3, PyMapping_Items isn't a macro, but a function that short-circuits dict
840 PyObject* items = PyMapping_Items(assoc);
841#endif
842 if (items && PySequence_Check(items)) {
843 PyObject* result = MapFromPairs(self, items);
844 Py_DECREF(items);
845 return result;
846 }
847
848 Py_XDECREF(items);
849 PyErr_Clear();
850
851 // okay to fall through as long as 'self' has not been created (is done in MapFromPairs)
852 }
853
854// tuple of pairs case (some mapping types are sequences)
855 if (PyTuple_GET_SIZE(args) == 1 && PySequence_Check(PyTuple_GET_ITEM(args, 0)))
856 return MapFromPairs(self, PyTuple_GET_ITEM(args, 0));
857
858// The given argument wasn't a mapping or tuple of pairs: forward to regular constructor
859 PyObject* realInit = PyObject_GetAttr(self, PyStrings::gRealInit);
860 if (realInit) {
861 PyObject* result = PyObject_Call(realInit, args, nullptr);
862 Py_DECREF(realInit);
863 return result;
864 }
865
866 return nullptr;
867}
868
869PyObject* STLContainsWithFind(PyObject* self, PyObject* obj)
870{
871// Implement python's __contains__ for std::map/std::set
872 PyObject* result = nullptr;
873
874 PyObject* iter = CallPyObjMethod(self, "find", obj);
875 if (CPPInstance_Check(iter)) {
876 PyObject* end = PyObject_CallMethodNoArgs(self, PyStrings::gEnd);
877 if (CPPInstance_Check(end)) {
878 if (!PyObject_RichCompareBool(iter, end, Py_EQ)) {
879 Py_INCREF(Py_True);
880 result = Py_True;
881 }
882 }
883 Py_XDECREF(end);
884 }
885 Py_XDECREF(iter);
886
887 if (!result) {
888 PyErr_Clear(); // e.g. wrong argument type, which should always lead to False
889 Py_INCREF(Py_False);
890 result = Py_False;
891 }
892
893 return result;
894}
895
896
897//- set behavior as primitives ------------------------------------------------
898PyObject* SetInit(PyObject* self, PyObject* args, PyObject* /* kwds */)
899{
900// Specialized set constructor to allow construction from Python sets.
901 if (PyTuple_GET_SIZE(args) == 1 && PySet_Check(PyTuple_GET_ITEM(args, 0))) {
902 PyObject* pyset = PyTuple_GET_ITEM(args, 0);
903
904 // construct an empty set, then fill it
905 PyObject* result = PyObject_CallMethodNoArgs(self, PyStrings::gRealInit);
906 if (!result)
907 return nullptr;
908
909 PyObject* iter = PyObject_GetIter(pyset);
910 if (iter) {
911 PyObject* ins_call = PyObject_GetAttrString(self, (char*)"insert");
912
913 IterItemGetter getter{iter};
914 Py_DECREF(iter);
915
916 PyObject* item = getter.get();
917 while (item) {
918 PyObject* insres = PyObject_CallFunctionObjArgs(ins_call, item, nullptr);
919 Py_DECREF(item);
920 if (!insres) {
921 Py_DECREF(ins_call);
922 Py_DECREF(result);
923 return nullptr;
924 } else
925 Py_DECREF(insres);
926 item = getter.get();
927 }
928 Py_DECREF(ins_call);
929 }
930
931 return result;
932 }
933
934// The given argument wasn't iterable: simply forward to regular constructor
935 PyObject* realInit = PyObject_GetAttr(self, PyStrings::gRealInit);
936 if (realInit) {
937 PyObject* result = PyObject_Call(realInit, args, nullptr);
938 Py_DECREF(realInit);
939 return result;
940 }
941
942 return nullptr;
943}
944
945
946//- STL container iterator support --------------------------------------------
947static const ptrdiff_t PS_END_ADDR = 7; // non-aligned address, so no clash
948static const ptrdiff_t PS_FLAG_ADDR = 11; // id.
949static const ptrdiff_t PS_COLL_ADDR = 13; // id.
950
951PyObject* LLSequenceIter(PyObject* self)
952{
953// Implement python's __iter__ for low level views used through STL-type begin()/end()
954 PyObject* iter = PyObject_CallMethodNoArgs(self, PyStrings::gBegin);
955
956 if (LowLevelView_Check(iter)) {
957 // builtin pointer iteration: can only succeed if a size is available
958 Py_ssize_t sz = PySequence_Size(self);
959 if (sz == -1) {
960 Py_DECREF(iter);
961 return nullptr;
962 }
963 PyObject* lliter = Py_TYPE(iter)->tp_iter(iter);
964 ((indexiterobject*)lliter)->ii_len = sz;
965 Py_DECREF(iter);
966 return lliter;
967 }
968
969 if (iter) {
970 Py_DECREF(iter);
971 PyErr_SetString(PyExc_TypeError, "unrecognized iterator type for low level views");
972 }
973
974 return nullptr;
975}
976
977PyObject* STLSequenceIter(PyObject* self)
978{
979// Implement python's __iter__ for std::iterator<>s
980 PyObject* iter = PyObject_CallMethodNoArgs(self, PyStrings::gBegin);
981 if (iter) {
982 PyObject* end = PyObject_CallMethodNoArgs(self, PyStrings::gEnd);
983 if (end) {
984 if (CPPInstance_Check(iter)) {
985 // use the data member cache to store extra state on the iterator object,
986 // without it being visible on the Python side
987 auto& dmc = ((CPPInstance*)iter)->GetDatamemberCache();
988 dmc.push_back(std::make_pair(PS_END_ADDR, end));
989
990 // set a flag, indicating first iteration (reset in __next__)
991 Py_INCREF(Py_False);
992 dmc.push_back(std::make_pair(PS_FLAG_ADDR, Py_False));
993
994 // make sure the iterated over collection remains alive for the duration
995 Py_INCREF(self);
996 dmc.push_back(std::make_pair(PS_COLL_ADDR, self));
997 } else {
998 // could store "end" on the object's dictionary anyway, but if end() returns
999 // a user-customized object, then its __next__ is probably custom, too
1000 Py_DECREF(end);
1001 }
1002 }
1003 }
1004 return iter;
1005}
1006
1007//- generic iterator support over a sequence with operator[] and size ---------
1008//-----------------------------------------------------------------------------
1009static PyObject* index_iter(PyObject* c) {
1010 indexiterobject* ii = PyObject_GC_New(indexiterobject, &IndexIter_Type);
1011 if (!ii) return nullptr;
1012
1013 Py_INCREF(c);
1014 ii->ii_container = c;
1015 ii->ii_pos = 0;
1016 ii->ii_len = PySequence_Size(c);
1017
1018 PyObject_GC_Track(ii);
1019 return (PyObject*)ii;
1020}
1021
1022
1023//- safe indexing for STL-like vector w/o iterator dictionaries ---------------
1024/* replaced by indexiterobject iteration, but may still have some future use ...
1025PyObject* CheckedGetItem(PyObject* self, PyObject* obj)
1026{
1027// Implement a generic python __getitem__ for STL-like classes that are missing the
1028// reflection info for their iterators. This is then used for iteration by means of
1029// consecutive indices, it such index is of integer type.
1030 Py_ssize_t size = PySequence_Size(self);
1031 Py_ssize_t idx = PyInt_AsSsize_t(obj);
1032 if ((size == (Py_ssize_t)-1 || idx == (Py_ssize_t)-1) && PyErr_Occurred()) {
1033 // argument conversion problem: let method itself resolve anew and report
1034 PyErr_Clear();
1035 return PyObject_CallMethodOneArg(self, PyStrings::gGetNoCheck, obj);
1036 }
1037
1038 bool inbounds = false;
1039 if (idx < 0) idx += size;
1040 if (0 <= idx && 0 <= size && idx < size)
1041 inbounds = true;
1042
1043 if (inbounds)
1044 return PyObject_CallMethodOneArg(self, PyStrings::gGetNoCheck, obj);
1045 else
1046 PyErr_SetString( PyExc_IndexError, "index out of range" );
1047
1048 return nullptr;
1049}*/
1050
1051
1052//- pair as sequence to allow tuple unpacking --------------------------------
1053PyObject* PairUnpack(PyObject* self, PyObject* pyindex)
1054{
1055// For std::map<> iteration, unpack std::pair<>s into tuples for the loop.
1056 long idx = PyLong_AsLong(pyindex);
1057 if (idx == -1 && PyErr_Occurred())
1058 return nullptr;
1059
1060 if (!CPPInstance_Check(self) || !((CPPInstance*)self)->GetObject()) {
1061 PyErr_SetString(PyExc_TypeError, "unsubscriptable object");
1062 return nullptr;
1063 }
1064
1065 if ((int)idx == 0)
1066 return PyObject_GetAttr(self, PyStrings::gFirst);
1067 else if ((int)idx == 1)
1068 return PyObject_GetAttr(self, PyStrings::gSecond);
1069
1070// still here? Trigger stop iteration
1071 PyErr_SetString(PyExc_IndexError, "out of bounds");
1072 return nullptr;
1073}
1074
1075//- simplistic len() functions -----------------------------------------------
1076PyObject* ReturnTwo(CPPInstance*, PyObject*) {
1077 return PyInt_FromLong(2);
1078}
1079
1080
1081//- shared/unique_ptr behavior -----------------------------------------------
1082PyObject* SmartPtrInit(PyObject* self, PyObject* args, PyObject* /* kwds */)
1083{
1084// since the shared/unique pointer will take ownership, we need to relinquish it
1085 PyObject* realInit = PyObject_GetAttr(self, PyStrings::gRealInit);
1086 if (realInit) {
1087 PyObject* result = PyObject_Call(realInit, args, nullptr);
1088 Py_DECREF(realInit);
1089 if (result && PyTuple_GET_SIZE(args) == 1 && CPPInstance_Check(PyTuple_GET_ITEM(args, 0))) {
1090 CPPInstance* cppinst = (CPPInstance*)PyTuple_GET_ITEM(args, 0);
1091 if (!(cppinst->fFlags & CPPInstance::kIsSmartPtr)) cppinst->CppOwns();
1092 }
1093 return result;
1094 }
1095 return nullptr;
1096}
1097
1098
1099//- string behavior as primitives --------------------------------------------
1100#if PY_VERSION_HEX >= 0x03000000
1101// TODO: this is wrong, b/c it doesn't order
1102static int PyObject_Compare(PyObject* one, PyObject* other) {
1103 return !PyObject_RichCompareBool(one, other, Py_EQ);
1104}
1105#endif
1106static inline
1107PyObject* CPyCppyy_PyString_FromCppString(std::string* s, bool native=true) {
1108 if (native)
1109 return PyBytes_FromStringAndSize(s->data(), s->size());
1110 return CPyCppyy_PyText_FromStringAndSize(s->data(), s->size());
1111}
1112
1113static inline
1114PyObject* CPyCppyy_PyString_FromCppString(std::wstring* s, bool native=true) {
1115 PyObject* pyobj = PyUnicode_FromWideChar(s->data(), s->size());
1116 if (pyobj && native) {
1117 PyObject* pybytes = PyUnicode_AsEncodedString(pyobj, "UTF-8", "strict");
1118 Py_DECREF(pyobj);
1119 pyobj = pybytes;
1120 }
1121 return pyobj;
1122}
1123
1124#define CPPYY_IMPL_STRING_PYTHONIZATION(type, name) \
1125static inline \
1126PyObject* name##StringGetData(PyObject* self, bool native=true) \
1127{ \
1128 if (CPyCppyy::CPPInstance_Check(self)) { \
1129 type* obj = ((type*)((CPPInstance*)self)->GetObject()); \
1130 if (obj) return CPyCppyy_PyString_FromCppString(obj, native); \
1131 } \
1132 PyErr_Format(PyExc_TypeError, "object mismatch (%s expected)", #type); \
1133 return nullptr; \
1134} \
1135 \
1136PyObject* name##StringStr(PyObject* self) \
1137{ \
1138 PyObject* pyobj = name##StringGetData(self, false); \
1139 if (!pyobj) { \
1140 /* do a native conversion to make printing possible (debatable) */ \
1141 PyErr_Clear(); \
1142 PyObject* pybytes = name##StringGetData(self, true); \
1143 if (pybytes) { /* should not fail */ \
1144 pyobj = PyObject_Str(pybytes); \
1145 Py_DECREF(pybytes); \
1146 } \
1147 } \
1148 return pyobj; \
1149} \
1150 \
1151PyObject* name##StringBytes(PyObject* self) \
1152{ \
1153 return name##StringGetData(self, true); \
1154} \
1155 \
1156PyObject* name##StringRepr(PyObject* self) \
1157{ \
1158 PyObject* data = name##StringGetData(self, true); \
1159 if (data) { \
1160 PyObject* repr = PyObject_Repr(data); \
1161 Py_DECREF(data); \
1162 return repr; \
1163 } \
1164 return nullptr; \
1165} \
1166 \
1167PyObject* name##StringIsEqual(PyObject* self, PyObject* obj) \
1168{ \
1169 PyObject* data = name##StringGetData(self, PyBytes_Check(obj)); \
1170 if (data) { \
1171 PyObject* result = PyObject_RichCompare(data, obj, Py_EQ); \
1172 Py_DECREF(data); \
1173 return result; \
1174 } \
1175 return nullptr; \
1176} \
1177 \
1178PyObject* name##StringIsNotEqual(PyObject* self, PyObject* obj) \
1179{ \
1180 PyObject* data = name##StringGetData(self, PyBytes_Check(obj)); \
1181 if (data) { \
1182 PyObject* result = PyObject_RichCompare(data, obj, Py_NE); \
1183 Py_DECREF(data); \
1184 return result; \
1185 } \
1186 return nullptr; \
1187}
1188
1189// Only define STLStringCompare:
1190#define CPPYY_IMPL_STRING_PYTHONIZATION_CMP(type, name) \
1191CPPYY_IMPL_STRING_PYTHONIZATION(type, name) \
1192PyObject* name##StringCompare(PyObject* self, PyObject* obj) \
1193{ \
1194 PyObject* data = name##StringGetData(self, PyBytes_Check(obj)); \
1195 int result = 0; \
1196 if (data) { \
1197 result = PyObject_Compare(data, obj); \
1198 Py_DECREF(data); \
1199 } \
1200 if (PyErr_Occurred()) \
1201 return nullptr; \
1202 return PyInt_FromLong(result); \
1203}
1204
1206CPPYY_IMPL_STRING_PYTHONIZATION_CMP(std::wstring, STLW)
1207
1208static inline std::string* GetSTLString(CPPInstance* self) {
1209 if (!CPPInstance_Check(self)) {
1210 PyErr_SetString(PyExc_TypeError, "std::string object expected");
1211 return nullptr;
1212 }
1213
1214 std::string* obj = (std::string*)self->GetObject();
1215 if (!obj)
1216 PyErr_SetString(PyExc_ReferenceError, "attempt to access a null-pointer");
1217
1218 return obj;
1219}
1220
1221PyObject* STLStringDecode(CPPInstance* self, PyObject* args, PyObject* kwds)
1222{
1223 std::string* obj = GetSTLString(self);
1224 if (!obj)
1225 return nullptr;
1226
1227 char* keywords[] = {(char*)"encoding", (char*)"errors", (char*)nullptr};
1228 const char* encoding; const char* errors;
1229 if (!PyArg_ParseTupleAndKeywords(args, kwds,
1230 const_cast<char*>("s|s"), keywords, &encoding, &errors))
1231 return nullptr;
1232
1233 return PyUnicode_Decode(obj->data(), obj->size(), encoding, errors);
1234}
1235
1236PyObject* STLStringContains(CPPInstance* self, PyObject* pyobj)
1237{
1238 std::string* obj = GetSTLString(self);
1239 if (!obj)
1240 return nullptr;
1241
1242 const char* needle = CPyCppyy_PyText_AsString(pyobj);
1243 if (!needle)
1244 return nullptr;
1245
1246 if (obj->find(needle) != std::string::npos) {
1248 }
1249
1251}
1252
1253PyObject* STLStringReplace(CPPInstance* self, PyObject* args, PyObject* /*kwds*/)
1254{
1255 std::string* obj = GetSTLString(self);
1256 if (!obj)
1257 return nullptr;
1258
1259// both str and std::string have a method "replace", but the Python version only
1260// accepts strings and takes no keyword arguments, whereas the C++ version has no
1261// overload that takes a string
1262
1263 if (2 <= PyTuple_GET_SIZE(args) && CPyCppyy_PyText_Check(PyTuple_GET_ITEM(args, 0))) {
1264 PyObject* pystr = CPyCppyy_PyText_FromStringAndSize(obj->data(), obj->size());
1265 PyObject* meth = PyObject_GetAttrString(pystr, (char*)"replace");
1266 Py_DECREF(pystr);
1267 PyObject* result = PyObject_CallObject(meth, args);
1268 Py_DECREF(meth);
1269 return result;
1270 }
1271
1272 PyObject* cppreplace = PyObject_GetAttrString((PyObject*)self, (char*)"__cpp_replace");
1273 if (cppreplace) {
1274 PyObject* result = PyObject_Call(cppreplace, args, nullptr);
1275 Py_DECREF(cppreplace);
1276 return result;
1277 }
1278
1279 PyErr_SetString(PyExc_AttributeError, "\'std::string\' object has no attribute \'replace\'");
1280 return nullptr;
1281}
1282
1283#define CPYCPPYY_STRING_FINDMETHOD(name, cppname, pyname) \
1284PyObject* STLString##name(CPPInstance* self, PyObject* args, PyObject* /*kwds*/) \
1285{ \
1286 std::string* obj = GetSTLString(self); \
1287 if (!obj) \
1288 return nullptr; \
1289 \
1290 PyObject* cppmeth = PyObject_GetAttrString((PyObject*)self, (char*)#cppname);\
1291 if (cppmeth) { \
1292 PyObject* result = PyObject_Call(cppmeth, args, nullptr); \
1293 Py_DECREF(cppmeth); \
1294 if (result) { \
1295 if (PyLongOrInt_AsULong64(result) == (PY_ULONG_LONG)std::string::npos) {\
1296 Py_DECREF(result); \
1297 return PyInt_FromLong(-1); \
1298 } \
1299 return result; \
1300 } \
1301 PyErr_Clear(); \
1302 } \
1303 \
1304 PyObject* pystr = CPyCppyy_PyText_FromStringAndSize(obj->data(), obj->size());\
1305 PyObject* pymeth = PyObject_GetAttrString(pystr, (char*)#pyname); \
1306 Py_DECREF(pystr); \
1307 PyObject* result = PyObject_CallObject(pymeth, args); \
1308 Py_DECREF(pymeth); \
1309 return result; \
1310}
1311
1312// both str and std::string have method "find" and "rfin"; try the C++ version first
1313// and fall back on the Python one in case of failure
1314CPYCPPYY_STRING_FINDMETHOD( Find, __cpp_find, find)
1315CPYCPPYY_STRING_FINDMETHOD(RFind, __cpp_rfind, rfind)
1316
1317PyObject* STLStringGetAttr(CPPInstance* self, PyObject* attr_name)
1318{
1319 std::string* obj = GetSTLString(self);
1320 if (!obj)
1321 return nullptr;
1322
1323 PyObject* pystr = CPyCppyy_PyText_FromStringAndSize(obj->data(), obj->size());
1324 PyObject* attr = PyObject_GetAttr(pystr, attr_name);
1325 Py_DECREF(pystr);
1326 return attr;
1327}
1328
1329
1330#if 0
1331PyObject* UTF8Repr(PyObject* self)
1332{
1333// force C++ string types conversion to Python str per Python __repr__ requirements
1334 PyObject* res = PyObject_CallMethodNoArgs(self, PyStrings::gCppRepr);
1335 if (!res || CPyCppyy_PyText_Check(res))
1336 return res;
1337 PyObject* str_res = PyObject_Str(res);
1338 Py_DECREF(res);
1339 return str_res;
1340}
1341
1342PyObject* UTF8Str(PyObject* self)
1343{
1344// force C++ string types conversion to Python str per Python __str__ requirements
1345 PyObject* res = PyObject_CallMethodNoArgs(self, PyStrings::gCppStr);
1346 if (!res || CPyCppyy_PyText_Check(res))
1347 return res;
1348 PyObject* str_res = PyObject_Str(res);
1349 Py_DECREF(res);
1350 return str_res;
1351}
1352#endif
1353
1354Py_hash_t STLStringHash(PyObject* self)
1355{
1356// std::string objects hash to the same values as Python strings to allow
1357// matches in dictionaries etc.
1358 PyObject* data = STLStringGetData(self, false);
1360 Py_DECREF(data);
1361 return h;
1362}
1363
1364
1365//- string_view behavior as primitive ----------------------------------------
1366PyObject* StringViewInit(PyObject* self, PyObject* args, PyObject* /* kwds */)
1367{
1368// if constructed from a Python unicode object, the constructor will convert it
1369// to a temporary byte string, which is likely to go out of scope too soon; so
1370// buffer it as needed
1371 PyObject* realInit = PyObject_GetAttr(self, PyStrings::gRealInit);
1372 if (realInit) {
1373 PyObject *strbuf = nullptr, *newArgs = nullptr;
1374 if (PyTuple_GET_SIZE(args) == 1) {
1375 PyObject* arg0 = PyTuple_GET_ITEM(args, 0);
1376 if (PyUnicode_Check(arg0)) {
1377 // convert to the expected bytes array to control the temporary
1378 strbuf = PyUnicode_AsEncodedString(arg0, "UTF-8", "strict");
1379 newArgs = PyTuple_New(1);
1380 Py_INCREF(strbuf);
1381 PyTuple_SET_ITEM(newArgs, 0, strbuf);
1382 } else if (PyBytes_Check(arg0)) {
1383 // tie the life time of the provided string to the string_view
1384 Py_INCREF(arg0);
1385 strbuf = arg0;
1386 }
1387 }
1388
1389 PyObject* result = PyObject_Call(realInit, newArgs ? newArgs : args, nullptr);
1390
1391 Py_XDECREF(newArgs);
1392 Py_DECREF(realInit);
1393
1394 // if construction was successful and a string buffer was used, add a
1395 // life line to it from the string_view bound object
1396 if (result && self && strbuf)
1397 PyObject_SetAttr(self, PyStrings::gLifeLine, strbuf);
1398 Py_XDECREF(strbuf);
1399
1400 return result;
1401 }
1402 return nullptr;
1403}
1404
1405
1406
1407//- STL iterator behavior ----------------------------------------------------
1408PyObject* STLIterNext(PyObject* self)
1409{
1410// Python iterator protocol __next__ for STL forward iterators.
1411 bool mustIncrement = true;
1412 PyObject* last = nullptr;
1413 if (CPPInstance_Check(self)) {
1414 auto& dmc = ((CPPInstance*)self)->GetDatamemberCache();
1415 for (auto& p: dmc) {
1416 if (p.first == PS_END_ADDR) {
1417 last = p.second;
1418 Py_INCREF(last);
1419 } else if (p.first == PS_FLAG_ADDR) {
1420 mustIncrement = p.second == Py_True;
1421 if (!mustIncrement) {
1422 Py_DECREF(p.second);
1423 Py_INCREF(Py_True);
1424 p.second = Py_True;
1425 }
1426 }
1427 }
1428 }
1429
1430 PyObject* next = nullptr;
1431 if (last) {
1432 // handle special case of empty container (i.e. self is end)
1433 if (!PyObject_RichCompareBool(last, self, Py_EQ)) {
1434 bool iter_valid = true;
1435 if (mustIncrement) {
1436 // prefer preinc, but allow post-inc; in both cases, it is "self" that has
1437 // the updated state to dereference
1438 PyObject* iter = PyObject_CallMethodNoArgs(self, PyStrings::gPreInc);
1439 if (!iter) {
1440 PyErr_Clear();
1441 static PyObject* dummy = PyInt_FromLong(1l);
1442 iter = PyObject_CallMethodOneArg(self, PyStrings::gPostInc, dummy);
1443 }
1444 iter_valid = iter && PyObject_RichCompareBool(last, self, Py_NE);
1445 Py_XDECREF(iter);
1446 }
1447
1448 if (iter_valid) {
1449 next = PyObject_CallMethodNoArgs(self, PyStrings::gDeref);
1450 if (!next) PyErr_Clear();
1451 }
1452 }
1453 Py_DECREF(last);
1454 }
1455
1456 if (!next) PyErr_SetString(PyExc_StopIteration, "");
1457 return next;
1458}
1459
1460
1461//- STL complex<T> behavior --------------------------------------------------
1462#define COMPLEX_METH_GETSET(name, cppname) \
1463static PyObject* name##ComplexGet(PyObject* self, void*) { \
1464 return PyObject_CallMethodNoArgs(self, cppname); \
1465} \
1466static int name##ComplexSet(PyObject* self, PyObject* value, void*) { \
1467 PyObject* result = PyObject_CallMethodOneArg(self, cppname, value); \
1468 if (result) { \
1469 Py_DECREF(result); \
1470 return 0; \
1471 } \
1472 return -1; \
1473} \
1474PyGetSetDef name##Complex{(char*)#name, (getter)name##ComplexGet, (setter)name##ComplexSet, nullptr, nullptr};
1475
1476COMPLEX_METH_GETSET(real, PyStrings::gCppReal)
1477COMPLEX_METH_GETSET(imag, PyStrings::gCppImag)
1478
1479static PyObject* ComplexComplex(PyObject* self) {
1480 PyObject* real = PyObject_CallMethodNoArgs(self, PyStrings::gCppReal);
1481 if (!real) return nullptr;
1482 double r = PyFloat_AsDouble(real);
1483 Py_DECREF(real);
1484 if (r == -1. && PyErr_Occurred())
1485 return nullptr;
1486
1487 PyObject* imag = PyObject_CallMethodNoArgs(self, PyStrings::gCppImag);
1488 if (!imag) return nullptr;
1489 double i = PyFloat_AsDouble(imag);
1490 Py_DECREF(imag);
1491 if (i == -1. && PyErr_Occurred())
1492 return nullptr;
1493
1494 return PyComplex_FromDoubles(r, i);
1495}
1496
1497static PyObject* ComplexRepr(PyObject* self) {
1498 PyObject* real = PyObject_CallMethodNoArgs(self, PyStrings::gCppReal);
1499 if (!real) return nullptr;
1500 double r = PyFloat_AsDouble(real);
1501 Py_DECREF(real);
1502 if (r == -1. && PyErr_Occurred())
1503 return nullptr;
1504
1505 PyObject* imag = PyObject_CallMethodNoArgs(self, PyStrings::gCppImag);
1506 if (!imag) return nullptr;
1507 double i = PyFloat_AsDouble(imag);
1508 Py_DECREF(imag);
1509 if (i == -1. && PyErr_Occurred())
1510 return nullptr;
1511
1512 std::ostringstream s;
1513 s << '(' << r << '+' << i << "j)";
1514 return CPyCppyy_PyText_FromString(s.str().c_str());
1515}
1516
1517static PyObject* ComplexDRealGet(CPPInstance* self, void*)
1518{
1519 return PyFloat_FromDouble(((std::complex<double>*)self->GetObject())->real());
1520}
1521
1522static int ComplexDRealSet(CPPInstance* self, PyObject* value, void*)
1523{
1524 double d = PyFloat_AsDouble(value);
1525 if (d == -1.0 && PyErr_Occurred())
1526 return -1;
1527 ((std::complex<double>*)self->GetObject())->real(d);
1528 return 0;
1529}
1530
1531PyGetSetDef ComplexDReal{(char*)"real", (getter)ComplexDRealGet, (setter)ComplexDRealSet, nullptr, nullptr};
1532
1533
1534static PyObject* ComplexDImagGet(CPPInstance* self, void*)
1535{
1536 return PyFloat_FromDouble(((std::complex<double>*)self->GetObject())->imag());
1537}
1538
1539static int ComplexDImagSet(CPPInstance* self, PyObject* value, void*)
1540{
1541 double d = PyFloat_AsDouble(value);
1542 if (d == -1.0 && PyErr_Occurred())
1543 return -1;
1544 ((std::complex<double>*)self->GetObject())->imag(d);
1545 return 0;
1546}
1547
1548PyGetSetDef ComplexDImag{(char*)"imag", (getter)ComplexDImagGet, (setter)ComplexDImagSet, nullptr, nullptr};
1549
1550static PyObject* ComplexDComplex(CPPInstance* self)
1551{
1552 double r = ((std::complex<double>*)self->GetObject())->real();
1553 double i = ((std::complex<double>*)self->GetObject())->imag();
1554 return PyComplex_FromDoubles(r, i);
1555}
1556
1557
1558} // unnamed namespace
1559
1560
1561//- public functions ---------------------------------------------------------
1562namespace CPyCppyy {
1563 std::set<std::string> gIteratorTypes;
1564}
1565
1566static inline
1567bool run_pythonizors(PyObject* pyclass, PyObject* pyname, const std::vector<PyObject*>& v)
1568{
1569 PyObject* args = PyTuple_New(2);
1570 Py_INCREF(pyclass); PyTuple_SET_ITEM(args, 0, pyclass);
1571 Py_INCREF(pyname); PyTuple_SET_ITEM(args, 1, pyname);
1572
1573 bool pstatus = true;
1574 for (auto pythonizor : v) {
1575 PyObject* result = PyObject_CallObject(pythonizor, args);
1576 if (!result) {
1577 pstatus = false; // TODO: detail the error handling
1578 break;
1579 }
1580 Py_DECREF(result);
1581 }
1582 Py_DECREF(args);
1583
1584 return pstatus;
1585}
1586
1587bool CPyCppyy::Pythonize(PyObject* pyclass, const std::string& name)
1588{
1589// Add pre-defined pythonizations (for STL and ROOT) to classes based on their
1590// signature and/or class name.
1591 if (!pyclass)
1592 return false;
1593
1594 CPPScope* klass = (CPPScope*)pyclass;
1595
1596//- method name based pythonization ------------------------------------------
1597
1598// for smart pointer style classes that are otherwise not known as such; would
1599// prefer operator-> as that returns a pointer (which is simpler since it never
1600// has to deal with ref-assignment), but operator* plays better with STL iters
1601// and algorithms
1602 if (HasAttrDirect(pyclass, PyStrings::gDeref) && !Cppyy::IsSmartPtr(klass->fCppType))
1603 Utility::AddToClass(pyclass, "__getattr__", (PyCFunction)DeRefGetAttr, METH_O);
1604 else if (HasAttrDirect(pyclass, PyStrings::gFollow) && !Cppyy::IsSmartPtr(klass->fCppType))
1605 Utility::AddToClass(pyclass, "__getattr__", (PyCFunction)FollowGetAttr, METH_O);
1606
1607// for pre-check of nullptr for boolean types
1608 if (HasAttrDirect(pyclass, PyStrings::gCppBool)) {
1609#if PY_VERSION_HEX >= 0x03000000
1610 const char* pybool_name = "__bool__";
1611#else
1612 const char* pybool_name = "__nonzero__";
1613#endif
1614 Utility::AddToClass(pyclass, pybool_name, (PyCFunction)NullCheckBool, METH_NOARGS);
1615 }
1616
1617// for STL containers, and user classes modeled after them
1618 if (HasAttrDirect(pyclass, PyStrings::gSize))
1619 Utility::AddToClass(pyclass, "__len__", "size");
1620
1621 if (!IsTemplatedSTLClass(name, "vector") && // vector is dealt with below
1622 !((PyTypeObject*)pyclass)->tp_iter) {
1623 if (HasAttrDirect(pyclass, PyStrings::gBegin) && HasAttrDirect(pyclass, PyStrings::gEnd)) {
1624 // obtain the name of the return type
1625 const auto& v = Cppyy::GetMethodIndicesFromName(klass->fCppType, "begin");
1626 if (!v.empty()) {
1627 // check return type; if not explicitly an iterator, add it to the "known" return
1628 // types to add the "next" method on use
1630 const std::string& resname = Cppyy::GetMethodResultType(meth);
1631 bool isIterator = gIteratorTypes.find(resname) != gIteratorTypes.end();
1632 if (!isIterator && Cppyy::GetScope(resname)) {
1633 if (resname.find("iterator") == std::string::npos)
1634 gIteratorTypes.insert(resname);
1635 isIterator = true;
1636 }
1637
1638 if (isIterator) {
1639 // install iterator protocol a la STL
1640 ((PyTypeObject*)pyclass)->tp_iter = (getiterfunc)STLSequenceIter;
1641 Utility::AddToClass(pyclass, "__iter__", (PyCFunction)STLSequenceIter, METH_NOARGS);
1642 } else {
1643 // still okay if this is some pointer type of builtin persuasion (general class
1644 // won't work: the return type needs to understand the iterator protocol)
1645 std::string resolved = Cppyy::ResolveName(resname);
1646 if (resolved.back() == '*' && Cppyy::IsBuiltin(resolved.substr(0, resolved.size()-1))) {
1647 ((PyTypeObject*)pyclass)->tp_iter = (getiterfunc)LLSequenceIter;
1648 Utility::AddToClass(pyclass, "__iter__", (PyCFunction)LLSequenceIter, METH_NOARGS);
1649 }
1650 }
1651 }
1652 }
1653 if (!((PyTypeObject*)pyclass)->tp_iter && // no iterator resolved
1654 HasAttrDirect(pyclass, PyStrings::gGetItem) && PyObject_HasAttr(pyclass, PyStrings::gLen)) {
1655 // Python will iterate over __getitem__ using integers, but C++ operator[] will never raise
1656 // a StopIteration. A checked getitem (raising IndexError if beyond size()) works in some
1657 // cases but would mess up if operator[] is meant to implement an associative container. So,
1658 // this has to be implemented as an iterator protocol.
1659 ((PyTypeObject*)pyclass)->tp_iter = (getiterfunc)index_iter;
1660 Utility::AddToClass(pyclass, "__iter__", (PyCFunction)index_iter, METH_NOARGS);
1661 }
1662 }
1663
1664// operator==/!= are used in op_richcompare of CPPInstance, which subsequently allows
1665// comparisons to None; if no operator is available, a hook is installed for lazy
1666// lookups in the global and/or class namespace
1667 if (HasAttrDirect(pyclass, PyStrings::gEq, true) && \
1668 Cppyy::GetMethodIndicesFromName(klass->fCppType, "__eq__").empty()) {
1669 PyObject* cppol = PyObject_GetAttr(pyclass, PyStrings::gEq);
1670 if (!klass->fOperators) klass->fOperators = new Utility::PyOperators();
1671 klass->fOperators->fEq = cppol;
1672 // re-insert the forwarding __eq__ from the CPPInstance in case there was a Python-side
1673 // override in the base class
1674 static PyObject* top_eq = nullptr;
1675 if (!top_eq) {
1676 PyObject* top_cls = PyObject_GetAttrString(gThisModule, "CPPInstance");
1677 top_eq = PyObject_GetAttr(top_cls, PyStrings::gEq);
1678 Py_DECREF(top_eq); // make it borrowed
1679 Py_DECREF(top_cls);
1680 }
1681 PyObject_SetAttr(pyclass, PyStrings::gEq, top_eq);
1682 }
1683
1684 if (HasAttrDirect(pyclass, PyStrings::gNe, true) && \
1685 Cppyy::GetMethodIndicesFromName(klass->fCppType, "__ne__").empty()) {
1686 PyObject* cppol = PyObject_GetAttr(pyclass, PyStrings::gNe);
1687 if (!klass->fOperators) klass->fOperators = new Utility::PyOperators();
1688 klass->fOperators->fNe = cppol;
1689 // re-insert the forwarding __ne__ (same reason as above for __eq__)
1690 static PyObject* top_ne = nullptr;
1691 if (!top_ne) {
1692 PyObject* top_cls = PyObject_GetAttrString(gThisModule, "CPPInstance");
1693 top_ne = PyObject_GetAttr(top_cls, PyStrings::gNe);
1694 Py_DECREF(top_ne); // make it borrowed
1695 Py_DECREF(top_cls);
1696 }
1697 PyObject_SetAttr(pyclass, PyStrings::gNe, top_ne);
1698 }
1699
1700#if 0
1701 if (HasAttrDirect(pyclass, PyStrings::gRepr, true)) {
1702 // guarantee that the result of __repr__ is a Python string
1703 Utility::AddToClass(pyclass, "__cpp_repr", "__repr__");
1704 Utility::AddToClass(pyclass, "__repr__", (PyCFunction)UTF8Repr, METH_NOARGS);
1705 }
1706
1707 if (HasAttrDirect(pyclass, PyStrings::gStr, true)) {
1708 // guarantee that the result of __str__ is a Python string
1709 Utility::AddToClass(pyclass, "__cpp_str", "__str__");
1710 Utility::AddToClass(pyclass, "__str__", (PyCFunction)UTF8Str, METH_NOARGS);
1711 }
1712#endif
1713
1714 // This pythonization is disabled for ROOT because it is a bit buggy
1715#if 0
1716 if (Cppyy::IsAggregate(((CPPClass*)pyclass)->fCppType) && name.compare(0, 5, "std::", 5) != 0) {
1717 // create a pseudo-constructor to allow initializer-style object creation
1718 Cppyy::TCppType_t kls = ((CPPClass*)pyclass)->fCppType;
1720 if (ndata) {
1721 std::string rname = name;
1723
1724 std::ostringstream initdef;
1725 initdef << "namespace __cppyy_internal {\n"
1726 << "void init_" << rname << "(" << name << "*& self";
1727 bool codegen_ok = true;
1728 std::vector<std::string> arg_types, arg_names, arg_defaults;
1729 arg_types.reserve(ndata); arg_names.reserve(ndata); arg_defaults.reserve(ndata);
1730 for (Cppyy::TCppIndex_t i = 0; i < ndata; ++i) {
1731 if (Cppyy::IsStaticData(kls, i) || !Cppyy::IsPublicData(kls, i))
1732 continue;
1733
1734 const std::string& txt = Cppyy::GetDatamemberType(kls, i);
1735 const std::string& res = Cppyy::IsEnum(txt) ? txt : Cppyy::ResolveName(txt);
1736 const std::string& cpd = TypeManip::compound(res);
1737 std::string res_clean = TypeManip::clean_type(res, false, true);
1738
1739 if (res_clean == "internal_enum_type_t")
1740 res_clean = txt; // restore (properly scoped name)
1741
1742 if (res.rfind(']') == std::string::npos && res.rfind(')') == std::string::npos) {
1743 if (!cpd.empty()) arg_types.push_back(res_clean+cpd);
1744 else arg_types.push_back("const "+res_clean+"&");
1745 arg_names.push_back(Cppyy::GetDatamemberName(kls, i));
1746 if ((!cpd.empty() && cpd.back() == '*') || Cppyy::IsBuiltin(res_clean))
1747 arg_defaults.push_back("0");
1748 else {
1749 Cppyy::TCppScope_t klsid = Cppyy::GetScope(res_clean);
1750 if (Cppyy::IsDefaultConstructable(klsid)) arg_defaults.push_back(res_clean+"{}");
1751 }
1752 } else {
1753 codegen_ok = false; // TODO: how to support arrays, anonymous enums, etc?
1754 break;
1755 }
1756 }
1757
1758 if (codegen_ok && !arg_types.empty()) {
1759 bool defaults_ok = arg_defaults.size() == arg_types.size();
1760 for (std::vector<std::string>::size_type i = 0; i < arg_types.size(); ++i) {
1761 initdef << ", " << arg_types[i] << " " << arg_names[i];
1762 if (defaults_ok) initdef << " = " << arg_defaults[i];
1763 }
1764 initdef << ") {\n self = new " << name << "{";
1765 for (std::vector<std::string>::size_type i = 0; i < arg_names.size(); ++i) {
1766 if (i != 0) initdef << ", ";
1767 initdef << arg_names[i];
1768 }
1769 initdef << "};\n} }";
1770
1771 if (Cppyy::Compile(initdef.str(), true /* silent */)) {
1772 Cppyy::TCppScope_t cis = Cppyy::GetScope("__cppyy_internal");
1773 const auto& mix = Cppyy::GetMethodIndicesFromName(cis, "init_"+rname);
1774 if (mix.size()) {
1775 if (!Utility::AddToClass(pyclass, "__init__",
1776 new CPPFunction(cis, Cppyy::GetMethod(cis, mix[0]))))
1777 PyErr_Clear();
1778 }
1779 }
1780 }
1781 }
1782 }
1783#endif
1784
1785
1786//- class name based pythonization -------------------------------------------
1787
1788 if (IsTemplatedSTLClass(name, "vector")) {
1789
1790 // std::vector<bool> is a special case in C++
1791 if (!sVectorBoolTypeID) sVectorBoolTypeID = (Cppyy::TCppType_t)Cppyy::GetScope("std::vector<bool>");
1792 if (klass->fCppType == sVectorBoolTypeID) {
1793 Utility::AddToClass(pyclass, "__getitem__", (PyCFunction)VectorBoolGetItem, METH_O);
1794 Utility::AddToClass(pyclass, "__setitem__", (PyCFunction)VectorBoolSetItem);
1795 } else {
1796 // constructor that takes python collections
1797 Utility::AddToClass(pyclass, "__real_init", "__init__");
1798 Utility::AddToClass(pyclass, "__init__", (PyCFunction)VectorInit, METH_VARARGS | METH_KEYWORDS);
1799
1800 // data with size
1801 Utility::AddToClass(pyclass, "__real_data", "data");
1802 Utility::AddToClass(pyclass, "data", (PyCFunction)VectorData);
1803
1804 // numpy array conversion
1805 Utility::AddToClass(pyclass, "__array__", (PyCFunction)VectorArray);
1806
1807 // checked getitem
1808 if (HasAttrDirect(pyclass, PyStrings::gLen)) {
1809 Utility::AddToClass(pyclass, "_getitem__unchecked", "__getitem__");
1810 Utility::AddToClass(pyclass, "__getitem__", (PyCFunction)VectorGetItem, METH_O);
1811 }
1812
1813 // vector-optimized iterator protocol
1814 ((PyTypeObject*)pyclass)->tp_iter = (getiterfunc)vector_iter;
1815
1816 // optimized __iadd__
1817 Utility::AddToClass(pyclass, "__iadd__", (PyCFunction)VectorIAdd, METH_VARARGS | METH_KEYWORDS);
1818
1819 // helpers for iteration
1820 const std::string& vtype = Cppyy::ResolveName(name+"::value_type");
1821 if (vtype.rfind("value_type") == std::string::npos) { // actually resolved?
1822 PyObject* pyvalue_type = CPyCppyy_PyText_FromString(vtype.c_str());
1823 PyObject_SetAttr(pyclass, PyStrings::gValueType, pyvalue_type);
1824 Py_DECREF(pyvalue_type);
1825 }
1826
1827 size_t typesz = Cppyy::SizeOf(name+"::value_type");
1828 if (typesz) {
1829 PyObject* pyvalue_size = PyLong_FromSsize_t(typesz);
1830 PyObject_SetAttr(pyclass, PyStrings::gValueSize, pyvalue_size);
1831 Py_DECREF(pyvalue_size);
1832 }
1833 }
1834 }
1835
1836 else if (IsTemplatedSTLClass(name, "array")) {
1837 // constructor that takes python associative collections
1838 Utility::AddToClass(pyclass, "__real_init", "__init__");
1839 Utility::AddToClass(pyclass, "__init__", (PyCFunction)ArrayInit, METH_VARARGS | METH_KEYWORDS);
1840 }
1841
1842 else if (IsTemplatedSTLClass(name, "map") || IsTemplatedSTLClass(name, "unordered_map")) {
1843 // constructor that takes python associative collections
1844 Utility::AddToClass(pyclass, "__real_init", "__init__");
1845 Utility::AddToClass(pyclass, "__init__", (PyCFunction)MapInit, METH_VARARGS | METH_KEYWORDS);
1846
1847 Utility::AddToClass(pyclass, "__contains__", (PyCFunction)STLContainsWithFind, METH_O);
1848 }
1849
1850 else if (IsTemplatedSTLClass(name, "set")) {
1851 // constructor that takes python associative collections
1852 Utility::AddToClass(pyclass, "__real_init", "__init__");
1853 Utility::AddToClass(pyclass, "__init__", (PyCFunction)SetInit, METH_VARARGS | METH_KEYWORDS);
1854
1855 Utility::AddToClass(pyclass, "__contains__", (PyCFunction)STLContainsWithFind, METH_O);
1856 }
1857
1858 else if (IsTemplatedSTLClass(name, "pair")) {
1859 Utility::AddToClass(pyclass, "__getitem__", (PyCFunction)PairUnpack, METH_O);
1860 Utility::AddToClass(pyclass, "__len__", (PyCFunction)ReturnTwo, METH_NOARGS);
1861 }
1862
1863 if (IsTemplatedSTLClass(name, "shared_ptr") || IsTemplatedSTLClass(name, "unique_ptr")) {
1864 Utility::AddToClass(pyclass, "__real_init", "__init__");
1865 Utility::AddToClass(pyclass, "__init__", (PyCFunction)SmartPtrInit, METH_VARARGS | METH_KEYWORDS);
1866 }
1867
1868 else if (!((PyTypeObject*)pyclass)->tp_iter && \
1869 (name.find("iterator") != std::string::npos || gIteratorTypes.find(name) != gIteratorTypes.end())) {
1870 ((PyTypeObject*)pyclass)->tp_iternext = (iternextfunc)STLIterNext;
1871 Utility::AddToClass(pyclass, CPPYY__next__, (PyCFunction)STLIterNext, METH_NOARGS);
1872 ((PyTypeObject*)pyclass)->tp_iter = (getiterfunc)PyObject_SelfIter;
1873 Utility::AddToClass(pyclass, "__iter__", (PyCFunction)PyObject_SelfIter, METH_NOARGS);
1874 }
1875
1876 else if (name == "string" || name == "std::string") { // TODO: ask backend as well
1877 Utility::AddToClass(pyclass, "__repr__", (PyCFunction)STLStringRepr, METH_NOARGS);
1878 Utility::AddToClass(pyclass, "__str__", (PyCFunction)STLStringStr, METH_NOARGS);
1879 Utility::AddToClass(pyclass, "__bytes__", (PyCFunction)STLStringBytes, METH_NOARGS);
1880 Utility::AddToClass(pyclass, "__cmp__", (PyCFunction)STLStringCompare, METH_O);
1881 Utility::AddToClass(pyclass, "__eq__", (PyCFunction)STLStringIsEqual, METH_O);
1882 Utility::AddToClass(pyclass, "__ne__", (PyCFunction)STLStringIsNotEqual, METH_O);
1883 Utility::AddToClass(pyclass, "__contains__", (PyCFunction)STLStringContains, METH_O);
1884 Utility::AddToClass(pyclass, "decode", (PyCFunction)STLStringDecode, METH_VARARGS | METH_KEYWORDS);
1885 Utility::AddToClass(pyclass, "__cpp_find", "find");
1886 Utility::AddToClass(pyclass, "find", (PyCFunction)STLStringFind, METH_VARARGS | METH_KEYWORDS);
1887 Utility::AddToClass(pyclass, "__cpp_rfind", "rfind");
1888 Utility::AddToClass(pyclass, "rfind", (PyCFunction)STLStringRFind, METH_VARARGS | METH_KEYWORDS);
1889 Utility::AddToClass(pyclass, "__cpp_replace", "replace");
1890 Utility::AddToClass(pyclass, "replace", (PyCFunction)STLStringReplace, METH_VARARGS | METH_KEYWORDS);
1891 Utility::AddToClass(pyclass, "__getattr__", (PyCFunction)STLStringGetAttr, METH_O);
1892
1893 // to allow use of std::string in dictionaries and findable with str
1894 ((PyTypeObject*)pyclass)->tp_hash = (hashfunc)STLStringHash;
1895 }
1896
1897 else if (name == "basic_string_view<char>" || name == "std::basic_string_view<char>") {
1898 Utility::AddToClass(pyclass, "__real_init", "__init__");
1899 Utility::AddToClass(pyclass, "__init__", (PyCFunction)StringViewInit, METH_VARARGS | METH_KEYWORDS);
1900 }
1901
1902 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> >") {
1903 Utility::AddToClass(pyclass, "__repr__", (PyCFunction)STLWStringRepr, METH_NOARGS);
1904 Utility::AddToClass(pyclass, "__str__", (PyCFunction)STLWStringStr, METH_NOARGS);
1905 Utility::AddToClass(pyclass, "__bytes__", (PyCFunction)STLWStringBytes, METH_NOARGS);
1906 Utility::AddToClass(pyclass, "__cmp__", (PyCFunction)STLWStringCompare, METH_O);
1907 Utility::AddToClass(pyclass, "__eq__", (PyCFunction)STLWStringIsEqual, METH_O);
1908 Utility::AddToClass(pyclass, "__ne__", (PyCFunction)STLWStringIsNotEqual, METH_O);
1909 }
1910
1911 else if (name == "complex<double>" || name == "std::complex<double>") {
1912 Utility::AddToClass(pyclass, "__cpp_real", "real");
1913 PyObject_SetAttrString(pyclass, "real", PyDescr_NewGetSet((PyTypeObject*)pyclass, &ComplexDReal));
1914 Utility::AddToClass(pyclass, "__cpp_imag", "imag");
1915 PyObject_SetAttrString(pyclass, "imag", PyDescr_NewGetSet((PyTypeObject*)pyclass, &ComplexDImag));
1916 Utility::AddToClass(pyclass, "__complex__", (PyCFunction)ComplexDComplex, METH_NOARGS);
1917 Utility::AddToClass(pyclass, "__repr__", (PyCFunction)ComplexRepr, METH_NOARGS);
1918 }
1919
1920 else if (IsTemplatedSTLClass(name, "complex")) {
1921 Utility::AddToClass(pyclass, "__cpp_real", "real");
1922 PyObject_SetAttrString(pyclass, "real", PyDescr_NewGetSet((PyTypeObject*)pyclass, &realComplex));
1923 Utility::AddToClass(pyclass, "__cpp_imag", "imag");
1924 PyObject_SetAttrString(pyclass, "imag", PyDescr_NewGetSet((PyTypeObject*)pyclass, &imagComplex));
1925 Utility::AddToClass(pyclass, "__complex__", (PyCFunction)ComplexComplex, METH_NOARGS);
1926 Utility::AddToClass(pyclass, "__repr__", (PyCFunction)ComplexRepr, METH_NOARGS);
1927 }
1928
1929// direct user access; there are two calls here:
1930// - explicit pythonization: won't fall through to the base classes and is preferred if present
1931// - normal pythonization: only called if explicit isn't present, falls through to base classes
1932 bool bUserOk = true; PyObject* res = nullptr;
1934 if (HasAttrDirect(pyclass, PyStrings::gExPythonize)) {
1935 res = PyObject_CallMethodObjArgs(pyclass, PyStrings::gExPythonize, pyclass, pyname, nullptr);
1936 bUserOk = (bool)res;
1937 } else {
1938 PyObject* func = PyObject_GetAttr(pyclass, PyStrings::gPythonize);
1939 if (func) {
1940 res = PyObject_CallFunctionObjArgs(func, pyclass, pyname, nullptr);
1941 Py_DECREF(func);
1942 bUserOk = (bool)res;
1943 } else
1944 PyErr_Clear();
1945 }
1946 if (!bUserOk) {
1947 Py_DECREF(pyname);
1948 return false;
1949 } else {
1950 Py_XDECREF(res);
1951 // pyname handed to args tuple below
1952 }
1953
1954// call registered pythonizors, if any: first run the namespace-specific pythonizors, then
1955// the global ones (the idea is to allow writing a pythonizor that see all classes)
1956 bool pstatus = true;
1957 std::string outer_scope = TypeManip::extract_namespace(name);
1958 if (!outer_scope.empty()) {
1959 auto p = gPythonizations.find(outer_scope);
1960 if (p != gPythonizations.end()) {
1962 name.substr(outer_scope.size()+2, std::string::npos).c_str());
1963 pstatus = run_pythonizors(pyclass, subname, p->second);
1964 Py_DECREF(subname);
1965 }
1966 }
1967
1968 if (pstatus) {
1969 auto p = gPythonizations.find("");
1970 if (p != gPythonizations.end())
1971 pstatus = run_pythonizors(pyclass, pyname, p->second);
1972 }
1973
1974 Py_DECREF(pyname);
1975
1976// phew! all done ...
1977 return pstatus;
1978}
#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
PyObject * CallPyObjMethod(PyObject *obj, const char *meth)
Set of helper functions that are invoked from the C++ implementation of pythonizations.
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
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
#define pyname
Cppyy::TCppType_t ObjectIsA(bool check_smart=true) const
Utility::PyOperators * fOperators
Definition CPPScope.h:61
Cppyy::TCppType_t fCppType
Definition CPPScope.h:55
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)
PyObject_HEAD PyObject * ii_container
Cppyy::TCppType_t vi_klass
CPyCppyy::Converter * vi_converter