/
_error.py
42 lines (32 loc) · 1.13 KB
/
_error.py
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
""" SDL errors.def
"""
from pygame._sdl import sdl, ffi
from numbers import Number
class SDLError(Exception):
"""SDL error."""
@classmethod
def from_sdl_error(cls):
return cls(ffi.string(sdl.SDL_GetError()))
def unpack_rect(rect):
"""Unpack the size and raise a type error if needed."""
# This is as liberal as pygame when used for pygame.surface, but
# more liberal for pygame.display. I don't think the inconsistency
# matters
if (not hasattr(rect, '__iter__') or
len(rect) != 2 or
not isinstance(rect[0], Number) or
not isinstance(rect[1], Number)):
raise TypeError("expected tuple of two integers but got %r"
% type(rect))
# We'll throw a conversion TypeError here if someone is using a
# complex number, but so does pygame.
return int(rect[0]), int(rect[1])
def get_error():
err = ffi.string(sdl.SDL_GetError())
if not isinstance(err, str):
return err.decode('utf8')
return err
def set_error(errmsg):
if not isinstance(errmsg, bytes):
errmsg = errmsg.encode('utf8')
sdl.SDL_SetError(errmsg)