Issue 7532: Extended slicing with classic class behaves strangely (original) (raw)

Created on 2009-12-17 16:54 by flox, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
case_slice.py flox,2009-12-17 16:54 Buggy behaviour of classic classes
issue7532_classic_getitem_v2.diff flox,2009-12-18 00:18 Patch, apply to trunk
issue7532_wontfix_tests.diff flox,2010-01-07 21:30 Patch, apply to trunk
issue7532_bytearray.diff flox,2010-01-09 22:59 Patch, apply to trunk
issue7532_wontfix_tests_py3k.diff flox,2010-01-09 23:09 Patch, apply to py3k
Messages (12)
msg96520 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2009-12-17 16:54
See attached example. The Classic class should behave like the New-style class.
msg96532 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-12-17 20:36
Hmm. This doesn't look like something that's easy to fix without affecting existing correct code; given that the behaviour has been around for a while (I'm not sure exactly how long), and that the issue is gone in 3.x, I suspect it may not be worth trying. Analysis: for classic classes, the ceval loop calls PySequence_GetSlice, which corrects the negative indices by adding the length of the sequence (as described in the docs) and then calls the tp_as_sequence->sq_slice slot on the type. That ends up calling instance_slice (in Objects/classobject.c), which discovers that the class doesn't implement __getslice__ and so passes the adjusted slice on to the __getitem__ method. I'm not sure how this could be changed to get the correct behaviour: PySequence_GetSlice would somehow need to know that it was going to end up calling __getitem__ rather than __getslice__. Raymond, any thoughts?
msg96533 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-12-17 20:42
Issue #974635 looks like the same thing. That issue was closed as a duplicate of issue #723806, though to my eyes #723806 doesn't look quite the same.
msg96536 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2009-12-17 22:03
Mark, Thank you for your analysis. I looked at similar implementation of tp_as_sequence->sq_slice slots in "stringobject.c" (and tuple, list). I've added extra controls before the _PySlice_FromIndices call to let it behave like new-style classes. I have updated the tests. It should be safe for 2.7, no?
msg96542 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2009-12-18 00:18
Patch augmented with extensive tests: * Classic class or New-style class * with or without __getslice__
msg96601 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-12-19 12:03
Interesting solution! While the patch itself looks fine to me, I'm not sure I like this solution much. It's fine to use this trick for list or tuple, but implementing it for all old-style classes at once seems a bit dangerous. With this patch, it seems to me that the rule describing exactly what __getitem__ receives (for an old-style class implementing __getitem__ but not __getslice__) becomes rather complicated, and can no longer be deduced from the documentation. I'd say leave the current behaviour as it is, and remind people that they should be using new-style classes wherever possible.
msg97157 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-01-03 09:57
Closing as won't fix.
msg97376 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-01-07 21:30
I would suggest to keep the tests, even if the bug is closed.
msg97379 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-01-07 21:36
Okay, sounds reasonable. Reopening to consider tests.
msg97467 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-01-09 18:55
Tests applied to trunk in r77391. Are you interested in producing a py3k version of the patch?
msg97475 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-01-09 22:59
Here it is, with some cleaning and simple Bytes/Bytearray tests. And Bytearray tests backported to 2.7.
msg97511 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-01-10 12:01
Thanks! Applied in r77408.
History
Date User Action Args
2022-04-11 14:56:55 admin set github: 51781
2010-01-10 12:01:12 mark.dickinson set status: open -> closedmessages: +
2010-01-09 23:09:15 flox set files: + issue7532_wontfix_tests_py3k.diff
2010-01-09 23:08:53 flox set files: - issue7532_wontfix_tests_py3k.diff
2010-01-09 22:59:48 flox set files: + issue7532_bytearray.diff
2010-01-09 22:59:26 flox set files: + issue7532_wontfix_tests_py3k.diffmessages: +
2010-01-09 18:55:03 mark.dickinson set messages: +
2010-01-07 21:36:24 mark.dickinson set status: closed -> openassignee: mark.dickinsonmessages: +
2010-01-07 21:30:58 flox set files: + issue7532_wontfix_tests.diffmessages: +
2010-01-03 09:57:42 mark.dickinson set status: open -> closedresolution: wont fixmessages: +
2009-12-19 12:03:25 mark.dickinson set messages: +
2009-12-18 00:19:28 flox set files: - issue7532_classic_getitem.diff
2009-12-18 00🔞01 flox set files: + issue7532_classic_getitem_v2.diffmessages: +
2009-12-17 22:03:12 flox set files: + issue7532_classic_getitem.diffkeywords: + patchmessages: +
2009-12-17 20:42:16 mark.dickinson set messages: +
2009-12-17 20:36:50 mark.dickinson set nosy: + rhettinger, mark.dickinsonmessages: +
2009-12-17 16:54:08 flox create