Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Pascal is a language for children wanting to be naughty. -- Dr. Kasi Ananthanarayanan


devel / comp.lang.python / Re: Seeking deeper understanding of python equality (==)

SubjectAuthor
* Seeking deeper understanding of python equality (==)Jonathan Kaczynski
`* Re: Seeking deeper understanding of python equality (==)Greg Ewing
 +- Re: Seeking deeper understanding of python equality (==)Jonathan Kaczynski
 +- Re: Seeking deeper understanding of python equality (==)Jonathan Kaczynski
 +- Re: Seeking deeper understanding of python equality (==)Eryk Sun
 `- Re: Seeking deeper understanding of python equality (==)Jonathan Kaczynski

1
Seeking deeper understanding of python equality (==)

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

 copy mid

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

 copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: jonathan...@guildeducation.com (Jonathan Kaczynski)
Newsgroups: comp.lang.python
Subject: Seeking deeper understanding of python equality (==)
Date: Fri, 6 May 2022 08:22:03 -0400
Lines: 78
Message-ID: <mailman.324.1651857051.20749.python-list@python.org>
References: <CAMA-GnV5+Z-Q7vo+G2x32ytjiVsuCLGxR2NX=BAQPrWGhOHH4Q@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de 6jOaPPe9yghqT0sXm9bGkwneo1RbpBcDhQ4YlTjB3PKg==
Return-Path: <jonathan.kaczynski@guildeducation.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=guildeducation.com header.i=@guildeducation.com
header.b=hvkOpWVg; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.004
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'url-ip:140.82/16': 0.03;
'def': 0.04; 'subject:python': 0.06; 'debugging': 0.07;
'explanation': 0.07; 'received:205': 0.07; 'comparison': 0.09;
'knowledge.': 0.09; '&gt;': 0.14; 'url:github': 0.14; 'import':
0.15; 'url-ip:140/8': 0.15; '-&gt;': 0.16; 'cat': 0.16;
'expected.': 0.16; 'occur.': 0.16; 'permitted': 0.16; 'program:':
0.16; 'python3': 0.16; 'received:205.220': 0.16; 'url:cpython':
0.16; 'warning:': 0.16; 'python': 0.16; 'to:addr:python-list':
0.20; 'ran': 0.22; 'install': 0.23; 'code': 0.23; 'run': 0.23;
'idea': 0.24; 'space': 0.26; 'library': 0.26;
'received:pphosted.com': 0.26; 'recently': 0.29; 'error': 0.29;
'think': 0.32; 'assume': 0.32; 'subject:skip:u 10': 0.32;
'message-id:@mail.gmail.com': 0.32; 'but': 0.32; "i'm": 0.33;
"i'll": 0.33; 'able': 0.34; 'same': 0.34; 'received:google.com':
0.34; 'trying': 0.35; 'also,': 0.36; 'source': 0.36; 'change':
0.36; 'received:filterd': 0.37; 'received:pps.filterd': 0.37;
'using': 0.37; 'received:209.85': 0.37; 'class': 0.37;
'received:209': 0.39; 'single': 0.39; 'still': 0.40; 'explain':
0.40; 'want': 0.40; 'method': 0.61; 'skip:i 20': 0.62; '8bit%:9':
0.64; 'skip:r 20': 0.64; 'thus': 0.64; 'compare': 0.69; 'skip:/
10': 0.69; 'surface': 0.69; '....': 0.76; 'dis': 0.84; 'skip:"
40': 0.84; 'skip:& 50': 0.84; 'steps.': 0.84; 'subject:Seeking':
0.84; '\xc2\xa01': 0.84; 'priority.': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=guildeducation.com; s=google;
h=mime-version:from:date:message-id:subject:to;
bh=kWjqZtWqWRYkLUKnaLiQwtf70AHDiwfBQNcfXC5ZKF8=;
b=hvkOpWVgFOINxNv0YhsqOV6LIUGyHz7DfPAZoVewdA+kU354heCEovwKqXEBfJ3cR/
o3+kQaaka+sZCNCgy6vUM0oxcMnPS0wGWJA30zelKUsJGUp3tKCi6n2ACAIdWD94PXp2
rKztcfaBxzhn26+5zu2wrMSGtYZYHk1O+9znd5h7ocqGwebcarhRIu5snitHklm9wL4N
CD28axNMRN/Z/Sb5PcYQFUZK9BKqT1DW3SfLwR9oDbXBhtzcSZpDwo83WoVW+FjiHg7x
PE70UfU6nNi15ONqBLEq2pfd3o8qzSoOXKfFVtss//XdH2r+0sdOeaEBNYms4TB+SIBf
LscQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=x-gm-message-state:mime-version:from:date:message-id:subject:to;
bh=kWjqZtWqWRYkLUKnaLiQwtf70AHDiwfBQNcfXC5ZKF8=;
b=DYU7G6f0t2o5E8Lyij6CoYRqG9LghcIBSvOA/isoYwWocfdcdrSKnI9ysfiaMGRatQ
CKlgMALy/R91uhV5Posy6FXx+jJU3GyDzzItU32HP5T3ZwYJxnet4vpyRYg2QWjhjBqB
vgYIG0eWtMBvldJZXsqUn958FmDWRhuU2SslCkU2yoguCxHL1g1n5MmIUZIvCeyH4NEg
UZUupFDot03BE8HUDOB30v50TDjFpbL42ZzSFsnWe4E6AdyOJ2R+rc+qNRfw8UGwqNRB
FIzUCNokKjDzNsnj1fxZZ8eKLfMkCXjtEBoSBiTlLFlPXgQ2dEv4ClptnWtNXR6HSt1p
8+Bg==
X-Gm-Message-State: AOAM533ISPCoueH8vhG/OXGppoZbvIMS+/b42Iy5HRylf490NXHBcTN2
O6F6P4qTJqzknU3UrYlsnyal4Cy/HSZJDKtoxyI1N9aFwrs9LpJcHLOgPEKcDDeaiyL2inXtqWf
yzwLtCTEv/sDLfh24slDwhYdFWbOzpk/0
X-Received: by 2002:a05:6870:340e:b0:ec:c40f:e630 with SMTP id
g14-20020a056870340e00b000ecc40fe630mr4118723oah.144.1651839734050;
Fri, 06 May 2022 05:22:14 -0700 (PDT)
X-Google-Smtp-Source: ABdhPJysw0Bm0RaIZTXfRkbbP5zqNp8RpfBg783MjexWXeiNuLsmdrlSktXY1sNktH960Z8vfaYdiDKoMNE/GobJTGM=
X-Received: by 2002:a05:6870:340e:b0:ec:c40f:e630 with SMTP id
g14-20020a056870340e00b000ecc40fe630mr4118708oah.144.1651839733727; Fri, 06
May 2022 05:22:13 -0700 (PDT)
X-Proofpoint-GUID: blxFJRM553ePlO1DcUJe_e6moRPeWVpn
X-Proofpoint-ORIG-GUID: blxFJRM553ePlO1DcUJe_e6moRPeWVpn
X-Proofpoint-Virus-Version: vendor=baseguard
engine=ICAP:2.0.205,Aquarius:18.0.858,Hydra:6.0.486,FMLib:17.11.64.514
definitions=2022-05-06_04,2022-05-06_01,2022-02-23_01
X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0
priorityscore=1501
impostorscore=0 lowpriorityscore=0 phishscore=0 malwarescore=0 bulkscore=0
mlxscore=0 adultscore=0 mlxlogscore=748 spamscore=0 suspectscore=0
clxscore=1011 classifier=spam adjust=0 reason=mlx scancount=1
engine=8.12.0-2202240000 definitions=main-2205060069
X-Mailman-Approved-At: Fri, 06 May 2022 13:10:51 -0400
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
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: <CAMA-GnV5+Z-Q7vo+G2x32ytjiVsuCLGxR2NX=BAQPrWGhOHH4Q@mail.gmail.com>
 by: Jonathan Kaczynski - Fri, 6 May 2022 12:22 UTC

Hi,

I was recently trying to explain how python equality works and ran into a
gap in my knowledge. I haven't found any good pages going beneath a surface
level explanation of python equality comparison.

I'll post my investigations below. What I think I'm looking for is where in
the source code (https://github.com/python/cpython) does the equality
comparison occur. I have an idea but wanted to ask first.

Using the dis module, we see the comparison operator is a single bytecode,
which is expected.

❯ docker run -it --rm ubuntu:jammy
root@919d94c98191:/# apt-get update
root@919d94c98191:/# apt-get --yes install python3
root@919d94c98191:/# cat >play.py <<EOF
import dis
import uuid

def test():
x = uuid.uuid4()
y = str(x)
x == y
return

dis.dis(test)
EOF
root@f33b02fef026:/# python3 play.py
.... snip ...
7 16 LOAD_FAST 0 (x)
18 LOAD_FAST 1 (y)
20 COMPARE_OP 2 (==)
22 POP_TOP
.... snip ...

Stepping through the code with gdb, we see it jump from the compare
operator to the dunder-eq method on the UUID object. What I want to be able
to do is explain the in-between steps. Also, if you change `x == y` to `y
== x`, you still see the same behavior, which I assume has to do with
dunder-eq being defined on the UUID class and thus given priority.

❯ docker run -it --rm ubuntu:jammy
root@919d94c98191:/# apt-get update
root@919d94c98191:/# apt-get --yes install dpkg-source-gitarchive
root@919d94c98191:/# sed -i 's/^# deb-src/deb-src/' /etc/apt/sources.list
root@919d94c98191:/# apt-get update
root@919d94c98191:/# apt-get --yes install gdb python3.10-dbg
root@919d94c98191:/# apt-get source python3.10-dbg
root@919d94c98191:/# cat >play.py <<EOF
import uuid
x = uuid.uuid4()
y = str(x)
breakpoint()
x == y
EOF
root@919d94c98191:/# gdb python3.10-dbg
(gdb) dir python3.10-3.10.4/Python
(gdb) run play.py
Starting program: /usr/bin/python3.10-dbg play.py

warning: Error disabling address space randomization: Operation not
permitted
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
> //play.py(5)<module>()
-> x == y
(Pdb) s
--Call--
> /usr/lib/python3.10/uuid.py(239)__eq__()
-> def __eq__(self, other):

Thank you,
Jonathan

Re: Seeking deeper understanding of python equality (==)

<jdm3i4F46vmU1@mid.individual.net>

 copy mid

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

 copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: greg.ew...@canterbury.ac.nz (Greg Ewing)
Newsgroups: comp.lang.python
Subject: Re: Seeking deeper understanding of python equality (==)
Date: Sat, 7 May 2022 14:24:03 +1200
Lines: 26
Message-ID: <jdm3i4F46vmU1@mid.individual.net>
References: <CAMA-GnV5+Z-Q7vo+G2x32ytjiVsuCLGxR2NX=BAQPrWGhOHH4Q@mail.gmail.com>
<mailman.324.1651857051.20749.python-list@python.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: individual.net WQBA6rmIYwxdTwV4vK2Neg73rAKlF7hwFAKbgcICfVVwQdrWJK
Cancel-Lock: sha1:tAyCLFW8WWdTCs10hlBYQTNoKAM=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:91.0)
Gecko/20100101 Thunderbird/91.3.2
Content-Language: en-US
In-Reply-To: <mailman.324.1651857051.20749.python-list@python.org>
 by: Greg Ewing - Sat, 7 May 2022 02:24 UTC

On 7/05/22 12:22 am, Jonathan Kaczynski wrote:
> Stepping through the code with gdb, we see it jump from the compare
> operator to the dunder-eq method on the UUID object. What I want to be able
> to do is explain the in-between steps.

Generally what happens with infix operators is that the interpreter
first looks for a dunder method on the left operand. If that method
doesn't exist or returns NotImplemented, it then looks for a dunder
method on the right operand.

There is an exception if the right operand is a subclass of the
left operand -- in that case the right operand's dunder method
takes precedence.

> Also, if you change `x == y` to `y
> == x`, you still see the same behavior, which I assume has to do with
> dunder-eq being defined on the UUID class and thus given priority.

No, in that case the conparison method of str will be getting
called first, but you won't see that in pdb because it doesn't
involve any Python code. Since strings don't know how to compare
themselves with uuids, it will return NotImplemented and the
interpreter will then call uuid's method.

--
Greg

Re: Seeking deeper understanding of python equality (==)

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

 copy mid

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

 copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: jonathan...@guildeducation.com (Jonathan Kaczynski)
Newsgroups: comp.lang.python
Subject: Re: Seeking deeper understanding of python equality (==)
Date: Fri, 13 May 2022 20:23:36 -0400
Lines: 60
Message-ID: <mailman.408.1652492848.20749.python-list@python.org>
References: <CAMA-GnV5+Z-Q7vo+G2x32ytjiVsuCLGxR2NX=BAQPrWGhOHH4Q@mail.gmail.com>
<mailman.324.1651857051.20749.python-list@python.org>
<jdm3i4F46vmU1@mid.individual.net>
<CAMA-GnUX4GOE0gy77+uBOUbx9=HBV4MeM5BNeLTDNnozjkdEGA@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de ZzisYxTUyxK2kWomlJURnAED/ZC/h/R6T6fwzQtH+k0g==
Return-Path: <jonathan.kaczynski@guildeducation.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=guildeducation.com header.i=@guildeducation.com
header.b=I/ZTlAO8; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.036
X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; 'looks': 0.02; '2022':
0.05; 'subject:python': 0.06; 'received:205': 0.07; 'cc:addr
:python-list': 0.09; 'operators': 0.09; 'originally': 0.09;
'responses': 0.09; '&gt;': 0.14; 'cc:no real name:2**0': 0.14;
'url:mailman': 0.15; 'greg': 0.16; 'interpreter': 0.16; 'middle,':
0.16; 'received:205.220': 0.16; 'responses,': 0.16; 'url:edit#':
0.16; 'wrote:': 0.16; 'python': 0.16; 'code.': 0.17;
'cc:addr:python.org': 0.20; "i've": 0.22; 'exception': 0.22;
'first,': 0.22; 'fri,': 0.22; 'returns': 0.22; 'code': 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;
'received:pphosted.com': 0.26; 'function': 0.27;
'received:209.85.210': 0.29; 'takes': 0.31; 'am,': 0.31; 'url-
ip:188/8': 0.31; 'think': 0.32; "doesn't": 0.32; 'assume': 0.32;
'attach': 0.32; 'here,': 0.32; 'subject:skip:u 10': 0.32;
'url:document': 0.32; 'message-id:@mail.gmail.com': 0.32; 'but':
0.32; "i'm": 0.33; 'there': 0.33; 'able': 0.34; 'same': 0.34;
'header:In-Reply-To:1': 0.34; 'received:google.com': 0.34;
'also,': 0.36; 'pdf': 0.36; 'source': 0.36; 'change': 0.36;
'missing': 0.37; 'received:filterd': 0.37; 'received:pps.filterd':
0.37; 'received:209.85': 0.37; 'class': 0.37; 'received:209':
0.39; 'methods': 0.39; 'still': 0.40; 'explain': 0.40; 'want':
0.40; 'method': 0.61; 'skip:o 10': 0.61; 'skip:h 10': 0.61;
'url:d': 0.62; 'here': 0.62; 'feel': 0.63; 'copy': 0.63;
'research': 0.64; 'involve': 0.64; 'thus': 0.64; 'your': 0.64;
'content-type:application/pdf': 0.66; 'generally': 0.67; 'right':
0.68; 'content-type:multipart/mixed': 0.68; 'filename:fname
piece:pdf': 0.68; 'compare': 0.69; 'relate': 0.69; 'took': 0.69;
'pieces': 0.70; 'left': 0.83; 'happens': 0.84; 'steps.': 0.84;
'strings': 0.84; 'subject:Seeking': 0.84; 'summarized': 0.84;
'for\xc2\xa0your': 0.91; 'priority.': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=guildeducation.com; s=google;
h=mime-version:references:in-reply-to:from:date:message-id:subject:to
:cc; bh=DTRFURNND0Ppe2m2wYg3Uy9bXXOrJOrHia3s55N0S/Y=;
b=I/ZTlAO8vAS8TZMoq+YOItP2Wq4tPQk23GPMGGmfoc0RmhJM9vENluEQIPrve5rSfP
wrtUUpkpBFKj4JODff7l2M/osRXubnDKzh0FXng1BA/MckKej/M2uPybq3ouX2TraMoY
I1qiiOFm/wtFD+M9e2DkBJMUFUT9mwKV9U+wlHHY9xoz/lxPMeMopAKMl7Cx/7+4Afri
SAQkE1T1tQpavvlVnOabTz2vOHEMPEJSsys+dIShVT8pVEJxt5rsYAyIsSJ2jXB7/M6A
ySwVbJQUC9h2K8kBiU8/fe9knEd63IAtsr3+IytUjnswwSCbplkqjB+WRjv7dgIkaGB2
eFyQ==
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=DTRFURNND0Ppe2m2wYg3Uy9bXXOrJOrHia3s55N0S/Y=;
b=NObOazikKeKWtiPl0S0OxjfUsFD9nEtG9bWiOiYNijnS/vFDlIaJuuFG6qBTBlfyM8
BUsvgvfRM3PqTdzGdZMBF1wY1lkiIYjW65rRFiZL84DNtNjSLC3bXvz1PexEKLvfqcF8
Znvzev4FGLYAMtxC8uIr5xyzWEtUEMTOb5Erf/jQxXKq5pRmL9vR2Xl9p96dgD0Svr1y
Rb8rZLNAhqMREydlzRVPTHvqMqSwtScFrGt4QRLBw+oUQwvGCMUMpvNhpbBTWM+XWhB9
MufHCXB53WjkTOQpmxMXl8iPgdNZHo3FWhFTA4wXryvnStUfXAoEcGtcyEe9bNyOOmkR
Rdxw==
X-Gm-Message-State: AOAM531iEVFMqoQgRUMuwT5rkMXHEiNWjrOVqyN1ZCoRrqbIdzhEH0tH
KLSBB4aAYTSagCsr1G8rnLxHyPAsZja9LdGLIt51q3RpNhCr0rKY3hoKdvHeKGvUz2+2pKnzMWl
FuFBM7cT1HcVJG/XFQexqv59ESztAtVQ/
X-Received: by 2002:a05:6870:2115:b0:e6:6ee9:628b with SMTP id
f21-20020a056870211500b000e66ee9628bmr8969789oae.1.1652487828732;
Fri, 13 May 2022 17:23:48 -0700 (PDT)
X-Google-Smtp-Source: ABdhPJxjDBuDrROqFyFHMwYhHcXDMKuzym5sL5JfGC4IzwD/VyXK460TGxgWvClq3s72SWfQkXkW7VCZz8ttobr84F4=
X-Received: by 2002:a05:6870:2115:b0:e6:6ee9:628b with SMTP id
f21-20020a056870211500b000e66ee9628bmr8969765oae.1.1652487827568; Fri, 13 May
2022 17:23:47 -0700 (PDT)
In-Reply-To: <jdm3i4F46vmU1@mid.individual.net>
X-Proofpoint-GUID: I0xjb8qpac2zu88loVyQd9Rt6zanRWce
X-Proofpoint-ORIG-GUID: I0xjb8qpac2zu88loVyQd9Rt6zanRWce
X-Proofpoint-Virus-Version: vendor=baseguard
engine=ICAP:2.0.205,Aquarius:18.0.858,Hydra:6.0.486,FMLib:17.11.64.514
definitions=2022-05-13_11,2022-05-13_01,2022-02-23_01
X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0
adultscore=0 impostorscore=0
suspectscore=0 spamscore=0 lowpriorityscore=0 priorityscore=1501
mlxscore=0 phishscore=0 clxscore=1015 mlxlogscore=999 malwarescore=0
bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1
engine=8.12.0-2202240000 definitions=main-2205140000
X-Mailman-Approved-At: Fri, 13 May 2022 21:47:26 -0400
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
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: <CAMA-GnUX4GOE0gy77+uBOUbx9=HBV4MeM5BNeLTDNnozjkdEGA@mail.gmail.com>
X-Mailman-Original-References: <CAMA-GnV5+Z-Q7vo+G2x32ytjiVsuCLGxR2NX=BAQPrWGhOHH4Q@mail.gmail.com>
<mailman.324.1651857051.20749.python-list@python.org>
<jdm3i4F46vmU1@mid.individual.net>
 by: Jonathan Kaczynski - Sat, 14 May 2022 00:23 UTC

Thank you for your responses, Sam and Greg.

The do_richcompare function is where my research originally took me, but I
feel like I'm still missing some pieces to the puzzle.

Here is my updated research since you posted your responses (I'll attach a
pdf copy too):
https://docs.google.com/document/d/10zgOMetEQtZCiYFnSS90pDnNZD7I_-MFohSy83pOieA/edit#
The summary section, in the middle, is where I've summarized my reading of
the source code.

Greg, your response here,

> Generally what happens with infix operators is that the interpreter
> first looks for a dunder method on the left operand. If that method
> doesn't exist or returns NotImplemented, it then looks for a dunder
> method on the right operand.

reads like the contents of the do_richcompare function.

What I think I'm missing is how do the dunder methods relate to
the tp_richcompare function?

Thank you,
Jonathan

On Fri, May 6, 2022 at 11:55 PM Greg Ewing <greg.ewing@canterbury.ac.nz>
wrote:

> On 7/05/22 12:22 am, Jonathan Kaczynski wrote:
> > Stepping through the code with gdb, we see it jump from the compare
> > operator to the dunder-eq method on the UUID object. What I want to be
> able
> > to do is explain the in-between steps.
>
> Generally what happens with infix operators is that the interpreter
> first looks for a dunder method on the left operand. If that method
> doesn't exist or returns NotImplemented, it then looks for a dunder
> method on the right operand.
>
> There is an exception if the right operand is a subclass of the
> left operand -- in that case the right operand's dunder method
> takes precedence.
>
> > Also, if you change `x == y` to `y
> > == x`, you still see the same behavior, which I assume has to do with
> > dunder-eq being defined on the UUID class and thus given priority.
>
> No, in that case the conparison method of str will be getting
> called first, but you won't see that in pdb because it doesn't
> involve any Python code. Since strings don't know how to compare
> themselves with uuids, it will return NotImplemented and the
> interpreter will then call uuid's method.
>
> --
> Greg
> --
> https://mail.python.org/mailman/listinfo/python-list
>

Re: Seeking deeper understanding of python equality (==)

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

 copy mid

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

 copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: jonathan...@guildeducation.com (Jonathan Kaczynski)
Newsgroups: comp.lang.python
Subject: Re: Seeking deeper understanding of python equality (==)
Date: Sat, 14 May 2022 08:33:55 -0400
Lines: 79
Message-ID: <mailman.412.1652532099.20749.python-list@python.org>
References: <CAMA-GnV5+Z-Q7vo+G2x32ytjiVsuCLGxR2NX=BAQPrWGhOHH4Q@mail.gmail.com>
<mailman.324.1651857051.20749.python-list@python.org>
<jdm3i4F46vmU1@mid.individual.net>
<CAMA-GnUX4GOE0gy77+uBOUbx9=HBV4MeM5BNeLTDNnozjkdEGA@mail.gmail.com>
<CAMA-GnWMeYz8=nqL5OMn9cd6m40F1SQdtMXwXW2eX+T7JzkHBg@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de Jc0Ow4Jfk+zQLKbo4cJ4/Q8n9LSI9xdVo4+8JvOnzQNg==
Return-Path: <jonathan.kaczynski@guildeducation.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=guildeducation.com header.i=@guildeducation.com
header.b=O+FbXXYI; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.013
X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'looks': 0.02; '(e.g.':
0.05; '2022': 0.05; 'subject:python': 0.06; 'class.': 0.07;
'explicitly': 0.07; 'received:205': 0.07; 'cc:addr:python-list':
0.09; 'operators': 0.09; 'originally': 0.09; 'responses': 0.09;
'&gt;': 0.14; 'cc:no real name:2**0': 0.14; 'url:mailman': 0.15;
'cpython,': 0.16; 'greg': 0.16; 'interpreter': 0.16; 'middle,':
0.16; 'received:205.220': 0.16; 'responses,': 0.16; 'searches,':
0.16; 'skip:. 20': 0.16; 'slot': 0.16; 'url:edit#': 0.16;
'wrote:': 0.16; 'python': 0.16; 'code.': 0.17;
'cc:addr:python.org': 0.20; "i've": 0.22; 'exception': 0.22;
'first,': 0.22; 'fri,': 0.22; 'returns': 0.22; 'skip:& 40': 0.22;
'code': 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; 'received:pphosted.com': 0.26;
'function': 0.27; 'takes': 0.31; 'am,': 0.31; 'comment': 0.31;
'received:209.85.160': 0.31; 'url-ip:188/8': 0.31; 'wondering':
0.31; 'think': 0.32; "doesn't": 0.32; '13,': 0.32; 'assume': 0.32;
'attach': 0.32; 'here,': 0.32; 'subject:skip:u 10': 0.32;
'url:document': 0.32; 'message-id:@mail.gmail.com': 0.32; 'but':
0.32; "i'm": 0.33; 'there': 0.33; 'able': 0.34; 'same': 0.34;
'header:In-Reply-To:1': 0.34; 'received:google.com': 0.34;
'trying': 0.35; 'also,': 0.36; 'pdf': 0.36; 'source': 0.36;
'change': 0.36; 'those': 0.36; 'missing': 0.37;
'received:filterd': 0.37; 'received:pps.filterd': 0.37;
'received:209.85': 0.37; 'class': 0.37; '8bit%:14': 0.38;
'received:209': 0.39; 'methods': 0.39; 'still': 0.40; 'explain':
0.40; 'want': 0.40; 'method': 0.61; 'url-ip:142.250.179/24': 0.61;
'skip:o 10': 0.61; 'skip:h 10': 0.61; 'url:d': 0.62; 'here': 0.62;
'feel': 0.63; 'copy': 0.63; '8bit%:9': 0.64; 'research': 0.64;
'involve': 0.64; 'thus': 0.64; 'your': 0.64; 'came': 0.65; 'skip:t
20': 0.66; 'generally': 0.67; 'right': 0.68; 'compare': 0.69;
'relate': 0.69; 'took': 0.69; 'pieces': 0.70; '8bit%:6': 0.71;
'left': 0.83; 'happens': 0.84; 'preceding': 0.84; 'says,': 0.84;
'steps.': 0.84; 'strings': 0.84; 'subject:Seeking': 0.84;
'summarized': 0.84; 'for\xc2\xa0your': 0.91; 'priority.': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=guildeducation.com; s=google;
h=mime-version:references:in-reply-to:from:date:message-id:subject:to
:cc; bh=HvSh9miW41/TwB8zrVTiuxkc7HCyiQEg0RRP7mK4M9k=;
b=O+FbXXYIu2jO/ae6tCmp+qZCJlo6zRfP9N6NHmVjkJfHSKCUiIp5wofSQ+NXfhjq1K
3PXyD499w/vZH6cIkBns7hePe/I9zf7t+j5Z2y+g+K96627fQIXTQLY24yvgnC7m1Ysa
wJE8iAyvVl0cbe6CTE8Bv0d1yUZbHLQx1tOXWqVMTkHDRf2nq3BO7BqzPI9vLU/TPjTI
CGb2hQWGYiz128Slqme4SfWTsK9agG20Zqh5Wz/pIe/vpMyqvDrT37cM5G8yLNokoWY/
V+yXkkAKrReNfBjzSqf1DYBsKsacRCHzzmgx2C4SY2ZxwtjcCreq4GzdpRG7jfNqF2dM
wCMw==
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=HvSh9miW41/TwB8zrVTiuxkc7HCyiQEg0RRP7mK4M9k=;
b=MADPuJu5+wqvkW9kXMRouBcH826QKutWIAVnYsKMy5tyOzxVJPe7s7msozo9tKLoKQ
05n8bfa2khw0ZfpTax76QOCjhnHhLwjabMTzA9vzXRJyGOupfB7VoXmNqX8zIoUJJxis
fnmcaFEHY/tXV4pUea2qgxBbdagTalGudLV1j2WRPACUFUowegCZopgu487pqbOI1e1+
/PzhsOGiyPT36QQDJkZK4L6mU0zNunkJ6bYbuPvjdWkBDs7q9PBTmJcvwctbTrcEDuSt
ohpFzhABXa2VEVp7nzCoGpK5vOwkv03qp/q0GLqpvAiwqXdcv7vNCc8c7VsNVXU1KbMa
c8Ww==
X-Gm-Message-State: AOAM533hnciGkCNQgPCcV71tFYfpY4RHMlRte52Ql/AqxtN2a+PnWJkf
9G2xQrcH9PpjFPxARHbPbGeKuvhBcWkJqfBrZcpMsJWFtv++jgkUbGjOkGpKBIZx3nz84/pdiRy
eNRTLRdcmwL/pPpwpNOBsKhwHaqbIrRcC
X-Received: by 2002:a9d:eec:0:b0:606:bc42:9b3e with SMTP id
99-20020a9d0eec000000b00606bc429b3emr3440994otj.105.1652531646967;
Sat, 14 May 2022 05:34:06 -0700 (PDT)
X-Google-Smtp-Source: ABdhPJzwCVeWO1l1je+vA57JjpsqWik+lAXSa5z50EQrkdGCZTBa1AUIWIds8I92sA8k982O/gMjqVfg1db7AbZjYZw=
X-Received: by 2002:a9d:eec:0:b0:606:bc42:9b3e with SMTP id
99-20020a9d0eec000000b00606bc429b3emr3440984otj.105.1652531646652; Sat, 14
May 2022 05:34:06 -0700 (PDT)
In-Reply-To: <CAMA-GnUX4GOE0gy77+uBOUbx9=HBV4MeM5BNeLTDNnozjkdEGA@mail.gmail.com>
X-Proofpoint-ORIG-GUID: d54ozKorDPSiOfcHuMGRym2jMHYmR4UV
X-Proofpoint-GUID: d54ozKorDPSiOfcHuMGRym2jMHYmR4UV
X-Proofpoint-Virus-Version: vendor=baseguard
engine=ICAP:2.0.205,Aquarius:18.0.858,Hydra:6.0.486,FMLib:17.11.64.514
definitions=2022-05-14_02,2022-05-13_01,2022-02-23_01
X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0
lowpriorityscore=0
spamscore=0 suspectscore=0 adultscore=0 priorityscore=1501 impostorscore=0
bulkscore=0 mlxlogscore=952 phishscore=0 malwarescore=0 clxscore=1015
mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1
engine=8.12.0-2202240000 definitions=main-2205140067
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
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: <CAMA-GnWMeYz8=nqL5OMn9cd6m40F1SQdtMXwXW2eX+T7JzkHBg@mail.gmail.com>
X-Mailman-Original-References: <CAMA-GnV5+Z-Q7vo+G2x32ytjiVsuCLGxR2NX=BAQPrWGhOHH4Q@mail.gmail.com>
<mailman.324.1651857051.20749.python-list@python.org>
<jdm3i4F46vmU1@mid.individual.net>
<CAMA-GnUX4GOE0gy77+uBOUbx9=HBV4MeM5BNeLTDNnozjkdEGA@mail.gmail.com>
 by: Jonathan Kaczynski - Sat, 14 May 2022 12:33 UTC

Trying some new searches, I came across slotdefs in ./Objects/typeobject.c,
and those are used in the resolve_slotdups function.

The comment preceding the function says, "Note that multiple names may map
to the same slot (e.g. __eq__, __ne__ etc. all map to tp_richcompare)".

So, I'm still wondering how Py_TYPE(v)->tp_richcompare resolves to __eq__
on a user-defined class. Conversely, my understanding is, for a type
defined in cpython, like str, there is usually an explicitly
defined tp_richcompare function.

Thank you,
Jonathan

On Fri, May 13, 2022 at 8:23 PM Jonathan Kaczynski <
jonathan.kaczynski@guildeducation.com> wrote:

> Thank you for your responses, Sam and Greg.
>
> The do_richcompare function is where my research originally took me, but I
> feel like I'm still missing some pieces to the puzzle.
>
> Here is my updated research since you posted your responses (I'll attach a
> pdf copy too):
> https://docs.google.com/document/d/10zgOMetEQtZCiYFnSS90pDnNZD7I_-MFohSy83pOieA/edit#
> The summary section, in the middle, is where I've summarized my reading of
> the source code.
>
> Greg, your response here,
>
>> Generally what happens with infix operators is that the interpreter
>> first looks for a dunder method on the left operand. If that method
>> doesn't exist or returns NotImplemented, it then looks for a dunder
>> method on the right operand.
>
> reads like the contents of the do_richcompare function.
>
> What I think I'm missing is how do the dunder methods relate to
> the tp_richcompare function?
>
> Thank you,
> Jonathan
>
>
> On Fri, May 6, 2022 at 11:55 PM Greg Ewing <greg.ewing@canterbury.ac.nz>
> wrote:
>
>> On 7/05/22 12:22 am, Jonathan Kaczynski wrote:
>> > Stepping through the code with gdb, we see it jump from the compare
>> > operator to the dunder-eq method on the UUID object. What I want to be
>> able
>> > to do is explain the in-between steps.
>>
>> Generally what happens with infix operators is that the interpreter
>> first looks for a dunder method on the left operand. If that method
>> doesn't exist or returns NotImplemented, it then looks for a dunder
>> method on the right operand.
>>
>> There is an exception if the right operand is a subclass of the
>> left operand -- in that case the right operand's dunder method
>> takes precedence.
>>
>> > Also, if you change `x == y` to `y
>> > == x`, you still see the same behavior, which I assume has to do with
>> > dunder-eq being defined on the UUID class and thus given priority.
>>
>> No, in that case the conparison method of str will be getting
>> called first, but you won't see that in pdb because it doesn't
>> involve any Python code. Since strings don't know how to compare
>> themselves with uuids, it will return NotImplemented and the
>> interpreter will then call uuid's method.
>>
>> --
>> Greg
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
>

Re: Seeking deeper understanding of python equality (==)

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

 copy mid

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

 copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: eryk...@gmail.com (Eryk Sun)
Newsgroups: comp.lang.python
Subject: Re: Seeking deeper understanding of python equality (==)
Date: Sat, 14 May 2022 12:51:51 -0500
Lines: 56
Message-ID: <mailman.413.1652550715.20749.python-list@python.org>
References: <CAMA-GnV5+Z-Q7vo+G2x32ytjiVsuCLGxR2NX=BAQPrWGhOHH4Q@mail.gmail.com>
<mailman.324.1651857051.20749.python-list@python.org>
<jdm3i4F46vmU1@mid.individual.net>
<CAMA-GnUX4GOE0gy77+uBOUbx9=HBV4MeM5BNeLTDNnozjkdEGA@mail.gmail.com>
<CAMA-GnWMeYz8=nqL5OMn9cd6m40F1SQdtMXwXW2eX+T7JzkHBg@mail.gmail.com>
<CACL+1at76jONrOCuHP-Xa_wS_L422LsijLfODnFZVxtsnLpPPw@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de 4InpyDYUKKf0CVZPtt/U7w0bEh87IaaUXydC1iQ5hCYg==
Return-Path: <eryksun@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=bpitKdDN;
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.114.3/32':
0.03; 'url-ip:140.82.114/24': 0.03; 'url-ip:140.82/16': 0.03;
'(e.g.': 0.05; 'subject:python': 0.06; 'class.': 0.07;
'explicitly': 0.07; 'cc:addr:python-list': 0.09; 'int': 0.09;
'skip:* 30': 0.09; 'cc:no real name:2**0': 0.14; 'url:github':
0.14; 'url-ip:140/8': 0.15; 'classes,': 0.16; 'cpython,': 0.16;
'debugger': 0.16; 'examine': 0.16; 'int)': 0.16; 'lambda': 0.16;
'objects:': 0.16; 'url:cpython': 0.16; 'url:v3': 0.16; 'wrote:':
0.16; 'uses': 0.19; 'cc:addr:python.org': 0.20; 'exception': 0.22;
'code': 0.23; 'skip:p 30': 0.23; 'cc:2**0': 0.25; 'object': 0.26;
'>>>': 0.28; 'default': 0.31; 'wondering': 0.31; "doesn't": 0.32;
'subject:skip:u 10': 0.32; 'message-id:@mail.gmail.com': 0.32;
"i'm": 0.33; 'there': 0.33; 'header:In-Reply-To:1': 0.34;
'received:google.com': 0.34; 'from:addr:gmail.com': 0.35;
'source': 0.36; "skip:' 10": 0.37; 'using': 0.37; "it's": 0.37;
'received:209.85': 0.37; 'class': 0.37; 'received:209': 0.39;
'two': 0.39; 'break': 0.39; 'still': 0.40; 'skip:* 20': 0.62;
'skip:o 20': 0.63; 'pass': 0.64; 'url:4': 0.64; 'attached': 0.68;
'skip:k 20': 0.78; '(first': 0.84; 'subject:Seeking': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=mime-version:in-reply-to:references:from:date:message-id:subject:to
:cc; bh=XiBDstfzgV6qwV2VBG4vaSMKkBjbPu90RO3uJaox2MU=;
b=bpitKdDNTGmwwInpNyfJbtp4Wk1e/XqGcxmRxSkCnfxHyvMiYNiWcIZIQ5HmSd6R2f
iskjml5vazEsXW4jW4Qt+eiUtMeRLNhXKKA6oXzd3QXgomUvqQAi+Eo3obF9TqPOP8yU
ebDcJcAz/dcyPaUEX9qCACjLSKxrYwwDwAtiE2Zr5T89pEkEsrlVJFWLsPh8c6UqCh6X
2x75mKerYjnuFaAILO77HtixUbt6mFPF0RODHzCYvU782NQyGDrJgJM141Xwj3cVQGux
alTcSC/3ECCskFOkf7/DvChkZA/+lY4PTIEdXPfgiPA/JhCGE556kVCqN4yp2MW1NRUt
II+Q==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=x-gm-message-state:mime-version:in-reply-to:references:from:date
:message-id:subject:to:cc;
bh=XiBDstfzgV6qwV2VBG4vaSMKkBjbPu90RO3uJaox2MU=;
b=nu24B3FO9yKwcn+8Fs5mDpiLceNXeufHcNb6O8Ent7kj76Ij9pukI1apkZ+UZ4UEVL
U72ntTxjRdbXRgjMYt5v1Bjcuqo7rQe6n4Gdkvwt34PRg3fWgWgPw9eB9Xsnj/Si46It
UUvCOdr6CUvNANUNRLqNCjxMV7WRUG3wHX/cacJAS+XsC4UZlMeuGM3CZx8fn4nDGx25
Ttuq6WL0OUtQghIkwxzoGZo4bT6GPaB+5N9gG1Pyrp1aVnXfCtfLc/LkzjyUxUhujqVl
5pRAguw4hfhnWX0WFLSEZFR2OVyx/WALEy+Zal7/0ORrqw4qD9DR7Qb/X/mu9zkpdrhT
YeBQ==
X-Gm-Message-State: AOAM530HGgx91IvklZYg7Vp3UQOsKKp2MB/XUky7io6m/bV0saTfiMnf
W482b1aMb4i+ZC0AoTLjabSZOQJ1HgJD3eqqV3mX/IeO
X-Google-Smtp-Source: ABdhPJxAg9JUUJogDtM6u/VwD7ddrsS8qmJ0MBUqfbgACeWqzMkOfp8ESLS0oy46IWeY0+vcVu5FkQ7Xe41m3uL1mnU=
X-Received: by 2002:a65:5c48:0:b0:382:2c7:28e9 with SMTP id
v8-20020a655c48000000b0038202c728e9mr8924448pgr.472.1652550712297; Sat, 14
May 2022 10:51:52 -0700 (PDT)
In-Reply-To: <CAMA-GnWMeYz8=nqL5OMn9cd6m40F1SQdtMXwXW2eX+T7JzkHBg@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: <CACL+1at76jONrOCuHP-Xa_wS_L422LsijLfODnFZVxtsnLpPPw@mail.gmail.com>
X-Mailman-Original-References: <CAMA-GnV5+Z-Q7vo+G2x32ytjiVsuCLGxR2NX=BAQPrWGhOHH4Q@mail.gmail.com>
<mailman.324.1651857051.20749.python-list@python.org>
<jdm3i4F46vmU1@mid.individual.net>
<CAMA-GnUX4GOE0gy77+uBOUbx9=HBV4MeM5BNeLTDNnozjkdEGA@mail.gmail.com>
<CAMA-GnWMeYz8=nqL5OMn9cd6m40F1SQdtMXwXW2eX+T7JzkHBg@mail.gmail.com>
 by: Eryk Sun - Sat, 14 May 2022 17:51 UTC

On 5/14/22, Jonathan Kaczynski <jonathan.kaczynski@guildeducation.com> wrote:
>
> So, I'm still wondering how Py_TYPE(v)->tp_richcompare resolves to __eq__
> on a user-defined class. Conversely, my understanding is, for a type
> defined in cpython, like str, there is usually an explicitly
> defined tp_richcompare function.

Sometimes it's simplest to directly examine an object using a native
debugger (e.g. gdb in Linux; cdb/windbg in Windows).

With a debugger attached to the interpreter, create two classes, one
that doesn't override __eq__() and one that does:

>>> class C:
... pass
...
>>> class D:
... __eq__ = lambda s, o: False
...

In CPython, the id() of an object is its address in memory:

>>> hex(id(C))
'0x2806a705790'
>>> hex(id(D))
'0x2806a6bbfe0'

Break into the attached debugger to examine the class objects:

>>> kernel32.DebugBreak()

(1968.1958): Break instruction exception - code 80000003 (first chance)
KERNELBASE!wil::details::DebugBreak+0x2:
00007ffd`8818fd12 cc int 3

Class C uses the default object_richcompare():

0:000> ?? *((python310!PyTypeObject *)0x2806a705790)->tp_richcompare
<function> 0x00007ffd`55cac288
_object* python310!object_richcompare+0(
_object*,
_object*,
int)

Class D uses slot_tp_richcompare():

0:000> ?? *((python310!PyTypeObject *)0x2806a6bbfe0)->tp_richcompare
<function> 0x00007ffd`55cdef1c
_object* python310!slot_tp_richcompare+0(
_object*,
_object*,
int)

Source code of slot_tp_richcompare():

https://github.com/python/cpython/blob/v3.10.4/Objects/typeobject.c#L7610-L7626

Re: Seeking deeper understanding of python equality (==)

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

 copy mid

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

 copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: jonathan...@guildeducation.com (Jonathan Kaczynski)
Newsgroups: comp.lang.python
Subject: Re: Seeking deeper understanding of python equality (==)
Date: Tue, 17 May 2022 07:15:24 -0400
Lines: 90
Message-ID: <mailman.430.1652786146.20749.python-list@python.org>
References: <CAMA-GnV5+Z-Q7vo+G2x32ytjiVsuCLGxR2NX=BAQPrWGhOHH4Q@mail.gmail.com>
<mailman.324.1651857051.20749.python-list@python.org>
<jdm3i4F46vmU1@mid.individual.net>
<CAMA-GnUX4GOE0gy77+uBOUbx9=HBV4MeM5BNeLTDNnozjkdEGA@mail.gmail.com>
<CAMA-GnWMeYz8=nqL5OMn9cd6m40F1SQdtMXwXW2eX+T7JzkHBg@mail.gmail.com>
<CACL+1at76jONrOCuHP-Xa_wS_L422LsijLfODnFZVxtsnLpPPw@mail.gmail.com>
<CAMA-GnXNOLEygbva86DRh25WDDJREhkzbfYHSZvHJ322cLF+MA@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de LoLB8FtwllKecxLqNq6fGgV34LriMZnoHz3FO+StgB7w==
Return-Path: <jonathan.kaczynski@guildeducation.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=guildeducation.com header.i=@guildeducation.com
header.b=cdGNlfC0; 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; 'thread': 0.05; 'subject:python':
0.06; '&gt;&gt;&gt;': 0.07; 'class.': 0.07; 'explicitly': 0.07;
'performing': 0.07; 'received:205': 0.07; 'cc:addr:python-list':
0.09; 'int': 0.09; 'library,': 0.09; 'linux': 0.09; 'skip:* 30':
0.09; 'steps': 0.11; '&gt;': 0.14; 'cc:no real name:2**0': 0.14;
'url:github': 0.14; 'url-ip:140/8': 0.15; 'classes,': 0.16;
'cpython': 0.16; 'cpython,': 0.16; 'debugger': 0.16; 'drops':
0.16; 'eryk': 0.16; 'examine': 0.16; 'functions.': 0.16; 'int)':
0.16; 'lambda': 0.16; 'objects:': 0.16; 'received:205.220': 0.16;
'responses,': 0.16; 'url:cpython': 0.16; 'url:v3': 0.16;
'windows.': 0.16; 'wrote:': 0.16; 'python': 0.16; 'uses': 0.19;
'figure': 0.19; 'cc:addr:python.org': 0.20; 'exception': 0.22;
'mac': 0.22; 'sat,': 0.22; 'skip:& 40': 0.22; "what's": 0.22;
'code': 0.23; 'goal': 0.23; 'skip:p 30': 0.23; 'idea': 0.24;
'cc:2**0': 0.25; 'seems': 0.26; 'object': 0.26; 'perform': 0.26;
'received:pphosted.com': 0.26; '>>>': 0.28; 'attempt': 0.31;
'default': 0.31; 'putting': 0.31; 'received:209.85.160': 0.31;
'wondering': 0.31; "doesn't": 0.32; 'assume': 0.32; 'signal':
0.32; 'subject:skip:u 10': 0.32; 'message-id:@mail.gmail.com':
0.32; 'but': 0.32; "i'm": 0.33; "i'll": 0.33; 'there': 0.33;
'script': 0.33; 'able': 0.34; 'header:In-Reply-To:1': 0.34;
'received:google.com': 0.34; 'running': 0.34; 'handling': 0.35;
'source': 0.36; 'received:filterd': 0.37; 'received:pps.filterd':
0.37; "skip:' 10": 0.37; 'using': 0.37; "it's": 0.37;
'received:209.85': 0.37; 'class': 0.37; 'thanks': 0.38;
'received:209': 0.39; 'two': 0.39; 'break': 0.39; 'still': 0.40;
"couldn't": 0.40; 'try': 0.40; 'should': 0.40; 'skip:i 20': 0.62;
'skip:* 20': 0.62; 'skip:o 20': 0.63; 'great': 0.63; 'pass': 0.64;
'url:4': 0.64; 'your': 0.64; 'attached': 0.68; 'drop': 0.69;
'functional': 0.69; 'reach': 0.69; 'front': 0.70; 'skip:k 20':
0.78; 'left': 0.83; '(first': 0.84; 'dozen': 0.84;
'subject:Seeking': 0.84; 'question?': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=guildeducation.com; s=google;
h=mime-version:references:in-reply-to:from:date:message-id:subject:to
:cc; bh=W0tEIt7ad4MycA7Du9wgG01SVRrFdpaQ4BO27DvyZsQ=;
b=cdGNlfC0x1+NVjG1gPR/8Il6NzBMsmlC3hYl0iZMDIbd6uiMvhnQVG1nq2rilhIjkf
VbLyZG6kChKXJ4oZuXZVOXoLB5tmWAS0ewZWeqtlrNIybhAzMEEHCJ2n0eDdqP7qqdf2
7y0qErKftckXpLWGkXzaVX/Lv2zJmrTtHflsyw1QuK6IKwimq+7PKN/xjknaVTRMQ5US
OKf+PL0fH/csnJaO2TtXh0X8qBRBfuDnZnaVZ7idMi1QHLz7BtHiqMydC7z4/JVeoMDu
vc5XGZabG9ZbO7OuYfWauLhHncsvYRjr6qvVX9nmwJyI1yxupQzzxAwILYPajHlx/aW2
az3w==
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=W0tEIt7ad4MycA7Du9wgG01SVRrFdpaQ4BO27DvyZsQ=;
b=k3J+9auYCuWv3Z9wvjLmLX1DNbm4/stzI+KJRW+jug+BdrIST5eAwcQy/TtifMqUaI
FJoN3+2CEWsTMiXVuUZIqARdsnHBdaL3CIYi1lSSA6tPriSDQq3U7Fb3SJiY4eDb6ckI
TO0fVHjeTHlq4pIYK4Zvggu7XAjJI2CSheq4O1R8gjgRlAJ9Ja6DT6eUfzHe/0ywwYuX
pVUxyGh5PW7saNI5snUOsYzdygYExaKk4K3fkqMIFuppzrFkXEZdyX9Ho9Xcf9ma7Yzl
5EVg/5xkF6Znul69rZIs60BQjWtT+mYjrgkd62oSuMWkEHjDFEn0unDZc5Oc1GeIVeEc
kwPw==
X-Gm-Message-State: AOAM530VE/p4iJ68n3afzm61Lxb2qvdgvZJWAI68l2EgykGa63g+L3Ke
o6FSj+IbloDHSZ57zII/kCnYERe/u1L9C+4zOchIvfLc/KZAbQ27PvKTuQ3L5MlYybJQciFYtAY
H8QlZlEeGQDL/7nbPXs+iyfGqYZLfKZmr
X-Received: by 2002:a05:6870:2115:b0:e6:6ee9:628b with SMTP id
f21-20020a056870211500b000e66ee9628bmr17111501oae.1.1652786136107;
Tue, 17 May 2022 04:15:36 -0700 (PDT)
X-Google-Smtp-Source: ABdhPJyEGpUK+4+Mnfzgk1KhEBHQe0uttGeEZeg06QZShwzwBmco3Elmo98zUJ10Rdj313cdXYVI5eNOx+cJG68E8Wk=
X-Received: by 2002:a05:6870:2115:b0:e6:6ee9:628b with SMTP id
f21-20020a056870211500b000e66ee9628bmr17111483oae.1.1652786135752; Tue, 17
May 2022 04:15:35 -0700 (PDT)
In-Reply-To: <CACL+1at76jONrOCuHP-Xa_wS_L422LsijLfODnFZVxtsnLpPPw@mail.gmail.com>
X-Proofpoint-ORIG-GUID: -RoZqWMhhJKgaPg7eCCXjAS9Y0nCbNBJ
X-Proofpoint-GUID: -RoZqWMhhJKgaPg7eCCXjAS9Y0nCbNBJ
X-Proofpoint-Virus-Version: vendor=baseguard
engine=ICAP:2.0.205,Aquarius:18.0.858,Hydra:6.0.486,FMLib:17.11.64.514
definitions=2022-05-17_02,2022-05-17_01,2022-02-23_01
X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0
lowpriorityscore=0
priorityscore=1501 mlxlogscore=538 clxscore=1015 impostorscore=0
mlxscore=0 phishscore=0 malwarescore=0 adultscore=0 bulkscore=0
suspectscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1
engine=8.12.0-2202240000 definitions=main-2205170069
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
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: <CAMA-GnXNOLEygbva86DRh25WDDJREhkzbfYHSZvHJ322cLF+MA@mail.gmail.com>
X-Mailman-Original-References: <CAMA-GnV5+Z-Q7vo+G2x32ytjiVsuCLGxR2NX=BAQPrWGhOHH4Q@mail.gmail.com>
<mailman.324.1651857051.20749.python-list@python.org>
<jdm3i4F46vmU1@mid.individual.net>
<CAMA-GnUX4GOE0gy77+uBOUbx9=HBV4MeM5BNeLTDNnozjkdEGA@mail.gmail.com>
<CAMA-GnWMeYz8=nqL5OMn9cd6m40F1SQdtMXwXW2eX+T7JzkHBg@mail.gmail.com>
<CACL+1at76jONrOCuHP-Xa_wS_L422LsijLfODnFZVxtsnLpPPw@mail.gmail.com>
 by: Jonathan Kaczynski - Tue, 17 May 2022 11:15 UTC

Thank you for your response Eryk.

I did try using gdb in my original post, but using breakpoint() left me in
the python layer (pdb), not the cpython layer (gdb) and I couldn't figure
out how to drop down.

I see you're using the kernel32 library, so I assume you're on windows. I
only have a mac and getting gdb functional on a mac seems to be onerous (
https://gist.github.com/mike-myers-tob/9a6013124bad7ff074d3297db2c98247),
so I'm performing my testing on a linux container running ubuntu.

The closest equivalent to putting kernel32.DebugBreak() in front of the x
== y line in my test script is os.kill(os.getpid(), signal.SIGTRAP).
Though, this drops me into the signal handling code in the cpython layer,
and after several dozen steps I did not reach the richcompare-related
functions. I'll make another attempt later today.

Thanks to everyone's responses, I have a great idea of what's going on,
now. I appreciate you all.

My goal now is to be able to work with the debugger, like Erik is, so that
next time I am able to perform this investigation in-full. Should I create
a new thread for this question?

Thank you,
Jonathan

On Sat, May 14, 2022 at 1:51 PM Eryk Sun <eryksun@gmail.com> wrote:

> On 5/14/22, Jonathan Kaczynski <jonathan.kaczynski@guildeducation.com>
> wrote:
> >
> > So, I'm still wondering how Py_TYPE(v)->tp_richcompare resolves to __eq__
> > on a user-defined class. Conversely, my understanding is, for a type
> > defined in cpython, like str, there is usually an explicitly
> > defined tp_richcompare function.
>
> Sometimes it's simplest to directly examine an object using a native
> debugger (e.g. gdb in Linux; cdb/windbg in Windows).
>
> With a debugger attached to the interpreter, create two classes, one
> that doesn't override __eq__() and one that does:
>
> >>> class C:
> ... pass
> ...
> >>> class D:
> ... __eq__ = lambda s, o: False
> ...
>
> In CPython, the id() of an object is its address in memory:
>
> >>> hex(id(C))
> '0x2806a705790'
> >>> hex(id(D))
> '0x2806a6bbfe0'
>
> Break into the attached debugger to examine the class objects:
>
> >>> kernel32.DebugBreak()
>
> (1968.1958): Break instruction exception - code 80000003 (first chance)
> KERNELBASE!wil::details::DebugBreak+0x2:
> 00007ffd`8818fd12 cc int 3
>
> Class C uses the default object_richcompare():
>
> 0:000> ?? *((python310!PyTypeObject *)0x2806a705790)->tp_richcompare
> <function> 0x00007ffd`55cac288
> _object* python310!object_richcompare+0(
> _object*,
> _object*,
> int)
>
> Class D uses slot_tp_richcompare():
>
> 0:000> ?? *((python310!PyTypeObject *)0x2806a6bbfe0)->tp_richcompare
> <function> 0x00007ffd`55cdef1c
> _object* python310!slot_tp_richcompare+0(
> _object*,
> _object*,
> int)
>
> Source code of slot_tp_richcompare():
>
>
> https://github.com/python/cpython/blob/v3.10.4/Objects/typeobject.c#L7610-L7626
>

1
server_pubkey.txt

rocksolid light 0.9.7
clearnet tor