[mdevaev@reki(g:master) g:/]$ python2 -V
Python 2.7.5
[mdevaev@reki(g:master) g:/]$ python2 test.py
=========================================================================== test session starts ============================================================================
platform linux2 -- Python 2.7.5 -- pytest-2.4.2
collected 8 items
test.py FF...F..
================================================================================= FAILURES =================================================================================
___________________________________________________________________________ test_stream_decoding ___________________________________________________________________________
def test_stream_decoding():
with BytesIO(b'd2:hii1ee') as f:
> mapping = bdecode(f)
test.py:8:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
f_or_data = <_io.BytesIO object at 0x1e18c50>
def bdecode(f_or_data):
"""
bdecodes data by looking up the type byte,
and using it to look up the respective decoding function,
which in turn is used to return the decoded object
The parameter can be a file opened in bytes mode,
bytes or a string (the last of which will be decoded)
"""
if isinstance(f_or_data, str):
f_or_data = f_or_data.encode()
if isinstance(f_or_data, bytes):
f_or_data = BytesIO(f_or_data)
#TODO: the following like is the only one that needs readahead.
#peek returns a arbitrary amount of bytes, so we have to slice.
if f_or_data.seekable():
first_byte = f_or_data.read(1)
f_or_data.seek(-1, SEEK_CUR)
else:
first_byte = f_or_data.peek(1)[:1]
btype = TYPES.get(first_byte)
if btype is not None:
> return btype(f_or_data)
bcoding.py:126:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
f = <_io.BytesIO object at 0x1e18c50>
def _decode_dict(f):
assert_btype(f.read(1), _TYPE_DICT)
ret = {}
> key = bdecode(f)
bcoding.py:86:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
f_or_data = <_io.BytesIO object at 0x1e18c50>
def bdecode(f_or_data):
"""
bdecodes data by looking up the type byte,
and using it to look up the respective decoding function,
which in turn is used to return the decoded object
The parameter can be a file opened in bytes mode,
bytes or a string (the last of which will be decoded)
"""
if isinstance(f_or_data, str):
f_or_data = f_or_data.encode()
if isinstance(f_or_data, bytes):
f_or_data = BytesIO(f_or_data)
#TODO: the following like is the only one that needs readahead.
#peek returns a arbitrary amount of bytes, so we have to slice.
if f_or_data.seekable():
first_byte = f_or_data.read(1)
f_or_data.seek(-1, SEEK_CUR)
else:
first_byte = f_or_data.peek(1)[:1]
btype = TYPES.get(first_byte)
if btype is not None:
return btype(f_or_data)
else: #Used in dicts and lists to designate an end
> assert_btype(f_or_data.read(1), _TYPE_END)
bcoding.py:128:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
byte = '2', typ = 'e'
def assert_btype(byte, typ):
if not byte == typ:
raise TypeError(
'Tried to decode type {!r} with identifier {!r} but got identifier {!r} instead'
> .format(TYPES[typ] or 'End', typ, byte))
E TypeError: Tried to decode type 'End' with identifier 'e' but got identifier '2' instead
bcoding.py:29: TypeError
___________________________________________________________________________ test_buffer_decoding ___________________________________________________________________________
def test_buffer_decoding():
> assert bdecode(b'3:one') == 'one'
test.py:12:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
f_or_data = <_io.BytesIO object at 0x1e18f50>
def bdecode(f_or_data):
"""
bdecodes data by looking up the type byte,
and using it to look up the respective decoding function,
which in turn is used to return the decoded object
The parameter can be a file opened in bytes mode,
bytes or a string (the last of which will be decoded)
"""
if isinstance(f_or_data, str):
f_or_data = f_or_data.encode()
if isinstance(f_or_data, bytes):
f_or_data = BytesIO(f_or_data)
#TODO: the following like is the only one that needs readahead.
#peek returns a arbitrary amount of bytes, so we have to slice.
if f_or_data.seekable():
first_byte = f_or_data.read(1)
f_or_data.seek(-1, SEEK_CUR)
else:
first_byte = f_or_data.peek(1)[:1]
btype = TYPES.get(first_byte)
if btype is not None:
return btype(f_or_data)
else: #Used in dicts and lists to designate an end
> assert_btype(f_or_data.read(1), _TYPE_END)
bcoding.py:128:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
byte = '3', typ = 'e'
def assert_btype(byte, typ):
if not byte == typ:
raise TypeError(
'Tried to decode type {!r} with identifier {!r} but got identifier {!r} instead'
> .format(TYPES[typ] or 'End', typ, byte))
E TypeError: Tried to decode type 'End' with identifier 'e' but got identifier '3' instead
bcoding.py:29: TypeError
______________________________________________________________________ test_decode_incomplete_buffer _______________________________________________________________________
def test_decode_incomplete_buffer():
with raises(ValueError):
> bdecode('1:')
test.py:31:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
f_or_data = <_io.BytesIO object at 0x1ca3470>
def bdecode(f_or_data):
"""
bdecodes data by looking up the type byte,
and using it to look up the respective decoding function,
which in turn is used to return the decoded object
The parameter can be a file opened in bytes mode,
bytes or a string (the last of which will be decoded)
"""
if isinstance(f_or_data, str):
f_or_data = f_or_data.encode()
if isinstance(f_or_data, bytes):
f_or_data = BytesIO(f_or_data)
#TODO: the following like is the only one that needs readahead.
#peek returns a arbitrary amount of bytes, so we have to slice.
if f_or_data.seekable():
first_byte = f_or_data.read(1)
f_or_data.seek(-1, SEEK_CUR)
else:
first_byte = f_or_data.peek(1)[:1]
btype = TYPES.get(first_byte)
if btype is not None:
return btype(f_or_data)
else: #Used in dicts and lists to designate an end
> assert_btype(f_or_data.read(1), _TYPE_END)
bcoding.py:128:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
byte = '1', typ = 'e'
def assert_btype(byte, typ):
if not byte == typ:
raise TypeError(
'Tried to decode type {!r} with identifier {!r} but got identifier {!r} instead'
> .format(TYPES[typ] or 'End', typ, byte))
E TypeError: Tried to decode type 'End' with identifier 'e' but got identifier '1' instead
bcoding.py:29: TypeError
==================================================================== 3 failed, 5 passed in 0.03 seconds ====================================================================