nebius/SWE-bench-extra huggingface dataset:
Repository: HBehrens/fixup_shaper_svg

Issue:
Improvement Request: Modification to get SVG preview working on the Origin
The current script (fixup_shaper_svg.py) is working as such.
Converted SVG files can be used and inserted on Origin.
However the SVG image preview is not working.

Reason for this is that in the output file the following information will be added:
encoding='cp1252'

When removing the encoding statement the file can be used on the Origin and the SVG preview is also working.
Would be great if someone could update the python script to remove "encoding='cp1252'" to finally get preview working.
Unfortunately i do not have enough python skills to do this on my own. ;-)

Comments:

Patch:
diff --git a/fixup_shaper_svg.py b/fixup_shaper_svg.py
index 2d304c5..0436b9c 100644
--- a/fixup_shaper_svg.py
+++ b/fixup_shaper_svg.py
@@ -61,13 +61,14 @@ def fixup_svg(infile, outfile):
     svg = xml.etree.ElementTree.parse(infile)
     for elem in svg.iter():
         fixup_element(elem)
-    svg.write(outfile, encoding='unicode', xml_declaration=True)
+    encoding = 'unicode' if outfile == sys.stdout else 'utf-8'
+    svg.write(outfile, encoding=encoding, xml_declaration=True)
 
 
 if __name__ == '__main__':
     parser = argparse.ArgumentParser()
     parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), default=sys.stdin)
-    parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'), default=sys.stdout)
+    parser.add_argument('outfile', nargs='?', type=argparse.FileType('wb'), default=sys.stdout)
     args = parser.parse_args()
 
     fixup_svg(infile=args.infile, outfile=args.outfile)


Test patch:
diff --git a/fixup_shaper_svg_tests.py b/fixup_shaper_svg_tests.py
index 362aa80..9e91696 100644
--- a/fixup_shaper_svg_tests.py
+++ b/fixup_shaper_svg_tests.py
@@ -1,6 +1,7 @@
 import unittest
+import io
 
-from fixup_shaper_svg import length_value_without_spaces, style_attribute_without_fill_rule
+from fixup_shaper_svg import fixup_svg, length_value_without_spaces, style_attribute_without_fill_rule
 
 
 class LengthWithUnit(unittest.TestCase):
@@ -42,5 +43,16 @@ class StyleWithUnsupportedAttributes(unittest.TestCase):
                          style_attribute_without_fill_rule(" fill-rule: evenodd; "))
 
 
+class XmlFileEncoding(unittest.TestCase):
+
+    def test_does_force_utf8(self):
+        infile = io.StringIO("""<?xml version='1.0' encoding='cp1252'?>
+<svg xmlns="http://www.w3.org/2000/svg"/>""")
+        with io.BytesIO() as outfile:
+            fixup_svg(infile, outfile)
+            self.assertEqual(b'<?xml version=\'1.0\' encoding=\'utf-8\'?>\n'
+                             b'<svg xmlns="http://www.w3.org/2000/svg" />', outfile.getvalue())
+
+
 if __name__ == '__main__':
     unittest.main()