Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

A feature is nothing more than a bug with seniority. -- Unknown source


devel / comp.lang.python / Re: Cpython: when to incref before insertdict

SubjectAuthor
o Re: Cpython: when to incref before insertdictInada Naoki

1
Re: Cpython: when to incref before insertdict

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: songofac...@gmail.com (Inada Naoki)
Newsgroups: comp.lang.python
Subject: Re: Cpython: when to incref before insertdict
Date: Sun, 6 Mar 2022 11:19:55 +0900
Lines: 48
Message-ID: <mailman.201.1646533223.2329.python-list@python.org>
References: <CABbU2U8d9scJo8pMaynoW25jmT-ipJc9DMfbEy2ccmEsr1GK7w@mail.gmail.com>
<CAEfz+TwbRyfhNqjnd4MJgFjk8kK3_q2otNxEoah9eytu=C49yg@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de 5wG3I0Ji/yjjze/bmdkPTwuZ+Rcd7EH4+vP0euvaup0g==
Return-Path: <songofacandy@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=eH03H+g+;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'url-ip:140.82/16': 0.03;
'(e.g.': 0.05; '2022': 0.05; 'mar': 0.07; 'subject:when': 0.07;
'cc:addr:python-list': 0.09; 'general,': 0.09; 'skip:_ 20': 0.09;
'url:github': 0.14; 'url-ip:140/8': 0.15; 'api.': 0.16; 'apis':
0.16; 'borrows': 0.16; 'call,': 0.16; 'cc:name:python list': 0.16;
'functions,': 0.16; 'hand,': 0.16; 'inada': 0.16; 'naoki': 0.16;
'part.': 0.16; 'slow': 0.16; 'url:cpython': 0.16; 'wrote:': 0.16;
'uses': 0.19; 'calls': 0.19; 'cc:addr:python.org': 0.20; 'all,':
0.20; 'returns': 0.22; 'sat,': 0.22; 'skip:_ 10': 0.22; 'cc:2**0':
0.25; 'again,': 0.26; 'takes': 0.31; 'before.': 0.31; 'assume':
0.32; 'words,': 0.32; 'message-id:@mail.gmail.com': 0.32; 'but':
0.32; 'path': 0.33; 'header:In-Reply-To:1': 0.34;
'received:google.com': 0.34; 'complex': 0.35; 'usual': 0.35;
'from:addr:gmail.com': 0.35; 'functions': 0.36; 'guide': 0.37;
'special': 0.37; 'received:209.85': 0.37; 'received:209': 0.39;
'received:209.85.208': 0.39; 'case.': 0.40; 'reference': 0.60;
'complete': 0.64; 'key': 0.64; 'during': 0.69; 'strong': 0.69;
'reference.': 0.81; 'sulla': 0.84; 'subject:before': 0.93
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=mime-version:references:in-reply-to:from:date:message-id:subject:to
:cc; bh=6HXYN8m0Uwz7873WfyWXJ0vweSn2zVcWoXLtAjePphU=;
b=eH03H+g+M4RxZwJ35y00VEZpzF+H3gH4OctAOoEo/zLS9XmTBbBMRjUDAoGAJwXrlh
rHi1wAJiRdkpl2bayFAjy4O+z6OOyx17rqmMJGNVZNV/mwwVwLHlEff3lS8Zc/on93+N
/3C0p2FpW6fz/zpNe9+S5iNrVvp1QyBFpXHJeqgdm/OlkogyD42iZhDIog37pcGeprVq
RG1wyuex38dSMqRxUphRxMbJWCkSYjX2LPYNmTjR3eqtvVDg13A0GfREWyzA5v9rhBhQ
jODDUZVvha9dtFqMB28rlE22itfTXBZtDKdX+Xwk9jo/Q1I6IKiufo098JVRiPWwDts+
bKOw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=x-gm-message-state:mime-version:references:in-reply-to:from:date
:message-id:subject:to:cc;
bh=6HXYN8m0Uwz7873WfyWXJ0vweSn2zVcWoXLtAjePphU=;
b=ERJehsRQUf2jPcl+6FlcmBOGu6y5HdfsXY2spjne38bHmx7ZtGj2v8nIAlVhtCE565
mprJz6HjtJ4/J0UlrN8tkGQ9v8NvaOMr9D15lH2NZlQ4GDs9bqdGHe8Bos8qUy+H3g2x
wLlezVkwBiXM2kJ7Pf8y6B26F0oiF91xiFx7DZrghGFN+0RlVWf2q/Tt6if+iKvJHJ7P
6nln6b8nOin6OmAttiSsDgnzsxb5ET4ddwe0E2AxHCzti16Ttvaw1LngmDJfYroRxfHf
Yfa6kWMjlmVezH+g8DZcw1p6rjOqZ+QL8uIomtWPacrxasjrR8+mEf8ZL87jpI8zTIem
LVNw==
X-Gm-Message-State: AOAM530PACAmbWmGVf3JfpXFgPDeixVTj1cNwyIDu5JF441aw+ryG3eb
8ksGCCXb6yewJ0LJQSLuSsWVJehfz1WIw2zTTzdpI0x/Jx0=
X-Google-Smtp-Source: ABdhPJzCljfszHYCD31h/yLMSXU370otc8GoDVHkXTMHdrtI4yVCV/YjzMQ/CHAN05HvlKSdl9mR1ut5s0ciafixrT0=
X-Received: by 2002:a05:6402:2709:b0:413:1871:3bc7 with SMTP id
y9-20020a056402270900b0041318713bc7mr5053660edd.71.1646533221779; Sat, 05 Mar
2022 18:20:21 -0800 (PST)
In-Reply-To: <CABbU2U8d9scJo8pMaynoW25jmT-ipJc9DMfbEy2ccmEsr1GK7w@mail.gmail.com>
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: <CAEfz+TwbRyfhNqjnd4MJgFjk8kK3_q2otNxEoah9eytu=C49yg@mail.gmail.com>
X-Mailman-Original-References: <CABbU2U8d9scJo8pMaynoW25jmT-ipJc9DMfbEy2ccmEsr1GK7w@mail.gmail.com>
 by: Inada Naoki - Sun, 6 Mar 2022 02:19 UTC

On Sat, Mar 5, 2022 at 11:22 PM Marco Sulla
<Marco.Sulla.Python@gmail.com> wrote:
>
> I noticed that some functions inside dictobject.c that call insertdict
> or PyDict_SetItem do an incref of key and value before the call, and a
> decref after it. An example is dict_merge.

First of all, insertdict and PyDict is totally different about
reference ownership handling.

* PyDict_SetItem borrows reference of key and value from the caller as
usual Python/C APIs. And it INCREF them before calling the
insertdict().
* insertdict() takes the reference from its caller. In other words,
insertdict() moves the owner of reference from its caller to the dict.

merge_dict is very special and complex case.
I assume you are talking about this part.
https://github.com/python/cpython/blob/6927632492cbad86a250aa006c1847e03b03e70b/Objects/dictobject.c#L2885-L2912

In general, when reference is borrowed from a caller, the reference is
available during the API.
But merge_dict borrows reference of key/value from other dict, not caller.
So dict_merge must have strong reference of key/value by INCREF before
calling any APIs (e.g. _PyDict_Contains_KnownHash).
That's why dict_merge calls INCREF key/value **twice** before calling
insertdict, and DECREF key/value **once** after it.

> Other functions, such as
> _PyDict_FromKeys, don't do an incref before.
>

Again, insertdict takes the reference. So _PyDict_FromKeys() **does**
INCREF before calling insertdict, when key/value is borrowed
reference.
https://github.com/python/cpython/blob/6927632492cbad86a250aa006c1847e03b03e70b/Objects/dictobject.c#L2287-L2290
https://github.com/python/cpython/blob/6927632492cbad86a250aa006c1847e03b03e70b/Objects/dictobject.c#L2309-L2311

On the other hand, slow path uses PyIter_Next() which returns strong
reference. So no need to INCREF it.
Additionally, the slow path uses PyDict_SetItem(), not insertdict().
PyDict_SetItem() does INCREF key/value for insertdict.
So the slow path need to DECREF(key).
https://github.com/python/cpython/blob/6927632492cbad86a250aa006c1847e03b03e70b/Objects/dictobject.c#L2327-L2329

This is complete guide why/when INCREF/DECREF key/value.
--
Inada Naoki <songofacandy@gmail.com>

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor