Manfred Moitzi avatar Manfred Moitzi committed a6ad4ae

working on Python 3 non us ascii testing problem (solved?)

Comments (0)

Files changed (2)

svgwrite/drawing.py

 
         :param fileobj: a *file-like* object
 
+        Python 3.x - set encoding at the open command::
+
+            use open('filename', 'w', encoding='utf-8')
         """
         # write xml header
         fileobj.write('<?xml version="1.0" encoding="utf-8" ?>\n')
 
     def save(self):
         """ Write the ``utf-8`` encoded XML string to :attr:`filename`. """
-        fileobj = open(self.filename, mode='w')
+        if PYTHON3:
+            fileobj = open(self.filename, mode='w', encoding='utf-8')
+        else:
+            fileobj = open(self.filename, mode='w')
         self.write(fileobj)
         fileobj.close()
 

tests/test_drawing.py

         dwg.write(f)
         result = f.getvalue()
         f.close()
-        self.assertEqual(result, '<?xml version="1.0" encoding="utf-8" ?>\n' \
-            '<svg baseProfile="full" height="100%" version="1.1" width="100%" '\
-            'xmlns="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" '\
-            'xmlns:xlink="http://www.w3.org/1999/xlink">'
-            '<title>\xc3\xb6\xc3\xa4\xc3\xbc</title><defs /></svg>')
+        if PYTHON3: # result is an unicode string!
+            self.assertEqual(result, '<?xml version="1.0" encoding="utf-8" ?>\n' \
+                '<svg baseProfile="full" height="100%" version="1.1" width="100%" '\
+                'xmlns="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" '\
+                'xmlns:xlink="http://www.w3.org/1999/xlink">'
+                '<title>öäü</title><defs /></svg>')
+        else:
+            self.assertEqual(result, '<?xml version="1.0" encoding="utf-8" ?>\n' \
+                '<svg baseProfile="full" height="100%" version="1.1" width="100%" '\
+                'xmlns="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" '\
+                'xmlns:xlink="http://www.w3.org/1999/xlink">'
+                '<title>\xc3\xb6\xc3\xa4\xc3\xbc</title><defs /></svg>')
 
 class TestDrawingTinyProfile(unittest.TestCase):
     def test_empty_drawing(self):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.