5 #define PY_SSIZE_T_CLEAN
11 Config* list2conf(PyObject *dict);
12 void List2Config(
Config& ret, PyObject *dict,
unsigned depth=0);
13 PyObject* conf2dict(
const Config *conf);
18 PyObject* PyGLPSPrint(PyObject *, PyObject *args);
19 Config* PyGLPSParse2Config(PyObject *, PyObject *args, PyObject *kws);
20 PyObject* PyGLPSParse(PyObject *, PyObject *args, PyObject *kws);
22 int registerModMachine(PyObject *mod);
23 int registerModState(PyObject *mod);
25 #define CATCH2V(CXX, PYEXC) catch(CXX& e) { if(!PyErr_Occurred()) PyErr_SetString(PyExc_##PYEXC, e.what()); return; }
26 #define CATCH3(CXX, PYEXC, RET) catch(CXX& e) { if(!PyErr_Occurred()) PyErr_SetString(PyExc_##PYEXC, e.what()); return RET; }
27 #define CATCH2(CXX, PYEXC) CATCH3(CXX, PYEXC, NULL)
28 #define CATCH1(RET) CATCH3(std::exception, RuntimeError, RET)
29 #define CATCH() CATCH2(std::exception, RuntimeError)
31 #if PY_MAJOR_VERSION >= 3
32 #define PyInt_Type PyLong_Type
33 #define PyInt_Check PyLong_Check
34 #define PyInt_FromLong PyLong_FromLong
35 #define PyInt_FromSize_t PyLong_FromSize_t
36 #define PyInt_AsLong PyLong_AsLong
37 #define PyString_Check PyUnicode_Check
38 #define PyString_FromString PyUnicode_FromString
39 #define MODINIT_RET(VAL) return (VAL)
42 #define MODINIT_RET(VAL) return
48 template<
typename T = PyObject>
51 PyRef() :_ptr(NULL) {}
62 explicit PyRef(PyObject* p) : _ptr((T*)p) {
64 throw std::bad_alloc();
67 PyRef(T* p, borrow) : _ptr(p) {
69 throw std::bad_alloc();
72 ~PyRef() {Py_CLEAR(_ptr);}
73 PyRef& operator =(
const PyRef& o)
94 throw std::bad_alloc();
98 void reset(T* p, borrow) {
100 throw std::bad_alloc();
101 PyObject *tmp = _ptr;
106 struct allow_null {};
107 T* reset(T* p, allow_null) {
112 T*
get()
const {
return _ptr; }
113 PyObject* releasePy() {
114 return (PyObject*)release();
116 PyObject* py()
const {
117 return (PyObject*)_ptr;
123 T& operator*()
const {
126 T* operator->()
const {
136 PyCString(
const PyRef<>& o) { *
this = o; }
137 PyCString& operator=(
const PyRef<>& o) { reset(o.py());
return *
this; }
138 void reset(PyObject *obj)
140 if(PY_MAJOR_VERSION>=3 || PyUnicode_Check(obj)) {
141 pystr.reset(PyUnicode_AsUTF8String(obj));
142 }
else if(PyBytes_Check(obj)) {
143 pystr.reset(obj, borrow());
146 const char *c_str(PyObject *obj) {
148 throw std::bad_alloc();
152 const char *c_str()
const {
153 const char *ret = PyBytes_AsString(pystr.py());
155 throw std::invalid_argument(
"Can't extract string from object");
160 PyCString(
const PyCString&);
161 PyCString& operator=(
const PyCString&);
168 PyGetBuf() : havebuf(false) {}
170 if(havebuf) PyBuffer_Release(&buf);
172 bool get(PyObject *obj) {
173 if(havebuf) PyBuffer_Release(&buf);
174 havebuf = PyObject_GetBuffer(obj, &buf, PyBUF_SIMPLE)==0;
175 if(!havebuf) PyErr_Clear();
178 size_t size()
const {
return havebuf ? buf.len : 0; }
179 void * data()
const {
return buf.buf; }
The abstract base class for all simulation state objects.
Associative configuration container.