Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated RFPDupeFilter line separator for correct universal newlines mode usage. #4283

Merged
merged 4 commits into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scrapy/dupefilters.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def request_seen(self, request):
return True
self.fingerprints.add(fp)
if self.file:
self.file.write(fp + os.linesep)
self.file.write(fp + '\n')

def request_fingerprint(self, request):
return request_fingerprint(request)
Expand Down
48 changes: 39 additions & 9 deletions tests/test_dupefilters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import tempfile
import unittest
import shutil
import os
import sys
from testfixtures import LogCapture

from scrapy.dupefilters import RFPDupeFilter
Expand Down Expand Up @@ -84,17 +86,21 @@ def test_dupefilter_path(self):
path = tempfile.mkdtemp()
try:
df = RFPDupeFilter(path)
df.open()
assert not df.request_seen(r1)
assert df.request_seen(r1)
df.close('finished')
try:
df.open()
assert not df.request_seen(r1)
assert df.request_seen(r1)
finally:
df.close('finished')

df2 = RFPDupeFilter(path)
df2.open()
assert df2.request_seen(r1)
assert not df2.request_seen(r2)
assert df2.request_seen(r2)
df2.close('finished')
try:
df2.open()
assert df2.request_seen(r1)
assert not df2.request_seen(r2)
assert df2.request_seen(r2)
finally:
df2.close('finished')
finally:
shutil.rmtree(path)

Expand Down Expand Up @@ -129,6 +135,30 @@ def request_fingerprint(self, request):

case_insensitive_dupefilter.close('finished')

def test_seenreq_newlines(self):
""" Checks against adding duplicate \r to
line endings on Windows platforms. """

r1 = Request('http://scrapytest.org/1')

path = tempfile.mkdtemp()
try:
df = RFPDupeFilter(path)
df.open()
df.request_seen(r1)
df.close('finished')

with open(os.path.join(path, 'requests.seen'), 'rb') as seen_file:
line = next(seen_file).decode()
assert not line.endswith('\r\r\n')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also check if the line ends with \n here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just updated the PR, how's that? It should check that universal line endings mode is selected and working as expected now on each platform (Linux/Mac and Windows).

if sys.platform == 'win32':
assert line.endswith('\r\n')
else:
assert line.endswith('\n')

finally:
shutil.rmtree(path)

def test_log(self):
with LogCapture() as l:
settings = {'DUPEFILTER_DEBUG': False,
Expand Down