Skip to content

Commit b4dd067

Browse files
committed
Fix copy behavior when extracting wrapped buffers
This reworks the method for extracting wrapped buffer to fix a bug in the existing copy behavior for a specific case. Signed-off-by: cwasicki <[email protected]>
1 parent 35e2d64 commit b4dd067

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/frequenz/sdk/timeseries/_ringbuffer/buffer.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,10 @@ def window(
254254

255255
@staticmethod
256256
def _wrapped_buffer_window(
257-
buffer: FloatArray, start_pos: int, end_pos: int, force_copy: bool = True
257+
buffer: FloatArray,
258+
start_pos: int,
259+
end_pos: int,
260+
force_copy: bool = True,
258261
) -> FloatArray:
259262
"""Get a wrapped window from the given buffer.
260263
@@ -274,17 +277,20 @@ def _wrapped_buffer_window(
274277
"""
275278
# Requested window wraps around the ends
276279
if start_pos >= end_pos:
280+
if isinstance(buffer, list):
281+
return buffer[start_pos:] + buffer[0:end_pos]
282+
assert isinstance(
283+
buffer, np.ndarray
284+
), f"Unsupported buffer type: {type(buffer)}"
277285
if end_pos > 0:
278-
if isinstance(buffer, list):
279-
return buffer[start_pos:] + buffer[0:end_pos]
280-
if isinstance(buffer, np.ndarray):
281-
return np.concatenate((buffer[start_pos:], buffer[0:end_pos]))
282-
assert False, f"Unknown buffer type: {type(buffer)}"
283-
return buffer[start_pos:]
286+
return np.concatenate((buffer[start_pos:], buffer[0:end_pos]))
287+
arr = buffer[start_pos:]
288+
else:
289+
arr = buffer[start_pos:end_pos]
284290

285291
if force_copy:
286-
return deepcopy(buffer[start_pos:end_pos])
287-
return buffer[start_pos:end_pos]
292+
return deepcopy(arr)
293+
return arr
288294

289295
def is_missing(self, timestamp: datetime) -> bool:
290296
"""Check if the given timestamp falls within a gap.

tests/timeseries/test_ringbuffer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,5 +511,5 @@ def test_wrapped_buffer_window() -> None:
511511
assert [3, 9] == list(res1_view)
512512
assert [3, 4] == list(res1_copy)
513513
assert [3, 9] == list(res2_view)
514-
# assert [3, 4] == list(res2_copy) #Fails because of a bug
514+
assert [3, 4] == list(res2_copy)
515515
assert [4, 0] == list(res3_copy)

0 commit comments

Comments
 (0)