🐛 Work around for a watchdog Py2 bug

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
This commit is contained in:
Gina Häußge committed 2020-08-05 11:10:04 +02:00
1 parent 58fa6004bb
commit ad6230074a
2 files changed
+25

No files matched your search

+1
View File
@@ -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()
+24
View File
@@ -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):
"""