Skip to content

Commit 434be44

Browse files
BoboTiGMickaël Schoentgen
authored andcommitted
Linux: Handle bad display value
1 parent 1a35199 commit 434be44

10 files changed

Lines changed: 34 additions & 30 deletions

File tree

.github/ISSUE_TEMPLATE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ General informations:
33
* OS name: _Debian GNU/Linux_
44
* OS version: __sid__
55
* OS architecture: _64 bits_
6+
* Monitor(s)'s resolution:
67
* Result of the command `python --version`: __Python 3.5.1+__
78

89

.travis.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ addons:
1616
before_install:
1717
- xpra --xvfb="Xorg +extension RANDR -config `pwd`/tests/res/dummy.xorg.conf -logfile ${HOME}/.xpra/xorg.log" start :42
1818

19+
env:
20+
- DISPLAY=":42"
21+
1922
install:
2023
- pip install flake8 numpy pillow pylint
21-
- python setup.py install
24+
- pip install -e .
2225

2326
script:
24-
- py.test --display=":42.0"
27+
- py.test --showlocals --display=":42.0"
2528
- flake8 mss
2629
- pylint mss
2730

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ dev 2017/xx/xx
88
- tests: a lot of tests added for better coverage
99
- MSS: possibility to use custom class to handle screen shot data
1010
- Mac: handle screenshot's width not divisible by 16 (fix #14, #19, #21)
11+
- Linux: handle bad display value
1112

1213
3.0.1 2017/07/06
1314
- fix examples links

mss/darwin.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ class CGPoint(ctypes.Structure):
3232
_fields_ = [('x', cgfloat()), ('y', cgfloat())]
3333

3434
def __repr__(self):
35-
return '{0}(left={cls.x} top={cls.y})'.format(
36-
type(self).__name__, cls=self)
35+
return '{0}(left={1} top={2})'.format(
36+
type(self).__name__, self.x, self.y)
3737

3838

3939
class CGSize(ctypes.Structure):
@@ -42,8 +42,8 @@ class CGSize(ctypes.Structure):
4242
_fields_ = [('width', cgfloat()), ('height', cgfloat())]
4343

4444
def __repr__(self):
45-
return '{0}(width={cls.width} height={cls.height})'.format(
46-
type(self).__name__, cls=self)
45+
return '{0}(width={1} height={2})'.format(
46+
type(self).__name__, self.width, self.height)
4747

4848

4949
class CGRect(ctypes.Structure):
@@ -52,8 +52,8 @@ class CGRect(ctypes.Structure):
5252
_fields_ = [('origin', CGPoint), ('size', CGSize)]
5353

5454
def __repr__(self):
55-
return '{0}<{cls.origin} {cls.size}>'.format(
56-
type(self).__name__, cls=self)
55+
return '{0}<{1} {2}>'.format(
56+
type(self).__name__, self.origin, self.size)
5757

5858

5959
class MSS(MSSBase):

mss/linux.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ def __init__(self, display=None):
136136
if not isinstance(display, bytes):
137137
display = display.encode('utf-8')
138138

139+
if b':' not in display:
140+
raise ScreenShotError('Bad display value.', locals())
141+
139142
x11 = ctypes.util.find_library('X11')
140143
if not x11:
141144
raise ScreenShotError('No X11 library found.', locals())
@@ -149,7 +152,6 @@ def __init__(self, display=None):
149152
self._set_argtypes()
150153
self._set_restypes()
151154

152-
# TODO: check if `display` is openable, else SEGFAULT
153155
self.display = self.xlib.XOpenDisplay(display)
154156
try:
155157
self.display.contents

mss/screenshot.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ def rgb(self):
128128
"""
129129

130130
if not self.__rgb:
131-
self.__rgb = bytearray(self.height * self.width * 3)
132-
self.__rgb[0::3], self.__rgb[1::3], self.__rgb[2::3] = \
131+
rgb = bytearray(self.height * self.width * 3)
132+
rgb[0::3], rgb[1::3], rgb[2::3] = \
133133
self.raw[2::4], self.raw[1::4], self.raw[0::4]
134-
self.__rgb = bytes(self.__rgb)
134+
self.__rgb = bytes(rgb)
135135

136136
return self.__rgb

tests/test_find_monitors.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ def test_dimensions(sct, is_travis):
2626
if not is_travis:
2727
assert mon['width'] > 0
2828
assert mon['height'] > 0
29-
30-
31-
def test_dimensions_travis(sct, is_travis):
32-
mon = sct.monitors[1]
33-
if is_travis:
29+
else:
3430
assert mon['width'] == 1920
3531
assert mon['height'] == 1440

tests/test_gnu_linux.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,17 @@ def test_factory_systems(monkeypatch):
4949
mss.mss()
5050

5151

52-
def test_implementation(monkeypatch):
52+
def test_implementation(monkeypatch, is_travis):
5353
import mss
5454

5555
# Bad `display` type
56-
mss.mss(display=TEXT(':0'))
56+
if not is_travis:
57+
mss.mss(display=TEXT(':0'))
58+
else:
59+
mss.mss(display=TEXT(':42'))
5760

58-
# TODO: SEGFAULT
59-
#with pytest.raises(ScreenShotError):
60-
# mss.mss(display=text('0'))
61+
with pytest.raises(ScreenShotError):
62+
mss.mss(display=TEXT('0'))
6163

6264
# No `DISPLAY` in envars
6365
monkeypatch.delenv('DISPLAY')

tests/test_implementation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ def test_repr(sct):
6565

6666
def test_factory_basics(monkeypatch):
6767
# Current system
68-
sct = mss.mss()
69-
assert isinstance(sct, MSSBase)
68+
with mss.mss() as sct:
69+
assert isinstance(sct, MSSBase)
7070

7171
# Unknown
7272
monkeypatch.setattr(platform, 'system', lambda: 'Chuck Norris')

tests/test_save.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@ def test_files_exist(sct):
1919
assert os.path.isfile('fullscreen.png')
2020

2121

22-
def test_callback():
22+
def test_callback(sct):
2323

2424
def on_exists(fname):
2525
if os.path.isfile(fname):
2626
new_file = fname + '.old'
2727
os.rename(fname, new_file)
2828

29-
with mss.mss() as sct:
30-
filename = sct.shot(mon=0, output='mon0.png', callback=on_exists)
31-
assert os.path.isfile(filename)
29+
filename = sct.shot(mon=0, output='mon0.png', callback=on_exists)
30+
assert os.path.isfile(filename)
3231

33-
filename = sct.shot(output='mon1.png', callback=on_exists)
34-
assert os.path.isfile(filename)
32+
filename = sct.shot(output='mon1.png', callback=on_exists)
33+
assert os.path.isfile(filename)

0 commit comments

Comments
 (0)