python/samba/gp_parse: Fix mulitple encode step with write_section
authorNoel Power <noel.power@suse.com>
Wed, 5 Sep 2018 14:23:01 +0000 (15:23 +0100)
committerNoel Power <npower@samba.org>
Mon, 5 Nov 2018 19:05:24 +0000 (20:05 +0100)
In python2 as far as I can see GptTmplInfParser.write_binary more
or less works by accident.

write_binary creates a writer for the 'utf8' codec, such a writer
should consume unicode and emit utf8 encoded bytes. This writer
is passed to each of the sections managed by GptTmplInfParser as
follows

    def write_binary(self, filename):
        with codecs.open(filename, 'wb+',
                         self.encoding) as f:
            for s in self.sections:
                self.sections[s].write_section(s, f)

And each section type itself is encoding its result to 'utf-16-le'
e.g.
    class UnicodeParam(AbstractParam):
         def write_section(self, header, fp):
            fp.write(u'[Unicode]\r\nUnicode=yes\r\n'.encode(self.encoding)

But this makes little sense, it seems like sections are encoded to one
encoding but the total file is supposed to be encoded as ut8??? Also
having an encoding per ParamType doesn't seem correct.

Bizarely in PY2 this works and it actually encodes the whole file as utf-16le
In PY3 you can't do this as the writer wants to deal with strings not bytes
(after the extra encode phase in 'write_section'.

So, changes here are to remove the unnecessary encoding in each 'write_section'
method, additionally in GptTmplInfParser.write_binary the
codecs.open call now uses the correct codec (e.g. 'utf-16-le') to write

Signed-off-by: Noel Power <noel.power@suse.com>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
python/samba/gp_parse/gp_inf.py

index 8fcd9d5..79e2815 100644 (file)
@@ -29,6 +29,7 @@ from samba.gp_parse import GPParser
 # [MS-GPSB] Security Protocol Extension
 class GptTmplInfParser(GPParser):
     sections = None
+    encoding = 'utf-16le'
 
     class AbstractParam:
         __metaclass__ = ABCMeta
@@ -67,10 +68,10 @@ class GptTmplInfParser(GPParser):
         def write_section(self, header, fp):
             if len(self.param_list) ==  0:
                 return
-            fp.write((u'[%s]\r\n' % header).encode(self.encoding))
+            fp.write(u'[%s]\r\n' % header)
             for key_out, val_out in self.param_list:
-                fp.write((u'%s = %s\r\n' % (key_out,
-                                            val_out)).encode(self.encoding))
+                fp.write(u'%s = %s\r\n' % (key_out,
+                                           val_out))
 
         def build_xml(self, xml_parent):
             for key_ini, val_ini in self.param_list:
@@ -99,9 +100,9 @@ class GptTmplInfParser(GPParser):
         def write_section(self, header, fp):
             if len(self.param_list) ==  0:
                 return
-            fp.write((u'[%s]\r\n' % header).encode(self.encoding))
+            fp.write(u'[%s]\r\n' % header)
             for param in self.param_list:
-                fp.write((u'%s\r\n' % param).encode(self.encoding))
+                fp.write(u'%s\r\n' % param)
 
         def build_xml(self, xml_parent):
             for val_ini in self.param_list:
@@ -129,10 +130,10 @@ class GptTmplInfParser(GPParser):
         def write_section(self, header, fp):
             if len(self.param_list) ==  0:
                 return
-            fp.write((u'[%s]\r\n' % header).encode(self.encoding))
+            fp.write(u'[%s]\r\n' % header)
             for key_out, val in self.param_list:
                 val_out = u','.join(val)
-                fp.write((u'%s = %s\r\n' % (key_out, val_out)).encode(self.encoding))
+                fp.write(u'%s = %s\r\n' % (key_out, val_out))
 
         def build_xml(self, xml_parent):
             for key_ini, sid_list in self.param_list:
@@ -188,9 +189,9 @@ class GptTmplInfParser(GPParser):
         def write_section(self, header, fp):
             if len(self.param_list) ==  0:
                 return
-            fp.write((u'[%s]\r\n' % header).encode(self.encoding))
+            fp.write(u'[%s]\r\n' % header)
             for param in self.param_list:
-                fp.write((u'"%s",%s,"%s"\r\n' % tuple(param)).encode(self.encoding))
+                fp.write(u'"%s",%s,"%s"\r\n' % tuple(param))
 
         def build_xml(self, xml_parent):
             for name_mode_acl in self.param_list:
@@ -225,12 +226,12 @@ class GptTmplInfParser(GPParser):
         def write_section(self, header, fp):
             if len(self.param_list) ==  0:
                 return
-            fp.write((u'[%s]\r\n' % header).encode(self.encoding))
+            fp.write(u'[%s]\r\n' % header)
 
             for key, val in self.param_list:
                 key_out = u'__'.join(key)
                 val_out = u','.join(val)
-                fp.write((u'%s = %s\r\n' % (key_out, val_out)).encode(self.encoding))
+                fp.write(u'%s = %s\r\n' % (key_out, val_out))
 
         def build_xml(self, xml_parent):
             for key_ini, sid_list in self.param_list:
@@ -266,7 +267,7 @@ class GptTmplInfParser(GPParser):
             pass
 
         def write_section(self, header, fp):
-            fp.write(u'[Unicode]\r\nUnicode=yes\r\n'.encode(self.encoding))
+            fp.write(u'[Unicode]\r\nUnicode=yes\r\n')
 
         def build_xml(self, xml_parent):
             # We do not bother storing this field
@@ -283,7 +284,7 @@ class GptTmplInfParser(GPParser):
 
         def write_section(self, header, fp):
             out = u'[Version]\r\nsignature="$CHICAGO$"\r\nRevision=1\r\n'
-            fp.write(out.encode(self.encoding))
+            fp.write(out)
 
         def build_xml(self, xml_parent):
             # We do not bother storing this field
@@ -332,7 +333,7 @@ class GptTmplInfParser(GPParser):
 
     def write_binary(self, filename):
         with codecs.open(filename, 'wb+',
-                         self.output_encoding) as f:
+                         self.encoding) as f:
             for s in self.sections:
                 self.sections[s].write_section(s, f)