Skip to content

Commit bcf3a51

Browse files
committed
Split ext error handling into a separate module
1 parent 3ee0ae4 commit bcf3a51

File tree

16 files changed

+87
-43
lines changed

16 files changed

+87
-43
lines changed

doc/modules/ext/err.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
`sdl2.ext.err` - Handling & Raising SDL Errors
2+
==============================================
3+
4+
This module defines a Python exception class for representing internal SDL2
5+
errors, as well as convenience functions for cleanly handling and raising
6+
SDL errors in Python code.
7+
8+
.. automodule:: sdl2.ext.err
9+
:members:

doc/modules/sdl2ext.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ objects to Numpy arrays:
4949
.. toctree::
5050
:maxdepth: 1
5151

52+
ext/err.rst
5253
ext/compat.rst
5354
ext/array.rst
5455
ext/pixelaccess.rst

doc/news.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,20 @@ New Features:
1616
initialize the audio subsystem as well as the default video subsystem).
1717
Previously, this function only initailized the video subsystem.
1818
* Updated to wrap new functions and constants in SDL2 2.24.0 (PR #246).
19+
* Added the error-handling :func:`sdl2.ext.raise_sdl_err` function to the public
20+
API.
1921

2022
Fixed Bugs:
2123

2224
* Fixed broken behaviour (and potential segfaults) with usage of
2325
:func:`sdl2.SDL_GUIDToString` on Python 3.6 and older (PR #246).
2426
* Fixed :func:`sdl2.ext.draw` when drawing on 1bpp surfaces (PR #242).
2527

28+
API Changes:
29+
30+
* Moved :class:`sdl2.ext.SDLError` and :func:`sdl2.ext.raise_sdl_err`
31+
internally to a new submodule :mod:`sdl2.ext.err`.
32+
2633

2734
0.9.13
2835
------

sdl2/ext/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from .events import *
1212
from .ebs import *
1313

14+
from .err import *
1415
from .common import *
1516
from .draw import *
1617
from .bitmapfont import *

sdl2/ext/bitmapfont.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .. import surface, rect, pixels
2-
from .common import SDLError, raise_sdl_err
2+
from .err import SDLError, raise_sdl_err
33
from .sprite import SoftwareSprite
44
from .surface import _get_target_surface
55
from .image import load_bmp

sdl2/ext/common.py

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
SDL_INIT_VIDEO, SDL_INIT_AUDIO, SDL_INIT_TIMER, SDL_INIT_HAPTIC,
55
SDL_INIT_JOYSTICK, SDL_INIT_GAMECONTROLLER, SDL_INIT_SENSOR, SDL_INIT_EVENTS,
66
)
7+
from .err import raise_sdl_err
78

89
_HASSDLTTF = True
910
try:
@@ -16,7 +17,7 @@
1617
except ImportError:
1718
_HASSDLIMAGE = False
1819

19-
__all__ = ["SDLError", "init", "quit", "get_events", "TestEventProcessor"]
20+
__all__ = ["init", "quit", "get_events", "TestEventProcessor"]
2021

2122

2223
_sdl_subsystems = {
@@ -31,38 +32,6 @@
3132
}
3233

3334

34-
class SDLError(Exception):
35-
"""An SDL2-specific exception class."""
36-
37-
def __init__(self, msg=None):
38-
"""Creates a new SDLError instance with the specified message.
39-
40-
If no msg is passed, it will try to get the current SDL2 error via
41-
:func:`sdl2.SDL_GetError`.
42-
43-
"""
44-
super(SDLError, self).__init__()
45-
self.msg = msg
46-
if not msg:
47-
self.msg = error.SDL_GetError()
48-
error.SDL_ClearError()
49-
50-
def __str__(self):
51-
return repr(self.msg)
52-
53-
54-
def raise_sdl_err(desc=None):
55-
# Raises and clears the latest SDL error. For internal use.
56-
errmsg = error.SDL_GetError().decode('utf-8')
57-
error.SDL_ClearError()
58-
e = "Error encountered"
59-
if desc:
60-
e += " " + desc
61-
if len(errmsg):
62-
e += ": {0}".format(errmsg)
63-
raise SDLError(e)
64-
65-
6635
def init(
6736
video=True, audio=False, timer=False, joystick=False, controller=False,
6837
haptic=False, sensor=False, events=True

sdl2/ext/draw.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import ctypes
22
from .compat import isiterable, UnsupportedError
3-
from .common import raise_sdl_err
3+
from .err import raise_sdl_err
44
from .array import to_ctypes
55
from .color import convert_to_color
66
from .. import surface, pixels, rect

sdl2/ext/err.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from .. import error
2+
3+
__all__ = ["SDLError", "raise_sdl_err"]
4+
5+
6+
class SDLError(Exception):
7+
"""A custom exception class for SDL2-specific errors.
8+
9+
Args:
10+
msg (str, optional): The error message for the exception. If not
11+
provided, the current SDL error (if any) will be retrieved using
12+
`:func:~sdl2.SDL_GetError`.
13+
"""
14+
15+
def __init__(self, msg=None):
16+
super(SDLError, self).__init__()
17+
self.msg = msg
18+
if not msg:
19+
self.msg = error.SDL_GetError()
20+
error.SDL_ClearError()
21+
22+
def __str__(self):
23+
return repr(self.msg)
24+
25+
26+
def raise_sdl_err(desc=None):
27+
"""Raises an exception for an internal SDL error.
28+
29+
The format of the exception message depends on whether a description is
30+
provided and whether `:func:~sdl2.SDL_GetError` returns an error string.
31+
If a description is given, it will be appended after the default text
32+
``Error encountered``. If SDL has set an error string, it will be appended
33+
to the end of the message following a colon (clearing the error in the
34+
process).
35+
36+
For example, if ``SDL_GetError() == b"unsupported pixel format"`` and the
37+
function is called as ``raise_sdl_err("creating the surface")``, the
38+
resulting exception message will be "Error encountered creating the surface:
39+
unsupported pixel format".
40+
41+
Args:
42+
desc (str. optional): A description of what SDL was trying to do when
43+
the error occurred. Will be placed after the text "Error encountered"
44+
in the exception message if provided.
45+
46+
Raises:
47+
:exc:`~SDLError`: An exception explaining the most recent SDL error.
48+
49+
"""
50+
errmsg = error.SDL_GetError().decode('utf-8')
51+
error.SDL_ClearError()
52+
e = "Error encountered"
53+
if desc:
54+
e += " " + desc
55+
if len(errmsg):
56+
e += ": {0}".format(errmsg)
57+
raise SDLError(e)

sdl2/ext/image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
from .. import endian, surface, pixels, error, rwops
3-
from .common import raise_sdl_err
3+
from .err import raise_sdl_err
44
from .compat import UnsupportedError, byteify, stringify
55
from .resources import _validate_path
66
from .surface import _get_target_surface

sdl2/ext/msgbox.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from ctypes import byref, c_int
22
from .color import Color
33
from .compat import isiterable, utf8
4-
from .common import raise_sdl_err
4+
from .err import raise_sdl_err
55
from .window import Window
66
from .. import dll, SDL_PumpEvents, SDL_Window
77
from .. import messagebox as mb

0 commit comments

Comments
 (0)