diff rpc.py @ 56:91b476ebc0f2

Run through 2to3
author Daniel O'Connor <doconnor@gsoft.com.au>
date Tue, 08 Dec 2020 14:00:45 +1030
parents cba1c44060f5
children
line wrap: on
line diff
--- a/rpc.py	Tue Dec 08 13:59:05 2020 +1030
+++ b/rpc.py	Tue Dec 08 14:00:45 2020 +1030
@@ -101,11 +101,11 @@
 	def unpack_callheader(self):
 		xid = self.unpack_uint(xid)
 		temp = self.unpack_enum()
-		if temp <> CALL:
-			raise BadRPCFormat, 'no CALL but ' + `temp`
+		if temp != CALL:
+			raise BadRPCFormat('no CALL but ' + repr(temp))
 		temp = self.unpack_uint()
-		if temp <> RPCVERSION:
-			raise BadRPCVerspion, 'bad RPC version ' + `temp`
+		if temp != RPCVERSION:
+			raise BadRPCVerspion('bad RPC version ' + repr(temp))
 		prog = self.unpack_uint()
 		vers = self.unpack_uint()
 		proc = self.unpack_uint()
@@ -117,39 +117,35 @@
 	def unpack_replyheader(self):
 		xid = self.unpack_uint()
 		mtype = self.unpack_enum()
-		if mtype <> REPLY:
-			raise RuntimeError, 'no REPLY but ' + `mtype`
+		if mtype != REPLY:
+			raise RuntimeError('no REPLY but ' + repr(mtype))
 		stat = self.unpack_enum()
 		if stat == MSG_DENIED:
 			stat = self.unpack_enum()
 			if stat == RPC_MISMATCH:
 				low = self.unpack_uint()
 				high = self.unpack_uint()
-				raise RuntimeError, \
-				  'MSG_DENIED: RPC_MISMATCH: ' + `low, high`
+				raise RuntimeError('MSG_DENIED: RPC_MISMATCH: ' + repr((low, high)))
 			if stat == AUTH_ERROR:
 				stat = self.unpack_uint()
-				raise RuntimeError, \
-					'MSG_DENIED: AUTH_ERROR: ' + `stat`
-			raise RuntimeError, 'MSG_DENIED: ' + `stat`
-		if stat <> MSG_ACCEPTED:
-			raise RuntimeError, \
-			  'Neither MSG_DENIED nor MSG_ACCEPTED: ' + `stat`
+				raise RuntimeError('MSG_DENIED: AUTH_ERROR: ' + repr(stat))
+			raise RuntimeError('MSG_DENIED: ' + repr(stat))
+		if stat != MSG_ACCEPTED:
+			raise RuntimeError('Neither MSG_DENIED nor MSG_ACCEPTED: ' + repr(stat))
 		verf = self.unpack_auth()
 		stat = self.unpack_enum()
 		if stat == PROG_UNAVAIL:
-			raise RuntimeError, 'call failed: PROG_UNAVAIL'
+			raise RuntimeError('call failed: PROG_UNAVAIL')
 		if stat == PROG_MISMATCH:
 			low = self.unpack_uint()
 			high = self.unpack_uint()
-			raise RuntimeError, \
-				'call failed: PROG_MISMATCH: ' + `low, high`
+			raise RuntimeError('call failed: PROG_MISMATCH: ' + repr((low, high)))
 		if stat == PROC_UNAVAIL:
-			raise RuntimeError, 'call failed: PROC_UNAVAIL'
+			raise RuntimeError('call failed: PROC_UNAVAIL')
 		if stat == GARBAGE_ARGS:
-			raise RuntimeError, 'call failed: GARBAGE_ARGS'
-		if stat <> SUCCESS:
-			raise RuntimeError, 'call failed: ' + `stat`
+			raise RuntimeError('call failed: GARBAGE_ARGS')
+		if stat != SUCCESS:
+			raise RuntimeError('call failed: ' + repr(stat))
 		return xid, verf
 		# Caller must get procedure-specific part of reply
 
@@ -200,7 +196,7 @@
     offset, hh = divmod(hh + offset, 24)
     d = d + offset
     _unix_epoch = time.mktime((y, m, d, hh, mm, ss, 0, 0, 0))
-    print "Unix epoch:", time.ctime(_unix_epoch)
+    print("Unix epoch:", time.ctime(_unix_epoch))
     return _unix_epoch
 
 
@@ -226,7 +222,7 @@
 
 	def makesocket(self):
 		# This MUST be overridden
-		raise RuntimeError, 'makesocket not defined'
+		raise RuntimeError('makesocket not defined')
 
 	def connsocket(self):
 		# Override this if you don't want/need a connection
@@ -245,7 +241,7 @@
 		#print "make_call() args = " + str(args)
 		# Don't normally override this (but see Broadcast)
 		if pack_func is None and args is not None:
-			raise TypeError, 'non-null args with null pack_func'
+			raise TypeError('non-null args with null pack_func')
 		#print "packed args"
 		self.start_call(proc)
 		if pack_func:
@@ -270,7 +266,7 @@
 
 	def do_call(self):
 		# This MUST be overridden
-		raise RuntimeError, 'do_call not defined'
+		raise RuntimeError('do_call not defined')
 
 	def mkcred(self):
 		# Override this to use more powerful credentials
@@ -297,7 +293,7 @@
 
 def sendfrag_with_timeout(sock,  last, frag, timeout_seconds=None):
 	x = len(frag)
-	if last: x = x | 0x80000000L
+	if last: x = x | 0x80000000
 	header = (chr(int(x>>24 & 0xff)) + chr(int(x>>16 & 0xff)) + \
 		  chr(int(x>>8 & 0xff)) + chr(int(x & 0xff)))
 	block=header+frag
@@ -307,7 +303,7 @@
 		if _select and timeout_seconds:
 			rlist, wlist, xlist=_select([],[sock],[], timeout_seconds)
 			if not wlist:
-				raise EOFError, "Blocked write in sendfrag()"		
+				raise EOFError("Blocked write in sendfrag()")		
 		nsent+=sock.send(block[nsent:])
 	
 def recvfrag_with_timeout(sock, timeout_seconds=None):
@@ -316,14 +312,14 @@
 		#print "Selecting with timeout...", timeout_seconds
 		rlist, wlist, xlist=_select([sock],[],[], timeout_seconds)
 		if not rlist:
-			raise EOFError, "No header at all in recvfrag()"
+			raise EOFError("No header at all in recvfrag()")
 
 	header = sock.recv(4)
 	if len(header) < 4:
 		raise EOFError
-	x = long(ord(header[0]))<<24 | ord(header[1])<<16 | \
+	x = int(ord(header[0]))<<24 | ord(header[1])<<16 | \
 	    ord(header[2])<<8 | ord(header[3])
-	last = ((x & 0x80000000L) != 0)
+	last = ((x & 0x80000000) != 0)
 	n = int(x & 0x7fffffff)
 	
 	frag=''
@@ -333,7 +329,7 @@
 			#print "Selecting with timeout...", timeout_seconds
 			rlist, wlist, xlist=_select([sock],[],[], timeout_seconds)
 			if not rlist:
-				raise EOFError, "No data after header in recvfrag()"
+				raise EOFError("No data after header in recvfrag()")
 		frag += sock.recv(n-len(frag))
 				
 	return last, frag
@@ -361,16 +357,17 @@
 	if last_resv_port_tried == None:
 		import os
 		last_resv_port_tried = FIRST + os.getpid() % (LAST-FIRST)
-	for i in range(last_resv_port_tried, LAST) + \
-		  range(FIRST, last_resv_port_tried):
+	for i in list(range(last_resv_port_tried, LAST)) + \
+		  list(range(FIRST, last_resv_port_tried)):
 		last_resv_port_tried = i
 		try:
 			sock.bind((host, i))
 			return last_resv_port_tried
-		except socket.error, (errno, msg):
-			if errno <> 114:
-				raise socket.error, (errno, msg)
-	raise RuntimeError, 'can\'t assign reserved port'
+		except socket.error as xxx_todo_changeme:
+			(errno, msg) = xxx_todo_changeme.args
+			if errno != 114:
+				raise socket.error(errno, msg)
+	raise RuntimeError('can\'t assign reserved port')
 
 
 # Client using TCP to a specific port
@@ -389,10 +386,10 @@
 		u = self.unpacker
 		u.reset(reply)
 		xid, verf = u.unpack_replyheader()
-		if xid <> self.lastxid:
+		if xid != self.lastxid:
 			# Can't really happen since this is TCP...
-			raise RuntimeError, 'wrong xid in reply ' + `xid` + \
-				' instead of ' + `self.lastxid`
+			raise RuntimeError('wrong xid in reply ' + repr(xid) + \
+				' instead of ' + repr(self.lastxid))
 
 
 # Client using UDP to a specific port
@@ -408,7 +405,7 @@
 		try:
 			from select import select
 		except ImportError:
-			print 'WARNING: select not found, RPC may hang'
+			print('WARNING: select not found, RPC may hang')
 			select = None
 		BUFSIZE = 8192 # Max UDP buffer size
 		timeout = 1
@@ -419,7 +416,7 @@
 				r, w, x = select(r, w, x, timeout)
 			if self.sock not in r:
 				count = count - 1
-				if count < 0: raise RuntimeError, 'timeout'
+				if count < 0: raise RuntimeError('timeout')
 				if timeout < 25: timeout = timeout *2
 ##				print 'RESEND', timeout, count
 				self.sock.send(call)
@@ -428,7 +425,7 @@
 			u = self.unpacker
 			u.reset(reply)
 			xid, verf = u.unpack_replyheader()
-			if xid <> self.lastxid:
+			if xid != self.lastxid:
 ##				print 'BAD xid'
 				continue
 			break
@@ -455,7 +452,7 @@
 
 	def make_call(self, proc, args, pack_func, unpack_func):
 		if pack_func is None and args is not None:
-			raise TypeError, 'non-null args with null pack_func'
+			raise TypeError('non-null args with null pack_func')
 		self.start_call(proc)
 		if pack_func:
 			pack_func(args)
@@ -464,7 +461,7 @@
 		try:
 			from select import select
 		except ImportError:
-			print 'WARNING: select not found, broadcast will hang'
+			print('WARNING: select not found, broadcast will hang')
 			select = None
 		BUFSIZE = 8192 # Max UDP buffer size (for reply)
 		replies = []
@@ -484,7 +481,7 @@
 			u = self.unpacker
 			u.reset(reply)
 			xid, verf = u.unpack_replyheader()
-			if xid <> self.lastxid:
+			if xid != self.lastxid:
 ##				print 'BAD xid'
 				continue
 			reply = unpack_func()
@@ -627,7 +624,7 @@
 		port = pmap.Getport((prog, vers, IPPROTO_TCP, 0))
 		pmap.close()
 		if port == 0:
-			raise RuntimeError, 'program not registered'
+			raise RuntimeError('program not registered')
 		RawTCPClient.__init__(self, host, prog, vers, port)
 
 
@@ -640,7 +637,7 @@
 		port = pmap.Getport((prog, vers, IPPROTO_UDP, 0))
 		pmap.close()
 		if port == 0:
-			raise RuntimeError, 'program not registered'
+			raise RuntimeError('program not registered')
 		RawUDPClient.__init__(self, host, prog, vers, port)
 
 
@@ -713,11 +710,11 @@
 		xid = self.unpacker.unpack_uint()
 		self.packer.pack_uint(xid)
 		temp = self.unpacker.unpack_enum()
-		if temp <> CALL:
+		if temp != CALL:
 			return None # Not worthy of a reply
 		self.packer.pack_uint(REPLY)
 		temp = self.unpacker.unpack_uint()
-		if temp <> RPCVERSION:
+		if temp != RPCVERSION:
 			self.packer.pack_uint(MSG_DENIED)
 			self.packer.pack_uint(RPC_MISMATCH)
 			self.packer.pack_uint(RPCVERSION)
@@ -726,17 +723,17 @@
 		self.packer.pack_uint(MSG_ACCEPTED)
 		self.packer.pack_auth((AUTH_NULL, make_auth_null()))
 		prog = self.unpacker.unpack_uint()
-		if prog <> self.prog:
+		if prog != self.prog:
 			self.packer.pack_uint(PROG_UNAVAIL)
 			return self.packer.get_buf()
 		vers = self.unpacker.unpack_uint()
-		if vers <> self.vers:
+		if vers != self.vers:
 			self.packer.pack_uint(PROG_MISMATCH)
 			self.packer.pack_uint(self.vers)
 			self.packer.pack_uint(self.vers)
 			return self.packer.get_buf()
 		proc = self.unpacker.unpack_uint()
-		methname = 'handle_' + `proc`
+		methname = 'handle_' + repr(proc)
 		try:
 			meth = getattr(self, methname)
 		except AttributeError:
@@ -768,7 +765,7 @@
 
 	def makesocket(self):
 		# This MUST be overridden
-		raise RuntimeError, 'makesocket not defined'
+		raise RuntimeError('makesocket not defined')
 
 	def bindsocket(self):
 		# Override this to bind to a different port (e.g. reserved)
@@ -786,13 +783,13 @@
 		mapping = self.prog, self.vers, self.prot, self.port
 		p = TCPPortMapperClient(self.host)
 		if not p.Set(mapping):
-			raise RuntimeError, 'register failed'
+			raise RuntimeError('register failed')
 
 	def unregister(self):
 		mapping = self.prog, self.vers, self.prot, self.port
 		p = TCPPortMapperClient(self.host)
 		if not p.Unset(mapping):
-			raise RuntimeError, 'unregister failed'
+			raise RuntimeError('unregister failed')
 
 	def makesocket(self):
 		self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@@ -810,8 +807,8 @@
 				call = recvrecord(sock)
 			except EOFError:
 				break
-			except socket.error, msg:
-				print 'socket error:', msg
+			except socket.error as msg:
+				print('socket error:', msg)
 				break
 			reply = self.handle(call)
 			if reply is not None:
@@ -852,13 +849,13 @@
 		mapping = self.prog, self.vers, self.prot, self.port
 		p = UDPPortMapperClient(self.host)
 		if not p.Set(mapping):
-			raise RuntimeError, 'register failed'
+			raise RuntimeError('register failed')
 
 	def unregister(self):
 		mapping = self.prog, self.vers, self.prot, self.port
 		p = UDPPortMapperClient(self.host)
 		if not p.Unset(mapping):
-			raise RuntimeError, 'unregister failed'
+			raise RuntimeError('unregister failed')
 
 	def makesocket(self):
 		self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
@@ -871,7 +868,7 @@
 	def session(self):
 		call, host_port = self.sock.recvfrom(8192)
 		reply = self.handle(call)
-		if reply <> None:
+		if reply != None:
 			self.sock.sendto(reply, host_port)
 
 
@@ -882,11 +879,11 @@
 	list = pmap.Dump()
 	list.sort()
 	for prog, vers, prot, port in list:
-		print prog, vers,
-		if prot == IPPROTO_TCP: print 'tcp',
-		elif prot == IPPROTO_UDP: print 'udp',
-		else: print prot,
-		print port
+		print(prog, vers, end=' ')
+		if prot == IPPROTO_TCP: print('tcp', end=' ')
+		elif prot == IPPROTO_UDP: print('udp', end=' ')
+		else: print(prot, end=' ')
+		print(port)
 
 
 # Test program for broadcast operation -- dump everybody's portmapper status
@@ -899,7 +896,7 @@
 		bcastaddr = '<broadcast>'
 	def rh(reply, fromaddr):
 		host, port = fromaddr
-		print host + '\t' + `reply`
+		print(host + '\t' + repr(reply))
 	pmap = BroadcastUDPPortMapperClient(bcastaddr)
 	pmap.set_reply_handler(rh)
 	pmap.set_timeout(5)
@@ -917,21 +914,21 @@
 		def handle_1(self):
 			arg = self.unpacker.unpack_string()
 			self.turn_around()
-			print 'RPC function 1 called, arg', `arg`
+			print('RPC function 1 called, arg', repr(arg))
 			self.packer.pack_string(arg + arg)
 	#
 	s = S('', 0x20000000, 1, 0)
 	try:
 		s.unregister()
-	except RuntimeError, msg:
-		print 'RuntimeError:', msg, '(ignored)'
+	except RuntimeError as msg:
+		print('RuntimeError:', msg, '(ignored)')
 	s.register()
-	print 'Service started...'
+	print('Service started...')
 	try:
 		s.loop()
 	finally:
 		s.unregister()
-		print 'Service interrupted.'
+		print('Service interrupted.')
 
 
 def testclt():
@@ -945,9 +942,9 @@
 				self.packer.pack_string, \
 				self.unpacker.unpack_string)
 	c = C(host, 0x20000000, 1)
-	print 'making call...'
+	print('making call...')
 	reply = c.call_1('hello, world, ')
-	print 'call returned', `reply`
+	print('call returned', repr(reply))
 
 def testclt2():
 	import sys
@@ -959,7 +956,7 @@
 				self.packer.pack_string, \
 				self.unpacker.unpack_string)
 	c = C(host, 0x20000000, 1)
-	print 'making call...'
+	print('making call...')
 	reply = c.call_1('hello, world, ')
-	print 'call returned', `reply`
+	print('call returned', repr(reply))