From ad6230074a3ed1d7451ef97f2393213f6801227c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 5 Aug 2020 11:10:04 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Work=20around=20for=20a=20watchd?= =?UTF-8?q?og=20Py2=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the PollingObserver is used under Py2 on a directory that contains utf8 file names, the default listdir implementation (scandir isn't natively available under Py2) will generate unicode instead of str. Due to an insufficent instance check in watchdog that will lead to a misidentification of an entry as an DirEntry object instead of a str, and the attempt to read the name property from that will cause an AttributeError. We monkey patch watchdog to use our bundled scandir backport where needed instead of trying to fix the instance check, as that will perform better anyhow. Closes #3676 --- src/octoprint/server/__init__.py | 1 + src/octoprint/server/util/watchdog.py | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/octoprint/server/__init__.py b/src/octoprint/server/__init__.py index 83ad86a0e..8bdc56f43 100644 --- a/src/octoprint/server/__init__.py +++ b/src/octoprint/server/__init__.py @@ -283,6 +283,7 @@ class Server(object): util.tornado.fix_json_encode() util.tornado.fix_websocket_check_origin() util.flask.fix_flask_jsonify() + util.watchdog.fix_scandir() self._setup_mimetypes() diff --git a/src/octoprint/server/util/watchdog.py b/src/octoprint/server/util/watchdog.py index 7da800542..b94f5010c 100644 --- a/src/octoprint/server/util/watchdog.py +++ b/src/octoprint/server/util/watchdog.py @@ -17,6 +17,30 @@ import octoprint.filemanager.util import octoprint.util +def fix_scandir(): + try: + from os import scandir + # nothing to fix, natively available, return + return + + except ImportError: + # not natively available, use backport + from scandir import scandir + + import watchdog.utils.dirsnapshot + OriginalDirectorySnapshot = watchdog.utils.dirsnapshot.DirectorySnapshot + class FixedDirectorySnapshot(OriginalDirectorySnapshot): + def __init__(self, listdir=scandir, *args, **kwargs): + OriginalDirectorySnapshot.__init__(self, listdir=listdir, *args, **kwargs) + watchdog.utils.dirsnapshot.DirectorySnapshot = FixedDirectorySnapshot + + import watchdog.observers.polling + OriginalPollingEmitter = watchdog.observers.polling.PollingEmitter + class FixedPollingEmitter(OriginalPollingEmitter): + def __init__(self, listdir=scandir, *args, **kwargs): + OriginalPollingEmitter.__init__(self, listdir=listdir, *args, **kwargs) + watchdog.observers.polling.PollingEmitter = FixedPollingEmitter + class GcodeWatchdogHandler(watchdog.events.PatternMatchingEventHandler): """