Skip to content

Commit 5fb92aa

Browse files
committed
Add mode to Codec, display C func in err
1 parent 8b3b5c9 commit 5fb92aa

5 files changed

Lines changed: 15 additions & 7 deletions

File tree

av/codec/codec.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ class Codec:
5151
def is_encoder(self) -> bool: ...
5252
@property
5353
def is_decoder(self) -> bool: ...
54+
@property
55+
def mode(self) -> Literal["r", "w"]: ...
5456
descriptor: Descriptor
5557
@property
5658
def name(self) -> str: ...

av/codec/codec.pyx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ cdef class Codec:
119119
raise RuntimeError("%s is both encoder and decoder.")
120120

121121
def __repr__(self):
122-
mode = "w" if self.is_encoder else "r"
122+
mode = self.mode
123123
return f"<av.{self.__class__.__name__} {self.name} {mode=}>"
124124

125125
def create(self, kind = None):
@@ -130,6 +130,10 @@ cdef class Codec:
130130
from .context import CodecContext
131131
return CodecContext.create(self)
132132

133+
@property
134+
def mode(self):
135+
return "w" if self.is_encoder else "r"
136+
133137
@property
134138
def is_decoder(self):
135139
return not self.is_encoder

av/codec/context.pyx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ cdef class CodecContext:
324324
cdef int res
325325
with nogil:
326326
res = lib.avcodec_send_frame(self.ptr, frame.ptr if frame is not None else NULL)
327-
err_check(res)
327+
err_check(res, func="avcodec_send_frame")
328328

329329
packet = self._recv_packet()
330330
while packet:
@@ -337,7 +337,7 @@ cdef class CodecContext:
337337
cdef int res
338338
with nogil:
339339
res = lib.avcodec_send_packet(self.ptr, packet.ptr if packet is not None else NULL)
340-
err_check(res)
340+
err_check(res, func="avcodec_send_packet")
341341

342342
out = []
343343
while True:
@@ -365,7 +365,7 @@ cdef class CodecContext:
365365

366366
if res == -EAGAIN or res == lib.AVERROR_EOF:
367367
return
368-
err_check(res)
368+
err_check(res, "avcodec_receive_frame")
369369

370370
frame = self._transfer_hwframe(frame)
371371

@@ -384,7 +384,7 @@ cdef class CodecContext:
384384
res = lib.avcodec_receive_packet(self.ptr, packet.ptr)
385385
if res == -EAGAIN or res == lib.AVERROR_EOF:
386386
return
387-
err_check(res)
387+
err_check(res, func="avcodec_receive_packet")
388388

389389
if not res:
390390
return packet

av/error.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11

22
cdef int stash_exception(exc_info=*)
3-
cpdef int err_check(int res, filename=*) except -1
3+
cpdef int err_check(int res, filename=*, func=*) except -1

av/error.pyx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ cdef int stash_exception(exc_info=None):
379379

380380
cdef int _last_log_count = 0
381381

382-
cpdef int err_check(int res, filename=None) except -1:
382+
cpdef int err_check(int res, filename=None, func=None) except -1:
383383
"""Raise appropriate exceptions from library return code."""
384384

385385
global _err_count
@@ -416,6 +416,8 @@ cpdef int err_check(int res, filename=None) except -1:
416416
lib.av_strerror(res, error_buffer, lib.AV_ERROR_MAX_STRING_SIZE)
417417
# Fallback to OS error string if no message
418418
message = error_buffer or os.strerror(code)
419+
if func is not None:
420+
message = func + "(): " + message
419421

420422
cls = classes.get(code, UndefinedError)
421423
raise cls(code, message, filename, log)

0 commit comments

Comments
 (0)