libTriton version 1.0 build 1592
Loading...
Searching...
No Matches
pyXFunctions.cpp
Go to the documentation of this file.
1
2/*
3** Copyright (C) - Triton
4**
5** This program is under the terms of the Apache License 2.0.
6*/
7
9#include <iostream>
10
11namespace triton {
12 namespace bindings {
13 namespace python {
14
15 static inline void notEnoughMemory(void) {
16 std::cerr << "[ERROR] Not enough memory for allocation" << std::endl;
17 exit(-1);
18 }
19
20
21 PyObject* xPyDict_New(void) {
22 PyObject* dict = PyDict_New();
23 if (!dict)
24 notEnoughMemory();
25 return dict;
26 }
27
28
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
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
45 PyObject* xPyString_FromString(const char *v) {
46 PyObject* s = PyStr_FromString(v);
47 if (!s)
48 notEnoughMemory();
49 return s;
50 }
51
52
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
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
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 */
PyObject * xPyClass_New(PyObject *b, PyObject *d, PyObject *n)
Creates a PyClass and raises an exception if it fails. dict is copied in Py3 ! All references are dec...
PyObject * xPyTuple_New(Py_ssize_t len)
Creates a PyTuple and raises an exception if it fails.
PyObject * xPyString_FromString(const char *v)
Creates a PyString and raises an exception if it fails.
PyObject * xPyList_New(Py_ssize_t len)
Creates a PyList and raises an exception if it fails.
int xPyDict_SetItem(PyObject *p, PyObject *key, PyObject *val)
Same as PyDict_SetItem but decrements reference on object and key.
PyObject * xPyDict_New(void)
Creates a PyDict and raises an exception if it fails.
int xPyDict_SetItemString(PyObject *p, const char *key, PyObject *val)
Same as PyDict_SetItemString but decrements reference on object.
The Triton namespace.