Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Port from python 2.5-2.7 to 2.6-2.7/3.2+.

- Besides the trivial syntax changes, this required changing the
  Connection.write_line loop to keep _wbuffer as a flat bytes string
  instead of a list of strings. (Since it was already flattening each
  string and then exploding it into a list of 1-char strings, there's
  no performance downside to thsi change.)

- As a side-effect of the changes, Connection.write and friends can
  now handle either bytes or unicode strings. Since the high-level
  methods are all in terms of JSON objects, this only really makes a
  difference for debugging bjsonrpc itself. (The json module will
  only give us str objects--bytes on 2.x, unicode on 3.x--and any
  non-ASCII characters will already be escaped for JSON anyway.)
  • Loading branch information...
commit bb21d2857b9591a923d37e266fc990b0660d2156 1 parent 1d9c1f6
@abarnert abarnert authored
View
58 bjsonrpc/connection.py
@@ -230,7 +230,7 @@ def getmaxtimeout(cls, operation):
def __init__(self, sck, address = None, handler_factory = None):
self._debug_socket = False
self._debug_dispatch = False
- self._buffer = ''
+ self._buffer = b''
self._sck = sck
self._address = address
self._handler = handler_factory
@@ -247,7 +247,7 @@ def __init__(self, sck, address = None, handler_factory = None):
self.method = Proxy(self, sync_type=1)
self.notify = Proxy(self, sync_type=2)
self.pipe = Proxy(self, sync_type=3)
- self._wbuffer = []
+ self._wbuffer = b''
self.write_lock = threading.RLock()
self.read_lock = threading.RLock()
self.getid_lock = threading.Lock()
@@ -406,7 +406,7 @@ def _format_exception(self, obj, method, args, kw, exc):
etype, evalue, etb = exc
funargs = ", ".join(
[repr(x) for x in args] +
- ["%s=%s" % (k, repr(x)) for k, x in kw.iteritems()]
+ ["%s=%r" % (k, kw[k]) for k in kw]
)
if len(funargs) > 40:
funargs = funargs[:37] + "..."
@@ -444,14 +444,14 @@ def _extract_params(self, request):
else:
req_kwargs = request.get("kwparams", {})
if req_kwargs:
- req_kwargs = dict((str(k), v) for k, v in req_kwargs.iteritems())
+ req_kwargs = dict((str(k), req_kwargs[k]) for k in req_kwargs)
return req_method, req_args, req_kwargs
def _find_object(self, req_method, req_args, req_kwargs):
if '.' in req_method: # local-object.
objectname, req_method = req_method.split('.')[:2]
if objectname not in self._objects:
- raise ValueError, "Invalid object identifier"
+ raise ValueError("Invalid object identifier")
elif req_method == '__delete__':
self._dispatch_delete(objectname)
else:
@@ -466,7 +466,7 @@ def _find_method(self, req_object, req_method, req_args, req_kwargs):
try:
req_function = req_object.get_method(req_method)
return req_function
- except ServerError, err:
+ except ServerError as err:
return str(err)
except Exception:
err = self._format_exception(req_object, req_method,
@@ -498,7 +498,7 @@ def dispatch_until_empty(self):
if not self.read_and_dispatch(timeout=0):
break
count += 1
- newline_idx = self._buffer.find('\n')
+ newline_idx = self._buffer.find(b'\n')
return count
def read_and_dispatch(self, timeout=None, thread=True, condition=None):
@@ -570,7 +570,7 @@ def _send(self, response):
txtResponse = None
try:
txtResponse = json.dumps(response, self)
- except Exception, e:
+ except Exception as e:
_log.error("An unexpected error ocurred when trying to create the message: %r", e)
response = {
'result': None,
@@ -615,7 +615,7 @@ def dispatch_item_single(self, item):
self._send_response(item, fn(*args, **kw))
elif fn:
self._send_error(item, fn)
- except ServerError, exc:
+ except ServerError as exc:
self._send_error(item, str(exc))
except Exception:
err = self._format_exception(obj, method, args, kw,
@@ -709,30 +709,34 @@ def write_line(self, data):
assert('\n' not in data)
self.write_lock.acquire()
try:
+ try:
+ data = data.encode('utf-8')
+ except AttributeError:
+ pass
if self._debug_socket:
- _log.debug("<:%d: %s", len(data), data[:130])
-
- self._wbuffer += list(str(data + '\n'))
+ _log.debug("<:%d: %s", len(data), data.decode('utf-8')[:130])
+
+ self._wbuffer += data + b'\n'
sbytes = 0
- while len(self._wbuffer) > 0:
+ while self._wbuffer:
try:
- sbytes = self._sck.send("".join(self._wbuffer))
+ sbytes = self._sck.send(self._wbuffer)
except IOError:
_log.debug("Read socket error: IOError (timeout: %r)",
self._sck.gettimeout())
_log.debug(traceback.format_exc(0))
- return ''
+ return 0
except socket.error:
_log.debug("Read socket error: socket.error (timeout: %r)",
self._sck.gettimeout())
_log.debug(traceback.format_exc(0))
- return ''
+ return 0
except:
raise
if sbytes == 0:
break
- self._wbuffer[0:sbytes] = []
- if len(self._wbuffer):
+ self._wbuffer = self._wbuffer[sbytes:]
+ if self._wbuffer:
_log.warning("%d bytes left in write buffer", len(self._wbuffer))
return len(self._wbuffer)
finally:
@@ -755,8 +759,8 @@ def read_line(self):
try:
data = self._readn()
if len(data) and self._debug_socket:
- _log.debug(">:%d: %s", len(data), data[:130])
- return data
+ _log.debug(">:%d: %s", len(data), data.decode('utf-8')[:130])
+ return data.decode('utf-8')
finally:
self.read_lock.release()
@@ -835,14 +839,14 @@ def _readn(self):
Internal function which reads from socket waiting for a newline
"""
streambuffer = self._buffer
- pos = streambuffer.find('\n')
+ pos = streambuffer.find(b'\n')
#_log.debug("read...")
#retry = 0
while pos == -1:
- data = ''
+ data = b''
try:
data = self._sck.recv(2048)
- except IOError, inst:
+ except IOError as inst:
_log.debug("Read socket error: IOError%r (timeout: %r)",
inst.args, self._sck.gettimeout())
if inst.errno == errno.EAGAIN:
@@ -855,19 +859,19 @@ def _readn(self):
# _log.debug("Retry %s", retry)
# continue
#_log.debug(traceback.format_exc(0))
- return ''
- except socket.error, inst:
+ return b''
+ except socket.error as inst:
_log.error("Read socket error: socket.error%r (timeout: %r)",
inst.args, self._sck.gettimeout())
#_log.debug(traceback.format_exc(0))
- return ''
+ return b''
except:
raise
if not data:
raise EofError(len(streambuffer))
#_log.debug("readbuf+: %r", data)
streambuffer += data
- pos = streambuffer.find('\n')
+ pos = streambuffer.find(b'\n')
self._buffer = streambuffer[pos + 1:]
streambuffer = streambuffer[:pos]
View
6 bjsonrpc/handlers.py
@@ -147,16 +147,16 @@ def add_method(self, *args, **kwargs):
try:
assert(method.__name__ not in self._methods)
except AssertionError:
- raise NameError, "Method with name %s already in the class methods!" % (method.__name__)
+ raise NameError("Method with name %s already in the class methods!" % (method.__name__))
self._methods[method.__name__] = method
- for name, method in kwargs.iteritems():
+ for name, method in kwargs.items():
if method.__name__ in self.nonpublic_methods:
continue
try:
assert(name not in self._methods)
except AssertionError:
- raise NameError, "Method with name %s already in the class methods!" % (method.__name__)
+ raise NameError("Method with name %s already in the class methods!" % (method.__name__))
self._methods[name] = method
View
4 bjsonrpc/jsonlib.py
@@ -36,7 +36,7 @@
except ImportError:
import json as j
except ImportError:
- print "FATAL: No suitable json library found!"
+ print("FATAL: No suitable json library found!")
raise
from pprint import pprint
@@ -68,4 +68,4 @@ def loads(argobj, conn):
raise
#raise ValueError("The String object is not a valid JSON data!")
- return ret
+ return ret
View
14 bjsonrpc/request.py
@@ -32,7 +32,10 @@
"""
-import Queue
+try:
+ from Queue import Queue
+except ImportError:
+ from queue import Queue
import logging
from threading import Event
import traceback
@@ -84,7 +87,7 @@ class Request(object):
def __init__(self, conn, request_data):
self.conn = conn
self.data = request_data
- self.responses = Queue.Queue()
+ self.responses = Queue()
# TODO: Now that we have a Queue, do we need an Event (and a cv)?
self.event_response = Event()
self.callbacks = []
@@ -125,7 +128,7 @@ def setresponse(self, value):
for callback in self.callbacks:
try:
callback(self)
- except Exception, exc:
+ except Exception as exc:
_log.error("Error on callback: %r", exc)
_log.debug(traceback.format_exc())
@@ -150,9 +153,12 @@ def __call__(self):
def __iter__(self):
return self
- def next(self):
+ def __next__(self):
return self.value
+ def next(self):
+ return self.__next__()
+
def close(self):
reqid, self.request_id, self.auto_close = self.request_id, None, False
if reqid:
View
3  test/test_main.py
@@ -6,7 +6,6 @@
import testserver1
import math
-from types import ListType
class TestJSONBasics(unittest.TestCase):
def setUp(self):
@@ -105,7 +104,7 @@ def test_tuple(self):
"""
rcall = self.conn.call
result = rcall.getabc()
- self.assertTrue(isinstance(result, ListType),"JSON library should convert tuples to lists!")
+ self.assertTrue(isinstance(result, list),"JSON library should convert tuples to lists!")
def test_kwparams(self):
Please sign in to comment.
Something went wrong with that request. Please try again.