This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: PyTuple_GetSlice does not always return a new tuple
Type: behavior Stage: resolved
Components: Documentation Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: docs@python Nosy List: MSeifert, docs@python, rhettinger, xiang.zhang
Priority: normal Keywords:

Created on 2017-03-15 02:40 by MSeifert, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg289632 - (view) Author: Michael Seifert (MSeifert) * Date: 2017-03-15 02:40
The PyTuple_GetSlice documentation says it "Take a slice of the tuple pointed to by p from low to high and return it as a new tuple." [0] However in case the start is <= 0 and the stop is >= tuplesize it doesn't return the promised "new tuple", it just returns the tuplepointer after incrementing it's refcount [1].

The behaviour is fine (it gave me a bit of a headache though), however could a note/warning/sentence be included in the docs mentioning that special case?

[0] https://docs.python.org/3/c-api/tuple.html#c.PyTuple_GetSlice
[1] https://github.com/python/cpython/blob/master/Objects/tupleobject.c#L414
msg289636 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2017-03-15 03:36
I think the description is fine. :-( What leads to your headache?
msg289638 - (view) Author: Michael Seifert (MSeifert) * Date: 2017-03-15 04:08
> What leads to your headache?

That depending on the arguments to "PyTuple_GetSlice" I get "SystemError: ..\Objects\tupleobject.c:156: bad argument to internal function" when using PyTuple_SetItem on the (supposedly) new tuple. 

Maybe I just misunderstood the word "new" in the documentation. :-)
msg289639 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2017-03-15 04:25
I think the docs are fine as-is.  Whether an identical tuple is new or not is an implementation detail.

IMO, the docs would create more confusion by trying to over-explain, "slicing of tuples always returns a new tuple when the result tuple is distinct from the original; however, in the case where the result tuple is not distinct, the implementation is at liberty to return the original tuple instead of a new tuple.  Since tuples are immutable, this should make no difference at all to the user."
msg289703 - (view) Author: Michael Seifert (MSeifert) * Date: 2017-03-16 02:53
I rather thought about something along the lines of: "Take a slice of the tuple pointed to by *p* from *low* to *high* and return it as a tuple. Whether the returned tuple is new or not is an implementation detail (and may depend on the value of the arguments)." Because from the point of the C-API tuples aren't immutable, at least not if they are new (refcount 1).

Thank you both for your feedback. I'll leave it closed as "not a bug", I got the point about over-explaining. :)
History
Date User Action Args
2022-04-11 14:58:44adminsetgithub: 73999
2017-03-16 02:53:00MSeifertsetmessages: + msg289703
2017-03-15 04:25:20rhettingersetstatus: open -> closed
resolution: not a bug
messages: + msg289639

stage: resolved
2017-03-15 04:17:32xiang.zhangsetnosy: + rhettinger
2017-03-15 04:08:48MSeifertsetmessages: + msg289638
2017-03-15 03:36:38xiang.zhangsetnosy: + xiang.zhang
messages: + msg289636
2017-03-15 02:41:35MSeifertsettitle: PyTuple_GetSlice documentation incorrect -> PyTuple_GetSlice does not always return a new tuple
2017-03-15 02:40:50MSeifertcreate