[6.0] 187ce707e Add python2/3 hack

Dridi Boukelmoune dridi.boukelmoune at gmail.com
Wed Oct 31 13:08:30 UTC 2018


commit 187ce707efa4dbf0ce27973930587dc1c19080a7
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 eb795e076..f1e0d02ab 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"]
@@ -164,7 +165,12 @@ class CounterSet(object):
         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")
@@ -288,7 +294,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')
@@ -437,10 +448,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