Skip to content

Commit 848f42f

Browse files
committed
Better use of Exceptions mechanism
1 parent 86f6dea commit 848f42f

7 files changed

Lines changed: 21 additions & 30 deletions

File tree

docs/source/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@
5151

5252
# General information about the project.
5353
project = 'Python MSS'
54-
copyright = '2016, Tiger-222'
54+
copyright = '2013-2016, Tiger-222'
5555
author = 'Tiger-222'
5656

5757
# The version info for the project you're documenting, acts as replacement for
5858
# |version| and |release|, also used in various other places throughout the
5959
# built documents.
6060
#
6161
# The short X.Y version.
62-
version = '2.0.18'
62+
version = '2.0.19'
6363
# The full version, including alpha/beta/rc tags.
6464
release = 'latest'
6565

mss/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from .exception import ScreenshotError
1414
from .factory import mss
1515

16-
__version__ = '2.0.18'
16+
__version__ = '2.0.19'
1717
__author__ = "Mickaël 'Tiger-222' Schoentgen"
1818
__copyright__ = '''
1919
Copyright (c) 2013-2016, Mickaël 'Tiger-222' Schoentgen

mss/base.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ def save(self, mon=0, output='monitor-%d.png', callback=None):
111111
try:
112112
monitor = self.monitors[mon_number]
113113
except IndexError:
114-
err = 'Monitor {0} does not exist.'.format(mon)
115-
raise ScreenshotError(err)
114+
raise ScreenshotError('Monitor does not exist.', locals())
116115

117116
if '%d' in output:
118117
output = output.replace('%d', str(mon_number))
@@ -159,5 +158,4 @@ def to_png(self, data, output):
159158
fileh.write(b''.join(iend))
160159
return
161160

162-
err = 'Error writing data to "{0}".'.format(output)
163-
raise ScreenshotError(err)
161+
raise ScreenshotError('Error writing data to file.', output)

mss/darwin.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def __init__(self):
6060

6161
coregraphics = find_library('CoreGraphics')
6262
if not coregraphics:
63-
raise ScreenshotError('No CoreGraphics library found.')
63+
raise ScreenshotError('No CoreGraphics library found.', locals())
6464
self.core = cdll.LoadLibrary(coregraphics)
6565

6666
self._set_argtypes()
@@ -146,8 +146,8 @@ def get_pixels(self, monitor):
146146

147147
image_ref = self.core.CGWindowListCreateImage(rect, 1, 0, 0)
148148
if not image_ref:
149-
err = 'CoreGraphics.CGWindowListCreateImage() failed.'
150-
raise ScreenshotError(err)
149+
raise ScreenshotError(
150+
'CoreGraphics.CGWindowListCreateImage() failed.', locals())
151151

152152
self.width = int(self.core.CGImageGetWidth(image_ref))
153153
self.height = int(self.core.CGImageGetHeight(image_ref))

mss/factory.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ def mss(**kwargs):
2828
elif operating_system == 'windows':
2929
from .windows import MSS
3030
else:
31-
err = 'System "{0}" not implemented.'.format(operating_system)
32-
raise ScreenshotError(err)
31+
raise ScreenshotError('System not (yet?) implemented.', locals())
3332

3433
return MSS(**kwargs)

mss/linux.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,19 @@ def __init__(self, display=None):
9696
try:
9797
display = environ['DISPLAY']
9898
except KeyError:
99-
raise ScreenshotError(
100-
'$DISPLAY not set. Stopping to prevent segfault.')
99+
raise ScreenshotError('$DISPLAY not set.', locals())
100+
101101
if not isinstance(display, bytes):
102102
display = bytes(display, 'utf-8')
103103

104104
x11 = find_library('X11')
105105
if not x11:
106-
raise ScreenshotError('No X11 library found.')
106+
raise ScreenshotError('No X11 library found.', locals())
107107
self.xlib = cdll.LoadLibrary(x11)
108108

109109
xrandr = find_library('Xrandr')
110110
if not xrandr:
111-
raise ScreenshotError('No Xrandr extension found.')
111+
raise ScreenshotError('No Xrandr extension found.', locals())
112112
self.xrandr = cdll.LoadLibrary(xrandr)
113113

114114
self._set_argtypes()
@@ -118,8 +118,8 @@ def __init__(self, display=None):
118118
try:
119119
self.display.contents
120120
except ValueError:
121-
raise ScreenshotError('Cannot open display "{0}".'.format(
122-
str(display.decode('utf-8'))))
121+
raise ScreenshotError('Cannot open display.', locals())
122+
123123
self.root = self.xlib.XDefaultRootWindow(
124124
self.display, self.xlib.XDefaultScreen(self.display))
125125

@@ -156,9 +156,8 @@ def validate(value, _, args):
156156
'''
157157

158158
if value == 0:
159-
err = 'xrandr.XRRGetScreenResources() failed.'
160-
err += ' NULL pointer received.'
161-
raise ScreenshotError(err)
159+
raise ScreenshotError(('xrandr.XRRGetScreenResources() failed.'
160+
' NULL pointer received.'), locals())
162161

163162
return args
164163

@@ -229,11 +228,7 @@ def get_pixels(self, monitor):
229228
monitor['width'], monitor['height'],
230229
0x00ffffff, 2) # ZPIXMAP
231230
if not ximage:
232-
err = 'xlib.XGetImage() failed. Monitor informations: '
233-
for key, val in sorted(monitor.items()):
234-
err = '{0}{1}: {2}, '.format(err, key, val)
235-
err = err.strip(', ')
236-
raise ScreenshotError(err)
231+
raise ScreenshotError('xlib.XGetImage() failed.', locals())
237232

238233
# Raw pixels values conversion
239234
bpp = ximage.contents.bits_per_pixel
@@ -243,9 +238,8 @@ def get_pixels(self, monitor):
243238
c_ubyte * self.height * self.width * 4))
244239
self.image = self.bgra_to_rgb(bytearray(data.contents))
245240
else:
246-
err = ('Not implemented for this configuration '
247-
'([XImage] bits per pixel = {0}).')
248-
raise ScreenshotError(err.format(bpp))
241+
raise ScreenshotError(('[XImage] bits per pixel value '
242+
'not (yet?) implemented.'), locals())
249243

250244
# Free
251245
self.xlib.XDestroyImage(ximage)

mss/windows.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def get_pixels(self, monitor):
153153
bits = windll.gdi32.GetDIBits(
154154
memdc, bmp, 0, monitor['height'], image_data, bmi, 0)
155155
if bits != self.height:
156-
raise ScreenshotError('gdi32.GetDIBits() failed.')
156+
raise ScreenshotError('gdi32.GetDIBits() failed.', locals())
157157
finally:
158158
# Clean up
159159
if srcdc:

0 commit comments

Comments
 (0)