Tuples should be immutable and safe in C, as well as in Python.
Chưa có ai nhận issue này.
- 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ả
Bug report
Bug description:
[Apologies if this sounds a bit like a rant. I'm not blaming anyone. Just because something is the wrong choice now, doesn't mean it wasn't the right choice historically]
Tuples are immutable in Python, but we play all sorts of games in C with tuples, filling them will NULLs, mutating them and reusing them.
We do this in the mistaken belief that it improves performance.
But it doesn't. It makes the code base more complicated and fragile as we need to work around tuples that misbehave and do strange things. Any local performance gain is overwhelmed by slowdowns caused by the extra complexity in tuple code, the garbage collector and a few other places.
So let's fix this.
We need to:
- Provide a new C API
PyTuple_MakePair(). Pairs are by far the most common type of tuple that we play games with. By providing a fast way to create pairs, we can provide an upgrade path for C code that creates tuples in unsafe ways to do so safely and quickly. - Deprecate
PyTuple_New. I don't know when we'll be able to remove it, but we should deprecate it ASAP. - Change
PyTuple_Newto fill the tuple with pointers toNoneinstead ofNULL. This doesn't fix the mutability issue, but it at least means the GC will only see valid objects. (This might break too much code, so we might just have to clearly document that tuples should be fully initialized in one go, before the tuple escapes the function it was created in) - Fix our own code to not use
PyTuple_New()or perform tuple shenanigans. We can't reasonably expect third-party package authors to follow the rules if we don't.
CPython versions tested on:
CPython main branch
Operating systems tested on:
No response
Linked PRs
- gh-127758
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ắt đầu bằng việc xem xét các C API PyTuple_New và PyTuple_MakePair được đề xuất, sau đó kiểm tra PR được liên kết gh-127758 để hiểu công việc đang được thực hiện. Công việc được xem là hoàn tất khi các thay đổi được yêu cầu về tính an toàn của tuple đã được xử lý, bao gồm việc chuyển đổi API, hành vi khởi tạo và loại bỏ việc sử dụng tuple nội bộ không an toàn.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- c, 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 tả rõ ràng
- Mức phù hợp với người mới
- 25/100