1 //! \file
2 /*
3 **  Copyright (C) - Triton
4 **
5 **  This program is under the terms of the Apache License 2.0.
6 */
7 
8 #include <triton/pythonXFunctions.hpp>
9 #include <iostream>
10 
11 namespace triton {
12   namespace bindings {
13     namespace python {
14 
notEnoughMemory(void)15       static inline void notEnoughMemory(void) {
16         std::cerr << "[ERROR] Not enough memory for allocation" << std::endl;
17         exit(-1);
18       }
19 
20 
xPyDict_New(void)21       PyObject* xPyDict_New(void) {
22         PyObject* dict = PyDict_New();
23         if (!dict)
24           notEnoughMemory();
25         return dict;
26       }
27 
28 
xPyList_New(Py_ssize_t len)29       PyObject* xPyList_New(Py_ssize_t len) {
30         PyObject* list = PyList_New(len);
31         if (!list)
32           notEnoughMemory();
33         return list;
34       }
35 
36 
xPyTuple_New(Py_ssize_t len)37       PyObject* xPyTuple_New(Py_ssize_t len) {
38         PyObject* tuple = PyTuple_New(len);
39         if (!tuple)
40           notEnoughMemory();
41         return tuple;
42       }
43 
44 
xPyString_FromString(const char * v)45       PyObject* xPyString_FromString(const char *v) {
46         PyObject* s = PyStr_FromString(v);
47         if (!s)
48           notEnoughMemory();
49         return s;
50       }
51 
52 
xPyClass_New(PyObject * b,PyObject * d,PyObject * n)53       PyObject* xPyClass_New(PyObject* b, PyObject* d, PyObject* n) {
54         PyObject* c = nullptr;
55 
56         if (b == NULL)
57           b = PyTuple_New(0);
58 
59         c = PyObject_CallFunctionObjArgs((PyObject*)&PyType_Type, n, b, d, NULL);
60         if (!c)
61           notEnoughMemory();
62 
63         Py_CLEAR(b);
64         Py_CLEAR(d);
65         Py_CLEAR(n);
66 
67         return c;
68       }
69 
70 
71 
xPyDict_SetItemString(PyObject * p,const char * key,PyObject * val)72       int xPyDict_SetItemString(PyObject* p, const char* key, PyObject* val) {
73         int r = PyDict_SetItemString(p, key, val);
74         Py_DECREF(val);
75         return r;
76       }
77 
78 
xPyDict_SetItem(PyObject * p,PyObject * key,PyObject * val)79       int xPyDict_SetItem(PyObject* p, PyObject* key, PyObject* val) {
80         int r = PyDict_SetItem(p, key, val);
81         Py_DECREF(val);
82         Py_DECREF(key);
83         return r;
84       }
85 
86     }; /* python namespace */
87   }; /* bindings namespace */
88 }; /* triton namespace */
89