Skip to content

Commit

Permalink
Python: PY_SSIZE_T_CLEAN add before including Python.h (#642)
Browse files Browse the repository at this point in the history
* PY_SSIZE_T_CLEAN add before including Python.h (needed since python 3.10 when using s# or y#) - should fix #635

* Python: Replaced int in Python parsing by Py_ssize_t

Co-authored-by: rex-schilasky <[email protected]>
  • Loading branch information
FlorianReimold and rex-schilasky committed Apr 28, 2022
1 parent a511414 commit 647e950
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
31 changes: 16 additions & 15 deletions lang/python/core/src/ecal_wrap.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
/**
* @brief eCAL python interface
**/
#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include "modsupport.h"

#include <ecal/ecal.h>

Expand All @@ -30,8 +33,6 @@
#include <unordered_map>
#include <atomic>

#include "Python.h"
#include "modsupport.h"

#ifdef _MSC_VER
#pragma warning(push)
Expand Down Expand Up @@ -416,15 +417,15 @@ PyObject* pub_send(PyObject* /*self*/, PyObject* args)
{
ECAL_HANDLE topic_handle = nullptr;
char* payload = nullptr;
int length = 0;
Py_ssize_t length = 0;
PY_LONG_LONG time = 0;

if (!PyArg_ParseTuple(args, "ny#L", &topic_handle, &payload, &length, &time))
return nullptr;

int sent{ 0 };
//Py_BEGIN_ALLOW_THREADS
sent = pub_send(topic_handle, payload, length, time);
sent = pub_send(topic_handle, payload, (int)length, time);
//Py_END_ALLOW_THREADS

return(Py_BuildValue("i", sent));
Expand Down Expand Up @@ -532,7 +533,7 @@ PyObject* sub_receive(PyObject* /*self*/, PyObject* args)
ret = sub_receive(topic_handle, &rcv_buf, &rcv_buf_len, &rcv_time, timeout);
Py_END_ALLOW_THREADS

PyObject* ret_obj = Py_BuildValue("iy#L", ret, rcv_buf, rcv_buf_len, rcv_time);
PyObject* ret_obj = Py_BuildValue("iy#L", ret, rcv_buf, (Py_ssize_t)rcv_buf_len, rcv_time);
ecal_free_mem((void*)rcv_buf);

return(ret_obj);
Expand All @@ -554,7 +555,7 @@ static void c_subscriber_callback(const char* topic_name_, const struct eCAL::SR
PyGILState_STATE state = PyGILState_Ensure();

PyObject* topic_name = Py_BuildValue("s", topic_name_);
PyObject* content = Py_BuildValue(python_formatter.c_str(), data_->buf, data_->size);
PyObject* content = Py_BuildValue(python_formatter.c_str(), data_->buf, (Py_ssize_t) data_->size);
PyObject* time = Py_BuildValue("L", data_->time);

PyObject* args = PyTuple_New(3);
Expand Down Expand Up @@ -846,9 +847,9 @@ static int c_server_method_callback(const std::string& method_name_, const std::
PyObject* method_name = Py_BuildValue("s", method_name_.c_str());

const std::string fmt("y#");
PyObject* req_type = Py_BuildValue(fmt.data(), req_type_.data(), req_type_.size());
PyObject* resp_type = Py_BuildValue(fmt.data(), resp_type_.data(), resp_type_.size());
PyObject* request = Py_BuildValue(fmt.data(), request_.data(), request_.size());
PyObject* req_type = Py_BuildValue(fmt.data(), req_type_.data(), (Py_ssize_t)req_type_.size());
PyObject* resp_type = Py_BuildValue(fmt.data(), resp_type_.data(), (Py_ssize_t)resp_type_.size());
PyObject* request = Py_BuildValue(fmt.data(), request_.data(), (Py_ssize_t)request_.size());

PyObject* args = PyTuple_New(4);
PyTuple_SetItem(args, 0, method_name);
Expand All @@ -869,11 +870,11 @@ static int c_server_method_callback(const std::string& method_name_, const std::

int cb_ret_state = 0;
const char* cb_response = nullptr;
int cb_response_len = 0;
Py_ssize_t cb_response_len = 0;
if (PyArg_ParseTuple(result, "iy#", &cb_ret_state, &cb_response, &cb_response_len))
{
ret_state = cb_ret_state;
response_ = std::string(cb_response, cb_response_len);
response_ = std::string(cb_response, (int)cb_response_len);
}
else
{
Expand Down Expand Up @@ -1046,12 +1047,12 @@ PyObject* client_call_method(PyObject* /*self*/, PyObject* args) // (client_ha
ECAL_HANDLE client_handle = nullptr;
const char* method_name = nullptr;
const char* request = nullptr;
int request_len = 0;
Py_ssize_t request_len = 0;

PyArg_ParseTuple(args, "nsy#", &client_handle, &method_name, &request, &request_len);

bool called_method{ false };
called_method = client_call_method(client_handle, method_name, request, request_len);
called_method = client_call_method(client_handle, method_name, request, (int)request_len);

return(Py_BuildValue("i", called_method));
}
Expand Down Expand Up @@ -1105,7 +1106,7 @@ static void c_client_callback(const struct eCAL::SServiceInfo& service_info_, co
PyDict_SetItemString(dict, "call_state", val); Py_DECREF(val);
PyTuple_SetItem(args, 0, dict);

val = Py_BuildValue("y#", response_.c_str(), response_.size());
val = Py_BuildValue("y#", response_.c_str(), (Py_ssize_t)response_.size());
PyTuple_SetItem(args, 1, val);

PyClientCallbackMapT::const_iterator iter = g_client_pycallback_map.find(handle_);
Expand Down Expand Up @@ -1439,7 +1440,7 @@ PyObject* mon_monitoring(PyObject* /*self*/, PyObject* /*args*/)
val = Py_BuildValue("s", topic.ttype().c_str());
PyDict_SetItemString(topicDict, "ttype", val); Py_DECREF(val);

val = Py_BuildValue("y#", topic.tdesc().c_str(), topic.tdesc().length());
val = Py_BuildValue("y#", topic.tdesc().c_str(), (Py_ssize_t)(topic.tdesc().length()));
PyDict_SetItemString(topicDict, "tdesc", val); Py_DECREF(val);

val = Py_BuildValue("i", topic.tsize());
Expand Down
13 changes: 7 additions & 6 deletions lang/python/ecalhdf5/src/ecalhdf5_wrap.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* @brief eCALHDF5 python interface
**/

#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include "modsupport.h"
#include "structmember.h"
Expand Down Expand Up @@ -195,7 +196,7 @@ static PyObject* Meas_GetChannelDescription(Meas *self, PyObject *args)

std::string description = self->hdf5_meas->GetChannelDescription(channel_name);

return(Py_BuildValue("y#", description.c_str(), description.size()));
return(Py_BuildValue("y#", description.c_str(), (Py_ssize_t) description.size()));
}

/****************************************/
Expand All @@ -205,12 +206,12 @@ static PyObject* Meas_SetChannelDescription(Meas *self, PyObject *args)
{
char* channel_name = nullptr;
char* description = nullptr;
int size = 0;
Py_ssize_t size = 0;

if (!PyArg_ParseTuple(args, "sy#", &channel_name, &description, &size))
return nullptr;

self->hdf5_meas->SetChannelDescription(channel_name, std::string(description, size));
self->hdf5_meas->SetChannelDescription(channel_name, std::string(description, (int)size));
Py_RETURN_NONE;
}

Expand Down Expand Up @@ -394,7 +395,7 @@ static PyObject* Meas_GetEntryData(Meas *self, PyObject *args)

PyObject* py_data;

py_data = Py_BuildValue("y#", data, data_size);
py_data = Py_BuildValue("y#", data, (Py_ssize_t)data_size);

free(data);
return py_data;
Expand All @@ -420,7 +421,7 @@ static PyObject* Meas_SetFileBaseName(Meas *self, PyObject *args)
static PyObject* Meas_AddEntryToFile(Meas *self, PyObject *args)
{
char* data(nullptr);
int size(0);
Py_ssize_t size(0);
long long snd_timestamp(0);
long long rcv_timestamp(0);
char* channel_name(nullptr);
Expand All @@ -429,7 +430,7 @@ static PyObject* Meas_AddEntryToFile(Meas *self, PyObject *args)
if (!PyArg_ParseTuple(args, "y#LLs|L", &data, &size, &snd_timestamp, &rcv_timestamp, &channel_name, &counter))
return nullptr;

return(Py_BuildValue("i", self->hdf5_meas->AddEntryToFile(data, size, snd_timestamp, rcv_timestamp, channel_name, 0, counter)));
return(Py_BuildValue("i", self->hdf5_meas->AddEntryToFile(data, (int)size, snd_timestamp, rcv_timestamp, channel_name, 0, counter)));
}

/****************************************/
Expand Down

0 comments on commit 647e950

Please sign in to comment.