`pickle.loads` will crash with self-references inside a custom hash function
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 36k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
Bug report
Bug description:
here is a reproduction of the issue:
import pickle
class Foo:
def __init__(self):
self.x: object = {self}
def __hash__(self):
return hash(self.x)
foo = Foo()
print(pickle.loads(pickle.dumps(foo)))
running this will result in the following exception:
Traceback (most recent call last):
File "/home/charles/vyper/foo.py", line 10, in <module>
foo = Foo()
^^^^^
File "/home/charles/vyper/foo.py", line 5, in __init__
self.x: object = {self}
^^^^^^
File "/home/charles/vyper/foo.py", line 8, in __hash__
return hash(self.x)
^^^^^^
AttributeError: 'Foo' object has no attribute 'x'
a workaround to the issue has been described at https://stackoverflow.com/a/44888113. however, i consider this a bug in the cpython implementation, because pickle theoretically handles object cycles (e.g., replacing line 5 with self.x = [self] poses no problem to the unpickler).
i suspect that cpython rehashes all items when reconstructing a dict or set, which makes the issue even more problematic, e.g. if the hash function has any side-effects, they will be executed by the unpickler.
build info:
$ python
Python 3.11.10 (main, Sep 7 2024, 18:35:41) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
CPython versions tested on:
3.11
Operating systems tested on:
Linux
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Báo cáo cung cấp một trình tái hiện tập trung vào pickle.dumps và pickle.loads, nhưng không nêu tệp mã nguồn CPython hay bài kiểm thử nào. Hãy bắt đầu bằng cách chạy nó trên môi trường Python 3.11/Linux đã nêu và lần theo đường đi của quá trình unpickling; để hoàn tất cần có hành vi được thống nhất đối với các hash tùy chỉnh tự tham chiếu, độ bao phủ hồi quy và không có tác dụng phụ ngoài ý muốn của hash.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- backend
- Loại issue
- Lỗi
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Cần làm rõ
- Mức phù hợp với người mới
- 25/100