Skip to content

Bug: gpxpy.parse(...) fails on all GPX files #285

@danielyli

Description

@danielyli

Hi, I just installed this library to try out but immediately encountered a parsing bug. Steps to reproduce below.

Summary

Invocations of gpxpy.parse(...) fail for all GPX files with GPXXMLSyntaxException: Error parsing XML: not well-formed (invalid token): line 1, column 0.

Steps to Reproduce

# Python 3.12.7 (main, Oct  1 2024, 02:05:46) [Clang 16.0.0 (clang-1600.0.26.3)]
# Type 'copyright', 'credits' or 'license' for more information
# IPython 8.28.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import gpxpy; gpx = gpxpy.parse('/tmp/gpxpy/test_files/route.gpx')

# [07:42:09] DEBUG    [gpxpy.parser::parse]  Error in:                    parser.py:137
#                     /tmp/gpxpy/test_files/route.gpx
#                     -----------
#                     Traceback (most recent call last):
#                       File "/tmp/.venv/lib/python3.12/site-packages/gpxpy/parser.py",
#                     line 134, in parse
#                         root = mod_etree.XML(self.xml)
#                                ^^^^^^^^^^^^^^^^^^^^^^^
#                       File "/opt/homebrew/Cellar/python@3.12/3.12.7/Frameworks/Python.framework/Versions/3.12/lib/python3.12/xml/etree/ElementTree.py", line
#                     1335, in XML
#                         parser.feed(text)
#                     xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 0
# ---------------------------------------------------------------------------
# ParseError                                Traceback (most recent call last)
# File /tmp/.venv/lib/python3.12/site-packages/gpxpy/parser.py:134, in GPXParser.parse(self, version)
#     133     else:
# --> 134         root = mod_etree.XML(self.xml)
#     135 except Exception as e:
#     136     # The exception here can be a lxml or ElementTree exception.
# 
# File /opt/homebrew/Cellar/python@3.12/3.12.7/Frameworks/Python.framework/Versions/3.12/lib/python3.12/xml/etree/ElementTree.py:1335, in XML(text, parser)
#    1334     parser = XMLParser(target=TreeBuilder())
# -> 1335 parser.feed(text)
#    1336 return parser.close()
# 
# ParseError: not well-formed (invalid token): line 1, column 0
# 
# The above exception was the direct cause of the following exception:
# 
# GPXXMLSyntaxException                     Traceback (most recent call last)
# Cell In[1], line 1
# ----> 1 import gpxpy; gpx = gpxpy.parse('/tmp/gpxpy/test_files/route.gpx')
# 
# File /tmp/.venv/lib/python3.12/site-packages/gpxpy/__init__.py:39, in parse(xml_or_file, version)
#      35 from . import parser as mod_parser
#      37 parser = mod_parser.GPXParser(xml_or_file)
# ---> 39 return parser.parse(version)
# 
# File /tmp/.venv/lib/python3.12/site-packages/gpxpy/parser.py:146, in GPXParser.parse(self, version)
#     137     log.debug('Error in:\n%s\n-----------\n', self.xml, exc_info=True)
#     139     # The library should work in the same way regardless of the
#     140     # underlying XML parser that's why the exception thrown
#     141     # here is GPXXMLSyntaxException (instead of simply throwing the
#    (...)
#     144     # But, if the user needs the original exception (lxml or ElementTree)
#     145     # it is available with GPXXMLSyntaxException.original_exception:
# --> 146     raise mod_gpx.GPXXMLSyntaxException(f'Error parsing XML: {e}', e)
#     148 if root is None:
#     149     raise mod_gpx.GPXException('Document must have a `gpx` root node.')
# 
# GPXXMLSyntaxException: Error parsing XML: not well-formed (invalid token): line 1, column 0

System Information

  • gpxpy 1.6.2
  • Python 3.12.7
  • MacOS 15.0.1 (arm64)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions