[Python-checkins] r68053 - in sandbox/trunk/pep0/pep0: output.py pep.py

benjamin.peterson python-checkins at python.org
Tue Dec 30 04:06:54 CET 2008


Author: benjamin.peterson
Date: Tue Dec 30 04:06:54 2008
New Revision: 68053

Log:
instead of using ValueErrors make a custom PEPError

Modified:
   sandbox/trunk/pep0/pep0/output.py
   sandbox/trunk/pep0/pep0/pep.py

Modified: sandbox/trunk/pep0/pep0/output.py
==============================================================================
--- sandbox/trunk/pep0/pep0/output.py	(original)
+++ sandbox/trunk/pep0/pep0/output.py	Tue Dec 30 04:06:54 2008
@@ -5,7 +5,7 @@
 from operator import attrgetter
 
 from . import constants
-from .pep import PEP
+from .pep import PEP, PEPError
 
 
 indent = u' '
@@ -47,8 +47,9 @@
         elif pep.status == 'Final':
             finished.append(pep)
         else:
-            raise ValueError("PEP %s unsorted (%s/%s)" % (pep.number,
-                                pep.type_, pep.status))
+            raise PEPError("unsorted (%s/%s)" %
+                           (pep.type_, pep.status),
+                           pep.filename, pep.number)
     return meta, info, accepted, open_, finished, dead
 
 

Modified: sandbox/trunk/pep0/pep0/pep.py
==============================================================================
--- sandbox/trunk/pep0/pep0/pep.py	(original)
+++ sandbox/trunk/pep0/pep0/pep.py	Tue Dec 30 04:06:54 2008
@@ -7,6 +7,21 @@
 from . import constants
 
 
+class PEPError(Exception):
+
+    def __init__(self, error, pep_file, pep_number=None):
+        super(PEPError, self).__init__(error)
+        self.filename = pep_file
+        self.number = pep_number
+
+    def __str__(self):
+        error_msg = super(PEPError, self).__str__()
+        if self.number is not None:
+            return "PEP %d: %r" % (self.number, error_msg)
+        else:
+            return "(%s): %r" % (self.filename, error_msg)
+
+
 class Author(object):
 
     """Represent PEP authors.
@@ -138,6 +153,7 @@
     def __init__(self, pep_file):
         """Init object from an open PEP file object."""
         # Parse the headers.
+        self.filename = pep_file
         pep_parser = HeaderParser()
         metadata = pep_parser.parse(pep_file)
         header_order = iter(self.headers)
@@ -147,46 +163,52 @@
                 while header_name != current_header and not required:
                     current_header, required = header_order.next()
                 if header_name != current_header:
-                    raise ValueError("for the PEP at %s, did not deal with "
-                    "%r before having to handle %r" % (pep_file.name,
-                        header_name, current_header))
+                    raise PEPError("did not deal with "
+                                   "%r before having to handle %r" %
+                                   (header_name, current_header),
+                                   pep_file.name)
         except StopIteration:
-            raise ValueError("headers missing or out of order for the PEP at "
-                                "%s" % pep_file.name)
+            raise PEPError("headers missing or out of order",
+                                pep_file.name)
         required = False
         try:
             while not required:
                 current_header, required = header_order.next()
             else:
-                raise ValueError("PEP at %s is missing its %r" %
-                        (pep_file.name, current_header))
+                raise PEPError("PEP is missing its %r" % (current_header,),
+                               pep_file.name)
         except StopIteration:
             pass
         # 'PEP'.
-        self.number = int(metadata['PEP'])
+        try:
+            self.number = int(metadata['PEP'])
+        except ValueError:
+            raise PEPParseError("PEP number isn't an integer", pep_file.name)
         # 'Title'.
         self.title = metadata['Title']
         # 'Type'.
         type_ = metadata['Type']
         if type_ not in self.type_values:
-            raise ValueError('%r is not a valid Type value (PEP %s)' %
-                                (type_, self.number))
+            raise PEPError('%r is not a valid Type value' % (type_,),
+                           pep_file.name, self.number)
         self.type_ = type_
         # 'Status'.
         status = metadata['Status']
         if status not in self.status_values:
-            raise ValueError("%r is not a valid Status value (PEP %s)" %
-                                (status, self.number))
+            raise PEPError("%r is not a valid Status value" %
+                           (status,), pep_file.name, self.number)
         # Special case for Active PEPs.
         if (status == u"Active" and
                 self.type_ not in ("Process", "Informational")):
-            raise ValueError("Only Process and Informational PEPs may have an "
-                                "Active status (PEP %s)" % self.number)
+            raise PEPError("Only Process and Informational PEPs may "
+                           "have an Active status", pep_file.name,
+                           self.number)
         self.status = status
         # 'Author'.
         authors_and_emails = self._parse_author(metadata['Author'])
         if len(authors_and_emails) < 1:
-            raise ValueError("no authors found (PEP %s)" % self.number)
+            raise PEPError("no authors found", pep_file.name,
+                           self.number)
         self.authors = map(Author, authors_and_emails)
 
     def _parse_author(self, data):


More information about the Python-checkins mailing list