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.

Title: curselection() in should return ints
Type: Stage:
Components: Tkinter Versions:
Status: closed Resolution:
Dependencies: Superseder: Tkinter.Listbox several minor issues
View: 6181
Assigned To: loewis Nosy List: ejoy, gpolo, loewis, werneck
Priority: normal Keywords: patch

Created on 2004-01-03 07:26 by ejoy, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit
curselection_tupleint.diff gpolo, 2008-05-10 17:58 patch applies on 2.6 and 3.0 (offset -7 lines)
Messages (10)
msg60444 - (view) Author: Zhang Le (ejoy) Date: 2004-01-03 07:26
I noticed that in curselection() always
return strings instead of ints, (users have to use
map(int, list) to get the int values) but why? Here's
the code I found in Python 2.2/2.3. It seems someone
has noticed the problem before.

    def curselection(self):
        """Return list of indices of currently selected
        # XXX Ought to apply self._getints()...
            self._w, 'curselection'))
msg64951 - (view) Author: Guilherme Polo (gpolo) * (Python committer) Date: 2008-04-04 22:12
Indeed, it should call self._getints, not sure why it was never done.
Fixing this gives curselection the correct behavior according to the Tk
Reference Manual:
msg66528 - (view) Author: Pedro Werneck (werneck) Date: 2008-05-10 14:29
Just a bare self._getints will raise an exception with no item selected
and an empty string returned, so I'm adding a patch to check for it and
return an empty tuple in that case, or the tuple with ints.

It's open for discussion if we should follow Tk documentation strictly
and return an empty string if no item selected, or keep consistency with
Python and other Tkinter parts and always return a tuple.
msg66530 - (view) Author: Guilherme Polo (gpolo) * (Python committer) Date: 2008-05-10 14:45
Not sure what you did, but calling _getints didn't raise an exception
here neither returned an empty string if no item was selected, it
returned None.
msg66533 - (view) Author: Pedro Werneck (werneck) Date: 2008-05-10 14:58
I used the tuple from splitlist() in both cases. I'm not sure if it
should return None on an empty selection since that is not documented
msg66534 - (view) Author: Guilherme Polo (gpolo) * (Python committer) Date: 2008-05-10 15:01
_getints already call splitlist, so there isn't much point in calling
_getints after calling splitlist.

And the Tk documentation says an empty string should be returned in case
there is no selection, but that is Tk. I don't see any problem in
returning None instead of '' here in Python.
msg66544 - (view) Author: Pedro Werneck (werneck) Date: 2008-05-10 17:48
Neither I do, but the current version already returns an empty tuple.
Since the map(int, curselection) idiom is widely used, changing to int
is not likely to break any code, but returning None on empty selection is.
msg66546 - (view) Author: Guilherme Polo (gpolo) * (Python committer) Date: 2008-05-10 17:51
Are you aware of any code checking against empty tuple specifically to
verify that nothing is selected ?
msg66548 - (view) Author: Guilherme Polo (gpolo) * (Python committer) Date: 2008-05-10 17:57
I found some code using map(int, curselection..) directly, so this new
patch fixes the problem related to returning None when nothing was selected.
msg89579 - (view) Author: Guilherme Polo (gpolo) * (Python committer) Date: 2009-06-21 19:43
Closing this in favour of issue6181 as it contains several other minor
fixes in Listbox that now have been tested in the
tk_and_idle_maintenance branch.
Date User Action Args
2022-04-11 14:56:02adminsetgithub: 39756
2009-06-21 19:43:32gpolosetstatus: open -> closed
superseder: Tkinter.Listbox several minor issues
messages: + msg89579
2008-05-10 22:32:22wernecksetfiles: -
2008-05-10 17:58:39gpolosetfiles: + curselection_tupleint.diff
2008-05-10 17:58:14gpolosetfiles: - curselection_tupleint.diff
2008-05-10 17:58:06gpolosetfiles: - curselection_tupleint.diff
2008-05-10 17:57:02gpolosetfiles: + curselection_tupleint.diff
messages: + msg66548
2008-05-10 17:51:43gpolosetmessages: + msg66546
2008-05-10 17:48:56wernecksetmessages: + msg66544
2008-05-10 15:01:56gpolosetmessages: + msg66534
2008-05-10 14:58:20wernecksetfiles: +
messages: + msg66533
2008-05-10 14:52:09wernecksetfiles: - issue869780_3.patch
2008-05-10 14:52:04wernecksetfiles: - issue869780_2.6.patch
2008-05-10 14:45:06gpolosetfiles: + curselection_tupleint.diff
messages: + msg66530
2008-05-10 14:33:03wernecksetfiles: + issue869780_3.patch
2008-05-10 14:29:59wernecksetfiles: + issue869780_2.6.patch
nosy: + werneck
messages: + msg66528
keywords: + patch
2008-04-04 22:12:36gpolosetnosy: + gpolo
messages: + msg64951
2004-01-03 07:26:24ejoycreate