Skip to content
This repository was archived by the owner on Dec 10, 2018. It is now read-only.

Commit

Permalink
expose socket handle in cython transports
Browse files Browse the repository at this point in the history
  • Loading branch information
maralla committed Apr 14, 2016
1 parent d9afc32 commit f52bab5
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 2 deletions.
18 changes: 18 additions & 0 deletions tests/test_cytransport.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,21 @@ def test_buffered_read():
t.flush()

assert t.read(4) == b"ping"


def test_transport_handle():
from thriftpy._compat import CYTHON
if not CYTHON:
return

from thriftpy.transport import TSocket
from thriftpy.transport.memory import TCyMemoryBuffer

s = TSocket()
s.set_handle('the sock')

assert TCyBufferedTransport(s).sock == 'the sock'
assert TCyFramedTransport(s).sock == 'the sock'
assert TCyMemoryBuffer().sock is None
assert TCyBufferedTransport(TCyFramedTransport(s)).sock == 'the sock'
assert TCyBufferedTransport(TCyMemoryBuffer()).sock is None
1 change: 0 additions & 1 deletion thriftpy/transport/buffered/cybuffered.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ cdef class TCyBufferedTransport(CyTransportBase):
"""binary reader/writer"""

cdef:
object trans
TCyBuffer rbuf, wbuf

def __init__(self, trans, int buf_size=DEFAULT_BUFFER):
Expand Down
2 changes: 2 additions & 0 deletions thriftpy/transport/cybase.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ cdef class TCyBuffer(object):


cdef class CyTransportBase(object):
cdef object trans

cdef c_read(self, int sz, char* out)
cdef c_write(self, char* data, int sz)
cdef c_flush(self)
Expand Down
6 changes: 6 additions & 0 deletions thriftpy/transport/cybase.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ cdef class CyTransportBase(object):
def clean(self):
pass

@property
def sock(self):
if not self.trans:
return
return getattr(self.trans, 'sock', None)

cdef get_string(self, int sz):
cdef:
char out[STACK_STRING_LEN]
Expand Down
1 change: 0 additions & 1 deletion thriftpy/transport/framed/cyframed.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ cdef extern from "../../protocol/cybin/endian_port.h":

cdef class TCyFramedTransport(CyTransportBase):
cdef:
object trans
TCyBuffer rbuf, rframe_buf, wframe_buf

def __init__(self, trans, int buf_size=DEFAULT_BUFFER):
Expand Down
1 change: 1 addition & 0 deletions thriftpy/transport/memory/cymemory.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ cdef class TCyMemoryBuffer(CyTransportBase):
cdef TCyBuffer buf

def __init__(self, value=b'', int buf_size=DEFAULT_BUFFER):
self.trans = None
self.buf = TCyBuffer(buf_size)

if value:
Expand Down

0 comments on commit f52bab5

Please sign in to comment.