[New-bugs-announce] [issue46361] Small ints aren't always cached properly

Brandt Bucher report at bugs.python.org
Wed Jan 12 17:50:40 EST 2022


New submission from Brandt Bucher <brandtbucher at gmail.com>:

To my surprise, it seems that it's possible to create "small" integers that should live in _PyLong_SMALL_INTS, but don't. Here are two examples I've found:

>>> import decimal
>>> i = int(decimal.Decimal(42))  # Modules/_decimal/_decimal.c:dec_as_long
>>> i
42
>>> i is 42
<stdin>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
False
>>> i = int.from_bytes(bytes([42]))  # Objects/longobject.c:_PyLong_FromByteArray
>>> i
42
>>> i is 42
<stdin>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
False

I'm not entirely sure if this is "allowed" or not, but in any case it seems beneficial to reach into the small ints here (provided it doesn't hurt performance, of course).

I'm testing out simple fixes for both of these now.

----------
assignee: brandtbucher
components: Interpreter Core
messages: 410437
nosy: brandtbucher
priority: normal
severity: normal
status: open
title: Small ints aren't always cached properly
type: behavior
versions: Python 3.11

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46361>
_______________________________________


More information about the New-bugs-announce mailing list