BoboTiG / python-mss

An ultra fast cross-platform multiple screenshots module in pure Python using ctypes.
https://pypi.org/project/mss/
MIT License
1.02k stars 93 forks source link

Better exception message for missing RANDR extension. #168

Closed ponty closed 4 years ago

ponty commented 4 years ago

General information:

Description

If RANDR extension is missing then ValueError: NULL pointer access is thrown. An exception with text RandR extension missing would be more helpful.

The exception in MSS and how other projects handle missing RANDR can be reproduced this way:

# vagrant init ubuntu/bionic64
# vagrant up
# vagrant ssh
vagrant@ubuntu-bionic:~$ sudo apt-get update
vagrant@ubuntu-bionic:~$ sudo apt-get install -y xvfb scrot python3-pip x11-xserver-utils
vagrant@ubuntu-bionic:~$ python3 -m pip install python-xlib mss
vagrant@ubuntu-bionic:~$ wget https://raw.githubusercontent.com/python-xlib/python-xlib/master/examples/xrandr.py
vagrant@ubuntu-bionic:~$ xvfb-run -s '-extension RANDR' scrot
vagrant@ubuntu-bionic:~$ xvfb-run -s '-extension RANDR' python3 xrandr.py
xrandr.py: server does not have the RANDR extension
vagrant@ubuntu-bionic:~$ xvfb-run -s '-extension RANDR' xrandr
RandR extension missing
vagrant@ubuntu-bionic:~$ python3 --version
Python 3.6.9
vagrant@ubuntu-bionic:~$ python3 -c "import mss;print(mss.__version__)"
5.1.0
vagrant@ubuntu-bionic:~$ xvfb-run -s '-extension RANDR' python3 -c "from mss import mss;mss().shot()"
Xlib:  extension "RANDR" missing on display ":99".
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/vagrant/.local/lib/python3.6/site-packages/mss/base.py", line 148, in shot
    return next(self.save(**kwargs))
  File "/home/vagrant/.local/lib/python3.6/site-packages/mss/base.py", line 111, in save
    monitors = self.monitors
  File "/home/vagrant/.local/lib/python3.6/site-packages/mss/linux.py", line 361, in monitors
    mon = xrandr.XRRGetScreenResourcesCurrent(display, self.drawable).contents
ValueError: NULL pointer access

2 projects where RANDR extension is checked:

BoboTiG commented 4 years ago

Hello,

Thanks for the detailled report. I think this is fixed on that branch: https://github.com/BoboTiG/python-mss/tree/fix-168-better-xrandr-check

Do you mind trying again using that version :pray: ?

BoboTiG commented 4 years ago

The new behavior will be:

root@150cfe4efd93:/python-mss# xvfb-run -s '-extension RANDR' python3 -c "from mss import mss;mss().shot()"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/python-mss/mss/factory.py", line 41, in mss
    return linux.MSS(**kwargs)
  File "/python-mss/mss/linux.py", line 231, in __init__
    raise ScreenShotError("No Xrandr extension found.")
mss.exception.ScreenShotError: No Xrandr extension found.