Skip to content

Commit

Permalink
Fixed an issue where XML parser errors could be reported on '<string>…
Browse files Browse the repository at this point in the history
…' instead of the actual input file.

 - Legacy-Id: 3723
  • Loading branch information
levkowetz committed Jul 16, 2020
1 parent c51345a commit fae96d4
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions cli/xml2rfc/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from urlparse import urlparse, urljoin, urlsplit

try:
import debug
from xml2rfc import debug
assert debug
except ImportError:
pass
Expand Down Expand Up @@ -199,8 +199,12 @@ def getReferenceRequest(self, request, include=False, line_no=0):
original = request # Used for the error message only
result = None # Our proper path
if request.endswith('.dtd') or request.endswith('.ent'):
if os.path.isabs(request) or urlparse(request).netloc:
# Absolute request, return as-is
if os.path.isabs(request) and os.path.exists(request):
# Absolute request, return as-is if it exists
attempts.append(request)
result = request
elif urlparse(request).netloc:
# Network request, return as-is
attempts.append(request)
result = request
else:
Expand Down Expand Up @@ -319,9 +323,9 @@ def getReferenceRequest(self, request, include=False, line_no=0):
# Verify the result -- either raise exception or return it
if not result or (not os.path.exists(result) and not urlparse(original).netloc):
if os.path.isabs(original):
xml2rfc.log.warn('A reference was requested with an absolute path, but not found '
'in that location. Removing the path component will cause xml2rfc to look for'
'the file automatically in standard locations.')
xml2rfc.log.warn('A reference was requested with an absolute path: "%s", but not found '
'in that location. Removing the path component will cause xml2rfc to look for '
'the file automatically in standard locations.' % original)
# Couldn't resolve. Throw an exception
error = XmlRfcError('Unable to resolve external request: '
+ '"' + original + '"', line_no=line_no, filename=self.source)
Expand Down Expand Up @@ -477,7 +481,7 @@ def __init__(self, source, verbose=None, quiet=None, options=base.default_option
if not library_dirs:
library_dirs = os.environ.get('XML_LIBRARY', '/usr/share/xml2rfc')
self.library_dirs = []
srcdir = os.path.abspath(os.path.dirname(self.source))
srcdir = os.path.abspath(os.path.dirname(self.source)) if source else ''
for raw_dir in re.split(':|;', library_dirs) + [ srcdir ]:
# Convert empty directory to source dir
if raw_dir == '':
Expand Down Expand Up @@ -517,6 +521,7 @@ def parse(self, remove_comments=True, remove_pis=False, quiet=False, strip_cdata

# Get an iterating parser object
file = six.BytesIO(text)
file.name = self.source
context = lxml.etree.iterparse(file,
dtd_validation=False,
load_dtd=True,
Expand Down Expand Up @@ -594,6 +599,7 @@ def parse(self, remove_comments=True, remove_pis=False, quiet=False, strip_cdata

# Parse the XML file into a tree and create an rfc instance
file = six.BytesIO(text)
file.name = self.source
tree = lxml.etree.parse(file, parser)
xmlrfc = XmlRfc(tree, self.default_dtd_path, nsmap=self.nsmap)
xmlrfc.source = self.source
Expand Down

0 comments on commit fae96d4

Please sign in to comment.