cmdtalk: remove remains of python2 support

This commit is contained in:
Jean-Francois Dockes 2021-09-23 11:19:36 +02:00
parent f20c189fa5
commit 7179e0dbf8

View File

@ -29,25 +29,13 @@ import shutil
import getopt import getopt
import traceback import traceback
PY3 = sys.version > '3' def makebytes(data):
if data is None:
if PY3: return b""
def makebytes(data): if isinstance(data, bytes):
if data is None: return data
return b"" else:
if isinstance(data, bytes): return data.encode("UTF-8")
return data
else:
return data.encode("UTF-8")
else:
def makebytes(data):
if data is None:
return ""
if isinstance(data, unicode):
return data.encode("UTF-8")
else:
return data
############################################ ############################################
# CmdTalk implements the communication protocol with the master # CmdTalk implements the communication protocol with the master
@ -116,10 +104,7 @@ class CmdTalk(object):
# followed by data. The param name is returned as str/unicode, the data # followed by data. The param name is returned as str/unicode, the data
# as bytes # as bytes
def readparam(self): def readparam(self):
if PY3: inf = self.infile.buffer
inf = self.infile.buffer
else:
inf = self.infile
s = inf.readline() s = inf.readline()
if s == b'': if s == b'':
if self.exitfunc: if self.exitfunc:
@ -143,7 +128,7 @@ class CmdTalk(object):
(paramsize, len(paramdata)), 1, 1) (paramsize, len(paramdata)), 1, 1)
else: else:
paramdata = b'' paramdata = b''
if PY3 and not self.nodecodeinput: if not self.nodecodeinput:
try: try:
paramdata = paramdata.decode('utf-8') paramdata = paramdata.decode('utf-8')
except Exception as ex: except Exception as ex:
@ -154,18 +139,11 @@ class CmdTalk(object):
# (paramname, paramsize, paramdata)) # (paramname, paramsize, paramdata))
return (paramname, paramdata) return (paramname, paramdata)
if PY3: def senditem(self, nm, data):
def senditem(self, nm, data): data = makebytes(data)
data = makebytes(data) l = len(data)
l = len(data) self.outfile.buffer.write(makebytes("%s: %d\n" % (nm, l)))
self.outfile.buffer.write(makebytes("%s: %d\n" % (nm, l))) self.breakwrite(self.outfile.buffer, data)
self.breakwrite(self.outfile.buffer, data)
else:
def senditem(self, nm, data):
data = makebytes(data)
l = len(data)
self.outfile.write(makebytes("%s: %d\n" % (nm, l)))
self.breakwrite(self.outfile, data)
# Send answer: document, ipath, possible eof. # Send answer: document, ipath, possible eof.
def answer(self, outfields): def answer(self, outfields):
@ -242,7 +220,7 @@ def main(proto, processor):
params[args[2*i]] = args[2*i+1] params[args[2*i]] = args[2*i+1]
res = processor.process(params) res = processor.process(params)
ioout = sys.stdout.buffer if PY3 else sys.stdout ioout = sys.stdout.buffer
for nm,value in res.items(): for nm,value in res.items():
#self.log("Senditem: [%s] -> [%s]" % (nm, value)) #self.log("Senditem: [%s] -> [%s]" % (nm, value))