Commits

Lynn Rees  committed b49f41d

- bytes

  • Participants
  • Parent commits 19ecb02

Comments (0)

Files changed (8)

File crossroads/__init__.py

-
+# -*- coding: utf-8 -*-
+'''Python bindings for Crossroads.I/O network messaging library.'''

File crossroads/constants.py

 # -*- coding: utf-8 -*-
-'''Crossroads constants.'''
+'''Crossroads I/O constants.'''
 
 ################################################################################
 ## CROSSROADS ERRORS ###########################################################

File crossroads/low.py

 # -*- coding: utf-8 -*-
-'''Low level API for ctypes binding to Crossroads.IO.'''
+'''Low level API for Crossroads.I/O.'''
 
 from functools import partial
 from ctypes import byref, sizeof, c_int, c_int64, c_char, c_size_t

File crossroads/lowest.py

 # -*- coding: utf-8 -*-
-'''Lowest-level ctypes bindings to Crossroads.IO API.'''
+'''Lowest-level bindings to Crossroads.I/O.'''
 
 from functools import partial
 from ctypes.util import find_library
     Structure, CDLL, POINTER, memmove, get_errno, c_void_p, c_int, c_char_p,
     c_ubyte, c_size_t, c_short, c_ulong)
 
-
-class XSBaseError(Exception):
-    '''Base exception.'''
-
-
-class XSOperationError(XSBaseError):
-    '''Operational Error.'''
-
-
-class XSError(XSBaseError):
-
-    def __init__(self, errno=None):
-        if errno is None:
-            errno = get_errno()
-        self.strerror = strerror(errno)
-        self.errno = errno
-
-    def __str__(self):
-        return '{self.errno}: {self.strerror}'.format(self=self)
+################################################################################
+## UTILITIES ###################################################################
+################################################################################
+array = lambda ctype, length: (ctype * length)()
 
 
 def _check_nonzero(result, func, arguments):
         raise XSError(errno)
     return result
 
+################################################################################
+## CTYPES BINDING ##############################################################
+################################################################################
 
 def _factory(lib, prefix, restype, func, *args):
     newfunc = getattr(lib, '_'.join([prefix, func]))
         newfunc.errcheck = _check_not_null
     return newfunc
 
-
 XS = CDLL(find_library('xs'), use_errno=True)
-array = lambda ctype, length: (ctype * length)()
 factory = partial(_factory, XS, 'xs')
 intfactory = partial(_factory, XS, 'xs', c_int)
 voidfactory = partial(_factory, XS, 'xs', c_void_p)
-
+# memory mover
 memmove.restype = c_void_p
 
 ################################################################################
 # stopwatch was started.
 # unsigned long xs_stopwatch_stop (void *watch);
 stopwatch_stop = factory(c_ulong, 'stopwatch_stop', c_void_p)
+
+################################################################################
+## EXCEPTIONS ##################################################################
+################################################################################
+
+
+class XSBaseError(Exception):
+    '''Base exception.'''
+
+
+class XSOperationError(XSBaseError):
+    '''Operational Error.'''
+
+
+class XSError(XSBaseError):
+
+    def __init__(self, errno=None):
+        if errno is None:
+            errno = get_errno()
+        self.strerror = strerror(errno)
+        self.errno = errno
+
+    def __str__(self):
+        return '{self.errno}: {self.strerror}'.format(self=self)

File crossroads/message.py

 
 from ctypes import sizeof, string_at, c_ubyte, byref, c_size_t, c_char, c_int
 
-from stuf.six import PY3, tobytes, tounicode, u, b, isunicode
+from stuf.six import PY3, tobytes, tounicode, u, isunicode, byteme
 
 from . import lowest as xs
 from .constants import MORE
 
     def __bytes__(self):
         if self.msg is None:
-            return b('')
+            return b''
         return tobytes(string_at(byref(self.msg), self.size))
 
     def __unicode__(self):
     def __init__(self, size, source):
         super(SendMsg, self).__init__()
         if isinstance(source, Msg):
-            self.msg = source = bytes(source)
+            self.msg = source = byteme(source)
         elif isunicode(source):
-            self.msg = source = b(source)
+            self.msg = source = tobytes(source)
         else:
             self.msg = source
         self.size = len(source) if size is None else size
 
 
+SLOTS2 = 'msg rc more last_rc'.split()
+
 class MsgXS(Msg):
 
     def close(self):
 
 class RecvMsg32(MsgXS):
 
-    __slots__ = SLOTS
+    __slots__ = SLOTS2
 
     def __init__(self):
         super(RecvMsg32, self).__init__()
 
 class SendMsg32(MsgXS):
 
-    __slots__ = SLOTS
+    __slots__ = SLOTS2
 
     def __init__(self, source):
         super(SendMsg32, self).__init__()
             self.msg = source.msg
         else:
             self.msg = xs.msg_t()
-            source = b(source)
+            source = tobytes(source)
             length = len(source)
             self.rc = xs.msg_init_size(self.msg, length)
             xs.memmove(xs.msg_data(self.msg), source, length)
 setup(
     name='crossroads',
     version=getversion('crossroads/__init__.py'),
-    description='Configuration over convention.',
+    description='Python binding for Crossroads.I/O.',
     long_description=open(join(getcwd(), 'README.rst'), 'r').read(),
-    keywords='networking queue socket crossroads io',
+    keywords='messaging queue networking socket crossroads.io TCP/IP',
     license='BSD',
     author='L. C. Rees',
     author_email='lcrees@gmail.com',

File tests/test_low_level.py

 # -*- coding: utf-8 -*-
-'''Low level tests for ctypes binding to Crossroads.IO.'''
+'''Low level tests for bindings to Crossroads.I/O.'''
 
 from stuf.six import unittest
 from stuf.utils import lrange
             pass
 
     def bounce(self, sb, sc):
+        from stuf.six import byteme
         content = b'12345678ABCDEFGH12345678abcdefgh'
         # send the message
         self.assertEqual(sc.send(content, more=True).last_rc, 32)
         self.assertEqual(len(buf2), 32)
         self.assertEqual(buf2.more, 0)
         # check whether the message is still the same
-        self.assertEqual(bytes(buf1), content)
+        self.assertEqual(byteme(buf1), content)
 
     def test_pair_ipc(self):
         sb = self.ctx.pair()
 
     def test_wireformat(self):
         import socket
+        from stuf.six import byteme
         # create the basic infrastructure
         push = self.ctx.push()
         pull = self.ctx.pull()
             self.assertEqual(rpush.send(b'\x04\0abc', 0), 5)
             buf = pull.recv(3)
             self.assertEqual(pull.last_rc, 3)
-            self.assertEqual(bytes(buf), b'abc')
+            self.assertEqual(byteme(buf), b'abc')
             # let's push this data into another socket
             self.assertEqual(push.send(buf).last_rc, 3)
             self.assertNotEqual(rpull.recv(3), b'\x04\0abc')
                 except self.xs.XSError as e:
                     self.assertTrue(e.errno, self.XS.EAGAIN)
             # there should be now 4 messages pending, consume them.
-            for i in lrange(4):
+            for i in lrange(4):  #  @UnusedVariable
                 sb.recv(0)
                 self.assertEqual(sb.last_rc, 0)
             # now it should be possible to send one more.
             with s1.bind_tcp('127.0.0.1:5858'), s2.connect_tcp('127.0.0.1:5858'):
                 self.assertTrue(s1.last_rc >= 0)
                 self.assertTrue(s2.last_rc >= 0)
-                for i in lrange(10):
+                for i in lrange(10):  #  @UnusedVariable
                     self.assertEqual(s2.send('test', nowait=True).last_rc, 4)
                     s1.recv(4)
                     self.assertEqual(s1.last_rc, 4)
             self.assertEqual(sub.last_rc, 0)
 
     def test_regrep_device(self):
+        from stuf.six import byteme
         # create a req/rep device
         xreq = self.ctx.xreq()
         xrep = self.ctx.xrep()
             self.assertEqual(req.send(b'ABC', 3, True).last_rc, 3)
             self.assertEqual(req.send(b'DEF', 3).last_rc, 3)
             # pass the reply through the device
-            for i in lrange(4):
+            for i in lrange(4):  #  @UnusedVariable
                 msg = xrep.recvmsg()
                 self.assertTrue(msg.last_rc >= 0)
                 rc = xreq.sendmsg(msg, True if msg.more else False).last_rc
             self.assertEqual(msg.more, 0)
             # receive the request
             buff = rep.recv(3)
-            self.assertEqual(bytes(buff), b'ABC')
+            self.assertEqual(byteme(buff), b'ABC')
             self.assertEqual(buff.last_rc, 3)
             self.assertEqual(buff.more, 1)
             buff = rep.recv(3)
             self.assertEqual(buff.last_rc, 3)
-            self.assertEqual(bytes(buff), b'DEF')
+            self.assertEqual(byteme(buff), b'DEF')
             self.assertEqual(buff.more, 0)
             # send the reply
             self.assertEqual(rep.send(b'GHI', 3, more=True).last_rc, 3)
             self.assertEqual(rep.send(b'JKL', 3).last_rc, 3)
             # pass the reply through the device
-            for i in lrange(4):
+            for i in lrange(4):  #  @UnusedVariable
                 msg = xreq.recvmsg()
                 self.assertTrue(msg.last_rc >= 0)
                 rc = xrep.sendmsg(msg, True if msg.more else False).last_rc
             # receive the reply
             buff = req.recv(3)
             self.assertEqual(buff.last_rc, 3)
-            self.assertEqual(bytes(buff), b'GHI')
+            self.assertEqual(byteme(buff), b'GHI')
             self.assertEqual(buff.more, 1)
             buff2 = req.recv(3)
             self.assertEqual(buff2.last_rc, 3)
-            self.assertEqual(bytes(buff2), b'JKL')
+            self.assertEqual(byteme(buff2), b'JKL')
             self.assertEqual(buff2.more, 0)
 
     def test_resubscribe(self):

File tests/test_lowest_level.py

 # -*- coding: utf-8 -*-
-'''Lowest level tests for ctypes binding to Crossroads.IO library.'''
+'''Lowest level tests for bindings to Crossroads.I/O.'''
 
 from stuf.utils import lrange
 from stuf.six import unittest
         self.assertEqual(xs.send(req, b'ABC', 3, XS.SNDMORE), 3)
         self.assertEqual(xs.send(req, b'DEF', 3, 0), 3)
         # pass the request through the device.
-        for i in lrange(4):
+        for i in lrange(4):  #  @UnusedVariable
             msg = xs.msg_t()
             self.assertEqual(xs.msg_init(msg), 0)
             rc = xs.recvmsg(xrep, msg, 0)
         self.assertEqual(xs.send(rep, b'GHI', 3, XS.SNDMORE), 3)
         self.assertEqual(xs.send(rep, b'JKL', 3, 0), 3)
         # pass the reply through the device.
-        for i in lrange(4):
+        for i in lrange(4):  #  @UnusedVariable
             msg = xs.msg_t()
             self.assertEqual(xs.msg_init(msg), 0)
             self.assertTrue(xs.recvmsg(xreq, msg, 0) >= 0)
             except xs.XSError as e:
                 self.assertTrue(e.errno, XS.EAGAIN)
         # There should be now 4 messages pending, consume them.
-        for i in lrange(4):
+        for i in lrange(4):  #  @UnusedVariable
             self.assertEqual(xs.recv(sb, None, 0, 0), 0)
         # Now it should be possible to send one more.
         self.assertEqual(xs.send(sc, None, 0, 0), 0)
         )
         self.assertTrue(xs.bind(s1, b'tcp://127.0.0.1:5858') >= 0)
         self.assertTrue(xs.connect(s2, b'tcp://127.0.0.1:5858') >= 0)
-        for i in lrange(10):
+        for i in lrange(10):  #  @UnusedVariable
             self.assertEqual(xs.send(s2, b'test', 4, XS.DONTWAIT), 4)
             buf = array(c_ubyte, 4)
             self.assertEqual(xs.recv(s1, buf, sizeof(buf), 0), 4)