-
Notifications
You must be signed in to change notification settings - Fork 377
/
Copy patherror.pyx
428 lines (317 loc) · 12 KB
/
error.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
cimport libav as lib
from libc.stdlib cimport free, malloc
from av.logging cimport get_last_error
import errno
import os
import sys
import traceback
from threading import local
# Will get extended with all of the exceptions.
__all__ = [
"ErrorType", "FFmpegError", "LookupError", "HTTPError", "HTTPClientError",
"UndefinedError",
]
cpdef code_to_tag(int code):
"""Convert an integer error code into 4-byte tag.
>>> code_to_tag(1953719668)
b'test'
"""
return bytes((
code & 0xff,
(code >> 8) & 0xff,
(code >> 16) & 0xff,
(code >> 24) & 0xff,
))
cpdef tag_to_code(bytes tag):
"""Convert a 4-byte error tag into an integer code.
>>> tag_to_code(b'test')
1953719668
"""
if len(tag) != 4:
raise ValueError("Error tags are 4 bytes.")
return (
(tag[0]) +
(tag[1] << 8) +
(tag[2] << 16) +
(tag[3] << 24)
)
class FFmpegError(Exception):
"""Exception class for errors from within FFmpeg.
.. attribute:: errno
FFmpeg's integer error code.
.. attribute:: strerror
FFmpeg's error message.
.. attribute:: filename
The filename that was being operated on (if available).
.. attribute:: log
The tuple from :func:`av.logging.get_last_log`, or ``None``.
"""
def __init__(self, code, message, filename=None, log=None):
self.errno = code
self.strerror = message
args = [code, message]
if filename or log:
args.append(filename)
if log:
args.append(log)
super(FFmpegError, self).__init__(*args)
self.args = tuple(args) # FileNotFoundError/etc. only pulls 2 args.
@property
def filename(self):
try:
return self.args[2]
except IndexError:
pass
@property
def log(self):
try:
return self.args[3]
except IndexError:
pass
def __str__(self):
msg = f"[Errno {self.errno}] {self.strerror}"
if self.filename:
msg = f"{msg}: {self.filename!r}"
if self.log:
msg = f"{msg}; last error log: [{self.log[1].strip()}] {self.log[2].strip()}"
return msg
# Our custom error, used in callbacks.
cdef int c_PYAV_STASHED_ERROR = tag_to_code(b"PyAV")
cdef str PYAV_STASHED_ERROR_message = "Error in PyAV callback"
# Bases for the FFmpeg-based exceptions.
class LookupError(FFmpegError, LookupError):
pass
class HTTPError(FFmpegError):
pass
class HTTPClientError(FFmpegError):
pass
# Tuples of (enum_name, enum_value, exc_name, exc_base).
_ffmpeg_specs = (
("BSF_NOT_FOUND", -lib.AVERROR_BSF_NOT_FOUND, "BSFNotFoundError", LookupError),
("BUG", -lib.AVERROR_BUG, None, RuntimeError),
("BUFFER_TOO_SMALL", -lib.AVERROR_BUFFER_TOO_SMALL, None, ValueError),
("DECODER_NOT_FOUND", -lib.AVERROR_DECODER_NOT_FOUND, None, LookupError),
("DEMUXER_NOT_FOUND", -lib.AVERROR_DEMUXER_NOT_FOUND, None, LookupError),
("ENCODER_NOT_FOUND", -lib.AVERROR_ENCODER_NOT_FOUND, None, LookupError),
("EOF", -lib.AVERROR_EOF, "EOFError", EOFError),
("EXIT", -lib.AVERROR_EXIT, None, None),
("EXTERNAL", -lib.AVERROR_EXTERNAL, None, None),
("FILTER_NOT_FOUND", -lib.AVERROR_FILTER_NOT_FOUND, None, LookupError),
("INVALIDDATA", -lib.AVERROR_INVALIDDATA, "InvalidDataError", ValueError),
("MUXER_NOT_FOUND", -lib.AVERROR_MUXER_NOT_FOUND, None, LookupError),
("OPTION_NOT_FOUND", -lib.AVERROR_OPTION_NOT_FOUND, None, LookupError),
("PATCHWELCOME", -lib.AVERROR_PATCHWELCOME, "PatchWelcomeError", None),
("PROTOCOL_NOT_FOUND", -lib.AVERROR_PROTOCOL_NOT_FOUND, None, LookupError),
("UNKNOWN", -lib.AVERROR_UNKNOWN, None, None),
("EXPERIMENTAL", -lib.AVERROR_EXPERIMENTAL, None, None),
("INPUT_CHANGED", -lib.AVERROR_INPUT_CHANGED, None, None),
("OUTPUT_CHANGED", -lib.AVERROR_OUTPUT_CHANGED, None, None),
("HTTP_BAD_REQUEST", -lib.AVERROR_HTTP_BAD_REQUEST, "HTTPBadRequestError", HTTPClientError),
("HTTP_UNAUTHORIZED", -lib.AVERROR_HTTP_UNAUTHORIZED, "HTTPUnauthorizedError", HTTPClientError),
("HTTP_FORBIDDEN", -lib.AVERROR_HTTP_FORBIDDEN, "HTTPForbiddenError", HTTPClientError),
("HTTP_NOT_FOUND", -lib.AVERROR_HTTP_NOT_FOUND, "HTTPNotFoundError", HTTPClientError),
("HTTP_OTHER_4XX", -lib.AVERROR_HTTP_OTHER_4XX, "HTTPOtherClientError", HTTPClientError),
("HTTP_SERVER_ERROR", -lib.AVERROR_HTTP_SERVER_ERROR, "HTTPServerError", HTTPError),
("PYAV_CALLBACK", c_PYAV_STASHED_ERROR, "PyAVCallbackError", RuntimeError),
)
cdef sentinel = object()
class EnumType(type):
def __new__(mcl, name, bases, attrs, *args):
# Just adapting the method signature.
return super().__new__(mcl, name, bases, attrs)
def __init__(self, name, bases, attrs, items):
self._by_name = {}
self._by_value = {}
self._all = []
for spec in items:
self._create(*spec)
def _create(self, name, value, doc=None, by_value_only=False):
# We only have one instance per value.
try:
item = self._by_value[value]
except KeyError:
item = self(sentinel, name, value, doc)
self._by_value[value] = item
return item
def __len__(self):
return len(self._all)
def __iter__(self):
return iter(self._all)
def __getitem__(self, key):
if isinstance(key, str):
return self._by_name[key]
if isinstance(key, int):
try:
return self._by_value[key]
except KeyError:
pass
raise KeyError(key)
if isinstance(key, self):
return key
raise TypeError(f"{self.__name__} indices must be str, int, or itself")
def _get(self, long value, bint create=False):
try:
return self._by_value[value]
except KeyError:
pass
if not create:
return
return self._create(f"{self.__name__.upper()}_{value}", value, by_value_only=True)
def get(self, key, default=None, create=False):
try:
return self[key]
except KeyError:
if create:
return self._get(key, create=True)
return default
cdef class EnumItem:
"""An enumeration of FFmpeg's error types.
.. attribute:: tag
The FFmpeg byte tag for the error.
.. attribute:: strerror
The error message that would be returned.
"""
cdef readonly str name
cdef readonly int value
def __cinit__(self, sentinel_, str name, int value, doc=None):
if sentinel_ is not sentinel:
raise RuntimeError(f"Cannot instantiate {self.__class__.__name__}.")
self.name = name
self.value = value
self.__doc__ = doc
def __repr__(self):
return f"<{self.__class__.__module__}.{self.__class__.__name__}:{self.name}(0x{self.value:x})>"
def __str__(self):
return self.name
def __int__(self):
return self.value
@property
def tag(self):
return code_to_tag(self.value)
ErrorType = EnumType("ErrorType", (EnumItem, ), {"__module__": __name__}, [x[:2] for x in _ffmpeg_specs])
for enum in ErrorType:
# Mimick the errno module.
globals()[enum.name] = enum
if enum.value == c_PYAV_STASHED_ERROR:
enum.strerror = PYAV_STASHED_ERROR_message
else:
enum.strerror = lib.av_err2str(-enum.value)
# Mimick the builtin exception types.
# See https://www.python.org/dev/peps/pep-3151/#new-exception-classes
# Use the named ones we have, otherwise default to OSError for anything in errno.
# See this command for the count of POSIX codes used:
#
# egrep -IR 'AVERROR\(E[A-Z]+\)' vendor/ffmpeg-4.2 |\
# sed -E 's/.*AVERROR\((E[A-Z]+)\).*/\1/' | \
# sort | uniq -c
#
# The biggest ones that don't map to PEP 3151 builtins:
#
# 2106 EINVAL -> ValueError
# 649 EIO -> IOError (if it is distinct from OSError)
# 4080 ENOMEM -> MemoryError
# 340 ENOSYS -> NotImplementedError
# 35 ERANGE -> OverflowError
classes = {}
def _extend_builtin(name, codes):
base = getattr(__builtins__, name, OSError)
cls = type(name, (FFmpegError, base), dict(__module__=__name__))
# Register in builder.
for code in codes:
classes[code] = cls
# Register in module.
globals()[name] = cls
__all__.append(name)
return cls
# PEP 3151 builtins.
_extend_builtin("PermissionError", (errno.EACCES, errno.EPERM))
_extend_builtin("BlockingIOError", (errno.EAGAIN, errno.EALREADY, errno.EINPROGRESS, errno.EWOULDBLOCK))
_extend_builtin("ChildProcessError", (errno.ECHILD, ))
_extend_builtin("ConnectionAbortedError", (errno.ECONNABORTED, ))
_extend_builtin("ConnectionRefusedError", (errno.ECONNREFUSED, ))
_extend_builtin("ConnectionResetError", (errno.ECONNRESET, ))
_extend_builtin("FileExistsError", (errno.EEXIST, ))
_extend_builtin("InterruptedError", (errno.EINTR, ))
_extend_builtin("IsADirectoryError", (errno.EISDIR, ))
_extend_builtin("FileNotFoundError", (errno.ENOENT, ))
_extend_builtin("NotADirectoryError", (errno.ENOTDIR, ))
_extend_builtin("BrokenPipeError", (errno.EPIPE, errno.ESHUTDOWN))
_extend_builtin("ProcessLookupError", (errno.ESRCH, ))
_extend_builtin("TimeoutError", (errno.ETIMEDOUT, ))
# Other obvious ones.
_extend_builtin("ValueError", (errno.EINVAL, ))
_extend_builtin("MemoryError", (errno.ENOMEM, ))
_extend_builtin("NotImplementedError", (errno.ENOSYS, ))
_extend_builtin("OverflowError", (errno.ERANGE, ))
# The rest of them (for now)
_extend_builtin("OSError", [code for code in errno.errorcode if code not in classes])
# Classes for the FFmpeg errors.
for enum_name, code, name, base in _ffmpeg_specs:
name = name or enum_name.title().replace("_", "") + "Error"
if base is None:
bases = (FFmpegError,)
elif issubclass(base, FFmpegError):
bases = (base,)
else:
bases = (FFmpegError, base)
cls = type(name, bases, {"__module__": __name__})
# Register in builder.
classes[code] = cls
# Register in module.
globals()[name] = cls
__all__.append(name)
del _ffmpeg_specs
# Storage for stashing.
cdef object _local = local()
cdef int _err_count = 0
cdef int stash_exception(exc_info=None):
global _err_count
existing = getattr(_local, "exc_info", None)
if existing is not None:
print >> sys.stderr, "PyAV library exception being dropped:"
traceback.print_exception(*existing)
_err_count -= 1 # Balance out the +=1 that is coming.
exc_info = exc_info or sys.exc_info()
_local.exc_info = exc_info
if exc_info:
_err_count += 1
return -c_PYAV_STASHED_ERROR
cdef int _last_log_count = 0
cpdef int err_check(int res, filename=None) except -1:
"""Raise appropriate exceptions from library return code."""
global _err_count
global _last_log_count
# Check for stashed exceptions.
if _err_count:
exc_info = getattr(_local, "exc_info", None)
if exc_info is not None:
_err_count -= 1
_local.exc_info = None
raise exc_info[0], exc_info[1], exc_info[2]
if res >= 0:
return res
# Grab details from the last log.
log_count, last_log = get_last_error()
if log_count > _last_log_count:
_last_log_count = log_count
log = last_log
else:
log = None
cdef int code = -res
cdef char* error_buffer = <char*>malloc(lib.AV_ERROR_MAX_STRING_SIZE * sizeof(char))
if error_buffer == NULL:
raise MemoryError()
try:
if code == c_PYAV_STASHED_ERROR:
message = PYAV_STASHED_ERROR_message
else:
lib.av_strerror(res, error_buffer, lib.AV_ERROR_MAX_STRING_SIZE)
# Fallback to OS error string if no message
message = error_buffer or os.strerror(code)
cls = classes.get(code, UndefinedError)
raise cls(code, message, filename, log)
finally:
free(error_buffer)
class UndefinedError(FFmpegError):
"""Fallback exception type in case FFmpeg returns an error we don't know about."""
pass