41 PyObject* dct = PyObject_GetAttr(pyclass, PyStrings::gDict);
57 PyObject* dct = PyObject_GetAttr(pyclass, PyStrings::gDict);
67inline bool IsTemplatedSTLClass(
const std::string&
name,
const std::string& klass) {
69 auto pos =
name.find(klass);
70 return (pos == 0 || pos == 5) &&
name.find(
"::",
name.rfind(
">")) == std::string::npos;
78 PyObject*
result = PyObject_CallMethod(obj,
const_cast<char*
>(meth),
const_cast<char*
>(
""));
89 obj,
const_cast<char*
>(meth),
const_cast<char*
>(
"O"), arg1);
103 if (idx >=
size || (idx < 0 && idx < -
size)) {
104 PyErr_SetString(PyExc_IndexError,
"index out of range");
113 pyindex = PyLong_FromSsize_t(
size+idx);
122 if ((step > 0 && stop <= start) || (step < 0 && start <= stop))
125 if (start < 0)
start = 0;
126 if (start >= nlen)
start = nlen-1;
127 if (step >= nlen) step = nlen;
129 stop = step > 0 ? std::min(nlen, stop) : (stop >= 0 ? stop : -1);
155 if (
name == PyStrings::gTypeCode ||
name == PyStrings::gCTypesType) {
164 PyErr_SetString(PyExc_TypeError,
"getattr(): attribute name must be string");
172 PyObject* val1 = PyObject_Str(self);
174 PyErr_Format(PyExc_AttributeError,
"%s has no attribute \'%s\'",
194 PyErr_SetString(PyExc_TypeError,
"getattr(): attribute name must be string");
209 PyErr_SetString(PyExc_TypeError,
"C++ object proxy expected");
220#if PY_VERSION_HEX < 0x03040000
221#define PyObject_LengthHint _PyObject_LengthHint
226 ItemGetter(
PyObject* pyobj) : fPyObject(pyobj) { Py_INCREF(fPyObject); }
227 virtual ~ItemGetter() { Py_DECREF(fPyObject); }
233struct CountedItemGetter :
public ItemGetter {
234 CountedItemGetter(
PyObject* pyobj) : ItemGetter(pyobj), fCur(0) {}
238struct TupleItemGetter :
public CountedItemGetter {
239 using CountedItemGetter::CountedItemGetter;
242 if (fCur < PyTuple_GET_SIZE(fPyObject)) {
243 PyObject* item = PyTuple_GET_ITEM(fPyObject, fCur++);
247 PyErr_SetString(PyExc_StopIteration,
"end of tuple");
252struct ListItemGetter :
public CountedItemGetter {
253 using CountedItemGetter::CountedItemGetter;
256 if (fCur < PyList_GET_SIZE(fPyObject)) {
257 PyObject* item = PyList_GET_ITEM(fPyObject, fCur++);
261 PyErr_SetString(PyExc_StopIteration,
"end of list");
266struct SequenceItemGetter :
public CountedItemGetter {
267 using CountedItemGetter::CountedItemGetter;
276 virtual PyObject* get() {
return PySequence_GetItem(fPyObject, fCur++); }
279struct IterItemGetter :
public ItemGetter {
280 using ItemGetter::ItemGetter;
282 virtual PyObject* get() {
return (*(
Py_TYPE(fPyObject)->tp_iternext))(fPyObject); }
285static ItemGetter* GetGetter(
PyObject* args)
288 ItemGetter* getter =
nullptr;
290 if (PyTuple_GET_SIZE(args) == 1) {
291 PyObject* fi = PyTuple_GET_ITEM(args, 0);
298 if (PyObject_CheckBuffer(fi))
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);
308 PyObject* iter = PyObject_GetIter(fi);
310 getter =
new IterItemGetter{iter};
328 PyObject* res = PyObject_CallMethod(vecin, (
char*)
"reserve", (
char*)
"n", sz);
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))) {
341 PyObject* eb_call = PyObject_GetAttrString(vecin, (
char*)
"emplace_back");
343 bool value_is_vector =
false;
347 value_is_vector =
true;
354 for (
int i = 0; ; ++i) {
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)) {
362 }
else if (PyList_CheckExact(item)) {
364 eb_args = PyTuple_New(isz);
366 PyObject* iarg = PyList_GET_ITEM(item, j);
368 PyTuple_SET_ITEM(eb_args, j, iarg);
373 PyErr_Format(PyExc_TypeError,
"argument %d is not a tuple or list", i);
377 PyObject* ebres = PyObject_CallObject(eb_call, eb_args);
385 if (PyErr_Occurred()) {
386 if (!(PyErr_ExceptionMatches(PyExc_IndexError) ||
387 PyErr_ExceptionMatches(PyExc_StopIteration)))
389 else { PyErr_Clear(); }
398 PyObject* pb_call = PyObject_GetAttrString(vecin, (
char*)
"push_back");
403 PyObject* pbres = PyObject_CallFunctionObjArgs(pb_call, item,
nullptr);
411 if (PyErr_Occurred()) {
412 if (!(PyErr_ExceptionMatches(PyExc_IndexError) ||
413 PyErr_ExceptionMatches(PyExc_StopIteration)))
415 else { PyErr_Clear(); }
431 ItemGetter* getter = GetGetter(args);
434 bool fill_ok =
FillVector(self, args, getter);
446 if (PyTuple_GET_SIZE(args) == 1) {
447 PyObject* fi = PyTuple_GET_ITEM(args, 0);
451 PyObject*
result = PyObject_CallMethodObjArgs(self, PyStrings::gInsert, vend, fi,
nullptr);
458 if (!PyErr_Occurred())
459 PyErr_SetString(PyExc_TypeError,
"argument is not iterable");
472 ItemGetter* getter = GetGetter(args);
482 bool fill_ok =
FillVector(self, args, getter);
494 PyObject* realInit = PyObject_GetAttr(self, PyStrings::gRealInit);
517 long clen = PyInt_AsLong(pylen);
533 PyObject* pydata = VectorData(self,
nullptr);
543 if (!vi)
return nullptr;
557 vi->
vi_stride = PyLong_AsLong(pyvalue_size);
558 Py_DECREF(pyvalue_size);
571 if (value_type.back() !=
'*')
599 Py_XDECREF(pyvalue_type);
602 vi->
ii_len = PySequence_Size(
v);
604 PyObject_GC_Track(vi);
611 if (PySlice_Check(
index)) {
613 PyErr_SetString(PyExc_TypeError,
"unsubscriptable object");
618 PyObject* nseq = PyObject_CallObject(pyclass,
nullptr);
624 if (!AdjustSlice(nlen, start, stop, step))
628 for (
Py_ssize_t i = start; i*sign < stop*sign; i += step) {
639 return CallSelfIndex(self, (
PyObject*)
index, PyStrings::gGetNoCheck);
650 PyErr_Format(PyExc_TypeError,
651 "require object of type std::vector<bool>, but %s given",
657 PyErr_SetString(PyExc_TypeError,
"unsubscriptable object");
661 if (PySlice_Check(idx)) {
663 PyObject* nseq = PyObject_CallObject(pyclass,
nullptr);
668 if (!AdjustSlice(nlen, start, stop, step))
672 for (
Py_ssize_t i = start; i*sign < stop*sign; i += step) {
687 int index = (
int)PyLong_AsLong(pyindex);
691 std::vector<bool>* vb = (std::vector<bool>*)self->
GetObject();
694 if (
bool((*vb)[
index]))
704 PyErr_Format(PyExc_TypeError,
705 "require object of type std::vector<bool>, but %s given",
711 PyErr_SetString(PyExc_TypeError,
"unsubscriptable object");
715 int bval = 0;
PyObject* idx =
nullptr;
716 if (!PyArg_ParseTuple(args,
const_cast<char*
>(
"Oi:__setitem__"), &idx, &bval))
723 int index = (
int)PyLong_AsLong(pyindex);
727 std::vector<bool>* vb = (std::vector<bool>*)self->
GetObject();
743 if (args && PyTuple_GET_SIZE(args) == 1 && PySequence_Check(PyTuple_GET_ITEM(args, 0))) {
749 PyObject* items = PyTuple_GET_ITEM(args, 0);
751 if (PySequence_Size(self) != fillsz) {
752 PyErr_Format(PyExc_ValueError,
"received sequence of size %zd where %zd expected",
753 fillsz, PySequence_Size(self));
758 PyObject* si_call = PyObject_GetAttr(self, PyStrings::gSetItem);
760 PyObject* item = PySequence_GetItem(items, i);
762 PyObject* sires = PyObject_CallFunctionObjArgs(si_call,
index, item,
nullptr);
779 PyObject* realInit = PyObject_GetAttr(self, PyStrings::gRealInit);
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);
802 if (pair && PySequence_Check(pair) && PySequence_Size(pair) == 2) {
803 PyObject* key = PySequence_GetItem(pair, 0);
805 sires = PyObject_CallFunctionObjArgs(si_call, key,
value,
nullptr);
813 if (!PyErr_Occurred())
814 PyErr_SetString(PyExc_TypeError,
"Failed to fill map (argument not a dict or sequence of pairs)");
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
837 PyObject* items = PyObject_CallMethod(assoc, (
char*)
"items",
nullptr);
840 PyObject* items = PyMapping_Items(assoc);
842 if (items && PySequence_Check(items)) {
855 if (PyTuple_GET_SIZE(args) == 1 && PySequence_Check(PyTuple_GET_ITEM(args, 0)))
856 return MapFromPairs(self, PyTuple_GET_ITEM(args, 0));
859 PyObject* realInit = PyObject_GetAttr(self, PyStrings::gRealInit);
878 if (!PyObject_RichCompareBool(iter, end, Py_EQ)) {
901 if (PyTuple_GET_SIZE(args) == 1 && PySet_Check(PyTuple_GET_ITEM(args, 0))) {
902 PyObject* pyset = PyTuple_GET_ITEM(args, 0);
909 PyObject* iter = PyObject_GetIter(pyset);
911 PyObject* ins_call = PyObject_GetAttrString(self, (
char*)
"insert");
913 IterItemGetter getter{iter};
918 PyObject* insres = PyObject_CallFunctionObjArgs(ins_call, item,
nullptr);
935 PyObject* realInit = PyObject_GetAttr(self, PyStrings::gRealInit);
947static const ptrdiff_t PS_END_ADDR = 7;
948static const ptrdiff_t PS_FLAG_ADDR = 11;
949static const ptrdiff_t PS_COLL_ADDR = 13;
971 PyErr_SetString(PyExc_TypeError,
"unrecognized iterator type for low level views");
987 auto& dmc = ((
CPPInstance*)iter)->GetDatamemberCache();
988 dmc.push_back(std::make_pair(PS_END_ADDR, end));
992 dmc.push_back(std::make_pair(PS_FLAG_ADDR, Py_False));
996 dmc.push_back(std::make_pair(PS_COLL_ADDR, self));
1011 if (!ii)
return nullptr;
1016 ii->
ii_len = PySequence_Size(
c);
1018 PyObject_GC_Track(ii);
1056 long idx = PyLong_AsLong(pyindex);
1057 if (idx == -1 && PyErr_Occurred())
1061 PyErr_SetString(PyExc_TypeError,
"unsubscriptable object");
1066 return PyObject_GetAttr(self, PyStrings::gFirst);
1067 else if ((
int)idx == 1)
1068 return PyObject_GetAttr(self, PyStrings::gSecond);
1071 PyErr_SetString(PyExc_IndexError,
"out of bounds");
1077 return PyInt_FromLong(2);
1085 PyObject* realInit = PyObject_GetAttr(self, PyStrings::gRealInit);
1088 Py_DECREF(realInit);
1100#if PY_VERSION_HEX >= 0x03000000
1103 return !PyObject_RichCompareBool(one, other, Py_EQ);
1107PyObject* CPyCppyy_PyString_FromCppString(std::string* s,
bool native=
true) {
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");
1124#define CPPYY_IMPL_STRING_PYTHONIZATION(type, name) \
1126PyObject* name##StringGetData(PyObject* self, bool native=true) \
1128 if (CPyCppyy::CPPInstance_Check(self)) { \
1129 type* obj = ((type*)((CPPInstance*)self)->GetObject()); \
1130 if (obj) return CPyCppyy_PyString_FromCppString(obj, native); \
1132 PyErr_Format(PyExc_TypeError, "object mismatch (%s expected)", #type); \
1136PyObject* name##StringStr(PyObject* self) \
1138 PyObject* pyobj = name##StringGetData(self, false); \
1142 PyObject* pybytes = name##StringGetData(self, true); \
1144 pyobj = PyObject_Str(pybytes); \
1145 Py_DECREF(pybytes); \
1151PyObject* name##StringBytes(PyObject* self) \
1153 return name##StringGetData(self, true); \
1156PyObject* name##StringRepr(PyObject* self) \
1158 PyObject* data = name##StringGetData(self, true); \
1160 PyObject* repr = PyObject_Repr(data); \
1167PyObject* name##StringIsEqual(PyObject* self, PyObject* obj) \
1169 PyObject* data = name##StringGetData(self, PyBytes_Check(obj)); \
1171 PyObject* result = PyObject_RichCompare(data, obj, Py_EQ); \
1178PyObject* name##StringIsNotEqual(PyObject* self, PyObject* obj) \
1180 PyObject* data = name##StringGetData(self, PyBytes_Check(obj)); \
1182 PyObject* result = PyObject_RichCompare(data, obj, Py_NE); \
1190#define CPPYY_IMPL_STRING_PYTHONIZATION_CMP(type, name) \
1191CPPYY_IMPL_STRING_PYTHONIZATION(type, name) \
1192PyObject* name##StringCompare(PyObject* self, PyObject* obj) \
1194 PyObject* data = name##StringGetData(self, PyBytes_Check(obj)); \
1197 result = PyObject_Compare(data, obj); \
1200 if (PyErr_Occurred()) \
1202 return PyInt_FromLong(result); \
1208static inline std::string* GetSTLString(
CPPInstance* self) {
1210 PyErr_SetString(PyExc_TypeError,
"std::string object expected");
1214 std::string* obj = (std::string*)self->
GetObject();
1216 PyErr_SetString(PyExc_ReferenceError,
"attempt to access a null-pointer");
1223 std::string* obj = GetSTLString(self);
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))
1233 return PyUnicode_Decode(obj->data(), obj->size(), encoding, errors);
1238 std::string* obj = GetSTLString(self);
1246 if (obj->find(needle) != std::string::npos) {
1255 std::string* obj = GetSTLString(self);
1265 PyObject* meth = PyObject_GetAttrString(pystr, (
char*)
"replace");
1272 PyObject* cppreplace = PyObject_GetAttrString((
PyObject*)self, (
char*)
"__cpp_replace");
1275 Py_DECREF(cppreplace);
1279 PyErr_SetString(PyExc_AttributeError,
"\'std::string\' object has no attribute \'replace\'");
1283#define CPYCPPYY_STRING_FINDMETHOD(name, cppname, pyname) \
1284PyObject* STLString##name(CPPInstance* self, PyObject* args, PyObject* ) \
1286 std::string* obj = GetSTLString(self); \
1290 PyObject* cppmeth = PyObject_GetAttrString((PyObject*)self, (char*)#cppname);\
1292 PyObject* result = PyObject_Call(cppmeth, args, nullptr); \
1293 Py_DECREF(cppmeth); \
1295 if (PyLongOrInt_AsULong64(result) == (PY_ULONG_LONG)std::string::npos) {\
1296 Py_DECREF(result); \
1297 return PyInt_FromLong(-1); \
1304 PyObject* pystr = CPyCppyy_PyText_FromStringAndSize(obj->data(), obj->size());\
1305 PyObject* pymeth = PyObject_GetAttrString(pystr, (char*)#pyname); \
1307 PyObject* result = PyObject_CallObject(pymeth, args); \
1308 Py_DECREF(pymeth); \
1319 std::string* obj = GetSTLString(self);
1337 PyObject* str_res = PyObject_Str(res);
1348 PyObject* str_res = PyObject_Str(res);
1371 PyObject* realInit = PyObject_GetAttr(self, PyStrings::gRealInit);
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)) {
1378 strbuf = PyUnicode_AsEncodedString(arg0,
"UTF-8",
"strict");
1379 newArgs = PyTuple_New(1);
1381 PyTuple_SET_ITEM(newArgs, 0, strbuf);
1389 PyObject*
result = PyObject_Call(realInit, newArgs ? newArgs : args, nullptr);
1391 Py_XDECREF(newArgs);
1392 Py_DECREF(realInit);
1396 if (
result && self && strbuf)
1397 PyObject_SetAttr(self, PyStrings::gLifeLine, strbuf);
1411 bool mustIncrement =
true;
1414 auto& dmc = ((
CPPInstance*)self)->GetDatamemberCache();
1415 for (
auto&
p: dmc) {
1416 if (
p.first == PS_END_ADDR) {
1419 }
else if (
p.first == PS_FLAG_ADDR) {
1420 mustIncrement =
p.second == Py_True;
1421 if (!mustIncrement) {
1422 Py_DECREF(
p.second);
1433 if (!PyObject_RichCompareBool(last, self, Py_EQ)) {
1434 bool iter_valid =
true;
1435 if (mustIncrement) {
1441 static PyObject* dummy = PyInt_FromLong(1l);
1444 iter_valid = iter && PyObject_RichCompareBool(last, self, Py_NE);
1450 if (!next) PyErr_Clear();
1456 if (!next) PyErr_SetString(PyExc_StopIteration,
"");
1462#define COMPLEX_METH_GETSET(name, cppname) \
1463static PyObject* name##ComplexGet(PyObject* self, void*) { \
1464 return PyObject_CallMethodNoArgs(self, cppname); \
1466static int name##ComplexSet(PyObject* self, PyObject* value, void*) { \
1467 PyObject* result = PyObject_CallMethodOneArg(self, cppname, value); \
1469 Py_DECREF(result); \
1474PyGetSetDef name##Complex{(char*)#name, (getter)name##ComplexGet, (setter)name##ComplexSet, nullptr, nullptr};
1481 if (!real)
return nullptr;
1482 double r = PyFloat_AsDouble(real);
1484 if (
r == -1. && PyErr_Occurred())
1488 if (!imag)
return nullptr;
1489 double i = PyFloat_AsDouble(imag);
1491 if (i == -1. && PyErr_Occurred())
1494 return PyComplex_FromDoubles(
r, i);
1499 if (!real)
return nullptr;
1500 double r = PyFloat_AsDouble(real);
1502 if (
r == -1. && PyErr_Occurred())
1506 if (!imag)
return nullptr;
1507 double i = PyFloat_AsDouble(imag);
1509 if (i == -1. && PyErr_Occurred())
1512 std::ostringstream s;
1513 s <<
'(' <<
r <<
'+' << i <<
"j)";
1519 return PyFloat_FromDouble(((std::complex<double>*)self->
GetObject())->real());
1524 double d = PyFloat_AsDouble(
value);
1525 if (
d == -1.0 && PyErr_Occurred())
1527 ((std::complex<double>*)self->
GetObject())->real(
d);
1531PyGetSetDef ComplexDReal{(
char*)
"real", (getter)ComplexDRealGet, (setter)ComplexDRealSet,
nullptr,
nullptr};
1536 return PyFloat_FromDouble(((std::complex<double>*)self->
GetObject())->imag());
1541 double d = PyFloat_AsDouble(
value);
1542 if (
d == -1.0 && PyErr_Occurred())
1544 ((std::complex<double>*)self->
GetObject())->imag(
d);
1548PyGetSetDef ComplexDImag{(
char*)
"imag", (getter)ComplexDImagGet, (setter)ComplexDImagSet,
nullptr,
nullptr};
1552 double r = ((std::complex<double>*)self->
GetObject())->real();
1553 double i = ((std::complex<double>*)self->
GetObject())->imag();
1554 return PyComplex_FromDoubles(
r, i);
1570 Py_INCREF(pyclass); PyTuple_SET_ITEM(args, 0, pyclass);
1573 bool pstatus =
true;
1574 for (
auto pythonizor :
v) {
1608 if (HasAttrDirect(pyclass, PyStrings::gCppBool)) {
1609#if PY_VERSION_HEX >= 0x03000000
1610 const char* pybool_name =
"__bool__";
1612 const char* pybool_name =
"__nonzero__";
1618 if (HasAttrDirect(pyclass, PyStrings::gSize))
1621 if (!IsTemplatedSTLClass(
name,
"vector") &&
1622 !((PyTypeObject*)pyclass)->tp_iter) {
1623 if (HasAttrDirect(pyclass, PyStrings::gBegin) && HasAttrDirect(pyclass, PyStrings::gEnd)) {
1633 if (resname.find(
"iterator") == std::string::npos)
1640 ((PyTypeObject*)pyclass)->tp_iter = (getiterfunc)STLSequenceIter;
1646 if (resolved.back() ==
'*' &&
Cppyy::IsBuiltin(resolved.substr(0, resolved.size()-1))) {
1647 ((PyTypeObject*)pyclass)->tp_iter = (getiterfunc)LLSequenceIter;
1653 if (!((PyTypeObject*)pyclass)->tp_iter &&
1654 HasAttrDirect(pyclass, PyStrings::gGetItem) && PyObject_HasAttr(pyclass, PyStrings::gLen)) {
1659 ((PyTypeObject*)pyclass)->tp_iter = (getiterfunc)index_iter;
1667 if (HasAttrDirect(pyclass, PyStrings::gEq,
true) && \
1669 PyObject* cppol = PyObject_GetAttr(pyclass, PyStrings::gEq);
1677 top_eq = PyObject_GetAttr(top_cls, PyStrings::gEq);
1681 PyObject_SetAttr(pyclass, PyStrings::gEq, top_eq);
1684 if (HasAttrDirect(pyclass, PyStrings::gNe,
true) && \
1686 PyObject* cppol = PyObject_GetAttr(pyclass, PyStrings::gNe);
1693 top_ne = PyObject_GetAttr(top_cls, PyStrings::gNe);
1697 PyObject_SetAttr(pyclass, PyStrings::gNe, top_ne);
1701 if (HasAttrDirect(pyclass, PyStrings::gRepr,
true)) {
1707 if (HasAttrDirect(pyclass, PyStrings::gStr,
true)) {
1721 std::string rname =
name;
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);
1739 if (res_clean ==
"internal_enum_type_t")
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+
"&");
1747 arg_defaults.push_back(
"0");
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];
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];
1769 initdef <<
"};\n} }";
1788 if (IsTemplatedSTLClass(
name,
"vector")) {
1792 if (klass->
fCppType == sVectorBoolTypeID) {
1798 Utility::AddToClass(pyclass,
"__init__", (PyCFunction)VectorInit, METH_VARARGS | METH_KEYWORDS);
1808 if (HasAttrDirect(pyclass, PyStrings::gLen)) {
1814 ((PyTypeObject*)pyclass)->tp_iter = (getiterfunc)vector_iter;
1817 Utility::AddToClass(pyclass,
"__iadd__", (PyCFunction)VectorIAdd, METH_VARARGS | METH_KEYWORDS);
1821 if (vtype.rfind(
"value_type") == std::string::npos) {
1823 PyObject_SetAttr(pyclass, PyStrings::gValueType, pyvalue_type);
1824 Py_DECREF(pyvalue_type);
1829 PyObject* pyvalue_size = PyLong_FromSsize_t(typesz);
1830 PyObject_SetAttr(pyclass, PyStrings::gValueSize, pyvalue_size);
1831 Py_DECREF(pyvalue_size);
1836 else if (IsTemplatedSTLClass(
name,
"array")) {
1839 Utility::AddToClass(pyclass,
"__init__", (PyCFunction)ArrayInit, METH_VARARGS | METH_KEYWORDS);
1842 else if (IsTemplatedSTLClass(
name,
"map") || IsTemplatedSTLClass(
name,
"unordered_map")) {
1845 Utility::AddToClass(pyclass,
"__init__", (PyCFunction)MapInit, METH_VARARGS | METH_KEYWORDS);
1850 else if (IsTemplatedSTLClass(
name,
"set")) {
1853 Utility::AddToClass(pyclass,
"__init__", (PyCFunction)SetInit, METH_VARARGS | METH_KEYWORDS);
1858 else if (IsTemplatedSTLClass(
name,
"pair")) {
1863 if (IsTemplatedSTLClass(
name,
"shared_ptr") || IsTemplatedSTLClass(
name,
"unique_ptr")) {
1865 Utility::AddToClass(pyclass,
"__init__", (PyCFunction)SmartPtrInit, METH_VARARGS | METH_KEYWORDS);
1868 else if (!((PyTypeObject*)pyclass)->tp_iter && \
1870 ((PyTypeObject*)pyclass)->tp_iternext = (iternextfunc)STLIterNext;
1872 ((PyTypeObject*)pyclass)->tp_iter = (getiterfunc)PyObject_SelfIter;
1876 else if (
name ==
"string" ||
name ==
"std::string") {
1884 Utility::AddToClass(pyclass,
"decode", (PyCFunction)STLStringDecode, METH_VARARGS | METH_KEYWORDS);
1886 Utility::AddToClass(pyclass,
"find", (PyCFunction)STLStringFind, METH_VARARGS | METH_KEYWORDS);
1888 Utility::AddToClass(pyclass,
"rfind", (PyCFunction)STLStringRFind, METH_VARARGS | METH_KEYWORDS);
1890 Utility::AddToClass(pyclass,
"replace", (PyCFunction)STLStringReplace, METH_VARARGS | METH_KEYWORDS);
1894 ((PyTypeObject*)pyclass)->tp_hash = (hashfunc)STLStringHash;
1897 else if (
name ==
"basic_string_view<char>" ||
name ==
"std::basic_string_view<char>") {
1899 Utility::AddToClass(pyclass,
"__init__", (PyCFunction)StringViewInit, METH_VARARGS | METH_KEYWORDS);
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> >") {
1911 else if (
name ==
"complex<double>" ||
name ==
"std::complex<double>") {
1913 PyObject_SetAttrString(pyclass,
"real", PyDescr_NewGetSet((PyTypeObject*)pyclass, &ComplexDReal));
1915 PyObject_SetAttrString(pyclass,
"imag", PyDescr_NewGetSet((PyTypeObject*)pyclass, &ComplexDImag));
1920 else if (IsTemplatedSTLClass(
name,
"complex")) {
1922 PyObject_SetAttrString(pyclass,
"real", PyDescr_NewGetSet((PyTypeObject*)pyclass, &realComplex));
1924 PyObject_SetAttrString(pyclass,
"imag", PyDescr_NewGetSet((PyTypeObject*)pyclass, &imagComplex));
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;
1938 PyObject* func = PyObject_GetAttr(pyclass, PyStrings::gPythonize);
1940 res = PyObject_CallFunctionObjArgs(func, pyclass,
pyname,
nullptr);
1942 bUserOk = (
bool)res;
1956 bool pstatus =
true;
1958 if (!outer_scope.empty()) {
1962 name.substr(outer_scope.size()+2, std::string::npos).c_str());
#define PyInt_FromSsize_t
#define CPyCppyy_PyText_FromStringAndSize
#define CPyCppyy_PySliceCast
#define CPyCppyy_PyText_AsString
static PyObject * PyObject_CallMethodOneArg(PyObject *obj, PyObject *name, PyObject *arg)
#define PyBytes_FromStringAndSize
#define CPyCppyy_PyText_Type
static PyObject * PyObject_CallMethodNoArgs(PyObject *obj, PyObject *name)
#define CPyCppyy_PyText_FromString
#define CPyCppyy_PyText_Check
#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)
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
Cppyy::TCppType_t ObjectIsA(bool check_smart=true) const
Utility::PyOperators * fOperators
Cppyy::TCppType_t fCppType
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)
bool AddToClass(PyObject *pyclass, const char *label, PyCFunction cfunc, int flags=METH_VARARGS)
PyTypeObject VectorIter_Type
static PyObject * GetAttrDirect(PyObject *pyclass, PyObject *pyname)
bool Pythonize(PyObject *pyclass, const std::string &name)
bool CPPOverload_Check(T *object)
std::map< std::string, std::vector< PyObject * > > gPythonizations
bool CPPScope_Check(T *object)
bool LowLevelView_Check(T *object)
bool CPPInstance_Check(T *object)
PyTypeObject IndexIter_Type
CPYCPPYY_EXTERN Converter * CreateConverter(const std::string &name, cdims_t=0)
std::set< std::string > gIteratorTypes
RPY_EXPORTED size_t SizeOf(TCppType_t klass)
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)
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)
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