Browse code

Ensure DOT files are read and passed as bytes.

Things just happened to work when locale was set to UTF-8, but would
fail otherwise. In particular it would fail on Windows where text files
don't default to UTF-8.

Fixes https://github.com/jrfonseca/xdot.py/issues/46

Jose Fonseca authored on 06/07/2017 16:18:31
Showing 2 changed files

... ...
@@ -22,7 +22,7 @@ before_install:
22 22
 
23 23
 script:
24 24
 - cd tests
25
-- xvfb-run -a -s '-screen 0 1024x768x24' python ../test.py *.dot graphs/*.gv
25
+- LANG=C xvfb-run -a -s '-screen 0 1024x768x24' python ../test.py *.dot graphs/*.gv
26 26
 - cd ..
27 27
 
28 28
 # https://docs.travis-ci.com/user/deployment/pypi/
... ...
@@ -124,8 +124,8 @@ class DotWidget(Gtk.DrawingArea):
124 124
 
125 125
     def set_dotcode(self, dotcode, filename=None):
126 126
         self.openfilename = None
127
-        if isinstance(dotcode, str):
128
-            dotcode = dotcode.encode('utf-8')
127
+        # By default DOT language is UTF-8, but it accepts other encodings
128
+        assert isinstance(dotcode, bytes)
129 129
         xdotcode = self.run_filter(dotcode)
130 130
         if xdotcode is None:
131 131
             return False
... ...
@@ -151,7 +151,7 @@ class DotWidget(Gtk.DrawingArea):
151 151
     def reload(self):
152 152
         if self.openfilename is not None:
153 153
             try:
154
-                fp = open(self.openfilename, 'rt')
154
+                fp = open(self.openfilename, 'rb')
155 155
                 self.set_dotcode(fp.read(), self.openfilename)
156 156
                 fp.close()
157 157
             except IOError:
... ...
@@ -607,7 +607,7 @@ class DotWindow(Gtk.Window):
607 607
 
608 608
     def open_file(self, filename):
609 609
         try:
610
-            fp = open(filename, 'rt')
610
+            fp = open(filename, 'rb')
611 611
             self.set_dotcode(fp.read(), filename)
612 612
             fp.close()
613 613
         except IOError as ex: