35 #ifndef PYTHONPP_EXT_H
36 #define PYTHONPP_EXT_H
44 #if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_TYPE)
46 { ob->ob_type =
type; }
47 #define Py_SET_TYPE(ob, type) _Py_SET_TYPE((PyObject*)(ob), type)
50 #if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_SIZE)
52 { ob->ob_size =
size; }
53 #define Py_SET_SIZE(ob, size) _Py_SET_SIZE((PyVarObject*)(ob), size)
56 #if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_REFCNT)
58 { ob->ob_refcnt = refcnt; }
59 #define Py_SET_REFCNT(ob, refcnt) _Py_SET_REFCNT((PyObject*)(ob), refcnt)
67 #if PY_MAJOR_VERSION >= 3
69 PyObject* Py_FindMethod(PyMethodDef
table[], PyObject *ob,
char *name)
73 return PyCFunction_New(
table +
i, ob);
76 CAttributeError
error(name);
83 typedef PyObject* (*TMethodVarArgsHandler)( PyObject*
self, PyObject* args );
84 typedef PyObject* (*TMethodKeywordHandler)( PyObject*
self, PyObject* args, PyObject* dict );
101 ml_name =
const_cast<char*
>( name );
104 ml_doc =
const_cast<char*
>( doc );
107 int flags = METH_VARARGS | METH_KEYWORDS,
108 const char* doc =
nullptr)
110 ml_name =
const_cast<char*
>(name);
111 ml_meth =
reinterpret_cast<PyCFunction
>(func);
113 ml_doc =
const_cast<char*
>(doc);
117 ml_name = other.ml_name;
118 ml_meth = other.ml_meth;
119 ml_flags = other.ml_flags;
120 ml_doc = other.ml_doc;
125 if (
this != &other) {
126 ml_name = other.ml_name;
127 ml_meth = other.ml_meth;
128 ml_flags = other.ml_flags;
129 ml_doc = other.ml_doc;
147 class CExtObjectBase;
156 PyTypeObject* base = &PyBaseObject_Type)
161 tp_basicsize = basic_size;
164 tp_flags = Py_TPFLAGS_DEFAULT;
188 tp_name =
const_cast<char*
>( name );
192 tp_doc =
const_cast<char*
>( descr );
219 #if PY_MAJOR_VERSION < 3 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 8)
222 tp_vectorcall_offset = 0;
228 #if PY_MAJOR_VERSION < 3
230 #elif PY_MINOR_VERSION >= 5
239 tp_as_sequence =
NULL;
240 tp_as_mapping =
NULL;
269 tp_weaklistoffset = 0L;
294 #if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 4
298 tp_subclasses =
NULL;
305 # if PY_MAJOR_VERSION >= 3
321 return l.GetObjType() ==
r.GetObjType();
327 return l.GetObjType() ==
r.GetObjType();
352 virtual PyObject*
Get(
void)
const = 0;
383 virtual PyObject*
Get(
void)
const
385 return PyLong_FromLong( *
m_Value );
391 long tmp_value = PyLong_AsLong(
value );
413 virtual PyObject*
Get(
void)
const
415 #if PY_MAJOR_VERSION >= 3
416 return PyUnicode_FromStringAndSize(
m_Value->data(),
m_Value->size());
418 return PyString_FromStringAndSize(
m_Value->data(),
m_Value->size() );
425 #if PY_VERSION_HEX >= 0x03030000
427 auto utf = PyUnicode_AsUTF8AndSize(
Get(), &
size);
429 #elif PY_MAJOR_VERSION >= 3
432 static_cast<size_t>(PyUnicode_GetSize(
Get())));
434 string tmp_value =
string( PyString_AsString(
value ),
static_cast<size_t>( PyString_Size(
value ) ) );
464 virtual PyObject*
Get(
void)
const
488 void resize(vector<SMethodDef>& container)
490 if (container.size() <
N) {
520 const CDict& kwargs);
548 template<
size_t N = 0>
564 resize<N>(hndl_list);
570 return CClass<N + 1>();
574 const char* doc =
nullptr)
578 resize<N>(hndl_list);
581 METH_VARARGS | METH_KEYWORDS, doc);
584 return CClass<N + 1>();
590 T* obj =
static_cast<T*
>(
self);
593 const CTuple args_tuple( args );
619 T* obj =
static_cast<T*
>(
self);
622 const CTuple args_tuple(args);
624 if (kwargs !=
nullptr) {
625 kwargs_dict = kwargs;
628 return IncRefCount((obj->*func)(args_tuple, kwargs_dict));
634 (
"Unknown error during execution of a method");
644 #ifndef NCBI_COMPILER_ICC
652 const char* descr = 0,
653 PyTypeObject* base = &PyBaseObject_Type
663 type.SetDescription(descr);
673 const char* descr = 0,
674 PyTypeObject* base = &PyBaseObject_Type
679 Declare(safe_name->c_str(), descr, base);
684 return CClass<0>().Def(name, func, doc);
687 const char* doc =
nullptr)
689 return CClass<0>().Def(name, func, doc);
712 delete static_cast<T*
>( obj );
719 PyObject_Init(
self,
GetType().GetObjType() );
768 return citer->second->Get();
799 const_cast<char*
>(name),
801 const_cast<char*
>(descr),
831 template<
size_t N = 0>
855 T* obj =
static_cast<T*
>(
self);
858 const CTuple args_tuple( args );
894 delete static_cast<T*
>( obj );
938 static void Declare(
const string& name, PyMethodDef* methods,
939 inquiry cleanup_hook =
NULL);
951 static void AddConst(
const string& name,
const string&
value );
957 #if PY_MAJOR_VERSION >= 3
958 static struct PyModuleDef m_ModuleDef;
965 #if PY_MAJOR_VERSION >= 3
966 struct PyModuleDef
CModuleExt::m_ModuleDef = {
967 PyModuleDef_HEAD_INIT,
981 inquiry cleanup_hook)
986 #if PY_MAJOR_VERSION >= 3
987 m_ModuleDef.m_name =
strdup(name.c_str());
988 m_ModuleDef.m_methods = methods;
989 m_ModuleDef.m_clear = cleanup_hook;
990 m_Module = PyModule_Create(&m_ModuleDef);
992 m_Module = Py_InitModule(
const_cast<char*
>( name.c_str() ), methods );
1001 if ( PyModule_AddObject(
m_Module,
const_cast<char*
>(name.c_str()),
value ) == -1 ) {
1002 throw CSystemError(
"Failed to add a constant value to a module");
1009 #if PY_MAJOR_VERSION >= 3
1011 = PyUnicode_FromStringAndSize(
value.data(),
value.size());
1014 = PyString_FromStringAndSize(
value.data(),
value.size() );
1023 PyObject* py_value = PyLong_FromLong(
value );
1032 template <
class T,
class B = CStandardError>
1056 m_Exception = PyErr_NewException(
const_cast<char*
>(full_name.c_str()), B::GetPyException(),
NULL );
1059 throw CSystemError(
"Unable to add an object to a module" );
1067 m_Exception = PyErr_NewException(
const_cast<char*
>(full_name.c_str()), B::GetPyException(), dict);
1070 throw CSystemError(
"Unable to add an object to a module" );
1106 #ifdef PYTHONPP_DEFINE_GLOBALS
container_type::const_iterator const_iterator
const_iterator end() const
const_iterator find(const key_type &key) const
static void SetString(const string &msg)
static PyObject * HandleMethodVarArgs(PyObject *self, PyObject *args)
CClass< N+1 > Def(const char *name, TMethodVarArgsFunc func, const char *doc=0)
static TMethodHndlList sm_MethodHndlList
CExtModule(const char *name, const char *descr=0)
static TMethodList sm_MethodList
CObject(T::* TMethodVarArgsFunc)(const CTuple &args)
vector< SMethodDef > TMethodHndlList
static PyObject * GetAttrImpl(PyObject *self, char *name)
static void deallocator(PyObject *obj)
static CClass< 1 > Def(const char *name, TMethodVarArgsFunc func, const char *doc=0)
vector< SFunct > TMethodList
static TMethodList & GetMethodList(void)
static TMethodHndlList & GetMethodHndlList(void)
static CExtType & GetType(void)
CClass< N+1 > Def(const char *name, TMethodVarArgsFunc func, const char *doc=0)
static PyObject * HandleMethodKWArgs(PyObject *self, PyObject *args, PyObject *kwargs)
CClass< N+1 > Def(const char *name, TMethodKWArgsFunc func, const char *doc=nullptr)
static PyObject * HandleMethodVarArgs(PyObject *self, PyObject *args)
static CClass< 1 > Def(const char *name, TMethodKWArgsFunc func, const char *doc=nullptr)
vector< SFunct > TMethodList
static TMethodHndlList sm_MethodHndlList
static void Declare(const string &name, const char *descr=0, PyTypeObject *base=&PyBaseObject_Type)
void ROAttr(const string &name, CObject &value)
static TMethodHndlList & GetMethodHndlList(void)
void RWAttr(const string &name, long &value)
CObject(T::* TMethodKWArgsFunc)(const CTuple &args, const CDict &kwargs)
void RWAttr(const string &name, string &value)
map< string, AutoPtr< bind::CBase > > TAttrList
vector< SMethodDef > TMethodHndlList
static PyTypeObject * sm_Base
static PyObject * GetAttrImpl(PyObject *self, char *name)
static void Declare(const char *name, const char *descr=0, PyTypeObject *base=&PyBaseObject_Type)
static TMethodList sm_MethodList
void ROAttr(const string &name, long &value)
static TMethodList & GetMethodList(void)
static CExtType & GetType(void)
static void PrepareForPython(CExtObject< T > *self)
static CClass< 1 > Def(const char *name, TMethodVarArgsFunc func, const char *doc=0)
static void deallocator(PyObject *obj)
static CObject & GetTypeObject(void)
CObject(T::* TMethodVarArgsFunc)(const CTuple &args)
void ROAttr(const string &name, string &value)
void SetDescription(const char *descr)
void SetName(const char *name)
void SupportGetAttr(getattrfunc func)
CExtType(size_t basic_size, destructor dr=standard_dealloc, PyTypeObject *base=&PyBaseObject_Type)
PyTypeObject * GetObjType(void)
const PyTypeObject * GetObjType(void) const
static const string & GetName(void)
static void AddConst(const string &name, const string &value)
static void AddConstValue(const string &name, PyObject *value)
static PyObject * m_Module
static void Declare(const string &name, PyMethodDef *methods, inquiry cleanup_hook=NULL)
static PyObject * GetPyModule(void)
PyObject * Get(void) const
CThreadingGuard – "Anti-guard" for Python's global interpreter lock, which it temporarily releases to...
static void SetMayRelease(bool may_release)
static bool sm_MayRelease
static void Declare(const string &name)
static void Declare(const string &name, const pythonpp::CDict &dict)
static PyObject * m_Exception
static PyObject * GetPyException(void)
CUserError(const string &msg, PyObject *err_type)
CUserError(const string &msg)
bool IsReadOnly(void) const
CBase(EBindType type=eReadOnly)
virtual PyObject * Get(void) const =0
virtual void SetInternal(PyObject *value) const
void Set(PyObject *value) const
CLong(long &value, EBindType type=eReadOnly)
virtual PyObject * Get(void) const
virtual void SetInternal(PyObject *value) const
virtual PyObject * Get(void) const
CObject(T &obj, TGetFunc get, TSetFunc set=NULL)
void(T::* TSetFunc)(PyObject *value)
PyObject *(T::* TGetFunc)(void) const
virtual void SetInternal(PyObject *value) const
virtual void SetInternal(PyObject *value) const
CString(string &value, EBindType type=eReadOnly)
virtual PyObject * Get(void) const
#define END_NCBI_SCOPE
End previously defined NCBI scope.
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
static CStringUTF8 AsUTF8(const CTempString &src, EEncoding encoding, EValidate validate=eNoValidate)
Convert into UTF8 from a C/C++ string.
<!DOCTYPE HTML >< html > n< header > n< title > PubSeq Gateway Help Page</title > n< style > n table
constexpr auto front(list< Head, As... >, T=T()) noexcept -> Head
const struct ncbi::grid::netcache::search::fields::SIZE size
void resize(vector< SMethodDef > &container)
PyObject * IncRefCount(PyObject *obj)
bool operator==(const CObject &l, const CExtType &r)
void standard_dealloc(PyObject *obj)
void DoNotDeallocate(void *)
void resize< 0 >(vector< SMethodDef > &)
const GenericPointer< typename T::ValueType > T2 value
int strcmp(const char *str1, const char *str2)
Static variables safety - create on demand, destroy on application termination.
double r(size_t dimension_, const Int4 *score_, const double *prob_, double theta_)
#define Py_SET_TYPE(ob, type)
static void _Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt)
static void _Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size)
#define Py_SET_REFCNT(ob, refcnt)
#define Py_SET_SIZE(ob, size)
static void _Py_SET_TYPE(PyObject *ob, PyTypeObject *type)
static SLJIT_INLINE sljit_ins l(sljit_gpr r, sljit_s32 d, sljit_gpr x, sljit_gpr b)
static SLJIT_INLINE sljit_ins msg(sljit_gpr r, sljit_s32 d, sljit_gpr x, sljit_gpr b)
SFunct(const TMethodVarArgsFunc &funct)
TMethodVarArgsFunc m_Funct
SFunct(const TMethodVarArgsFunc &funct)
TMethodVarArgsFunc m_Funct
SFunct(const TMethodKWArgsFunc &funct)
Introduces constructor methods for a python PyMethodDef structure ...
SMethodDef & operator=(const SMethodDef &other)
SMethodDef(const char *name, PyCFunctionWithKeywords func, int flags=METH_VARARGS|METH_KEYWORDS, const char *doc=nullptr)
SMethodDef(const char *name, PyCFunction func, int flags=1, const char *doc=NULL)
SMethodDef(const SMethodDef &other)