Skip to content

Commit

Permalink
Merge ce9db5d into 260d23a
Browse files Browse the repository at this point in the history
  • Loading branch information
waveform80 committed Jun 2, 2019
2 parents 260d23a + ce9db5d commit a419da8
Show file tree
Hide file tree
Showing 28 changed files with 7,903 additions and 912 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -2,12 +2,16 @@
.pydevproject
.idea/
.coverage
coverage/
.cache/
.tox/
.eggs/
*.egg-info/
*.pyc
*.pyd
*.so
dist/
docs/_build/
build/
virtualenv/
.vs
50 changes: 47 additions & 3 deletions cbor2/__init__.py
@@ -1,3 +1,47 @@
from .decoder import load, loads, CBORDecoder, CBORDecodeError # noqa
from .encoder import dump, dumps, CBOREncoder, CBOREncodeError, shareable_encoder # noqa
from .types import CBORTag, CBORSimpleValue, undefined # noqa
from .decoder import load, loads, CBORDecoder # noqa
from .encoder import dump, dumps, CBOREncoder, shareable_encoder # noqa
from .types import ( # noqa
CBORError,
CBOREncodeError,
CBORDecodeError,
CBORTag,
CBORSimpleValue,
undefined
)

try:
from _cbor2 import * # noqa
except ImportError:
# Couldn't import the optimized C version; ignore the failure and leave the
# pure Python implementations in place.
pass
else:
# The pure Python implementations are replaced with the optimized C
# variants, but we still need to create the encoder dictionaries for the C
# variant here (this is much simpler than doing so in C, and doesn't affect
# overall performance as it's a one-off initialization cost).
def _init_cbor2():
from collections import OrderedDict
from .encoder import default_encoders, canonical_encoders
from .types import CBORTag, CBORSimpleValue, undefined # noqa
import _cbor2
_cbor2.default_encoders = OrderedDict([
((
_cbor2.CBORSimpleValue if type_ is CBORSimpleValue else
_cbor2.CBORTag if type_ is CBORTag else
type(_cbor2.undefined) if type_ is type(undefined) else
type_
), getattr(_cbor2.CBOREncoder, method.__name__))
for type_, method in default_encoders.items()
])
_cbor2.canonical_encoders = OrderedDict([
((
_cbor2.CBORSimpleValue if type_ is CBORSimpleValue else
_cbor2.CBORTag if type_ is CBORTag else
type(_cbor2.undefined) if type_ is type(undefined) else
type_
), getattr(_cbor2.CBOREncoder, method.__name__))
for type_, method in canonical_encoders.items()
])
_init_cbor2()
del _init_cbor2
25 changes: 23 additions & 2 deletions cbor2/compat.py
Expand Up @@ -33,14 +33,35 @@ def int2bytes(i):
pad = ('', '0')[n & 1]
return unhexlify(pad + hexstr)

def recursive_repr(fillvalue='...'):
# Back-ported from Python 3.2
from thread import get_ident

def decorating_function(user_function):
repr_running = set()

def wrapper(self):
key = id(self), get_ident()
if key in repr_running:
return fillvalue
repr_running.add(key)
try:
result = user_function(self)
finally:
repr_running.discard(key)
return result
return wrapper
return decorating_function

byte_as_integer = ord
timezone.utc = timezone(timedelta(0))
xrange = xrange # noqa: F821
range = xrange # noqa: F821
long = long # noqa: F821
unicode = unicode # noqa: F821
else:
from collections.abc import Mapping # noqa: F401
from datetime import timezone
from reprlib import recursive_repr # noqa: F401

def byte_as_integer(bytestr):
return bytestr[0]
Expand All @@ -55,7 +76,7 @@ def int2bytes(i):
bits = i.bit_length()
return i.to_bytes((bits + 7) // 8, 'big')

xrange = range
range = range
long = int
unicode = str

Expand Down

0 comments on commit a419da8

Please sign in to comment.