annotate Lib/xml/sax/_exceptions.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents cb1e432b4919
children fdf907708f49
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
39275
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1 """Different kinds of SAX Exceptions"""
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
2 import sys
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
3 if sys.platform[:4] == "java":
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
4 from java.lang import Exception
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
5 del sys
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
6
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
7 # ===== SAXEXCEPTION =====
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
8
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
9 class SAXException(Exception):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
10 """Encapsulate an XML error or warning. This class can contain
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
11 basic error or warning information from either the XML parser or
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
12 the application: you can subclass it to provide additional
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
13 functionality, or to add localization. Note that although you will
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
14 receive a SAXException as the argument to the handlers in the
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
15 ErrorHandler interface, you are not actually required to throw
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
16 the exception; instead, you can simply read the information in
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
17 it."""
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
18
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
19 def __init__(self, msg, exception=None):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
20 """Creates an exception. The message is required, but the exception
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
21 is optional."""
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
22 self._msg = msg
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
23 self._exception = exception
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
24 Exception.__init__(self, msg)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
25
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
26 def getMessage(self):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
27 "Return a message for this exception."
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
28 return self._msg
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
29
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
30 def getException(self):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
31 "Return the embedded exception, or None if there was none."
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
32 return self._exception
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
33
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
34 def __str__(self):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
35 "Create a string representation of the exception."
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
36 return self._msg
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
37
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
38 def __getitem__(self, ix):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
39 """Avoids weird error messages if someone does exception[ix] by
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
40 mistake, since Exception has __getitem__ defined."""
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
41 raise AttributeError("__getitem__")
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
42
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
43
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
44 # ===== SAXPARSEEXCEPTION =====
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
45
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
46 class SAXParseException(SAXException):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
47 """Encapsulate an XML parse error or warning.
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
48
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
49 This exception will include information for locating the error in
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
50 the original XML document. Note that although the application will
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
51 receive a SAXParseException as the argument to the handlers in the
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
52 ErrorHandler interface, the application is not actually required
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
53 to throw the exception; instead, it can simply read the
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
54 information in it and take a different action.
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
55
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
56 Since this exception is a subclass of SAXException, it inherits
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
57 the ability to wrap another exception."""
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
58
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
59 def __init__(self, msg, exception, locator):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
60 "Creates the exception. The exception parameter is allowed to be None."
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
61 SAXException.__init__(self, msg, exception)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
62 self._locator = locator
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
63
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
64 # We need to cache this stuff at construction time.
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
65 # If this exception is thrown, the objects through which we must
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
66 # traverse to get this information may be deleted by the time
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
67 # it gets caught.
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
68 self._systemId = self._locator.getSystemId()
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
69 self._colnum = self._locator.getColumnNumber()
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
70 self._linenum = self._locator.getLineNumber()
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
71
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
72 def getColumnNumber(self):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
73 """The column number of the end of the text where the exception
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
74 occurred."""
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
75 return self._colnum
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
76
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
77 def getLineNumber(self):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
78 "The line number of the end of the text where the exception occurred."
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
79 return self._linenum
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
80
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
81 def getPublicId(self):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
82 "Get the public identifier of the entity where the exception occurred."
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
83 return self._locator.getPublicId()
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
84
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
85 def getSystemId(self):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
86 "Get the system identifier of the entity where the exception occurred."
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
87 return self._systemId
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
88
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
89 def __str__(self):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
90 "Create a string representation of the exception."
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
91 sysid = self.getSystemId()
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
92 if sysid is None:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
93 sysid = "<unknown>"
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
94 linenum = self.getLineNumber()
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
95 if linenum is None:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
96 linenum = "?"
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
97 colnum = self.getColumnNumber()
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
98 if colnum is None:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
99 colnum = "?"
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
100 return "%s:%s:%s: %s" % (sysid, linenum, colnum, self._msg)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
101
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
102
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
103 # ===== SAXNOTRECOGNIZEDEXCEPTION =====
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
104
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
105 class SAXNotRecognizedException(SAXException):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
106 """Exception class for an unrecognized identifier.
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
107
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
108 An XMLReader will raise this exception when it is confronted with an
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
109 unrecognized feature or property. SAX applications and extensions may
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
110 use this class for similar purposes."""
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
111
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
112
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
113 # ===== SAXNOTSUPPORTEDEXCEPTION =====
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
114
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
115 class SAXNotSupportedException(SAXException):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
116 """Exception class for an unsupported operation.
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
117
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
118 An XMLReader will raise this exception when a service it cannot
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
119 perform is requested (specifically setting a state or value). SAX
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
120 applications and extensions may use this class for similar
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
121 purposes."""
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
122
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
123 # ===== SAXNOTSUPPORTEDEXCEPTION =====
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
124
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
125 class SAXReaderNotAvailable(SAXNotSupportedException):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
126 """Exception class for a missing driver.
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
127
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
128 An XMLReader module (driver) should raise this exception when it
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
129 is first imported, e.g. when a support module cannot be imported.
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
130 It also may be raised during parsing, e.g. if executing an external
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
131 program is not permitted."""