Modify

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:
Cc:

Description

The following summarizes it pretty well, I guess:

>>> import yaml
>>> u = u"\N{skull and crossbones}"
>>> yaml.load(yaml.dump(u))
u'\u2620'
>>> import gtk
>>> yaml.load(yaml.dump(u))
'\xe2\x98\xa0'

Attachments

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()
'ascii'
>>> import gtk
>>> sys.getdefaultencoding()
'utf-8' 

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/constructor.py b/yaml/constructor.py
index d1d4f6a..cf4b051 100644
--- a/yaml/constructor.py
+++ b/yaml/constructor.py
@@ -383,7 +383,7 @@ class SafeConstructor(BaseConstructor):
     def construct_yaml_str(self, node):
         value = self.construct_scalar(node)
         try:
-            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].

View

Add a comment

Modify Ticket

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


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

 
Note: See TracTickets for help on using tickets.