Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

The generation of random numbers is too important to be left to chance.


devel / comp.lang.python / Re: Puzzling behaviour of Py_IncRef

SubjectAuthor
o Re: Puzzling behaviour of Py_IncRefBarry Scott

1
Re: Puzzling behaviour of Py_IncRef

<mailman.272.1643312303.3079.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=16824&group=comp.lang.python#16824

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!3.eu.feeder.erje.net!feeder.erje.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: bar...@barrys-emacs.org (Barry Scott)
Newsgroups: comp.lang.python
Subject: Re: Puzzling behaviour of Py_IncRef
Date: Thu, 27 Jan 2022 19:38:11 +0000
Lines: 78
Message-ID: <mailman.272.1643312303.3079.python-list@python.org>
References: <8201bfa3-8626-a509-bbde-801566e2bc33@btinternet.com>
<58EDEC0D-8698-48EF-A5B4-B203224DD4EE@barrys-emacs.org>
<2d1960e6-17bd-f060-4c66-1aa61ca9c48c@btinternet.com>
<B850DED3-A37B-4968-9801-574DD9AA13E2@barrys-emacs.org>
Mime-Version: 1.0 (Mac OS X Mail 15.0 \(3693.40.0.1.81\))
Content-Type: text/plain;
charset=utf-8
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de LfhaepljE4LyJB+O1DfWvAAEX+Nigp0pHLzlxpTzxXvQ==
Return-Path: <barry@barrys-emacs.org>
X-Original-To: Python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.006
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'argument': 0.04; 'debug':
0.07; 'debugging': 0.07; 'cc:addr:python-list': 0.09;
'difficult.': 0.09; 'from:addr:barry': 0.09; 'linux': 0.09;
'macos': 0.09; 'received:217.70': 0.09; 'received:gandi.net':
0.09; 'received:mail.gandi.net': 0.09; 'cc:no real name:2**0':
0.14; 'url:mailman': 0.15; '(so': 0.16; '2022,': 0.16; '>>>>>':
0.16; 'barry': 0.16; 'debugger': 0.16; 'easiest': 0.16; 'from:addr
:barrys-emacs.org': 0.16; 'from:name:barry scott': 0.16;
'incremented': 0.16; 'message-id:@barrys-emacs.org': 0.16; 'odd':
0.16; 'wrote:': 0.16; 'python': 0.16; 'api': 0.17; 'code.': 0.17;
'calls': 0.19; 'cc:addr:python.org': 0.20; 'language': 0.21;
'code': 0.23; 'run': 0.23; 'url-ip:188.166.95.178/32': 0.25; 'url-
ip:188.166.95/24': 0.25; 'url:listinfo': 0.25; 'cc:2**0': 0.25;
'url-ip:188.166/16': 0.25; 'tried': 0.26; 'behavior': 0.26;
'object': 0.26; 'coming': 0.27; 'function': 0.27; '>>>': 0.28;
'expect': 0.28; 'code,': 0.31; 'url-ip:188/8': 0.31; 'execution':
0.32; 'but': 0.32; 'there': 0.33; 'script': 0.33; 'windows': 0.34;
'same': 0.34; 'mean': 0.34; 'header:In-Reply-To:1': 0.34;
'meaning': 0.35; 'count': 0.36; 'those': 0.36; 'really': 0.37;
"it's": 0.37; 'way': 0.38; 'means': 0.38; 'thanks': 0.38;
'setting': 0.39; 'use': 0.39; 'break': 0.39; 'level.': 0.40;
'reports': 0.40; 'try': 0.40; 'should': 0.40; 'reference': 0.60;
'mode': 0.62; 'email': 0.63; 'about.': 0.64; 'your': 0.64; 'lead':
0.67; 'back': 0.67; 'outside': 0.67; 'received:217': 0.67;
'times': 0.69; 'care': 0.71; 'low': 0.74; 'watch': 0.76;
'anthony': 0.84; 'from.': 0.84; 'macro': 0.84; 'statement.': 0.84;
'tony': 0.84; 'caused': 0.86; 'tend': 0.91; 'mixed': 0.93;
'immediate': 0.97
In-Reply-To: <2d1960e6-17bd-f060-4c66-1aa61ca9c48c@btinternet.com>
X-Mailer: Apple Mail (2.3693.40.0.1.81)
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <B850DED3-A37B-4968-9801-574DD9AA13E2@barrys-emacs.org>
X-Mailman-Original-References: <8201bfa3-8626-a509-bbde-801566e2bc33@btinternet.com>
<58EDEC0D-8698-48EF-A5B4-B203224DD4EE@barrys-emacs.org>
<2d1960e6-17bd-f060-4c66-1aa61ca9c48c@btinternet.com>
 by: Barry Scott - Thu, 27 Jan 2022 19:38 UTC

> On 27 Jan 2022, at 07:46, Tony Flury <tony.flury@btinternet.com> wrote:
>
>
> On 26/01/2022 22:41, Barry wrote:
>>
>>
>> Run python and your code under a debugger and check the ref count of the object as you step through the code.
>>
>> Don’t just step through your code but also step through the C python code.
>> That will allow you to see how this works at a low level.
>> Setting a watch point on the ref count will allow you run the code and just break as the ref count changes.
>>
>> That is what I do when a see odd c api behaviour.
>>
>> Barry
>
>
> Thanks - I have tried a few times on a few projects to run a debugger in mixed language mode and never had any success.
>
> I will have to try again.

You mean debugging Python and C/C++? In this case your python code is a simple test script and it's C that you care about.
Should not be difficult. I tend to use linux as my lead debug platform as its the easiest to work with. But Windows and macOS
also have very good debuggers.

Barry

>
>
>>> As posted in the original message - immediately before the call to the C function/method sys.getrefcount reports the count to be 2 (meaning it is actually a 1).
>>>
>>> Inside the C function the ref count is incremented and the Py_REFCNT macro reports the count as 3 inside the C function as expected (1 for the name in the Python code, 1 for the argument as passed to the C function, and 1 for the increment), so outside the function one would expect the ref count to now be 2 (since the reference caused by calling the function is then reversed).
>>>
>>> However - Immediately outside the C function and back in the Python code sys.getrefcount reports the count to be 2 again - meaning it is now really 1. So that means that the refcount has been decremented twice in-between the return of the C function and the execution of the immediate next python statement. I understand one of those decrements - the parameter's ref count is incremented on the way in so the same object is decremented on the way out (so that calls don't leak references) but I don't understand where the second decrement is coming from.
>>>
>>> Again there is nothing in the Python code that would cause that decrement - the decrement behavior is in the Python runtime.
>>>
>>>>> --
>>>>> Anthony Flury
>>>>> email :anthony.flury@btinternet.com
>>>>>
>>>>> --
>>>>> https://mail.python.org/mailman/listinfo/python-list
>>>>>
>>> --
>>> Anthony Flury
>>> email :anthony.flury@btinternet.com
>
> --
> Anthony Flury
> email : anthony.flury@btinternet.com
>

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor