Ticket #52 (closed defect: fixed)

Opened 7 years ago

Last modified 7 years ago

importing gtk breaks unicode loading

Reported by: anonymous Owned by: xi
Priority: normal Component: pyyaml
Severity: normal Keywords:


The following summarizes it pretty well, I guess:

>>> import yaml
>>> u = u"\N{skull and crossbones}"
>>> yaml.load(yaml.dump(u))
>>> import gtk
>>> yaml.load(yaml.dump(u))


Change History

comment:1 Changed 7 years ago by anonymous

Ok, I looked a little closer and the problem seems to be that pygtk changes the default encoding and yaml relies on this:

>>> import sys
>>> sys.getdefaultencoding()
>>> import gtk
>>> sys.getdefaultencoding()

I think GTK shouldn't do that but I'll try to prepare a patch for PyYAML anyways.

comment:2 Changed 7 years ago by anonymous

Make conversion explicit and not reliant on default encoding. Ensure behaviour as documented in PyYAMLDocumentation

diff --git a/yaml/ b/yaml/
index d1d4f6a..cf4b051 100644
--- a/yaml/
+++ b/yaml/
@@ -383,7 +383,7 @@ class SafeConstructor(BaseConstructor):
     def construct_yaml_str(self, node):
         value = self.construct_scalar(node)
-            return str(value)
+            return value.encode("ascii")
         except UnicodeEncodeError:
             return value

comment:3 Changed 7 years ago by xi

  • Status changed from new to closed
  • Resolution set to fixed

Thanks for the patch, fixed [251].


Add a comment

Modify Ticket

Change Properties
<Author field>
as closed
The resolution will be deleted. Next status will be 'reopened'

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.