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: turtle.pencolor() chokes on unicode
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Rosuav, apalala, python-dev, terry.reedy
Priority: normal Keywords: patch

Created on 2012-08-10 16:03 by apalala, last changed 2022-04-11 14:57 by admin. This issue is now closed.

File name Uploaded Description Edit
turtle_unicode.patch apalala, 2012-08-10 16:55 isninstance(x, basestring) instead of isinstance(x, str) review
Messages (9)
msg167883 - (view) Author: Juancarlo Añez (apalala) * Date: 2012-08-10 16:03
>>> t.pencolor(u'red')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in pencolor
  File "/usr/lib/python2.7/lib-tk/", line 2166, in pencolor
    color = self._colorstr(args)
  File "/usr/lib/python2.7/lib-tk/", line 2600, in _colorstr
    return self.screen._colorstr(args)
  File "/usr/lib/python2.7/lib-tk/", line 1111, in _colorstr
    r, g, b = [round(255.0*x) for x in (r, g, b)]
TypeError: can't multiply sequence by non-int of type 'float'
msg167893 - (view) Author: Juancarlo Añez (apalala) * Date: 2012-08-10 16:55
This patch solves the problem by making turtle check for string against basestring insted of str.
msg167894 - (view) Author: Juancarlo Añez (apalala) * Date: 2012-08-10 16:57
The bug showed up in a script that used:

from __future__ import unicode_literals
msg212561 - (view) Author: Chris Angelico (Rosuav) * Date: 2014-03-02 15:36
Presumably this is not an issue in 3.x. Is this considered a bug fix (in which case the patch should almost certainly be applied - it looks perfectly safe), or a feature enhancement (in which case this should get closed wontfix)? Looks like low-hanging fruit.
msg212586 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-03-02 20:01
The particular error message is an artifact of 'red' having 3 letters, so that r,g,b = 'red' 'works'. turtle.pencolor(u'blue') raises
turtle.TurtleGraphicsError: bad color arguments: blue.

In general, 'solves the problem' is a bit vague. In this case, it should mean 'make the exception go away and actually actually changes the pencolor', which the patch does. Chunk 2 patches ._colorstr. Once a unicode passes the revised test, the subsequent line
        if self._iscolorstring(color) or color == "":
works because a) ._iscolorstring calls into tk, which is unicode based, and b) in 2.x, the comparison u''=='' is True. To be applied, test/ would need to have one or more tests added.

Chunks 2, 3, and 4 should fix all color issues. Chunk 1 is about file names instead. Though not stated, the intent appears to be to fix all possible issues with unicode_literals, as should be done if any are.  (I did verify that there are no other isinstance(x, str) checks.) In particular, chunk 1 needs a test. Does
                if data.lower().endswith(".gif") and isfile(data):
                    data = TurtleScreen._image(data)
work when data is unicode? The unicode==str comparison works. Does os.path.isfile(unicode) work?  The os.path doc does not specify 'path', but unicode seems to work. TurtleScreen._image returns
        return TK.PhotoImage(file=filename)
I have not found any doc on whether the file option on 2.7 widgets can be unicode or not.

I asked on pydev about the generic issue of a 2.7 param being documented as a 'string' and the __future__ import changing str to unicode in thread 'unicode_string future, str -> basestring, fix or feature'.
msg212588 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-03-02 20:39
The pronouncement on pydev is that this is a bugfix because the input are meant to be text and not specifically binary.
msg212609 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-03-03 02:39
But there is also substantial disagreement. I will not do anything yet.
msg212793 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-03-06 04:16
New changeset 1be39dc4409a by Terry Jan Reedy in branch '2.7':
Issue #15618: Make itself work when run from a module with
msg212794 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-03-06 04:18
I tested by changing a few strings in the demo at the end of the file to unicode.  Any problems with os.path.isfile or Tk are different issues.  Thanks for the patch.
Date User Action Args
2022-04-11 14:57:34adminsetgithub: 59823
2014-03-06 04:18:39terry.reedysetstatus: open -> closed
resolution: fixed
messages: + msg212794

stage: patch review -> resolved
2014-03-06 04:16:29python-devsetnosy: + python-dev
messages: + msg212793
2014-03-03 02:39:42terry.reedysetmessages: + msg212609
2014-03-02 20:39:49terry.reedysetmessages: + msg212588
2014-03-02 20:01:52terry.reedysetmessages: + msg212586
stage: patch review
2014-03-02 15:36:28Rosuavsetnosy: + Rosuav
messages: + msg212561
2012-08-10 18:11:49terry.reedysetnosy: + terry.reedy
2012-08-10 16:57:16apalalasetmessages: + msg167894
2012-08-10 16:55:59apalalasetfiles: + turtle_unicode.patch
keywords: + patch
messages: + msg167893
2012-08-10 16:03:19apalalacreate