Rhino Python locale decoding

Dear all,

I need to get object names and to convert them to UTF-8 for XML export.

By the past, I had used this for converting rhino names to UTF-8:

xmlString = rhinoString.decode(locale.getpreferredencoding()).encode('utf-8')

But recently, I found that the script does not work with non ascii characters, and that locale.getpreferredencoding() returns ‘ascii’ while rhino definitely uses another encoding, raising an exception in decode(). Since I’m almost sure that this has worked by the past, it seems some changes in Rhino IronPython have broken the script.

  1. Do rhino strings in python have a fixed encoding we could use in decode() without trying to detect the locale?
  2. If not, what is the proper way to detect the locale?

Thanks in advance

Best regards,

Etienne

Hi Etienne

This territory is entirely IronPython-dependent, so using sys.version (2.7.0.0 in Rhino 5 and 2.7.5.0 in Rhino WIP) and checking what IronPython does in that version should tell you information about this.

All .Net string are in-memory UTF-16 for the runtime, but this does not really matter.
The _EditPythonScript editor saves files in UTF-8. So you can have:

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

at the beginning of files.

GhPython uses an in-memory string, that therefore is UTF-16. But you really never notice this.
On my system, print(sys.stdout.encoding) gives windows_1252.

I do not think that the locale module works well in IronPython. I would try to use overloads that take encodings, if possible. But I do not exactly know what you are trying to do, so I can only speculate.