ext/pycall/pycall.c in pycall-1.4.2 vs ext/pycall/pycall.c in pycall-1.5.0
- old
+ new
@@ -964,14 +964,14 @@
key = rb_sym_to_s(key);
}
key_cstr = StringValueCStr(key);
pyvalue = pycall_pyobject_from_ruby(value);
- if (Py_API(PyDict_SetItemString)(kwargs, key_cstr, pyvalue) < 0) {
- return ST_STOP;
- }
- return ST_CONTINUE;
+ int res = Py_API(PyDict_SetItemString)(kwargs, key_cstr, pyvalue);
+ pycall_Py_DecRef(pyvalue);
+
+ return (res < 0) ? ST_STOP : ST_CONTINUE;
}
static void
pycall_interrupt_python_thread(void *ptr)
{
@@ -1059,10 +1059,14 @@
}
}
}
res = pyobject_call(pycallable, args, kwargs); /* New reference */
+ pycall_Py_DecRef(args);
+ if (kwargs) {
+ pycall_Py_DecRef(kwargs);
+ }
if (!res) {
pycall_pyerror_fetch_and_raise("PyObject_Call in pycall_call_python_callable");
}
obj = pycall_pyobject_to_ruby(res);
pycall_Py_DecRef(res);
@@ -1097,13 +1101,13 @@
pycall_pyerror_fetch_and_raise("pycall_pyobject_from_ruby in pycall_pyobject_wrapper_wrapper_method");
}
name_cstr[RSTRING_LEN(name) - 1] = '\0';
res = Py_API(PyObject_SetAttrString)(pyobj, name_cstr, attr);
+ pycall_Py_DecRef(attr);
name_cstr[RSTRING_LEN(name) - 1] = '=';
if (res == -1) {
- pycall_Py_DecRef(attr);
pycall_pyerror_fetch_and_raise("PyObject_SetAttrString in pycall_pyobject_wrapper_wrapper_method");
}
return val;
}
@@ -1117,11 +1121,13 @@
return pycall_pyobject_to_ruby(attr);
if (PyType_Check(attr) || PyClass_Check(attr))
return pycall_pyobject_to_ruby(attr);
- return pycall_call_python_callable(attr, argc, argv);
+ VALUE obj = pycall_call_python_callable(attr, argc, argv);
+ pycall_Py_DecRef(attr);
+ return obj;
}
static VALUE
pycall_libpython_helpers_m_define_wrapper_method(VALUE mod, VALUE wrapper, VALUE name)
{
@@ -1201,10 +1207,11 @@
pyobj = get_pyobj_ptr(pyptr);
pyobj_key = pycall_convert_index(key);
pyobj_v = Py_API(PyObject_GetItem)(pyobj, pyobj_key);
+ pycall_Py_DecRef(pyobj_key);
if (!pyobj_v) {
pycall_pyerror_fetch_and_raise("PyObject_GetItem in pycall_libpython_helpers_m_getitem");
}
obj = pycall_pyobject_to_ruby(pyobj_v);
@@ -1221,15 +1228,15 @@
pyobj = check_get_pyobj_ptr(pyptr, NULL);
pyobj_key = pycall_convert_index(key);
pyobj_value = pycall_pyobject_from_ruby(v);
res = Py_API(PyObject_SetItem)(pyobj, pyobj_key, pyobj_value);
+ pycall_Py_DecRef(pyobj_key);
+ pycall_Py_DecRef(pyobj_value);
if (res == -1) {
pycall_pyerror_fetch_and_raise("PyObject_SetItem in pycall_libpython_helpers_m_setitem");
}
- Py_API(Py_DecRef(pyobj_key));
- Py_API(Py_DecRef(pyobj_value));
return v;
}
static VALUE
@@ -1240,10 +1247,11 @@
pyobj = check_get_pyobj_ptr(pyptr, NULL);
pyobj_key = pycall_convert_index(key);
res = Py_API(PyObject_DelItem)(pyobj, pyobj_key);
+ pycall_Py_DecRef(pyobj_key);
if (res == -1) {
pycall_pyerror_fetch_and_raise("PyObject_DelItem");
}
return Qnil;
@@ -1271,10 +1279,11 @@
int res;
pyobj = check_get_pyobj_ptr(pyptr, Py_API(PyDict_Type));
pyobj_key = pycall_pyobject_from_ruby(key);
res = Py_API(PyDict_Contains)(pyobj, pyobj_key);
+ pycall_Py_DecRef(pyobj_key);
if (res == -1) {
pycall_pyerror_fetch_and_raise("PyDict_Contains");
}
return res ? Qtrue : Qfalse;
@@ -1309,10 +1318,11 @@
if (!Py_API(PySequence_Check)(pyobj))
rb_raise(rb_eTypeError, "unexpected Python type %s (expected a Python sequence object)", Py_TYPE(pyobj)->tp_name);
pyobj_key = pycall_pyobject_from_ruby(key);
res = Py_API(PySequence_Contains)(pyobj, pyobj_key);
+ pycall_Py_DecRef(pyobj_key);
if (res == -1) {
pycall_pyerror_fetch_and_raise("PySequence_Contains");
}
return res ? Qtrue : Qfalse;
@@ -1706,10 +1716,11 @@
ary = rb_ary_new_capa(n);
for (i = 0; i < n; ++i) {
PyObject *pytem = Py_API(PyTuple_GetItem)(pyobj, i);
Py_API(Py_IncRef)(pytem);
rb_ary_push(ary, pycall_pyobject_to_ruby(pytem));
+ pycall_Py_DecRef(pytem);
}
return ary;
}
@@ -1724,10 +1735,11 @@
n = Py_API(PySequence_Size)(pyobj);
ary = rb_ary_new_capa(n);
for (i = 0; i < n; ++i) {
PyObject *pytem = Py_API(PySequence_GetItem)(pyobj, i);
rb_ary_push(ary, pycall_pyobject_to_ruby(pytem));
+ pycall_Py_DecRef(pytem);
}
return ary;
}
@@ -1936,15 +1948,13 @@
int res;
pyobj_key = pycall_pyobject_from_ruby(key);
pyobj_value = pycall_pyobject_from_ruby(value);
res = Py_API(PyObject_SetItem)(pydictobj, pyobj_key, pyobj_value);
- if (res == -1) {
- return ST_STOP;
- }
- Py_API(Py_DecRef)(pyobj_key);
- Py_API(Py_DecRef)(pyobj_value);
- return ST_CONTINUE;
+ pycall_Py_DecRef(pyobj_key);
+ pycall_Py_DecRef(pyobj_value);
+
+ return (res == -1) ? ST_STOP : ST_CONTINUE;
}
PyObject *
pycall_pydict_from_ruby(VALUE obj)
{