diff --git a/cbor2/types.py b/cbor2/types.py index 04d1050b..b0ab530c 100644 --- a/cbor2/types.py +++ b/cbor2/types.py @@ -44,8 +44,8 @@ class CBORTag: __slots__ = 'tag', 'value' def __init__(self, tag, value): - if not isinstance(tag, int): - raise TypeError('CBORTag tags must be integer numbers') + if not isinstance(tag, int) or tag not in range(2**64): + raise TypeError('CBORTag tags must be positive integers less than 2**64') self.tag = tag self.value = value diff --git a/tests/test_encoder.py b/tests/test_encoder.py index 3e9346b2..6beaa220 100644 --- a/tests/test_encoder.py +++ b/tests/test_encoder.py @@ -520,3 +520,8 @@ def test_encode_stringrefs_dict(impl): 'd81901' 'd81900' ) assert impl.dumps(value, string_referencing=True, canonical=True) == expected + + +def test_negative_tag(impl): + with pytest.raises(TypeError): + impl.dumps(impl.CBORTag(-1, 'value'))