Skip to content

Commit ef4c318

Browse files
authored
Drop unnecessary hack with WeakMethod (#987)
1 parent 331f6d5 commit ef4c318

File tree

3 files changed

+4
-57
lines changed

3 files changed

+4
-57
lines changed

aiokafka/protocol/message.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
zstd_decode,
1414
)
1515
from aiokafka.errors import UnsupportedCodecError
16-
from aiokafka.util import WeakMethod
1716

1817
from .struct import Struct
1918
from .types import AbstractType, Bytes, Int8, Int32, Int64, Schema, UInt32
@@ -63,7 +62,6 @@ def __init__(self, value, key=None, magic=0, attributes=0, crc=0, timestamp=None
6362
self.attributes = attributes
6463
self.key = key
6564
self.value = value
66-
self.encode = WeakMethod(self._encode_self)
6765

6866
@property
6967
def timestamp_type(self):
@@ -79,7 +77,7 @@ def timestamp_type(self):
7977
else:
8078
return 0
8179

82-
def _encode_self(self, recalc_crc=True):
80+
def encode(self, recalc_crc=True):
8381
version = self.magic
8482
if version == 1:
8583
fields = (
@@ -129,7 +127,7 @@ def decode(cls, data):
129127

130128
def validate_crc(self):
131129
if self._validated_crc is None:
132-
raw_msg = self._encode_self(recalc_crc=False)
130+
raw_msg = self.encode(recalc_crc=False)
133131
self._validated_crc = crc32(raw_msg[4:])
134132
if self.crc == self._validated_crc:
135133
return True
@@ -170,7 +168,7 @@ def decompress(self):
170168
return MessageSet.decode(raw_bytes, bytes_to_read=len(raw_bytes))
171169

172170
def __hash__(self):
173-
return hash(self._encode_self(recalc_crc=False))
171+
return hash(self.encode(recalc_crc=False))
174172

175173

176174
class PartialMessage(bytes):

aiokafka/protocol/struct.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from io import BytesIO
22

3-
from aiokafka.util import WeakMethod
4-
53
from .abstract import AbstractType
64
from .types import Schema
75

@@ -25,19 +23,7 @@ def __init__(self, *args, **kwargs):
2523
)
2624
)
2725

28-
# overloading encode() to support both class and instance
29-
# Without WeakMethod() this creates circular ref, which
30-
# causes instances to "leak" to garbage
31-
self.encode = WeakMethod(self._encode_self)
32-
33-
@classmethod
34-
def encode(cls, item):
35-
bits = []
36-
for i, field in enumerate(cls.SCHEMA.fields):
37-
bits.append(field.encode(item[i]))
38-
return b"".join(bits)
39-
40-
def _encode_self(self):
26+
def encode(self):
4127
return self.SCHEMA.encode([self.__dict__[name] for name in self.SCHEMA.names])
4228

4329
@classmethod

aiokafka/util.py

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
import asyncio
44
import os
5-
import weakref
65
from asyncio import AbstractEventLoop
7-
from types import MethodType
86
from typing import (
97
Any,
108
Awaitable,
@@ -104,38 +102,3 @@ def get_running_loop() -> asyncio.AbstractEventLoop:
104102

105103
INTEGER_MAX_VALUE = 2**31 - 1
106104
INTEGER_MIN_VALUE = -(2**31)
107-
108-
109-
class WeakMethod:
110-
"""
111-
Callable that weakly references a method and the object it is bound to. It
112-
is based on https://stackoverflow.com/a/24287465.
113-
114-
Arguments:
115-
116-
object_dot_method: A bound instance method (i.e. 'object.method').
117-
"""
118-
119-
def __init__(self, object_dot_method: MethodType) -> None:
120-
self.target = weakref.ref(object_dot_method.__self__)
121-
self._target_id = id(self.target())
122-
self.method = weakref.ref(object_dot_method.__func__)
123-
self._method_id = id(self.method())
124-
125-
def __call__(self, *args: Any, **kwargs: Any) -> Any:
126-
"""
127-
Calls the method on target with args and kwargs.
128-
"""
129-
method = self.method()
130-
assert method is not None
131-
return method(self.target(), *args, **kwargs)
132-
133-
def __hash__(self) -> int:
134-
return hash(self.target) ^ hash(self.method)
135-
136-
def __eq__(self, other: Any) -> bool:
137-
if not isinstance(other, WeakMethod):
138-
return False
139-
return (
140-
self._target_id == other._target_id and self._method_id == other._method_id
141-
)

0 commit comments

Comments
 (0)