Discussion:
Does Py_False or Py_True need de-referencing if used in a PyObject_Compare?
Robert Steckroth
2012-12-13 16:21:16 UTC
Permalink
Does the below statement have a memory leak?

if ( PyObject_Compare(update_var, Py_False)

Do I need to assign the Py_False first and DECREF it, or is the above
snippet ok alone.
--
Bust0ut, Surgemcgee: Systems Engineer ---
surgemcgee.com
Django_Teamplate3d
Robert Steckroth
2012-12-13 16:57:24 UTC
Permalink
Well, from what I have gathered, the reference count does not need to be
maintain as far as PyBool objects go.
It seem to be fine even if the object is set from a argument. Hmmm...


On Thu, Dec 13, 2012 at 11:21 AM, Robert Steckroth <
Post by Robert Steckroth
Does the below statement have a memory leak?
if ( PyObject_Compare(update_var, Py_False)
Do I need to assign the Py_False first and DECREF it, or is the above
snippet ok alone.
--
Bust0ut, Surgemcgee: Systems Engineer ---
surgemcgee.com
Django_Teamplate3d
--
Bust0ut, Surgemcgee: Systems Engineer ---
surgemcgee.com
Django_Teamplate3d
Stefan Behnel
2012-12-15 14:33:27 UTC
Permalink
Post by Robert Steckroth
Well, from what I have gathered, the reference count does not need to be
maintain as far as PyBool objects go.
They are like any other object, i.e. they need reference counting.
Post by Robert Steckroth
It seem to be fine even if the object is set from a argument. Hmmm...
That's a different case. As long as you are sure that someone (better you)
owns a reference to the object that you pass as argument, you don't need to
increase its refcount just for the call. That's easy to get wrong, though.
Post by Robert Steckroth
Post by Robert Steckroth
Does the below statement have a memory leak?
if ( PyObject_Compare(update_var, Py_False)
Why should it?
Post by Robert Steckroth
Post by Robert Steckroth
Do I need to assign the Py_False first and DECREF it, or is the above
snippet ok alone.
I keep being surprised by what some people take their fun in programming
from. Good to see that you're getting actual functionality done regardless.

Stefan
Stefan Behnel
2012-12-15 14:06:39 UTC
Permalink
Post by Robert Steckroth
Well, from what I have gathered, the reference count does not need to be
maintain as far as PyBool objects go.
They are like any other object, i.e. they need reference counting.
Post by Robert Steckroth
It seem to be fine even if the object is set from a argument. Hmmm...
That's a different case. As long as you are sure that someone (better you)
owns a reference to the object that you pass as argument, you don't need to
increase its refcount just for the call. That's easy to get wrong, though.
Post by Robert Steckroth
Post by Robert Steckroth
Does the below statement have a memory leak?
if ( PyObject_Compare(update_var, Py_False)
Why should it?
Post by Robert Steckroth
Post by Robert Steckroth
Do I need to assign the Py_False first and DECREF it, or is the above
snippet ok alone.
I keep being surprised by what some people take their fun in programming
from. Good to see that you're getting actual functionality done regardless.

Stefan
Campbell Barton
2012-12-15 14:46:50 UTC
Permalink
On Fri, Dec 14, 2012 at 3:21 AM, Robert Steckroth
Post by Robert Steckroth
Does the below statement have a memory leak?
if ( PyObject_Compare(update_var, Py_False)
Do I need to assign the Py_False first and DECREF it, or is the above
snippet ok alone.
The python api docs will often say if they use/steal references.
If your not sure you could just printf("%d\n", Py_REFCNT(Py_False));
before and after calling.
Gustavo Carneiro
2012-12-17 11:23:59 UTC
Permalink
Post by Robert Steckroth
Does the below statement have a memory leak?
if ( PyObject_Compare(update_var, Py_False)
Do I need to assign the Py_False first and DECREF it, or is the above
snippet ok alone.
It's safe, but note that it is more correct to use PyObject_IsTrue: there
are more values that can be considered true or false, besides python's True
and False objects.

if (!PyObject_IsTrue(update_var)) { ... }
--
Gustavo J. A. M. Carneiro
"The universe is always one step beyond logic." -- Frank Herbert
Campbell Barton
2012-12-17 13:59:17 UTC
Permalink
Take care using PyObject_IsTrue since it will accept many PyObject's
(floats, lists, dicts etc), if you want to be strict with checking
your input its not always so good.
Instead you can use PyLong_AsLong() this works for True/False and any int type.

eg:
int param = PyLong_AsLong(value);
if (param == -1 && PyErr_Occurred()) {
.... error out ...
}
... check the int now ...
Post by Gustavo Carneiro
PyObject_IsTrue(
--
- Campbell
Gustavo Carneiro
2012-12-17 16:45:37 UTC
Permalink
You are not wrong. On the other hand, I often depend on the fact that an
empty list or dictionary is false, and true if not empty. It is quite
convenient, and I find it reasonably logical.
Post by Campbell Barton
Take care using PyObject_IsTrue since it will accept many PyObject's
(floats, lists, dicts etc), if you want to be strict with checking
your input its not always so good.
Instead you can use PyLong_AsLong() this works for True/False and any int type.
int param = PyLong_AsLong(value);
if (param == -1 && PyErr_Occurred()) {
.... error out ...
}
... check the int now ...
Post by Gustavo Carneiro
PyObject_IsTrue(
--
- Campbell
_______________________________________________
capi-sig mailing list
http://mail.python.org/mailman/listinfo/capi-sig
--
Gustavo J. A. M. Carneiro
"The universe is always one step beyond logic." -- Frank Herbert
Campbell Barton
2012-12-18 04:48:53 UTC
Permalink
Received: from localhost (HELO mail.python.org) (127.0.0.1)
by albatross.python.org with SMTP; 18 Dec 2012 05:48:55 +0100
Received: from mail-pb0-f45.google.com (mail-pb0-f45.google.com
[209.85.160.45])
(using TLSv1 with cipher ECDHE-RSA-RC4-SHA (128/128 bits))
(No client certificate requested)
by mail.python.org (Postfix) with ESMTPS
for <capi-sig-+ZN9ApsXKcEdnm+***@public.gmane.org>; Tue, 18 Dec 2012 05:48:54 +0100 (CET)
Received: by mail-pb0-f45.google.com with SMTP id mc8so133954pbc.32
for <capi-sig-+ZN9ApsXKcEdnm+***@public.gmane.org>; Mon, 17 Dec 2012 20:48:53 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;
h=mime-version:in-reply-to:references:date:message-id:subject:from:to
:content-type; bh=CjX2OMN2oc3nc0u2jWkI8Oioxt+M67z7vpmsheTqa4c=;
b=jT/+OHO7buhR4E6e6OzPgt2osd1Zq1DU31XP7mJZ+TJ6vO+vI3FxfI92QXbcf6oN/R
MXD1EOqQpVnEmwB/hSLw/gcRW4Uayf1RQwoUmJtdbtFmkcbFNIqkBGo/FERdeAw7czgW
XQo5Fw9Kzu1no72ZMdlHgQEk529vDOezazS/u99aV8gHDxLh/x0WTWQvThA0Ovz8WmKp
1SsAeG4jLLmFGgDgUdSpRs9WrFnm81ZX/+sCtKgRZBuHnjuu2PhG8lwpPAERNoTb6Moi
VjxfAOQOiG5JkFtCUs4k4gEu8h/A1VtZbivx2bpRCNU20F2ZyYpHNP3rSLE5t9DxHUpa
r9iA==
Received: by 10.68.197.8 with SMTP id iq8mr2760863pbc.56.1355806133353; Mon,
17 Dec 2012 20:48:53 -0800 (PST)
Received: by 10.66.232.74 with HTTP; Mon, 17 Dec 2012 20:48:53 -0800 (PST)
In-Reply-To: <CAO-CpEJuMeJ-T=u1OX=2RFcuza=O135z_d+THa+V81uknQxFfw-JsoAwUIsXosN+***@public.gmane.org>
X-BeenThere: capi-sig-+ZN9ApsXKcEdnm+***@public.gmane.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: Mailing list for discussing the Python/C API <capi-sig.python.org>
List-Unsubscribe: <http://mail.python.org/mailman/options/capi-sig>,
<mailto:capi-sig-request-+ZN9ApsXKcEdnm+***@public.gmane.org?subject=unsubscribe>
List-Archive: <http://mail.python.org/pipermail/capi-sig/>
List-Post: <mailto:capi-sig-+ZN9ApsXKcEdnm+***@public.gmane.org>
List-Help: <mailto:capi-sig-request-+ZN9ApsXKcEdnm+***@public.gmane.org?subject=help>
List-Subscribe: <http://mail.python.org/mailman/listinfo/capi-sig>,
<mailto:capi-sig-request-+ZN9ApsXKcEdnm+***@public.gmane.org?subject=subscribe>
Errors-To: capi-sig-bounces+gcpc-capi-sig=m.gmane.org-+ZN9ApsXKcEdnm+***@public.gmane.org
Sender: "capi-sig" <capi-sig-bounces+gcpc-capi-sig=m.gmane.org-+ZN9ApsXKcEdnm+***@public.gmane.org>
Archived-At: <http://permalink.gmane.org/gmane.comp.python.capi/131>

It depends, sometimes you wont want to allow this for attribute
assignment since it can be misleading.
eg:

foo.bar = []
Post by Gustavo Carneiro
You are not wrong. On the other hand, I often depend on the fact that an
empty list or dictionary is false, and true if not empty. It is quite
convenient, and I find it reasonably logical.
Post by Campbell Barton
Take care using PyObject_IsTrue since it will accept many PyObject's
(floats, lists, dicts etc), if you want to be strict with checking
your input its not always so good.
Instead you can use PyLong_AsLong() this works for True/False and any int type.
int param = PyLong_AsLong(value);
if (param == -1 && PyErr_Occurred()) {
.... error out ...
}
... check the int now ...
Post by Gustavo Carneiro
PyObject_IsTrue(
--
- Campbell
_______________________________________________
capi-sig mailing list
http://mail.python.org/mailman/listinfo/capi-sig
--
Gustavo J. A. M. Carneiro
"The universe is always one step beyond logic." -- Frank Herbert
--
- Campbell
Loading...