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