Correct use of PyTuple_Pack?

Hi all, I'm trying to make sure I'm using PyTuple_Pack() correctly.
This code leaks references, yes?
return PyTuple_Pack(2, PyString_FromString("some string"),
PyInt_FromLong(42));
And this is the correct way to do it, yes?
py_msg = PyString_FromString("some string");
py_type = PyInt_FromLong(42);
py_return_tuple = PyTuple_Pack(2, py_msg, py_type);
Py_DECREF(py_msg);
Py_DECREF(py_type);
return py_return_tuple;
Thanks for the confirmation Philip

Philip Semanchuk <philip@semanchuk.com> writes:
I'm trying to make sure I'm using PyTuple_Pack() correctly.
This code leaks references, yes?
return PyTuple_Pack(2, PyString_FromString("some string"), PyInt_FromLong(42));
And this is the correct way to do it, yes?
py_msg = PyString_FromString("some string"); py_type = PyInt_FromLong(42);
py_return_tuple = PyTuple_Pack(2, py_msg, py_type);
Py_DECREF(py_msg); Py_DECREF(py_type);
return py_return_tuple;
Yes, and yes. Note that you can use Py_BuildValue to create a new tuple and steal the new references:
return Py_BuildValue("NN", PyString_FromString("some string"), PyInt_FromLong(42));

On Feb 12, 2009, at 5:14 PM, Hrvoje Niksic wrote:
Philip Semanchuk <philip@semanchuk.com> writes:
I'm trying to make sure I'm using PyTuple_Pack() correctly.
This code leaks references, yes?
return PyTuple_Pack(2, PyString_FromString("some string"), PyInt_FromLong(42));
And this is the correct way to do it, yes?
py_msg = PyString_FromString("some string"); py_type = PyInt_FromLong(42);
py_return_tuple = PyTuple_Pack(2, py_msg, py_type);
Py_DECREF(py_msg); Py_DECREF(py_type);
return py_return_tuple;
Yes, and yes. Note that you can use Py_BuildValue to create a new tuple and steal the new references:
return Py_BuildValue("NN", PyString_FromString("some string"), PyInt_FromLong(42));
I much prefer your version. Thanks.
participants (2)
-
Hrvoje Niksic
-
Philip Semanchuk