Skip to content

Commit

Permalink
Catching AttributeError when repr(fifo) that hasn't had fifo.write(fr…
Browse files Browse the repository at this point in the history
…ame) called yet

Co-authored-by: Jeremy Lainé <jeremy.laine@m4x.org>
  • Loading branch information
ekalosak and jlaine committed Oct 31, 2023
1 parent 24bcd1f commit a26cda1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
23 changes: 15 additions & 8 deletions av/audio/fifo.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@ cdef class AudioFifo:
"""A simple audio sample FIFO (First In First Out) buffer."""

def __repr__(self):
return '<av.%s %s samples of %dhz %s %s at 0x%x>' % (
self.__class__.__name__,
self.samples,
self.sample_rate,
self.layout,
self.format,
id(self),
)
try:
result = '<av.%s %s samples of %dhz %s %s at 0x%x>' % (
self.__class__.__name__,
self.samples,
self.sample_rate,
self.layout,
self.format,
id(self),
)
except AttributeError:
result = '<av.%s uninitialized, use fifo.write(frame), at 0x%x>' % (
self.__class__.__name__,
id(self),
)
return result

def __dealloc__(self):
if self.ptr:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_audiofifo.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,27 @@ def test_data(self):
def test_pts_simple(self):
fifo = av.AudioFifo()

# ensure __repr__ does not crash
self.assertTrue(
str(fifo).startswith(
"<av.AudioFifo uninitialized, use fifo.write(frame), at 0x"
)
)

iframe = av.AudioFrame(samples=1024)
iframe.pts = 0
iframe.sample_rate = 48000
iframe.time_base = "1/48000"

fifo.write(iframe)

# ensure __repr__ was updated
self.assertTrue(
str(fifo).startswith(
"<av.AudioFifo 1024 samples of 48000hz <av.AudioLayout 'stereo'> <av.AudioFormat s16> at 0x"
)
)

oframe = fifo.read(512)
self.assertTrue(oframe is not None)
self.assertEqual(oframe.pts, 0)
Expand Down

0 comments on commit a26cda1

Please sign in to comment.