Python/Rhinoscriptsyntax - some kind of funky unicode error?

unicode

#1

I am getting an error in the rs.CheckListBox() method, and it seems to be some kind of weird unicode problem.

Here is the test script:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import rhinoscriptsyntax as rs

names=["Default","0","Existant"]
#names=["Default","0","Existant","à démolir"]
name_list=[(name,False) for name in names]
print name_list
msg="Pick an item"
CLB=rs.CheckListBox(name_list,msg)
for item in CLB:
   if item[1]: print "Chosen: {}".format(item[0])

Which runs OK. Now uncomment line #7 (the one that has the list with 2 accented characters) and run it again.

It errors out with
"Message: Unable to translate bytes [E0] at index 0 from specified code page to Unicode."

Now these are standard accented characters, notably typed with à = Alt+0224 and é = Alt+0233 . Normally they are accepted as long as I have the

#!/usr/bin/env python
# -*- coding: utf-8 -*-

at the top of the script. So what is going on here?

Note that the print name_list line also spits out this:

[('Default', False), ('0', False), ('Existant', False), (u'\xe0 d\xe9molir', False)]

Is this some sort of bug, or am I doing something wrong?

Thanks, --Mitch


(Steve Baer) #2

You aren’t doing anything wrong. This is an outstanding bug that I’m still trying to figure out how to fix. The CheckListBox function itself doesn’t know how to deal with strings.


#3

Ahh… too bad. :frowning_face:


(Willem Derks) #4

Hi Steve,

I’ve encountered an issue that might be related. So I just drop it here:

# coding=utf-8

diam = 'Ø'

print diam
print str(diam) #the str() function throws an error 

Thanks
-Willem


#5

Had that problem too and fixed it by using the “unicode()” conversion instead of “str()” in a customized CheckListBox function:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import Rhino
import rhinoscriptsyntax as rs

def CheckListBox(items, message=None, title=None):
    checkstates = [item[1] for item in items]
    itemstrs = [unicode(item[0]) for item in items]
    newcheckstates = Rhino.UI.Dialogs.ShowCheckListBox(title, message, itemstrs, checkstates)
    if newcheckstates:
        rc = zip(itemstrs, newcheckstates)
        return rc
    return scriptcontext.errorhandler()

#names=["Default","0","Existant"]
names=["Default","0","Existant","à démolir"]
name_list=[(name,False) for name in names]

print name_list
msg="Pick an item"
CLB=CheckListBox(name_list,msg)
for item in CLB:
   if item[1]: print "Chosen: {}".format(item[0])

https://mcneel.myjetbrains.com/youtrack/issue/RH-38621


#6

There is also this:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

diam = 'Ø'

print diam
print "{}".format(diam) #format function seems to convert automatically

–Mitch


#7

str() in rs.CheckListBox() will throw an error on that formatted string anyways


#8

Yes, you would still have to modify the CheckListBox definition in userinterface.py either with unicode() or format(). But, of course, until the error is officially fixed, a custom definition in the script will make it work everywhere.

–Mitch


(Steve Baer) #9

Thanks Jess, I’ll look into using that technique.


(Willem Derks) #10

Thanks Jess!
Really usefull pointers.

-Willem


#11

All thanks go to @dale !
https://mcneel.myjetbrains.com/youtrack/issue/RH-38621