python / python/cpython

Code object creation (const interning) can mutate tuples

Đang mở
#121,954 0 bình luận 0 reaction 1 người được giao Xem trên GitHub

@encukou đang làm issue này rồi.

Từ ngày 18/7/2024.

type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.