Skip to content

Commit

Permalink
Drop Python 2.7 support and update encoding handling (#703)
Browse files Browse the repository at this point in the history
* drop python 2.7 support

* switch to os.fsencode / os.fsdecode on Unixes

* updated tests

* remove Python 2.7 from CI

* removed obsolete __future__ imports

* removed Py 2.7 references from docs

* removed Py 2.7 references from readme

* add test for path with invalid bytes

* add entries to changelog
  • Loading branch information
SamSchott authored Nov 23, 2020
1 parent 4dcb0f7 commit 432c31f
Show file tree
Hide file tree
Showing 21 changed files with 36 additions and 124 deletions.
16 changes: 0 additions & 16 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ env:
jobs:
fast_finish: true
include:
- name: Python 2.7 on GNU/Linux
os: linux
python: "2.7"
env: TOXENV=py27
- name: Python 3.5 on GNU/Linux
os: linux
python: "3.5"
Expand Down Expand Up @@ -44,10 +40,6 @@ jobs:
os: linux
python: nightly
env: TOXENV=py310
- name: PyPy 2.7 on GNU/Linux
os: linux
python: "pypy"
env: TOXENV=pypy
- name: PyPy 3.6 on GNU/Linux
os: linux
python: "pypy3"
Expand Down Expand Up @@ -118,14 +110,6 @@ jobs:
- pyenv install --skip-existing 3.10-dev
- pyenv global system 3.10-dev
env: TOXENV=py310
- name: Python 2.7 on Windows
os: windows
language: shell
before_install:
- choco install python2
env:
- TOXENV=py27
- export PATH="/c/Python27:/c/Python27/Scripts:$PATH"
- name: Python 3.5 on Windows
os: windows
language: shell
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ appropriate observer like in the example above, do::

Dependencies
------------
1. Python 2.7, 3.4 or above.
1. Python 3.4 or above.
2. pathtools_
3. XCode_ (only on Mac OS X)
4. PyYAML_ (only for ``watchmedo`` script)
Expand Down
5 changes: 3 additions & 2 deletions changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ Changelog

2021-xx-xx • `full history <https://github.com/gorakhargosh/watchdog/compare/v0.10.4...master>`__

- (`# <https://github.com/gorakhargosh/watchdog/pull/>`_)
- Thanks to our beloved contributors: @
- Allow file paths on Unix that don't follow the file system encoding (`# <https://github.com/gorakhargosh/watchdog/pull/703>`_)
- Drop support for Python 2.7 (`# <https://github.com/gorakhargosh/watchdog/pull/703>`_)
- Thanks to our beloved contributors: @SamSchott


0.10.4
Expand Down
8 changes: 1 addition & 7 deletions docs/source/hacking.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,7 @@ Steps to setting up a clean environment:
3. Linux

For example Debian and Python 2.7:

.. code:: bash
$ sudo apt-get install python-pip python-virtualenv
For Python 3:
For example Debian:

.. code:: bash
Expand Down
2 changes: 1 addition & 1 deletion docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Watchdog

Python API library and shell utilities to monitor file system events.

Works on Python 2.7 and 3.4+. If you want to use an old version of Python, you should stick with watchdog < 0.10.0.
Works on Python 3.4+. If you want to use an old version of Python, you should stick with watchdog < 0.10.0.

Directory monitoring made easy with
-----------------------------------
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@
'Operating System :: Microsoft :: Windows :: Windows NT/2000',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
Expand Down
9 changes: 4 additions & 5 deletions src/watchdog/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@
import re
from pathtools.patterns import match_any_paths
from watchdog.utils import has_attribute
from watchdog.utils import unicode_paths


EVENT_TYPE_MOVED = 'moved'
Expand Down Expand Up @@ -441,9 +440,9 @@ def dispatch(self, event):

paths = []
if has_attribute(event, 'dest_path'):
paths.append(unicode_paths.decode(event.dest_path))
paths.append(os.fsdecode(event.dest_path))
if event.src_path:
paths.append(unicode_paths.decode(event.src_path))
paths.append(os.fsdecode(event.src_path))

if match_any_paths(paths,
included_patterns=self.patterns,
Expand Down Expand Up @@ -520,9 +519,9 @@ def dispatch(self, event):

paths = []
if has_attribute(event, 'dest_path'):
paths.append(unicode_paths.decode(event.dest_path))
paths.append(os.fsdecode(event.dest_path))
if event.src_path:
paths.append(unicode_paths.decode(event.src_path))
paths.append(os.fsdecode(event.src_path))

if any(r.match(p) for r in self.ignore_regexes for p in paths):
return
Expand Down
1 change: 0 additions & 1 deletion src/watchdog/observers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import with_statement
import threading
from watchdog.utils import BaseThread
from watchdog.utils.compat import queue
Expand Down
2 changes: 0 additions & 2 deletions src/watchdog/observers/fsevents.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
:platforms: Mac OS X
"""

from __future__ import with_statement

import os
import sys
import threading
Expand Down
2 changes: 1 addition & 1 deletion src/watchdog/observers/fsevents2.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def __init__(self, path):
self._run_loop = None

if isinstance(path, bytes):
path = path.decode('utf-8')
path = os.fsdecode(path)
self._path = unicodedata.normalize('NFC', path)

context = None
Expand Down
9 changes: 3 additions & 6 deletions src/watchdog/observers/inotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@
"""

from __future__ import with_statement

import os
import threading
from .inotify_buffer import InotifyBuffer
Expand All @@ -92,7 +90,6 @@
generate_sub_moved_events,
generate_sub_created_events,
)
from watchdog.utils import unicode_paths


class InotifyEmitter(EventEmitter):
Expand All @@ -117,7 +114,7 @@ def __init__(self, event_queue, watch, timeout=DEFAULT_EMITTER_TIMEOUT):
self._inotify = None

def on_thread_start(self):
path = unicode_paths.encode(self.watch.path)
path = os.fsencode(self.watch.path)
self._inotify = InotifyBuffer(path, self.watch.is_recursive)

def on_thread_stop(self):
Expand Down Expand Up @@ -176,10 +173,10 @@ def queue_events(self, timeout, full_events=False):
self.queue_event(DirModifiedEvent(os.path.dirname(src_path)))

def _decode_path(self, path):
""" Decode path only if unicode string was passed to this emitter. """
"""Decode path only if unicode string was passed to this emitter. """
if isinstance(self.watch.path, bytes):
return path
return unicode_paths.decode(path)
return os.fsdecode(path)


class InotifyFullEmitter(InotifyEmitter):
Expand Down
4 changes: 1 addition & 3 deletions src/watchdog/observers/inotify_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import with_statement
import os
import errno
import struct
Expand All @@ -26,7 +25,6 @@
from ctypes import c_int, c_char_p, c_uint32
from watchdog.utils import has_attribute
from watchdog.utils import UnsupportedLibc
from watchdog.utils.unicode_paths import decode


def _load_libc():
Expand Down Expand Up @@ -588,4 +586,4 @@ def __repr__(self):
mask_string = self._get_mask_string(self.mask)
s = '<%s: src_path=%r, wd=%d, mask=%s, cookie=%d, name=%s>'
return s % (type(self).__name__, self.src_path, self.wd, mask_string,
self.cookie, decode(self.name))
self.cookie, os.fsdecode(self.name))
1 change: 0 additions & 1 deletion src/watchdog/observers/kqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
"""

from __future__ import with_statement
from watchdog.utils import platform

import threading
Expand Down
1 change: 0 additions & 1 deletion src/watchdog/observers/polling.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
:special-members:
"""

from __future__ import with_statement
import threading
from functools import partial
from watchdog.utils import stat as default_stat
Expand Down
64 changes: 0 additions & 64 deletions src/watchdog/utils/unicode_paths.py

This file was deleted.

2 changes: 0 additions & 2 deletions tests/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
:author: yesudeep@google.com (Yesudeep Mangalapilly)
"""

from __future__ import with_statement

import os
import os.path
import tempfile
Expand Down
25 changes: 21 additions & 4 deletions tests/test_emitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import unicode_literals
import os
import time
import pytest
Expand All @@ -23,7 +22,6 @@
from . import Queue, Empty
from .shell import mkdir, touch, mv, rm
from watchdog.utils import platform
from watchdog.utils.unicode_paths import str_cls
from watchdog.events import (
FileDeletedEvent,
FileModifiedEvent,
Expand Down Expand Up @@ -105,6 +103,25 @@ def test_create():
assert isinstance(event, DirModifiedEvent)


@pytest.mark.flaky(max_runs=5, min_passes=1, rerun_filter=rerun_filter)
@pytest.mark.skipif(
platform.is_darwin() or platform.is_windows(),
reason="Windows and macOS enforce proper encoding"
)
def test_create_wrong_encoding():
start_watching()
open(p('a_\udce4'), 'a').close()

event = event_queue.get(timeout=5)[0]
assert event.src_path == p('a_\udce4')
assert isinstance(event, FileCreatedEvent)

if not platform.is_windows():
event = event_queue.get(timeout=5)[0]
assert os.path.normpath(event.src_path) == os.path.normpath(p(''))
assert isinstance(event, DirModifiedEvent)


@pytest.mark.flaky(max_runs=5, min_passes=1, rerun_filter=rerun_filter)
def test_delete():
touch(p('a'))
Expand Down Expand Up @@ -303,10 +320,10 @@ def test_fast_subdirectory_creation_deletion():

@pytest.mark.flaky(max_runs=5, min_passes=1, rerun_filter=rerun_filter)
def test_passing_unicode_should_give_unicode():
start_watching(str_cls(p("")))
start_watching(str(p("")))
touch(p('a'))
event = event_queue.get(timeout=5)[0]
assert isinstance(event.src_path, str_cls)
assert isinstance(event.src_path, str)


@pytest.mark.skipif(platform.is_windows(),
Expand Down
2 changes: 0 additions & 2 deletions tests/test_inotify_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import unicode_literals

import pytest
from watchdog.utils import platform

Expand Down
1 change: 0 additions & 1 deletion tests/test_inotify_c.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import unicode_literals

import pytest
from watchdog.utils import platform
Expand Down
2 changes: 0 additions & 2 deletions tests/test_observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import unicode_literals

import pytest
from watchdog.events import FileSystemEventHandler, FileModifiedEvent
from watchdog.utils.compat import Event
Expand Down
1 change: 0 additions & 1 deletion tests/test_watchmedo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import pytest

Expand Down

0 comments on commit 432c31f

Please sign in to comment.