Skip to content

Commit

Permalink
Fix SkipRepeatsQueue._put() raises AttributeError (#817) (#818)
Browse files Browse the repository at this point in the history
* Fix SkipRepeatsQueue._put() raises AttributeError (#817)

This fixes an issue with SkipRepeatsQueue._put() which causes it to raise
an AttributeError on the first put. This is done by adding a check to test
if _last_item is set to None, which is the value set by __init__()

* Update changelog.rst

Co-authored-by: Mickaël Schoentgen <contact@tiger-222.fr>
  • Loading branch information
AndreiB97 and BoboTiG committed Jul 28, 2021
1 parent 1be65c0 commit 384dfea
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
3 changes: 2 additions & 1 deletion changelog.rst
Expand Up @@ -10,7 +10,8 @@ Changelog

- [watchmedo] Fix usage of ``os.setsid()`` and ``os.killpg()`` Unix-only functions. (`#809 <https://github.com/gorakhargosh/watchdog/pull/809>`_)
- [mac] Fix missing ``FileModifiedEvent`` on permission or ownership changes of a file. (`#809 <https://github.com/gorakhargosh/watchdog/pull/814>`_)
- Thanks to our beloved contributors: @replabrobin, @BoboTiG, @SamSchott
- [mac] Fix a possible ``AttributeError`` in ``SkipRepeatsQueue._put()``. (`#818 <https://github.com/gorakhargosh/watchdog/pull/818>`_)
- Thanks to our beloved contributors: @replabrobin, @BoboTiG, @SamSchott, @AndreiB97

2.1.3
~~~~~
Expand Down
2 changes: 1 addition & 1 deletion src/watchdog/utils/bricks.py
Expand Up @@ -87,7 +87,7 @@ def _init(self, maxsize):
self._last_item = None

def _put(self, item):
if item != self._last_item:
if self._last_item is None or item != self._last_item:
super()._put(item)
self._last_item = item
else:
Expand Down
13 changes: 13 additions & 0 deletions tests/test_skip_repeats_queue.py
Expand Up @@ -16,6 +16,7 @@
# limitations under the License.

import pytest
import watchdog.events as events
from watchdog.utils.bricks import SkipRepeatsQueue

from .markers import cpython_only
Expand Down Expand Up @@ -58,6 +59,18 @@ def test_allow_nonconsecutive():
assert q.empty()


def test_put_with_watchdog_events():
# FileSystemEvent.__ne__() uses the key property without
# doing any type checking. Since _last_item is set to
# None in __init__(), an AttributeError is raised when
# FileSystemEvent.__ne__() tries to use None.key
queue = SkipRepeatsQueue()
dummy_file = 'dummy.txt'
event = events.FileCreatedEvent(dummy_file)
queue.put(event)
assert queue.get() is event


def test_prevent_consecutive():
q = SkipRepeatsQueue()

Expand Down

0 comments on commit 384dfea

Please sign in to comment.