@@ -58,8 +58,9 @@ def __init__(self: ptr, resolver: Resolver, wrapper: type | None = None) -> None
5858 """
5959 super ().__init__ (resolver )
6060 self .wrapper = wrapper
61- self ._cached_unwrap : obj | None = None
61+ self ._cached_unwrap : obj | bytes | None = None
6262 self ._cache_valid : bool = False
63+ self ._cached_length : int | None = None
6364
6465 def get (self : ptr ) -> int :
6566 """Return the value of the pointer."""
@@ -82,14 +83,15 @@ def invalidate(self: ptr) -> None:
8283 """Clear the cached unwrap result."""
8384 self ._cached_unwrap = None
8485 self ._cache_valid = False
86+ self ._cached_length = None
8587
86- def unwrap (self : ptr , length : int | None = None ) -> obj :
88+ def unwrap (self : ptr , length : int | None = None ) -> obj | bytes :
8789 """Return the object pointed to by the pointer.
8890
8991 Args:
9092 length: The length of the object in memory this points to.
9193 """
92- if self ._cache_valid :
94+ if self ._cache_valid and self . _cached_length == length :
9395 return self ._cached_unwrap
9496
9597 address = self .get ()
@@ -105,15 +107,16 @@ def unwrap(self: ptr, length: int | None = None) -> obj:
105107
106108 self ._cached_unwrap = result
107109 self ._cache_valid = True
110+ self ._cached_length = length
108111 return result
109112
110- def try_unwrap (self : ptr , length : int | None = None ) -> obj | None :
113+ def try_unwrap (self : ptr , length : int | None = None ) -> obj | bytes | None :
111114 """Return the object pointed to by the pointer, if it is valid.
112115
113116 Args:
114117 length: The length of the object in memory this points to.
115118 """
116- if self ._cache_valid :
119+ if self ._cache_valid and self . _cached_length == length :
117120 return self ._cached_unwrap
118121
119122 address = self .get ()
0 commit comments