Skip to content
This repository

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

Merged
merged 1 commit into from over 1 year ago

2 participants

abarnert David Martínez Martí
abarnert

NOTE: Breaks 2.5 compatibility!

  • 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.)

abarnert 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.)
bb21d28
David Martínez Martí deavid merged commit a7e13a7 into from October 31, 2012
David Martínez Martí deavid closed this October 31, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Oct 31, 2012
abarnert 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.)
bb21d28
This page is out of date. Refresh to see the latest.
58  bjsonrpc/connection.py
@@ -230,7 +230,7 @@ def getmaxtimeout(cls, operation):
230 230
     def __init__(self, sck, address = None, handler_factory = None):
231 231
         self._debug_socket = False
232 232
         self._debug_dispatch = False
233  
-        self._buffer = ''
  233
+        self._buffer = b''
234 234
         self._sck = sck
235 235
         self._address = address
236 236
         self._handler = handler_factory 
@@ -247,7 +247,7 @@ def __init__(self, sck, address = None, handler_factory = None):
247 247
         self.method = Proxy(self, sync_type=1)
248 248
         self.notify = Proxy(self, sync_type=2)
249 249
         self.pipe = Proxy(self, sync_type=3)
250  
-        self._wbuffer = []
  250
+        self._wbuffer = b''
251 251
         self.write_lock = threading.RLock()
252 252
         self.read_lock = threading.RLock()
253 253
         self.getid_lock = threading.Lock()
@@ -406,7 +406,7 @@ def _format_exception(self, obj, method, args, kw, exc):
406 406
         etype, evalue, etb = exc
407 407
         funargs = ", ".join(
408 408
             [repr(x) for x in args] +  
409  
-            ["%s=%s" % (k, repr(x)) for k, x in kw.iteritems()]
  409
+            ["%s=%r" % (k, kw[k]) for k in kw]
410 410
             )
411 411
         if len(funargs) > 40: 
412 412
             funargs = funargs[:37] + "..."
@@ -444,14 +444,14 @@ def _extract_params(self, request):
444 444
         else:
445 445
             req_kwargs = request.get("kwparams", {})
446 446
         if req_kwargs: 
447  
-            req_kwargs = dict((str(k), v) for k, v in req_kwargs.iteritems())
  447
+            req_kwargs = dict((str(k), req_kwargs[k]) for k in req_kwargs)
448 448
         return req_method, req_args, req_kwargs
449 449
         
450 450
     def _find_object(self, req_method, req_args, req_kwargs):
451 451
         if '.' in req_method: # local-object.
452 452
             objectname, req_method = req_method.split('.')[:2]
453 453
             if objectname not in self._objects: 
454  
-                raise ValueError, "Invalid object identifier"
  454
+                raise ValueError("Invalid object identifier")
455 455
             elif req_method == '__delete__':
456 456
                 self._dispatch_delete(objectname)
457 457
             else:
@@ -466,7 +466,7 @@ def _find_method(self, req_object, req_method, req_args, req_kwargs):
466 466
         try:
467 467
             req_function = req_object.get_method(req_method)
468 468
             return req_function
469  
-        except ServerError, err:
  469
+        except ServerError as err:
470 470
             return str(err)
471 471
         except Exception:
472 472
             err = self._format_exception(req_object, req_method,
@@ -498,7 +498,7 @@ def dispatch_until_empty(self):
498 498
             if not self.read_and_dispatch(timeout=0): 
499 499
                 break
500 500
             count += 1
501  
-            newline_idx = self._buffer.find('\n')
  501
+            newline_idx = self._buffer.find(b'\n')
502 502
         return count
503 503
             
504 504
     def read_and_dispatch(self, timeout=None, thread=True, condition=None):
@@ -570,7 +570,7 @@ def _send(self, response):
570 570
         txtResponse = None
571 571
         try:
572 572
             txtResponse = json.dumps(response, self)
573  
-        except Exception, e:
  573
+        except Exception as e:
574 574
             _log.error("An unexpected error ocurred when trying to create the message: %r", e)
575 575
             response = {
576 576
                 'result': None,
@@ -615,7 +615,7 @@ def dispatch_item_single(self, item):
615 615
                     self._send_response(item, fn(*args, **kw))
616 616
                 elif fn:
617 617
                     self._send_error(item, fn)
618  
-            except ServerError, exc:
  618
+            except ServerError as exc:
619 619
                 self._send_error(item, str(exc))
620 620
             except Exception:
621 621
                 err = self._format_exception(obj, method, args, kw,
@@ -709,30 +709,34 @@ def write_line(self, data):
709 709
         assert('\n' not in data)
710 710
         self.write_lock.acquire()
711 711
         try:
  712
+            try:
  713
+                data = data.encode('utf-8')
  714
+            except AttributeError:
  715
+                pass
712 716
             if self._debug_socket: 
713  
-                _log.debug("<:%d: %s", len(data), data[:130])
714  
-            
715  
-            self._wbuffer += list(str(data + '\n'))
  717
+                _log.debug("<:%d: %s", len(data), data.decode('utf-8')[:130])
  718
+
  719
+            self._wbuffer += data + b'\n'
716 720
             sbytes = 0
717  
-            while len(self._wbuffer) > 0:
  721
+            while self._wbuffer:
718 722
                 try:
719  
-                    sbytes = self._sck.send("".join(self._wbuffer))
  723
+                    sbytes = self._sck.send(self._wbuffer)
720 724
                 except IOError:
721 725
                     _log.debug("Read socket error: IOError (timeout: %r)",
722 726
                         self._sck.gettimeout())
723 727
                     _log.debug(traceback.format_exc(0))
724  
-                    return ''
  728
+                    return 0
725 729
                 except socket.error:
726 730
                     _log.debug("Read socket error: socket.error (timeout: %r)",
727 731
                         self._sck.gettimeout())
728 732
                     _log.debug(traceback.format_exc(0))
729  
-                    return ''
  733
+                    return 0
730 734
                 except:
731 735
                     raise
732 736
                 if sbytes == 0: 
733 737
                     break
734  
-                self._wbuffer[0:sbytes] = []
735  
-            if len(self._wbuffer):
  738
+                self._wbuffer = self._wbuffer[sbytes:]
  739
+            if self._wbuffer:
736 740
                 _log.warning("%d bytes left in write buffer", len(self._wbuffer))
737 741
             return len(self._wbuffer)
738 742
         finally:
@@ -755,8 +759,8 @@ def read_line(self):
755 759
         try:
756 760
             data = self._readn()
757 761
             if len(data) and self._debug_socket: 
758  
-                _log.debug(">:%d: %s", len(data), data[:130])
759  
-            return data
  762
+                _log.debug(">:%d: %s", len(data), data.decode('utf-8')[:130])
  763
+            return data.decode('utf-8')
760 764
         finally:
761 765
             self.read_lock.release()
762 766
             
@@ -835,14 +839,14 @@ def _readn(self):
835 839
             Internal function which reads from socket waiting for a newline
836 840
         """
837 841
         streambuffer = self._buffer
838  
-        pos = streambuffer.find('\n')
  842
+        pos = streambuffer.find(b'\n')
839 843
         #_log.debug("read...")
840 844
         #retry = 0
841 845
         while pos == -1:
842  
-            data = ''
  846
+            data = b''
843 847
             try:
844 848
                 data = self._sck.recv(2048)
845  
-            except IOError, inst:
  849
+            except IOError as inst:
846 850
                 _log.debug("Read socket error: IOError%r (timeout: %r)",
847 851
                     inst.args, self._sck.gettimeout())
848 852
                 if inst.errno == errno.EAGAIN:
@@ -855,19 +859,19 @@ def _readn(self):
855 859
                         #    _log.debug("Retry %s", retry)
856 860
                         #    continue
857 861
                 #_log.debug(traceback.format_exc(0))
858  
-                return ''
859  
-            except socket.error, inst:
  862
+                return b''
  863
+            except socket.error as inst:
860 864
                 _log.error("Read socket error: socket.error%r (timeout: %r)", 
861 865
                     inst.args, self._sck.gettimeout())
862 866
                 #_log.debug(traceback.format_exc(0))
863  
-                return ''
  867
+                return b''
864 868
             except:
865 869
                 raise
866 870
             if not data:
867 871
                 raise EofError(len(streambuffer))
868 872
             #_log.debug("readbuf+: %r", data)
869 873
             streambuffer += data
870  
-            pos = streambuffer.find('\n')
  874
+            pos = streambuffer.find(b'\n')
871 875
 
872 876
         self._buffer = streambuffer[pos + 1:]
873 877
         streambuffer = streambuffer[:pos]
6  bjsonrpc/handlers.py
@@ -147,16 +147,16 @@ def add_method(self, *args, **kwargs):
147 147
             try:
148 148
                 assert(method.__name__ not in self._methods)
149 149
             except AssertionError:
150  
-                raise NameError, "Method with name %s already in the class methods!" % (method.__name__)
  150
+                raise NameError("Method with name %s already in the class methods!" % (method.__name__))
151 151
             self._methods[method.__name__] = method
152 152
             
153  
-        for name, method in kwargs.iteritems():
  153
+        for name, method in kwargs.items():
154 154
             if method.__name__ in self.nonpublic_methods: 
155 155
                 continue
156 156
             try:
157 157
                 assert(name not in self._methods)
158 158
             except AssertionError:
159  
-                raise NameError, "Method with name %s already in the class methods!" % (method.__name__)
  159
+                raise NameError("Method with name %s already in the class methods!" % (method.__name__))
160 160
                 
161 161
             self._methods[name] = method
162 162
 
4  bjsonrpc/jsonlib.py
@@ -36,7 +36,7 @@
36 36
 except ImportError:
37 37
     import json as j
38 38
 except ImportError:
39  
-    print "FATAL: No suitable json library found!"
  39
+    print("FATAL: No suitable json library found!")
40 40
     raise
41 41
 from pprint import pprint
42 42
 
@@ -68,4 +68,4 @@ def loads(argobj, conn):
68 68
         raise
69 69
         #raise ValueError("The String object is not a valid JSON data!")
70 70
     
71  
-    return ret
  71
+    return ret
14  bjsonrpc/request.py
@@ -32,7 +32,10 @@
32 32
 
33 33
 """
34 34
 
35  
-import Queue
  35
+try:
  36
+    from Queue import Queue
  37
+except ImportError:
  38
+    from queue import Queue
36 39
 import logging
37 40
 from threading import Event
38 41
 import traceback
@@ -84,7 +87,7 @@ class Request(object):
84 87
     def __init__(self, conn, request_data):
85 88
         self.conn = conn
86 89
         self.data = request_data
87  
-        self.responses = Queue.Queue()
  90
+        self.responses = Queue()
88 91
         # TODO: Now that we have a Queue, do we need an Event (and a cv)?
89 92
         self.event_response = Event()
90 93
         self.callbacks = []
@@ -125,7 +128,7 @@ def setresponse(self, value):
125 128
         for callback in self.callbacks: 
126 129
             try:
127 130
                 callback(self)
128  
-            except Exception, exc:
  131
+            except Exception as exc:
129 132
                 _log.error("Error on callback: %r", exc)
130 133
                 _log.debug(traceback.format_exc())
131 134
                 
@@ -150,9 +153,12 @@ def __call__(self):
150 153
     def __iter__(self):
151 154
         return self
152 155
 
153  
-    def next(self):
  156
+    def __next__(self):
154 157
         return self.value
155 158
 
  159
+    def next(self):
  160
+        return self.__next__()
  161
+
156 162
     def close(self):
157 163
         reqid, self.request_id, self.auto_close = self.request_id, None, False
158 164
         if reqid:
3  test/test_main.py
@@ -6,7 +6,6 @@
6 6
 
7 7
 import testserver1
8 8
 import math
9  
-from types import ListType
10 9
 
11 10
 class TestJSONBasics(unittest.TestCase):
12 11
     def setUp(self):
@@ -105,7 +104,7 @@ def test_tuple(self):
105 104
         """
106 105
         rcall = self.conn.call 
107 106
         result = rcall.getabc()
108  
-        self.assertTrue(isinstance(result, ListType),"JSON library should convert tuples to lists!")
  107
+        self.assertTrue(isinstance(result, list),"JSON library should convert tuples to lists!")
109 108
         
110 109
         
111 110
     def test_kwparams(self):
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.