[6.1] 74976accf Add python2/3 hack

hermunn hermunn at varnish-software.com
Wed Oct 24 09:29:22 UTC 2018


commit 74976accf9c2194495e6441d4b8a461dbbd69f3f
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Tue Oct 9 08:36:02 2018 +0000

    Add python2/3 hack

diff --git a/lib/libvcc/vsctool.py b/lib/libvcc/vsctool.py
index 6c96388e5..bd3e68af3 100644
--- a/lib/libvcc/vsctool.py
+++ b/lib/libvcc/vsctool.py
@@ -41,6 +41,7 @@ import getopt
 import json
 import sys
 import collections
+import codecs
 
 # Parameters of 'varnish_vsc_begin', first element is default
 TYPES = ["counter", "gauge", "bitmap"]
@@ -153,7 +154,12 @@ class CounterSet(object):
         '''Emit .h file'''
         assert self.completed
         fon = "VSC_" + self.name + ".h"
-        fo = open(fon, "w")
+        try:
+            # Python3
+            fo = open(fon, "w", encoding="UTF-8")
+        except TypeError:
+            # Python2
+            fo = open(fon, "w")
         genhdr(fo, self.name)
         fo.write(self.struct + " {\n")
         for i in self.mbrs:
@@ -249,7 +255,12 @@ class CounterSet(object):
         '''Emit .c file'''
         assert self.completed
         fon = "VSC_" + self.name + ".c"
-        fo = open(fon, "w")
+        try:
+            # Python3
+            fo = open(fon, "w", encoding="UTF-8")
+        except TypeError:
+            # Python2
+            fo = open(fon, "w")
         genhdr(fo, self.name)
         fo.write('#include "config.h"\n')
         fo.write('#include <stdio.h>\n')
@@ -396,10 +407,22 @@ def mainfunc(argv):
     rstfile = None
     for f, v in optlist:
         if f == '-r':
+            try:
+                # Python3
+                sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())
+            except AttributeError:
+                # Python2
+                pass
             rstfile = sys.stdout
 
     vscset = []
-    scs = open(args[0]).read().split("\n.. ")
+    try:
+        # Python3
+        f = open(args[0], encoding="UTF-8")
+    except TypeError:
+        # Python2
+        f = open(args[0])
+    scs = f.read().split("\n.. ")
     if rstfile:
         rstfile.write(scs[0])
     for i in scs[1:]:


More information about the varnish-commit mailing list