Skip to content

Commit cb32255

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 20bd659 commit cb32255

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
@@ -301,7 +301,10 @@ def window(
301301

302302
@staticmethod
303303
def _wrapped_buffer_window(
304-
buffer: FloatArray, start_pos: int, end_pos: int, force_copy: bool = True
304+
buffer: FloatArray,
305+
start_pos: int,
306+
end_pos: int,
307+
force_copy: bool = True,
305308
) -> FloatArray:
306309
"""Get a wrapped window from the given buffer.
307310
@@ -321,17 +324,20 @@ def _wrapped_buffer_window(
321324
"""
322325
# Requested window wraps around the ends
323326
if start_pos >= end_pos:
327+
if isinstance(buffer, list):
328+
return buffer[start_pos:] + buffer[0:end_pos]
329+
assert isinstance(
330+
buffer, np.ndarray
331+
), f"Unsupported buffer type: {type(buffer)}"
324332
if end_pos > 0:
325-
if isinstance(buffer, list):
326-
return buffer[start_pos:] + buffer[0:end_pos]
327-
if isinstance(buffer, np.ndarray):
328-
return np.concatenate((buffer[start_pos:], buffer[0:end_pos]))
329-
assert False, f"Unknown buffer type: {type(buffer)}"
330-
return buffer[start_pos:]
333+
return np.concatenate((buffer[start_pos:], buffer[0:end_pos]))
334+
arr = buffer[start_pos:]
335+
else:
336+
arr = buffer[start_pos:end_pos]
331337

332338
if force_copy:
333-
return deepcopy(buffer[start_pos:end_pos])
334-
return buffer[start_pos:end_pos]
339+
return deepcopy(arr)
340+
return arr
335341

336342
def is_missing(self, timestamp: datetime) -> bool:
337343
"""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
@@ -581,5 +581,5 @@ def test_wrapped_buffer_window() -> None:
581581
assert [3, 9] == list(res1_view)
582582
assert [3, 4] == list(res1_copy)
583583
assert [3, 9] == list(res2_view)
584-
# assert [3, 4] == list(res2_copy) #Fails because of a bug
584+
assert [3, 4] == list(res2_copy)
585585
assert [4, 0] == list(res3_copy)

0 commit comments

Comments
 (0)