python / python/cpython

Code object creation (const interning) can mutate tuples

オープン
#121,954 コメント 0 件 リアクション 0 件 担当者 1 名 GitHub で見る

@encukou がすでに取り組んでいます。

2024年7月18日 から。

type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

In https://github.com/python/cpython/issues/121863#issuecomment-2232849007, Mark suspected something is off in const interning for code objects. Indeed, there is. For example:

# a bit of setup
import sys
interned = sys.intern("foobar")
def f():
    var = "foobar"

# my_tuple = (None, 'foobar'), but with an un-interned string
my_tuple = (None, str(b'foobar', 'ascii'))  

print(id(my_tuple), id(my_tuple[1]))

# pass my_tuple as an input argument for a new code object
f.__code__.replace(co_consts=my_tuple)

# there's now a different object in the tuple!
print(id(my_tuple), id(my_tuple[1]))

I'm working on a change to:

  • create new tuples instead of mutating old ones
  • intern more strings before passing them to PyCode constructors, so that we usually don't need to create new tuples
Linked PRs
  • gh-122180

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。