New submission from Nathaniel Smith: Following on from the discussion starting here:
http://thread.gmane.org/gmane.comp.python.devel/150438/focus=150604 Here's a patch to improve __class__ assignment. 1) We remove the HEAPTYPE check from object_set_class, and move it to same_slots_added. This fixes an outright bug: previously it was possible for non-HEAPTYPE types to get passed into same_slots_added (either b/c the loop in compatible_for_assignment ended up with non-HEAPTYPE types, or via __bases__ assignment), and it would then wander off following pointers through random memory. 2) Now that object_set_class can potentially accept non-HEAPTYPE types, adjust the reference counting appropriately. 3) To clarify the logic of compatible_for_assignment, rename equiv_structs to compatible_with_tp_base, tweak accordingly, and add a nice comment explaining the logic. (compatible_for_assignment is equiv_structs's only caller so this is totally safe.) 4) Now that equiv_structs/compatible_with_tp_base has a clearer purpose, also move the tp_dealloc check from compatible_for_assignment into compatible_with_tp_base. In the process, add special-case handling for subclass_dealloc, b/c subclass_dealloc delegates to the parent class tp_dealloc anyway. These changes together make it possible to assign to module instances's __class__ slot, which is useful for reasons described in the above thread. So I added a test for this to check the new code. ---------- components: Interpreter Core files: better-__class__-assignment-v1.patch keywords: patch messages: 232061 nosy: njs priority: normal severity: normal status: open title: Improved handling of __class__ assignment Added file: http://bugs.python.org/file37346/better-__class__-assignment-v1.patch _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue22986> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com