diff --git a/asynctnt/__init__.py b/asynctnt/__init__.py index 865a14b..2bbd098 100644 --- a/asynctnt/__init__.py +++ b/asynctnt/__init__.py @@ -1,4 +1,4 @@ from .connection import Connection, connect from .iproto.protocol import Iterator, Response -__version__ = '0.0.8' +__version__ = '0.0.9' diff --git a/asynctnt/connection.py b/asynctnt/connection.py index ceac410..7d1a3fb 100644 --- a/asynctnt/connection.py +++ b/asynctnt/connection.py @@ -466,7 +466,6 @@ def ping(self, *, timeout=-1): """ Ping request coroutine :param timeout: Request timeout - :rtype: asynctnt.Response """ return self._db.ping(timeout=timeout) @@ -478,7 +477,6 @@ def call16(self, func_name, args=None, *, timeout=-1): :param func_name: function name to call :param args: arguments to pass to the function (list object) :param timeout: Request timeout - :rtype: asynctnt.Response """ return self._db.call16(func_name, args, timeout=timeout) @@ -493,7 +491,6 @@ def call(self, func_name, args=None, *, timeout=-1): :param func_name: function name to call :param args: arguments to pass to the function (list object) :param timeout: Request timeout - :rtype: asynctnt.Response """ return self._db.call(func_name, args, timeout=timeout) @@ -506,7 +503,6 @@ def eval(self, expression, args=None, *, timeout=-1): :param args: arguments to pass to the function, that will execute your expression (list object) :param timeout: Request timeout - :rtype: asynctnt.Response """ return self._db.eval(expression, args, timeout=timeout) @@ -526,7 +522,6 @@ def select(self, space, key=None, **kwargs): * asynctnt.Iterator object * string with an iterator name :param timeout: Request timeout - :rtype: asynctnt.Response """ return self._db.select(space, key, **kwargs) @@ -538,7 +533,6 @@ def insert(self, space, t, *, replace=False, timeout=-1): :param t: tuple to insert (list object) :param replace: performs replace request instead of insert :param timeout: Request timeout - :rtype: asynctnt.Response """ return self._db.insert(space, t, replace=replace, timeout=timeout) @@ -550,7 +544,6 @@ def replace(self, space, t, *, timeout=-1): :param space: space id or space name. :param t: tuple to insert (list object) :param timeout: Request timeout - :rtype: asynctnt.Response """ return self._db.replace(space, t, timeout=timeout) @@ -563,7 +556,6 @@ def delete(self, space, key, **kwargs): :param key: key to delete :param index: index id or name :param timeout: Request timeout - :rtype: asynctnt.Response """ return self._db.delete(space, key, **kwargs) @@ -592,7 +584,6 @@ def upsert(self, space, t, operations, **kwargs): Please refer to https://tarantool.org/doc/book/box/box_space.html?highlight=update#lua-function.space_object.update :param timeout: Request timeout - :rtype: asynctnt.Response """ return self._db.upsert(space, t, operations, **kwargs) diff --git a/asynctnt/iproto/buffer.pxd b/asynctnt/iproto/buffer.pxd index 73c3962..6f56043 100644 --- a/asynctnt/iproto/buffer.pxd +++ b/asynctnt/iproto/buffer.pxd @@ -15,13 +15,14 @@ cdef class WriteBuffer: int _view_count # Number of memoryviews attached to the buffer bytes _encoding + str _encoding_str ssize_t __op_offset ssize_t __sync_offset ssize_t __schema_id_offset @staticmethod - cdef WriteBuffer new(bytes encoding=*) + cdef WriteBuffer new(bytes encoding, str encoding_str) cdef inline _check_readonly(self) cdef inline len(self) diff --git a/asynctnt/iproto/buffer.pyx b/asynctnt/iproto/buffer.pyx index ef14a66..c4b3650 100644 --- a/asynctnt/iproto/buffer.pyx +++ b/asynctnt/iproto/buffer.pyx @@ -27,6 +27,7 @@ cdef class WriteBuffer: self._size = _BUFFER_INITIAL_SIZE self._length = 0 self._encoding = None + self._encoding_str = None self.__op_offset = -1 self.__sync_offset = -1 self.__schema_id_offset = -1 @@ -54,10 +55,11 @@ cdef class WriteBuffer: self._view_count -= 1 @staticmethod - cdef WriteBuffer new(bytes encoding=None): + cdef WriteBuffer new(bytes encoding, str encoding_str): cdef WriteBuffer buf buf = WriteBuffer.__new__(WriteBuffer) buf._encoding = encoding + buf._encoding_str = encoding_str return buf cdef inline _check_readonly(self): @@ -323,7 +325,7 @@ cdef class WriteBuffer: return self._encode_bin(p, o_string_str, o_string_len) elif isinstance(o, str): - o_string_temp = encode_unicode_string(o, self._encoding) + o_string_temp = o.encode(self._encoding_str, 'strict') o_string_str = NULL o_string_len = 0 cpython.bytes.PyBytes_AsStringAndSize(o_string_temp, diff --git a/asynctnt/iproto/db.pxd b/asynctnt/iproto/db.pxd index 8555a66..8a35b37 100644 --- a/asynctnt/iproto/db.pxd +++ b/asynctnt/iproto/db.pxd @@ -5,6 +5,7 @@ cdef class Db: cdef: BaseProtocol _protocol bytes _encoding + str _encoding_str @staticmethod cdef inline Db new(BaseProtocol protocol) diff --git a/asynctnt/iproto/db.pyx b/asynctnt/iproto/db.pyx index 7d79ab4..a8aa2bd 100644 --- a/asynctnt/iproto/db.pyx +++ b/asynctnt/iproto/db.pyx @@ -9,12 +9,14 @@ cdef class Db: def __cinit__(self): self._protocol = None self._encoding = None + self._encoding_str = None @staticmethod cdef inline Db new(BaseProtocol protocol): cdef Db db = Db.__new__(Db) db._protocol = protocol db._encoding = protocol.encoding + db._encoding_str = decode_string(db._encoding, b'ascii') return db cdef inline uint64_t next_sync(self): @@ -44,7 +46,7 @@ cdef class Db: op = tnt.TP_PING sync = self.next_sync() schema_id = self._protocol._schema_id - buf = WriteBuffer.new(self._encoding) + buf = WriteBuffer.new(self._encoding, self._encoding_str) buf.write_header(sync, op, schema_id) buf.write_length() return Request.new(op, sync, schema_id, buf) @@ -59,7 +61,7 @@ cdef class Db: op = tnt.TP_CALL_16 sync = self.next_sync() schema_id = self._protocol._schema_id - buf = WriteBuffer.new(self._encoding) + buf = WriteBuffer.new(self._encoding, self._encoding_str) buf.write_header(sync, op, schema_id) buf.encode_request_call(func_name, args) buf.write_length() @@ -75,7 +77,7 @@ cdef class Db: op = tnt.TP_CALL sync = self.next_sync() schema_id = self._protocol._schema_id - buf = WriteBuffer.new(self._encoding) + buf = WriteBuffer.new(self._encoding, self._encoding_str) buf.write_header(sync, op, schema_id) buf.encode_request_call(func_name, args) buf.write_length() @@ -91,7 +93,7 @@ cdef class Db: op = tnt.TP_EVAL sync = self.next_sync() schema_id = self._protocol._schema_id - buf = WriteBuffer.new(self._encoding) + buf = WriteBuffer.new(self._encoding, self._encoding_str) buf.write_header(sync, op, schema_id) buf.encode_request_eval(expression, args) buf.write_length() @@ -108,7 +110,7 @@ cdef class Db: op = tnt.TP_SELECT sync = self.next_sync() schema_id = self._protocol._schema_id - buf = WriteBuffer.new(self._encoding) + buf = WriteBuffer.new(self._encoding, self._encoding_str) buf.write_header(sync, op, schema_id) buf.encode_request_select(space, index, key, offset, limit, iterator) @@ -125,7 +127,7 @@ cdef class Db: op = tnt.TP_INSERT if not replace else tnt.TP_REPLACE sync = self.next_sync() schema_id = self._protocol._schema_id - buf = WriteBuffer.new(self._encoding) + buf = WriteBuffer.new(self._encoding, self._encoding_str) buf.write_header(sync, op, schema_id) buf.encode_request_insert(space, t) buf.write_length() @@ -141,7 +143,7 @@ cdef class Db: op = tnt.TP_DELETE sync = self.next_sync() schema_id = self._protocol._schema_id - buf = WriteBuffer.new(self._encoding) + buf = WriteBuffer.new(self._encoding, self._encoding_str) buf.write_header(sync, op, schema_id) buf.encode_request_delete(space, index, key) buf.write_length() @@ -158,7 +160,7 @@ cdef class Db: op = tnt.TP_UPDATE sync = self.next_sync() schema_id = self._protocol._schema_id - buf = WriteBuffer.new(self._encoding) + buf = WriteBuffer.new(self._encoding, self._encoding_str) buf.write_header(sync, op, schema_id) buf.encode_request_update(space, index, key, operations) buf.write_length() @@ -174,7 +176,7 @@ cdef class Db: op = tnt.TP_UPSERT sync = self.next_sync() schema_id = self._protocol._schema_id - buf = WriteBuffer.new(self._encoding) + buf = WriteBuffer.new(self._encoding, self._encoding_str) buf.write_header(sync, op, schema_id) buf.encode_request_upsert(space, t, operations) buf.write_length() @@ -193,7 +195,7 @@ cdef class Db: op = tnt.TP_AUTH sync = self.next_sync() schema_id = self._protocol._schema_id - buf = WriteBuffer.new(self._encoding) + buf = WriteBuffer.new(self._encoding, self._encoding_str) buf.write_header(sync, op, schema_id) username_bytes = encode_unicode_string(username, self._encoding) diff --git a/asynctnt/iproto/unicode.pxd b/asynctnt/iproto/unicode.pxd index d3490dc..3603266 100644 --- a/asynctnt/iproto/unicode.pxd +++ b/asynctnt/iproto/unicode.pxd @@ -1 +1,2 @@ cdef bytes encode_unicode_string(str s, bytes encoding=*) +cdef str decode_string(bytes b, bytes encoding=*) diff --git a/asynctnt/iproto/unicode.pyx b/asynctnt/iproto/unicode.pyx index 5f38dbb..7ad80b1 100644 --- a/asynctnt/iproto/unicode.pyx +++ b/asynctnt/iproto/unicode.pyx @@ -11,3 +11,9 @@ cdef bytes encode_unicode_string(str s, bytes encoding=b'utf-8'): s, encoding, b'strict' ) return b + + +cdef str decode_string(bytes b, bytes encoding=b'utf-8'): + return cpython.unicode.PyUnicode_FromEncodedObject( + b, encoding, b'strict' + ) diff --git a/temp/bench.py b/temp/bench.py index cc1acaf..27865e1 100644 --- a/temp/bench.py +++ b/temp/bench.py @@ -46,7 +46,7 @@ async def bench_asynctnt(n, b, loop=None): loop = loop or asyncio.get_event_loop() conn = asynctnt.Connection(host='127.0.0.1', - port=3303, + port=3305, username='t1', password='t1', reconnect_timeout=1, loop=loop) diff --git a/temp/init.lua b/temp/init.lua index 850898c..bb4e9ff 100644 --- a/temp/init.lua +++ b/temp/init.lua @@ -1,5 +1,5 @@ box.cfg{ - listen = 3303, + listen = 3305, wal_mode = 'none' } diff --git a/temp/t.py b/temp/t.py index dd9938a..0a55548 100644 --- a/temp/t.py +++ b/temp/t.py @@ -40,7 +40,7 @@ async def main(loop): conn = None try: coro = asyncio.ensure_future( - asynctnt.connect(host='127.0.0.1', port=3303, + asynctnt.connect(host='127.0.0.1', port=3305, username='t1', password='t1', fetch_schema=True, auto_refetch_schema=True, @@ -69,23 +69,19 @@ async def main(loop): # res = await conn.select('tester') # print(res.body) # res = await conn.call('test', timeout=0) - res = await conn.call('long', [10]) + class A(): + def __init__(self, a): + super(A, self).__init__() + self.a = a + + def __str__(self): + return "A({})".format(self.a) + + res = await conn.call('func_param', [A(100)]) print(res) print(res.body) print(res.schema_id) return - - res = await conn.call('long', [2]) - print(res) - if res is not None: - print(res.body) - print(res.schema_id) - - res = await conn.call('long', [5]) - print(res) - if res is not None: - print(res.body) - print(res.schema_id) # res = await conn.refetch_schema() # res = await conn.insert('tester', (2, 'hello', 3)) # res = await conn.update('tester', [2], [(':', 1, 1, 3, 'yo!')])