[Python-checkins] r86139 - in python/branches/py3k: Doc/library/nntplib.rst Lib/nntplib.py Lib/test/test_nntplib.py Misc/NEWS

antoine.pitrou python-checkins at python.org
Wed Nov 3 19:18:43 CET 2010


Author: antoine.pitrou
Date: Wed Nov  3 19:18:43 2010
New Revision: 86139

Log:
Issue #10281: nntplib now returns None for absent fields in the OVER/XOVER
response, instead of raising an exception.



Modified:
   python/branches/py3k/Doc/library/nntplib.rst
   python/branches/py3k/Lib/nntplib.py
   python/branches/py3k/Lib/test/test_nntplib.py
   python/branches/py3k/Misc/NEWS

Modified: python/branches/py3k/Doc/library/nntplib.rst
==============================================================================
--- python/branches/py3k/Doc/library/nntplib.rst	(original)
+++ python/branches/py3k/Doc/library/nntplib.rst	Wed Nov  3 19:18:43 2010
@@ -252,6 +252,8 @@
      (including headers and body)
    * the ``:lines`` metadata: the number of lines in the article body
 
+   The value of each item is either a string, or :const:`None` if not present.
+
    It is advisable to use the :func:`decode_header` function on header
    values when they may contain non-ASCII characters::
 

Modified: python/branches/py3k/Lib/nntplib.py
==============================================================================
--- python/branches/py3k/Lib/nntplib.py	(original)
+++ python/branches/py3k/Lib/nntplib.py	Wed Nov  3 19:18:43 2010
@@ -205,11 +205,12 @@
             is_metadata = field_name.startswith(':')
             if i >= n_defaults and not is_metadata:
                 # Non-default header names are included in full in the response
-                h = field_name + ":"
-                if token[:len(h)].lower() != h:
+                # (unless the field is totally empty)
+                h = field_name + ": "
+                if token and token[:len(h)].lower() != h:
                     raise NNTPDataError("OVER/XOVER response doesn't include "
                                         "names of additional headers")
-                token = token[len(h):].lstrip(" ")
+                token = token[len(h):] if token else None
             fields[fmt[i]] = token
         overview.append((article_number, fields))
     return overview

Modified: python/branches/py3k/Lib/test/test_nntplib.py
==============================================================================
--- python/branches/py3k/Lib/test/test_nntplib.py	(original)
+++ python/branches/py3k/Lib/test/test_nntplib.py	Wed Nov  3 19:18:43 2010
@@ -457,7 +457,7 @@
                     "\tThu, 22 Jul 2010 09:14:14 -0400"
                     "\t<A29863FA-F388-40C3-AA25-0FD06B09B5BF at gmail.com>"
                     "\t\t6683\t16"
-                    "\tXref: news.gmane.org gmane.comp.python.authors:58"
+                    "\t"
                     "\n"
                 # An UTF-8 overview line from fr.comp.lang.python
                 "59\tRe: Message d'erreur incompréhensible (par moi)"
@@ -824,6 +824,8 @@
             ":lines": "16",
             "xref": "news.gmane.org gmane.comp.python.authors:57"
             })
+        art_num, over = overviews[1]
+        self.assertEqual(over["xref"], None)
         art_num, over = overviews[2]
         self.assertEqual(over["subject"],
                          "Re: Message d'erreur incompréhensible (par moi)")
@@ -1028,6 +1030,29 @@
             ':lines': '17',
             'xref': 'news.example.com misc.test:3000363',
         })
+        # Second example; here the "Xref" field is totally absent (including
+        # the header name) and comes out as None
+        lines = [
+            '3000234\tI am just a test article\t"Demo User" '
+            '<nobody at example.com>\t6 Oct 1998 04:38:40 -0500\t'
+            '<45223423 at example.com>\t<45454 at example.net>\t1234\t'
+            '17\t\t',
+        ]
+        overview = nntplib._parse_overview(lines, fmt)
+        (art_num, fields), = overview
+        self.assertEqual(fields['xref'], None)
+        # Third example; the "Xref" is an empty string, while "references"
+        # is a single space.
+        lines = [
+            '3000234\tI am just a test article\t"Demo User" '
+            '<nobody at example.com>\t6 Oct 1998 04:38:40 -0500\t'
+            '<45223423 at example.com>\t \t1234\t'
+            '17\tXref: \t',
+        ]
+        overview = nntplib._parse_overview(lines, fmt)
+        (art_num, fields), = overview
+        self.assertEqual(fields['references'], ' ')
+        self.assertEqual(fields['xref'], '')
 
     def test_parse_datetime(self):
         def gives(a, b, *c):

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Wed Nov  3 19:18:43 2010
@@ -59,6 +59,9 @@
 Library
 -------
 
+- Issue #10281: nntplib now returns None for absent fields in the OVER/XOVER
+  response, instead of raising an exception.
+
 - wsgiref now implements and validates PEP 3333, rather than an experimental
   extension of PEP 333.  (Note: earlier versions of Python 3.x may have
   incorrectly validated some non-compliant applications as WSGI compliant;


More information about the Python-checkins mailing list