]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[2.7] bpo-32861: robotparser fix incomplete __str__ methods. (GH-5711) (GH-6795)...
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 14 May 2018 22:09:47 +0000 (01:09 +0300)
committerGitHub <noreply@github.com>
Mon, 14 May 2018 22:09:47 +0000 (01:09 +0300)
The robotparser's __str__ representation now includes wildcard
entries.
(cherry picked from commit c3fa1f2b93fa4bf96a8aadc74ee196384cefa31e)

Co-authored-by: Michael Lazar <lazar.michael22@gmail.com>.
Lib/robotparser.py
Lib/test/test_robotparser.py
Misc/ACKS
Misc/NEWS.d/next/Library/2018-04-02-20-44-54.bpo-32861.HeBjzN.rst [new file with mode: 0644]

index a7137a3064ff3cd133894e1b9f9b945daf1f1790..4e13f7f780ca3c955c04b8399906358d9e2fedb2 100644 (file)
@@ -160,7 +160,10 @@ class RobotFileParser:
 
 
     def __str__(self):
-        return ''.join([str(entry) + "\n" for entry in self.entries])
+        entries = self.entries
+        if self.default_entry is not None:
+            entries = entries + [self.default_entry]
+        return '\n'.join(map(str, entries)) + '\n'
 
 
 class RuleLine:
index 8ed5d89036190d20578c657b73ec4d2f635f5b01..ba7ccf8b58da24b57718f336156346123f99b8c2 100644 (file)
@@ -136,6 +136,31 @@ Disallow: /cyberworld/map/
     bad = ['/cyberworld/map/index.html']
 
 
+class StringFormattingTest(BaseRobotTest, unittest.TestCase):
+    robots_txt = """\
+User-agent: *
+Crawl-delay: 1
+Request-rate: 3/15
+Disallow: /cyberworld/map/ # This is an infinite virtual URL space
+
+# Cybermapper knows where to go.
+User-agent: cybermapper
+Disallow: /some/path
+    """
+
+    expected_output = """\
+User-agent: cybermapper
+Disallow: /some/path
+
+User-agent: *
+Disallow: /cyberworld/map/
+
+"""
+
+    def test_string_formatting(self):
+        self.assertEqual(str(self.parser), self.expected_output)
+
+
 class RobotHandler(BaseHTTPRequestHandler):
 
     def do_GET(self):
@@ -226,6 +251,7 @@ def test_main():
         UseFirstUserAgentWildcardTest,
         EmptyQueryStringTest,
         DefaultEntryTest,
+        StringFormattingTest,
         PasswordProtectedSiteTestCase,
         NetworkTestCase)
 
index 458f31e6a6b75a470a494f9b6567de2ac3cbd032..394fbb93f2f184163bc2239f3d70be44461c96ff 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -807,6 +807,7 @@ Ben Laurie
 Simon Law
 Julia Lawall
 Chris Lawrence
+Michael Lazar
 Brian Leair
 Mathieu Leduc-Hamel
 Amandine Lee
diff --git a/Misc/NEWS.d/next/Library/2018-04-02-20-44-54.bpo-32861.HeBjzN.rst b/Misc/NEWS.d/next/Library/2018-04-02-20-44-54.bpo-32861.HeBjzN.rst
new file mode 100644 (file)
index 0000000..13defbb
--- /dev/null
@@ -0,0 +1,3 @@
+The urllib.robotparser's ``__str__`` representation now includes wildcard
+entries and the "Crawl-delay" and "Request-rate" fields. Patch by
+Michael Lazar.