Commit 46130584 authored by Guilhem Saurel's avatar Guilhem Saurel

[Python 3] PyCObject → PyCapsule

parent ccedef6b
......@@ -85,22 +85,22 @@ PyObject* plug(PyObject* /*self*/, PyObject* args) {
if (!PyArg_ParseTuple(args, "OO", &objOut, &objIn)) return NULL;
if (!PyCObject_Check(objOut)) {
if (!PyCapsule_CheckExact(objOut)) {
PyErr_SetString(PyExc_TypeError,
"first argument should be a pointer to"
" signalBase<int>.");
return NULL;
}
if (!PyCObject_Check(objIn)) {
if (!PyCapsule_CheckExact(objIn)) {
PyErr_SetString(PyExc_TypeError,
"second argument should be a pointer to"
" signalBase<int>.");
return NULL;
}
pObjIn = PyCObject_AsVoidPtr(objIn);
pObjIn = PyCapsule_GetPointer(objIn, "dynamic_graph.Signal");
SignalBase<int>* signalIn = (SignalBase<int>*)pObjIn;
pObjOut = PyCObject_AsVoidPtr(objOut);
pObjOut = PyCapsule_GetPointer(objOut, "dynamic_graph.Signal");
SignalBase<int>* signalOut = (SignalBase<int>*)pObjOut;
std::ostringstream os;
......
......@@ -63,8 +63,8 @@ PyObject* create(PyObject* /*self*/, PyObject* args) {
CATCH_ALL_EXCEPTIONS();
}
// Return the pointer as a PyCObject
return PyCObject_FromVoidPtr((void*)obj, NULL);
// Return the pointer as a PyCapsule
return PyCapsule_New((void*)obj, "dynamic_graph.Entity", NULL);
}
/**
......@@ -76,12 +76,12 @@ PyObject* getName(PyObject* /*self*/, PyObject* args) {
std::string name;
if (!PyArg_ParseTuple(args, "O", &object)) return NULL;
if (!PyCObject_Check(object)) {
PyErr_SetString(PyExc_TypeError, "function takes a PyCObject as argument");
if (!PyCapsule_CheckExact(object)) {
PyErr_SetString(PyExc_TypeError, "function takes a PyCapsule as argument");
return NULL;
}
pointer = PyCObject_AsVoidPtr(object);
pointer = PyCapsule_GetPointer(object, "dynamic_graph.Entity");
Entity* entity = (Entity*)pointer;
try {
......@@ -100,12 +100,12 @@ PyObject* getClassName(PyObject* /*self*/, PyObject* args) {
std::string name;
if (!PyArg_ParseTuple(args, "O", &object)) return NULL;
if (!PyCObject_Check(object)) {
PyErr_SetString(PyExc_TypeError, "function takes a PyCObject as argument");
if (!PyCapsule_CheckExact(object)) {
PyErr_SetString(PyExc_TypeError, "function takes a PyCapsule as argument");
return NULL;
}
pointer = PyCObject_AsVoidPtr(object);
pointer = PyCapsule_GetPointer(object, "dynamic_graph.Entity");
Entity* entity = (Entity*)pointer;
try {
......@@ -125,12 +125,12 @@ PyObject* hasSignal(PyObject* /*self*/, PyObject* args) {
if (!PyArg_ParseTuple(args, "Os", &object, &name)) Py_RETURN_FALSE;
if (!PyCObject_Check(object)) {
PyErr_SetString(PyExc_TypeError, "function takes a PyCObject as argument");
if (!PyCapsule_CheckExact(object)) {
PyErr_SetString(PyExc_TypeError, "function takes a PyCapsule as argument");
Py_RETURN_FALSE;
}
pointer = PyCObject_AsVoidPtr(object);
pointer = PyCapsule_GetPointer(object, "dynamic_graph.Entity");
Entity* entity = (Entity*)pointer;
bool hasSignal = false;
......@@ -155,12 +155,12 @@ PyObject* getSignal(PyObject* /*self*/, PyObject* args) {
if (!PyArg_ParseTuple(args, "Os", &object, &name)) return NULL;
if (!PyCObject_Check(object)) {
PyErr_SetString(PyExc_TypeError, "function takes a PyCObject as argument");
if (!PyCapsule_CheckExact(object)) {
PyErr_SetString(PyExc_TypeError, "function takes a PyCapsule as argument");
return NULL;
}
pointer = PyCObject_AsVoidPtr(object);
pointer = PyCapsule_GetPointer(object, "dynamic_graph.Entity");
Entity* entity = (Entity*)pointer;
SignalBase<int>* signal = NULL;
......@@ -171,7 +171,7 @@ PyObject* getSignal(PyObject* /*self*/, PyObject* args) {
// Return the pointer to the signal without destructor since the signal
// is not owned by the calling object but by the Entity.
return PyCObject_FromVoidPtr((void*)signal, NULL);
return PyCapsule_New((void*)signal, "dynamic_graph.Signal", NULL);
}
PyObject* listSignals(PyObject* /*self*/, PyObject* args) {
......@@ -180,9 +180,9 @@ PyObject* listSignals(PyObject* /*self*/, PyObject* args) {
if (!PyArg_ParseTuple(args, "O", &object)) return NULL;
if (!PyCObject_Check(object)) return NULL;
if (!PyCapsule_CheckExact(object)) return NULL;
pointer = PyCObject_AsVoidPtr(object);
pointer = PyCapsule_GetPointer(object, "dynamic_graph.Entity");
Entity* entity = (Entity*)pointer;
try {
......@@ -193,7 +193,7 @@ PyObject* listSignals(PyObject* /*self*/, PyObject* args) {
for (Entity::SignalMap::iterator it = signalMap.begin(); it != signalMap.end(); it++) {
SignalBase<int>* signal = it->second;
PyObject* pySignal = PyCObject_FromVoidPtr((void*)signal, NULL);
PyObject* pySignal = PyCapsule_New((void*)signal, "dynamic_graph.Signal", NULL);
PyTuple_SET_ITEM(result, count, pySignal);
count++;
}
......@@ -212,11 +212,11 @@ PyObject* executeCommand(PyObject* /*self*/, PyObject* args) {
return NULL;
}
// Retrieve the entity instance
if (!PyCObject_Check(object)) {
if (!PyCapsule_CheckExact(object)) {
PyErr_SetString(PyExc_TypeError, "first argument is not an object");
return NULL;
}
pointer = PyCObject_AsVoidPtr(object);
pointer = PyCapsule_GetPointer(object, "dynamic_graph.Entity");
Entity* entity = (Entity*)pointer;
// Retrieve the argument tuple
if (!PyTuple_Check(argTuple)) {
......@@ -273,11 +273,11 @@ PyObject* listCommands(PyObject* /*self*/, PyObject* args) {
}
// Retrieve the entity instance
if (!PyCObject_Check(object)) {
PyErr_SetString(PyExc_TypeError, "function takes a PyCObject as argument");
if (!PyCapsule_CheckExact(object)) {
PyErr_SetString(PyExc_TypeError, "function takes a PyCapsule as argument");
return NULL;
}
void* pointer = PyCObject_AsVoidPtr(object);
void* pointer = PyCapsule_GetPointer(object, "dynamic_graph.Entity");
Entity* entity = (Entity*)pointer;
typedef std::map<const std::string, command::Command*> CommandMap;
CommandMap map = entity->getNewStyleCommandMap();
......@@ -301,11 +301,11 @@ PyObject* getCommandDocstring(PyObject* /*self*/, PyObject* args) {
}
// Retrieve the entity instance
if (!PyCObject_Check(object)) {
if (!PyCapsule_CheckExact(object)) {
PyErr_SetString(dgpyError, "first argument is not an object");
return NULL;
}
void* pointer = PyCObject_AsVoidPtr(object);
void* pointer = PyCapsule_GetPointer(object, "dynamic_graph.Entity");
Entity* entity = (Entity*)pointer;
typedef std::map<const std::string, command::Command*> commandMap_t;
typedef std::map<const std::string, command::Command*>::iterator iterator_t;
......@@ -330,11 +330,11 @@ PyObject* getDocString(PyObject* /*self*/, PyObject* args) {
}
// Retrieve the entity instance
if (!PyCObject_Check(object)) {
if (!PyCapsule_CheckExact(object)) {
PyErr_SetString(dgpyError, "first argument is not an object");
return NULL;
}
void* pointer = PyCObject_AsVoidPtr(object);
void* pointer = PyCapsule_GetPointer(object, "dynamic_graph.Entity");
Entity* entity = (Entity*)pointer;
try {
return Py_BuildValue("s", entity->getDocString().c_str());
......@@ -351,11 +351,11 @@ PyObject* getDocString(PyObject* /*self*/, PyObject* args) {
PyObject* display(PyObject* /*self*/, PyObject* args) {
/* Retrieve the entity instance. */
PyObject* object = NULL;
if (!PyArg_ParseTuple(args, "O", &object) || (!PyCObject_Check(object))) {
if (!PyArg_ParseTuple(args, "O", &object) || (!PyCapsule_CheckExact(object))) {
PyErr_SetString(dgpyError, "first argument is not an object");
return NULL;
}
void* pointer = PyCObject_AsVoidPtr(object);
void* pointer = PyCapsule_GetPointer(object, "dynamic_graph.Entity");
Entity* entity = (Entity*)pointer;
/* Run the display function. */
......@@ -375,12 +375,12 @@ PyObject* setLoggerVerbosityLevel(PyObject* /*self*/, PyObject* args) {
if (!PyArg_ParseTuple(args, "OO", &object, &objectVerbosityLevel)) return NULL;
// Retrieve the entity instance
if (!PyCObject_Check(object)) {
if (!PyCapsule_CheckExact(object)) {
PyErr_SetString(PyExc_TypeError, "First argument should be an object");
return NULL;
}
void* pointer = PyCObject_AsVoidPtr(object);
void* pointer = PyCapsule_GetPointer(object, "dynamic_graph.Entity");
Entity* entity = (Entity*)pointer;
// Retrieve object verbosity level
......@@ -430,12 +430,12 @@ PyObject* getLoggerVerbosityLevel(PyObject* /*self*/, PyObject* args) {
if (!PyArg_ParseTuple(args, "O", &object)) return NULL;
// Retrieve the entity instance
if (!PyCObject_Check(object)) {
if (!PyCapsule_CheckExact(object)) {
PyErr_SetString(PyExc_TypeError, "first argument is not an object");
return NULL;
}
void* pointer = PyCObject_AsVoidPtr(object);
void* pointer = PyCapsule_GetPointer(object, "dynamic_graph.Entity");
Entity* entity = (Entity*)pointer;
LoggerVerbosity alv;
......@@ -456,12 +456,12 @@ PyObject* getStreamPrintPeriod(PyObject* /*self*/, PyObject* args) {
if (!PyArg_ParseTuple(args, "O", &object)) return NULL;
// Retrieve the entity instance
if (!PyCObject_Check(object)) {
if (!PyCapsule_CheckExact(object)) {
PyErr_SetString(PyExc_TypeError, "first argument is not an object");
return NULL;
}
void* pointer = PyCObject_AsVoidPtr(object);
void* pointer = PyCapsule_GetPointer(object, "dynamic_graph.Entity");
Entity* entity = (Entity*)pointer;
double r;
......@@ -482,12 +482,12 @@ PyObject* setStreamPrintPeriod(PyObject* /*self*/, PyObject* args) {
if (!PyArg_ParseTuple(args, "Od", &object, &streamPrintPeriod)) return NULL;
// Retrieve the entity instance
if (!PyCObject_Check(object)) {
if (!PyCapsule_CheckExact(object)) {
PyErr_SetString(PyExc_TypeError, "First argument should be an object");
return NULL;
}
void* pointer = PyCObject_AsVoidPtr(object);
void* pointer = PyCapsule_GetPointer(object, "dynamic_graph.Entity");
Entity* entity = (Entity*)pointer;
try {
......@@ -515,12 +515,12 @@ PyObject* getTimeSample(PyObject* /*self*/, PyObject* args) {
if (!PyArg_ParseTuple(args, "O", &object)) return NULL;
// Retrieve the entity instance
if (!PyCObject_Check(object)) {
if (!PyCapsule_CheckExact(object)) {
PyErr_SetString(PyExc_TypeError, "first argument is not an object");
return NULL;
}
void* pointer = PyCObject_AsVoidPtr(object);
void* pointer = PyCapsule_GetPointer(object, "dynamic_graph.Entity");
Entity* entity = (Entity*)pointer;
double r;
......@@ -541,12 +541,12 @@ PyObject* setTimeSample(PyObject* /*self*/, PyObject* args) {
if (!PyArg_ParseTuple(args, "Od", &object, &timeSample)) return NULL;
// Retrieve the entity instance
if (!PyCObject_Check(object)) {
if (!PyCapsule_CheckExact(object)) {
PyErr_SetString(PyExc_TypeError, "First argument should be an object");
return NULL;
}
void* pointer = PyCObject_AsVoidPtr(object);
void* pointer = PyCapsule_GetPointer(object, "dynamic_graph.Entity");
Entity* entity = (Entity*)pointer;
try {
......
......@@ -26,7 +26,7 @@ using namespace convert;
namespace signalBase {
static void destroy(void* self);
static void destroy(PyObject* self);
/**
\brief Create an instance of SignalBase
......@@ -40,7 +40,7 @@ PyObject* create(PyObject* /*self*/, PyObject* args) {
obj = new SignalBase<int>(std::string(name));
// Return the pointer
return PyCObject_FromVoidPtr((void*)obj, destroy);
return PyCapsule_New((void*)obj, "dynamic_graph.Signal", destroy);
}
template <class T>
......@@ -117,13 +117,13 @@ PyObject* createSignalWrapper(PyObject* /*self*/, PyObject* args) {
psc->signalRegistration(*obj);
// Return the pointer
return PyCObject_FromVoidPtr((void*)obj, destroy);
return PyCapsule_New((void*)obj, "dynamic_graph.SignalWrapper", destroy);
}
/**
\brief Destroy an instance of InvertedPendulum
*/
static void destroy(void* self) {
static void destroy(PyObject* self) {
SignalBase<int>* obj = (SignalBase<int>*)self;
delete obj;
}
......@@ -132,9 +132,9 @@ PyObject* getTime(PyObject* /*self*/, PyObject* args) {
void* pointer = NULL;
PyObject* object = NULL;
if (!PyArg_ParseTuple(args, "O", &object)) return NULL;
if (!PyCObject_Check(object)) return NULL;
if (!PyCapsule_CheckExact(object)) return NULL;
pointer = PyCObject_AsVoidPtr(object);
pointer = PyCapsule_GetPointer(object, "dynamic_graph.Signal");
SignalBase<int>* obj = (SignalBase<int>*)pointer;
int time = obj->getTime();
......@@ -146,12 +146,12 @@ PyObject* setTime(PyObject* /*self*/, PyObject* args) {
PyObject* object = NULL;
int time;
if (!PyArg_ParseTuple(args, "Oi", &object, &time)) return NULL;
if (!PyCObject_Check(object)) {
if (!PyCapsule_CheckExact(object)) {
PyErr_SetString(dgpyError, "object should be a C object");
return NULL;
}
pointer = PyCObject_AsVoidPtr(object);
pointer = PyCapsule_GetPointer(object, "dynamic_graph.Signal");
SignalBase<int>* obj = (SignalBase<int>*)pointer;
obj->setTime(time);
......@@ -162,9 +162,9 @@ PyObject* display(PyObject* /*self*/, PyObject* args) {
void* pointer = NULL;
PyObject* object = NULL;
if (!PyArg_ParseTuple(args, "O", &object)) return NULL;
if (!PyCObject_Check(object)) return NULL;
if (!PyCapsule_CheckExact(object)) return NULL;
pointer = PyCObject_AsVoidPtr(object);
pointer = PyCapsule_GetPointer(object, "dynamic_graph.Signal");
SignalBase<int>* obj = (SignalBase<int>*)pointer;
std::ostringstream oss;
......@@ -181,9 +181,9 @@ PyObject* displayDependencies(PyObject* /*self*/, PyObject* args) {
PyObject* object = NULL;
int time;
if (!PyArg_ParseTuple(args, "OI", &object, &time)) return NULL;
if (!PyCObject_Check(object)) return NULL;
if (!PyCapsule_CheckExact(object)) return NULL;
pointer = PyCObject_AsVoidPtr(object);
pointer = PyCapsule_GetPointer(object, "dynamic_graph.Signal");
SignalBase<int>* obj = (SignalBase<int>*)pointer;
std::ostringstream oss;
......@@ -198,9 +198,9 @@ PyObject* getValue(PyObject* /*self*/, PyObject* args) {
void* pointer = NULL;
PyObject* object = NULL;
if (!PyArg_ParseTuple(args, "O", &object)) return NULL;
if (!PyCObject_Check(object)) return NULL;
if (!PyCapsule_CheckExact(object)) return NULL;
pointer = PyCObject_AsVoidPtr(object);
pointer = PyCapsule_GetPointer(object, "dynamic_graph.Signal");
SignalBase<int>* signal = (SignalBase<int>*)pointer;
try {
......@@ -292,9 +292,9 @@ PyObject* getName(PyObject* /*self*/, PyObject* args) {
void* pointer = NULL;
PyObject* object = NULL;
if (!PyArg_ParseTuple(args, "O", &object)) return NULL;
if (!PyCObject_Check(object)) return NULL;
if (!PyCapsule_CheckExact(object)) return NULL;
pointer = PyCObject_AsVoidPtr(object);
pointer = PyCapsule_GetPointer(object, "dynamic_graph.Signal");
SignalBase<int>* signal = (SignalBase<int>*)pointer;
std::string name;
......@@ -310,9 +310,9 @@ PyObject* getClassName(PyObject* /*self*/, PyObject* args) {
void* pointer = NULL;
PyObject* object = NULL;
if (!PyArg_ParseTuple(args, "O", &object)) return NULL;
if (!PyCObject_Check(object)) return NULL;
if (!PyCapsule_CheckExact(object)) return NULL;
pointer = PyCObject_AsVoidPtr(object);
pointer = PyCapsule_GetPointer(object, "dynamic_graph.Signal");
SignalBase<int>* signal = (SignalBase<int>*)pointer;
std::string name;
......@@ -330,9 +330,9 @@ PyObject* setValue(PyObject* /*self*/, PyObject* args) {
char* valueString = NULL;
if (!PyArg_ParseTuple(args, "Os", &object, &valueString)) return NULL;
if (!PyCObject_Check(object)) return NULL;
if (!PyCapsule_CheckExact(object)) return NULL;
pointer = PyCObject_AsVoidPtr(object);
pointer = PyCapsule_GetPointer(object, "dynamic_graph.Signal");
SignalBase<int>* signal = (SignalBase<int>*)pointer;
std::ostringstream os;
os << valueString;
......@@ -350,9 +350,9 @@ PyObject* recompute(PyObject* /*self*/, PyObject* args) {
PyObject* object = NULL;
unsigned int time;
if (!PyArg_ParseTuple(args, "OI", &object, &time)) return NULL;
if (!PyCObject_Check(object)) return NULL;
if (!PyCapsule_CheckExact(object)) return NULL;
pointer = PyCObject_AsVoidPtr(object);
pointer = PyCapsule_GetPointer(object, "dynamic_graph.Signal");
SignalBase<int>* signal = (SignalBase<int>*)pointer;
try {
signal->recompute(time);
......@@ -365,9 +365,9 @@ PyObject* unplug(PyObject* /*self*/, PyObject* args) {
void* pointer = NULL;
PyObject* object = NULL;
if (!PyArg_ParseTuple(args, "O", &object)) return NULL;
if (!PyCObject_Check(object)) return NULL;
if (!PyCapsule_CheckExact(object)) return NULL;
pointer = PyCObject_AsVoidPtr(object);
pointer = PyCapsule_GetPointer(object, "dynamic_graph.Signal");
SignalBase<int>* signal = (SignalBase<int>*)pointer;
try {
signal->unplug();
......@@ -380,9 +380,9 @@ PyObject* isPlugged(PyObject*, PyObject* args) {
void* pointer = NULL;
PyObject* object = NULL;
if (!PyArg_ParseTuple(args, "O", &object)) return NULL;
if (!PyCObject_Check(object)) return NULL;
if (!PyCapsule_CheckExact(object)) return NULL;
pointer = PyCObject_AsVoidPtr(object);
pointer = PyCapsule_GetPointer(object, "dynamic_graph.Signal");
SignalBase<int>* signal = (SignalBase<int>*)pointer;
bool plugged = false;
try {
......@@ -399,9 +399,9 @@ PyObject* getPlugged(PyObject*, PyObject* args) {
void* pointer = NULL;
PyObject* object = NULL;
if (!PyArg_ParseTuple(args, "O", &object)) return NULL;
if (!PyCObject_Check(object)) return NULL;
if (!PyCapsule_CheckExact(object)) return NULL;
pointer = PyCObject_AsVoidPtr(object);
pointer = PyCapsule_GetPointer(object, "dynamic_graph.Signal");
SignalBase<int>* signal = (SignalBase<int>*)pointer;
SignalBase<int>* otherSignal = 0;
try {
......@@ -415,7 +415,7 @@ PyObject* getPlugged(PyObject*, PyObject* args) {
CATCH_ALL_EXCEPTIONS();
// Return the pointer to the signal without destructor since the signal
// is not owned by the calling object.
return PyCObject_FromVoidPtr((void*)otherSignal, NULL);
return PyCapsule_New((void*)otherSignal, "dynamic_graph.Signal", NULL);
}
} // namespace signalBase
} // namespace python
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment