8 #include <initializer_list>
12 #define PY11_ENFORCE 1
28 PPyObject(PyObject* p):_p(p)
31 operator PyObject*()
const
36 template<
typename T>
explicit operator T()
const
62 static py_initer __init;
69 class obj :
private details::py_initer_wrap{
81 const_cast<obj&
>(*this)._p = NULL;
84 void enter(PyObject*
p)noexcept
90 void __enter()noexcept
103 obj()noexcept:_p(NULL)
119 obj& operator=(
const obj& o)
132 obj(PyObject* p,
bool borrowed =
false)noexcept:_p(p)
138 obj& operator=(PyObject* p)noexcept
154 obj& operator=(
obj&& o)noexcept
166 obj(std::initializer_list<obj> l):_p(PyTuple_New(l.
size()))
170 PyTuple_SET_ITEM(_p, i++, x._p);
175 obj& operator = (std::initializer_list<obj> l)
177 _p = PyTuple_New(l.size());
180 PyTuple_SET_ITEM(_p, i++, x._p);
228 return (!_p) || PyObject_Not(_p);
233 obj(
int i):_p(PyInt_FromLong(i))
238 obj(
long i):_p(PyInt_FromLong(i))
245 return _p && PyInt_Check(_p);
253 long r = PyInt_AsLong(_p);
254 if(r== -1 && PyErr_Occurred() != NULL){
262 obj(
double v):_p(PyFloat_FromDouble(v))
269 return _p && PyFloat_Check(_p);
277 double r = PyFloat_AsDouble(_p);
278 if(r== -1.0 && PyErr_Occurred() != NULL){
286 obj(
const char* s):_p(PyString_FromString(s))
295 if(PyString_Check(_p)){
296 p = PyString_AsString(_p);
298 else if(PyUnicode_Check(_p)){
299 p = PyUnicode_AS_DATA(_p);
301 else if(PyByteArray_Check(_p)){
302 p = PyByteArray_AsString(_p);
315 PyObject* p = PyObject_Type(_p);
330 throw type_err(
"is_a with null type");
332 int r = PyObject_TypeCheck(_p, t._p->ob_type);
345 return PyObject_HasAttr(_p, o._p);
352 return PyObject_HasAttrString(_p, s);
360 PyObject* p = PyObject_GetAttr(_p, o._p);
372 PyObject* p = PyObject_GetAttrString(_p, s);
384 PyObject* p = PyObject_GetAttrString(_p, s);
396 int r = PyObject_SetAttr(_p, a._p, v._p);
406 int r = PyObject_SetAttrString(_p, a, v._p);
416 int r = PyObject_DelAttr(_p, a._p);
426 int r = PyObject_DelAttrString(_p, a);
444 int r = PyObject_RichCompareBool(_p, o._p, Py_LT);
451 bool operator <= (
const obj& o)
const
459 int r = PyObject_RichCompareBool(_p, o._p, Py_LE);
466 bool operator == (
const obj& o)
const
474 int r = PyObject_RichCompareBool(_p, o._p, Py_EQ);
476 throw val_err(
"op == failed");
481 bool operator != (
const obj& o)
const
489 int r = PyObject_RichCompareBool(_p, o._p, Py_NE);
491 throw val_err(
"op != failed");
496 bool operator > (
const obj& o)
const
504 int r = PyObject_RichCompareBool(_p, o._p, Py_GT);
506 throw val_err(
"op > failed");
511 bool operator >= (
const obj& o)
const
519 int r = PyObject_RichCompareBool(_p, o._p, Py_GE);
521 throw val_err(
"op >= failed");
533 PyObject* p = PyObject_Repr(_p);
544 PyObject* p = PyObject_Str(_p);
556 PyObject* p = PyObject_Unicode(_p);
558 throw val_err(
"unicode failed");
562 void output(std::ostream& s)
const
568 if(PyString_Check(_p)){
569 const char* p = PyString_AsString(_p);
571 throw val_err(
"bad internal string");
585 return (_p) && (PyCallable_Check(_p));
593 PyObject* r = PyObject_CallFunctionObjArgs(_p,
obj(
a)._p..., NULL);
595 throw type_err(
"operator() failed");
604 PyObject* r = PyObject_CallObject(_p, args._p);
615 PyObject* r = PyObject_Call(_p, args._p, kw._p);
629 if(PySequence_Check(_p)){
630 long r = PySequence_Size(_p);
634 else if(PyMapping_Check(_p)){
635 long r = PyMapping_Size(_p);
639 else if(PyAnySet_Check(_p)){
640 long r = PySet_Size(_p);
654 if(PySequence_Check(_p)){
655 int r = PySequence_Contains(_p, x._p);
659 else if(PyMapping_Check(_p)){
660 int r = PyMapping_HasKey(_p, x._p);
664 else if(PyAnySet_Check(_p)){
665 int r = PySet_Contains(_p, x._p);
679 PyObject* p = PyObject_GetItem(_p, o._p);
691 int r = PyObject_SetItem(_p, key._p, value._p);
701 int r = PyObject_DelItem(_p, key._p);
722 PyObject* p = PyObject_Dir(_p);
733 return _p->ob_refcnt;
750 inline std::ostream& operator <<(std::ostream& s,
const obj& o)
764 #include "_tuple.hpp"
794 PyObject* r = PySequence_List(_p);
802 PyObject* r = PySequence_Tuple(_p);
810 #endif // PY11_PY_HPP
obj call(const obj &args, const obj &kw) const
call with args, and key/value pairs.
void set_attr(const char *a, const obj &v)
set attr.
obj(const obj &o) noexcept
copy ctor.
bool operator!() const
test null.
Py_ssize_t refcnt() const
refcnt.
obj(obj &&o) noexcept
move ctor.
obj(PyObject *p, bool borrowed=false) noexcept
create from Py functions.
void del_item(const obj &key)
del_item.
bool is_a(const obj &t) const
type check
void set_item(const obj &key, const obj &value)
set_item.
void del_attr(const char *a)
del attr.
double as_double() const
as_double from obj.
bool is_int() const
is_int.
iter begin() const
get the begin iter.
list to_list() const
get a list clone.
PyObject * transfer() noexcept
transfer the ownership of inner object.
bool operator<(const obj &o) const
<
obj to_unicode() const
unicode.
obj type() const
py object type.
PPyObject p() const
get inner PyObject.
iter end() const
get the end iter.
obj attr(const char *s) const
get attr.
const char * c_str() const
c_str().
obj call(const obj &args) const
call with args.
iter for the c++11 range loop.
const obj operator[](const obj &o) const
get item.
obj a(const char *s) const
get attr, short form.
void release() noexcept
release the contained object
obj dir() const
py object dir.
long size() const
'len' as 'size'.
bool has_attr(const obj &o) const
has attr.
void del_attr(const obj &a)
del attr.
bool has_attr(const char *s) const
has attr.
tuple to_tuple() const
get a tuple clone.
obj operator()(argT &&...a) const
call using operator.
obj attr(const obj &o) const
get attr.
long as_long() const
get long from obj
obj(std::initializer_list< obj > l)
tuple.
bool has(const obj &x) const
'in' as 'has'.
void set_attr(const obj &a, const obj &v)
set attr.
obj(double v)
double ctor.
bool is_callable() const
test callable.
bool is_float() const
is_float.