File: err.py

package info (click to toggle)
pysdl2 0.9.17%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,328 kB
  • sloc: python: 24,685; makefile: 36; sh: 8
file content (57 lines) | stat: -rw-r--r-- 1,908 bytes parent folder | download
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
from .. import error

__all__ = ["SDLError", "raise_sdl_err"]


class SDLError(Exception):
    """A custom exception class for SDL2-specific errors.
    
    Args:
        msg (str, optional): The error message for the exception. If not
            provided, the current SDL error (if any) will be retrieved using
            `:func:~sdl2.SDL_GetError`.
    """

    def __init__(self, msg=None):
        super(SDLError, self).__init__()
        self.msg = msg
        if not msg:
            self.msg = error.SDL_GetError()
            error.SDL_ClearError()

    def __str__(self):
        return repr(self.msg)


def raise_sdl_err(desc=None):
    """Raises an exception for an internal SDL error.
    
    The format of the exception message depends on whether a description is
    provided and whether `:func:~sdl2.SDL_GetError` returns an error string.
    If a description is given, it will be appended after the default text
    ``Error encountered``. If SDL has set an error string, it will be appended
    to the end of the message following a colon (clearing the error in the
    process).
    
    For example, if ``SDL_GetError() == b"unsupported pixel format"`` and the
    function is called as ``raise_sdl_err("creating the surface")``, the
    resulting exception message will be "Error encountered creating the surface:
    unsupported pixel format".

    Args:
        desc (str. optional): A description of what SDL was trying to do when
            the error occurred. Will be placed after the text "Error encountered"
            in the exception message if provided.

    Raises:
        :exc:`~SDLError`: An exception explaining the most recent SDL error.

    """
    errmsg = error.SDL_GetError().decode('utf-8')
    error.SDL_ClearError()
    e = "Error encountered"
    if desc:
        e += " " + desc
    if len(errmsg):
        e += ": {0}".format(errmsg)
    raise SDLError(e)