Changeset 391 for python/trunk/Lib/xml/sax
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/xml/sax/__init__.py
r2 r391 60 60 61 61 import os, sys 62 if os.environ.has_key("PY_SAX_PARSER"):62 if "PY_SAX_PARSER" in os.environ: 63 63 default_parser_list = os.environ["PY_SAX_PARSER"].split(",") 64 64 del os -
python/trunk/Lib/xml/sax/_exceptions.py
r2 r391 13 13 functionality, or to add localization. Note that although you will 14 14 receive a SAXException as the argument to the handlers in the 15 ErrorHandler interface, you are not actually required to throw15 ErrorHandler interface, you are not actually required to raise 16 16 the exception; instead, you can simply read the information in 17 17 it.""" … … 51 51 receive a SAXParseException as the argument to the handlers in the 52 52 ErrorHandler interface, the application is not actually required 53 to throwthe exception; instead, it can simply read the53 to raise the exception; instead, it can simply read the 54 54 information in it and take a different action. 55 55 … … 63 63 64 64 # We need to cache this stuff at construction time. 65 # If this exception is thrown, the objects through which we must65 # If this exception is raised, the objects through which we must 66 66 # traverse to get this information may be deleted by the time 67 67 # it gets caught. -
python/trunk/Lib/xml/sax/expatreader.py
r2 r391 109 109 def prepareParser(self, source): 110 110 if source.getSystemId() is not None: 111 self._parser.SetBase(source.getSystemId()) 111 base = source.getSystemId() 112 if isinstance(base, unicode): 113 base = base.encode('utf-8') 114 self._parser.SetBase(base) 112 115 113 116 # Redefined setContentHandler to allow changing handlers during parsing … … 408 411 409 412 if __name__ == "__main__": 410 import xml.sax 413 import xml.sax.saxutils 411 414 p = create_parser() 412 p.setContentHandler(xml.sax. XMLGenerator())415 p.setContentHandler(xml.sax.saxutils.XMLGenerator()) 413 416 p.setErrorHandler(xml.sax.ErrorHandler()) 414 p.parse(" ../../../hamlet.xml")417 p.parse("http://www.ibiblio.org/xml/examples/shakespeare/hamlet.xml") -
python/trunk/Lib/xml/sax/handler.py
r2 r391 7 7 of the interfaces. 8 8 9 $Id : handler.py 35816 2004-05-06 03:47:48Z fdrake$9 $Id$ 10 10 """ 11 11 -
python/trunk/Lib/xml/sax/saxutils.py
r2 r391 5 5 6 6 import os, urlparse, urllib, types 7 import io 8 import sys 7 9 import handler 8 10 import xmlreader … … 12 14 except AttributeError: 13 15 _StringTypes = [types.StringType] 14 15 # See whether the xmlcharrefreplace error handler is16 # supported17 try:18 from codecs import xmlcharrefreplace_errors19 _error_handling = "xmlcharrefreplace"20 del xmlcharrefreplace_errors21 except ImportError:22 _error_handling = "strict"23 16 24 17 def __dict_replace(s, d): … … 82 75 83 76 77 def _gettextwriter(out, encoding): 78 if out is None: 79 import sys 80 out = sys.stdout 81 82 if isinstance(out, io.RawIOBase): 83 buffer = io.BufferedIOBase(out) 84 # Keep the original file open when the TextIOWrapper is 85 # destroyed 86 buffer.close = lambda: None 87 else: 88 # This is to handle passed objects that aren't in the 89 # IOBase hierarchy, but just have a write method 90 buffer = io.BufferedIOBase() 91 buffer.writable = lambda: True 92 buffer.write = out.write 93 try: 94 # TextIOWrapper uses this methods to determine 95 # if BOM (for UTF-16, etc) should be added 96 buffer.seekable = out.seekable 97 buffer.tell = out.tell 98 except AttributeError: 99 pass 100 # wrap a binary writer with TextIOWrapper 101 class UnbufferedTextIOWrapper(io.TextIOWrapper): 102 def write(self, s): 103 super(UnbufferedTextIOWrapper, self).write(s) 104 self.flush() 105 return UnbufferedTextIOWrapper(buffer, encoding=encoding, 106 errors='xmlcharrefreplace', 107 newline='\n') 108 84 109 class XMLGenerator(handler.ContentHandler): 85 110 86 111 def __init__(self, out=None, encoding="iso-8859-1"): 87 if out is None:88 import sys89 out = sys.stdout90 112 handler.ContentHandler.__init__(self) 91 self._out = out 113 out = _gettextwriter(out, encoding) 114 self._write = out.write 115 self._flush = out.flush 92 116 self._ns_contexts = [{}] # contains uri -> prefix dicts 93 117 self._current_context = self._ns_contexts[-1] … … 95 119 self._encoding = encoding 96 120 97 def _write(self, text):98 if isinstance(text, str):99 self._out.write(text)100 else:101 self._out.write(text.encode(self._encoding, _error_handling))102 103 121 def _qname(self, name): 104 122 """Builds a qualified name from a (ns_url, localname) pair""" 105 123 if name[0]: 124 # Per http://www.w3.org/XML/1998/namespace, The 'xml' prefix is 125 # bound by definition to http://www.w3.org/XML/1998/namespace. It 126 # does not need to be declared and will not usually be found in 127 # self._current_context. 128 if 'http://www.w3.org/XML/1998/namespace' == name[0]: 129 return 'xml:' + name[1] 106 130 # The name is in a non-empty namespace 107 131 prefix = self._current_context[name[0]] … … 115 139 116 140 def startDocument(self): 117 self._write( '<?xml version="1.0" encoding="%s"?>\n' %141 self._write(u'<?xml version="1.0" encoding="%s"?>\n' % 118 142 self._encoding) 143 144 def endDocument(self): 145 self._flush() 119 146 120 147 def startPrefixMapping(self, prefix, uri): … … 128 155 129 156 def startElement(self, name, attrs): 130 self._write( '<' + name)157 self._write(u'<' + name) 131 158 for (name, value) in attrs.items(): 132 self._write( ' %s=%s' % (name, quoteattr(value)))133 self._write( '>')159 self._write(u' %s=%s' % (name, quoteattr(value))) 160 self._write(u'>') 134 161 135 162 def endElement(self, name): 136 self._write( '</%s>' % name)163 self._write(u'</%s>' % name) 137 164 138 165 def startElementNS(self, name, qname, attrs): 139 self._write( '<' + self._qname(name))166 self._write(u'<' + self._qname(name)) 140 167 141 168 for prefix, uri in self._undeclared_ns_maps: 142 169 if prefix: 143 self._ out.write(' xmlns:%s="%s"' % (prefix, uri))170 self._write(u' xmlns:%s="%s"' % (prefix, uri)) 144 171 else: 145 self._ out.write(' xmlns="%s"' % uri)172 self._write(u' xmlns="%s"' % uri) 146 173 self._undeclared_ns_maps = [] 147 174 148 175 for (name, value) in attrs.items(): 149 self._write( ' %s=%s' % (self._qname(name), quoteattr(value)))150 self._write( '>')176 self._write(u' %s=%s' % (self._qname(name), quoteattr(value))) 177 self._write(u'>') 151 178 152 179 def endElementNS(self, name, qname): 153 self._write( '</%s>' % self._qname(name))180 self._write(u'</%s>' % self._qname(name)) 154 181 155 182 def characters(self, content): 183 if not isinstance(content, unicode): 184 content = unicode(content, self._encoding) 156 185 self._write(escape(content)) 157 186 158 187 def ignorableWhitespace(self, content): 188 if not isinstance(content, unicode): 189 content = unicode(content, self._encoding) 159 190 self._write(content) 160 191 161 192 def processingInstruction(self, target, data): 162 self._write( '<?%s %s?>' % (target, data))193 self._write(u'<?%s %s?>' % (target, data)) 163 194 164 195 … … 288 319 289 320 if source.getByteStream() is None: 290 sysid = source.getSystemId() 291 basehead = os.path.dirname(os.path.normpath(base)) 292 sysidfilename = os.path.join(basehead, sysid) 293 if os.path.isfile(sysidfilename): 321 try: 322 sysid = source.getSystemId() 323 basehead = os.path.dirname(os.path.normpath(base)) 324 encoding = sys.getfilesystemencoding() 325 if isinstance(sysid, unicode): 326 if not isinstance(basehead, unicode): 327 try: 328 basehead = basehead.decode(encoding) 329 except UnicodeDecodeError: 330 sysid = sysid.encode(encoding) 331 else: 332 if isinstance(basehead, unicode): 333 try: 334 sysid = sysid.decode(encoding) 335 except UnicodeDecodeError: 336 basehead = basehead.encode(encoding) 337 sysidfilename = os.path.join(basehead, sysid) 338 isfile = os.path.isfile(sysidfilename) 339 except UnicodeError: 340 isfile = False 341 if isfile: 294 342 source.setSystemId(sysidfilename) 295 343 f = open(sysidfilename, "rb") 296 344 else: 297 source.setSystemId(urlparse.urljoin(base, s ysid))345 source.setSystemId(urlparse.urljoin(base, source.getSystemId())) 298 346 f = urllib.urlopen(source.getSystemId()) 299 347 -
python/trunk/Lib/xml/sax/xmlreader.py
r2 r391 69 69 SAX parsers are not required to provide localization for errors 70 70 and warnings; if they cannot support the requested locale, 71 however, they must throwa SAX exception. Applications may71 however, they must raise a SAX exception. Applications may 72 72 request a locale change in the middle of a parse.""" 73 73 raise SAXNotSupportedException("Locale support not implemented") … … 323 323 324 324 def __contains__(self, name): 325 return self._attrs.has_key(name)325 return name in self._attrs 326 326 327 327 def get(self, name, alternative=None):
Note:
See TracChangeset
for help on using the changeset viewer.