Skip to content

Commit

Permalink
remove py27-isms
Browse files Browse the repository at this point in the history
  • Loading branch information
Kriechi committed Aug 5, 2020
1 parent 68a0d8f commit 6299a8a
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 58 deletions.
18 changes: 14 additions & 4 deletions src/hpack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,23 @@
from .hpack import Encoder, Decoder
from .struct import HeaderTuple, NeverIndexedHeaderTuple
from .exceptions import (
HPACKError, HPACKDecodingError, InvalidTableIndex, OversizedHeaderListError, InvalidTableSizeError
HPACKError,
HPACKDecodingError,
InvalidTableIndex,
OversizedHeaderListError,
InvalidTableSizeError
)

__all__ = [
'Encoder', 'Decoder', 'HPACKError', 'HPACKDecodingError',
'InvalidTableIndex', 'HeaderTuple', 'NeverIndexedHeaderTuple',
'OversizedHeaderListError', 'InvalidTableSizeError'
'Encoder',
'Decoder',
'HeaderTuple',
'NeverIndexedHeaderTuple',
'HPACKError',
'HPACKDecodingError',
'InvalidTableIndex',
'OversizedHeaderListError',
'InvalidTableSizeError',
]

__version__ = '4.0.0+dev'
4 changes: 2 additions & 2 deletions src/hpack/hpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def _to_bytes(string):
return string if isinstance(string, bytes) else string.encode('utf-8')


class Encoder(object):
class Encoder:
"""
An HPACK encoder object. This object takes HTTP headers and emits encoded
HTTP/2 header blocks.
Expand Down Expand Up @@ -371,7 +371,7 @@ def _encode_table_size_change(self):
return block


class Decoder(object):
class Decoder:
"""
An HPACK decoder object.
Expand Down
2 changes: 1 addition & 1 deletion src/hpack/huffman.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from .compat import to_byte, decode_hex


class HuffmanEncoder(object):
class HuffmanEncoder:
"""
Encodes a string according to the Huffman encoding table defined in the
HPACK specification.
Expand Down
4 changes: 2 additions & 2 deletions src/hpack/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class HeaderTuple(tuple):

indexable = True

def __new__(_cls, *args):
return tuple.__new__(_cls, args)
def __new__(cls, *args):
return tuple.__new__(cls, args)


class NeverIndexedHeaderTuple(HeaderTuple):
Expand Down
2 changes: 1 addition & 1 deletion src/hpack/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def table_entry_size(name, value):
return 32 + len(name) + len(value)


class HeaderTable(object):
class HeaderTable:
"""
Implements the combined static and dynamic header table
Expand Down
8 changes: 4 additions & 4 deletions test/test_encode_decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
from hypothesis import given
from hypothesis.strategies import integers, binary, one_of

from hpack import HPACKDecodingError
from hpack.hpack import encode_integer, decode_integer
from hpack.exceptions import HPACKDecodingError


class TestIntegerEncoding(object):
class TestIntegerEncoding:
# These tests are stolen from the HPACK spec.
def test_encoding_10_with_5_bit_prefix(self):
val = encode_integer(10, 5)
Expand All @@ -29,7 +29,7 @@ def test_encoding_42_with_8_bit_prefix(self):
assert val == bytearray(b'\x2a')


class TestIntegerDecoding(object):
class TestIntegerDecoding:
# These tests are stolen from the HPACK spec.
def test_decoding_10_with_5_bit_prefix(self):
val = decode_integer(b'\x0a', 5)
Expand All @@ -52,7 +52,7 @@ def test_decode_insufficient_data_fails(self):
decode_integer(b'\x1f', 5)


class TestEncodingProperties(object):
class TestEncodingProperties:
"""
Property-based tests for our integer encoder and decoder.
"""
Expand Down
24 changes: 15 additions & 9 deletions test/test_hpack.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
# -*- coding: utf-8 -*-
from hpack.hpack import Encoder, Decoder, _dict_to_iterable, _to_bytes
from hpack.exceptions import (
HPACKDecodingError, InvalidTableIndex, OversizedHeaderListError,
InvalidTableSizeError
)
from hpack.struct import HeaderTuple, NeverIndexedHeaderTuple
import itertools
import pytest

from hypothesis import given
from hypothesis.strategies import text, binary, sets, one_of

from hpack import (
Encoder,
Decoder,
HeaderTuple,
NeverIndexedHeaderTuple,
HPACKDecodingError,
InvalidTableIndex,
OversizedHeaderListError,
InvalidTableSizeError,
)
from hpack.hpack import _dict_to_iterable, _to_bytes

try:
unicode = unicode
except NameError:
unicode = str


class TestHPACKEncoder(object):
class TestHPACKEncoder:
# These tests are stolen entirely from the IETF specification examples.
def test_literal_header_field_with_indexing(self):
"""
Expand Down Expand Up @@ -338,7 +344,7 @@ def test_evicting_header_table_objects(self):
assert len(e.header_table.dynamic_entries) == 1


class TestHPACKDecoder(object):
class TestHPACKDecoder:
# These tests are stolen entirely from the IETF specification examples.
def test_literal_header_field_with_indexing(self):
"""
Expand Down Expand Up @@ -737,7 +743,7 @@ def test_truncated_header_value(self):
d.decode(data)


class TestDictToIterable(object):
class TestDictToIterable:
"""
The dict_to_iterable function has some subtle requirements: validates that
everything behaves as expected.
Expand Down
6 changes: 3 additions & 3 deletions test/test_hpack_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
long time to run, so they're outside the main test suite, but they need to be
run before every change to HPACK.
"""
from hpack.hpack import Decoder, Encoder
from hpack.struct import HeaderTuple
from binascii import unhexlify
from pytest import skip

from hpack import Decoder, Encoder, HeaderTuple

class TestHPACKDecoderIntegration(object):

class TestHPACKDecoderIntegration:
def test_can_decode_a_story(self, story):
d = Decoder()

Expand Down
8 changes: 4 additions & 4 deletions test/test_huffman.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# -*- coding: utf-8 -*-
from hpack.exceptions import HPACKDecodingError
from hpack.huffman_table import decode_huffman
from hpack import HPACKDecodingError
from hpack.huffman import HuffmanEncoder
from hpack.huffman_constants import REQUEST_CODES, REQUEST_CODES_LENGTH
from hpack.huffman_table import decode_huffman

from hypothesis import given, example
from hypothesis.strategies import binary


class TestHuffman(object):
class TestHuffman:

def test_request_huffman_decoder(self):
assert (
Expand All @@ -34,7 +34,7 @@ def test_request_huffman_encode(self):
)


class TestHuffmanDecoder(object):
class TestHuffmanDecoder:
@given(data=binary())
@example(b'\xff')
@example(b'\x5f\xff\xff\xff\xff')
Expand Down
4 changes: 2 additions & 2 deletions test/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
"""
import pytest

from hpack.struct import HeaderTuple, NeverIndexedHeaderTuple
from hpack import HeaderTuple, NeverIndexedHeaderTuple


class TestHeaderTuple(object):
class TestHeaderTuple:
def test_is_tuple(self):
"""
HeaderTuple objects are tuples.
Expand Down
38 changes: 12 additions & 26 deletions test/test_table.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
# -*- coding: utf-8 -*-
from hpack.table import HeaderTable, table_entry_size
from hpack.exceptions import InvalidTableIndex
import pytest
import sys
_ver = sys.version_info
is_py2 = _ver[0] == 2
is_py3 = _ver[0] == 3

from hpack import InvalidTableIndex
from hpack.table import HeaderTable, table_entry_size


class TestPackageFunctions(object):
class TestPackageFunctions:
def test_table_entry_size(self):
res = table_entry_size(b'TestName', b'TestValue')
assert res == 49


class TestHeaderTable(object):
class TestHeaderTable:
def test_get_by_index_dynamic_table(self):
tbl = HeaderTable()
off = len(HeaderTable.STATIC_TABLE)
Expand Down Expand Up @@ -54,24 +51,13 @@ def test_repr(self):
tbl.add(b'TestName1', b'TestValue1')
tbl.add(b'TestName2', b'TestValue2')
tbl.add(b'TestName2', b'TestValue2')
# Meh, I hate that I have to do this to test
# repr
if is_py3:
exp = (
"HeaderTable(4096, False, deque(["
"(b'TestName2', b'TestValue2'), "
"(b'TestName2', b'TestValue2'), "
"(b'TestName1', b'TestValue1')"
"]))"
)
else:
exp = (
"HeaderTable(4096, False, deque(["
"('TestName2', 'TestValue2'), "
"('TestName2', 'TestValue2'), "
"('TestName1', 'TestValue1')"
"]))"
)
exp = (
"HeaderTable(4096, False, deque(["
"(b'TestName2', b'TestValue2'), "
"(b'TestName2', b'TestValue2'), "
"(b'TestName1', b'TestValue1')"
"]))"
)
res = repr(tbl)
assert res == exp

Expand Down

0 comments on commit 6299a8a

Please sign in to comment.