Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[glifLib] Wrap XML library exceptions with glifLib types when parsing glifs #3029

Merged
merged 2 commits into from
Mar 16, 2023

Conversation

Hoolean
Copy link
Contributor

@Hoolean Hoolean commented Mar 9, 2023

Hello!

This PR abstracts across XML library exceptions with glifLib errors when parsing glyphs, which allows dependent projects to catch syntax errors without requiring logic to account for which XML library fonttools is using internally (e.g. for implementing fonttools/ufoLib2#264).

The PR also also adds tests to ensure that the exception we expose remains stable across future releases.

Affected functions

  • glifLib.readGlyphFromString()
  • glifLib.GlyphSet.readGlyph()

Alternative

If we wanted to abstract over lxml/xml exceptions more broadly, we could catch them in etree.py and wrap them with a fonttools-specific XmlParsingError; this would also have the cost of maintaining wrapper functions (e.g. for etree.fromstring()) where there currently are none.

This allows dependent projects to catch errors parsing glifs without
requiring logic to account for which XML library fonttools is using
internally (e.g. for implementing fonttools/ufoLib2#264).

This commit also adds tests to ensure that the exception we expose when
glifs have invalid syntax remains stable across future releases.
This commit annotates errors from GlyphSet.readGlyph() with the details
of the glyph that originated them (e.g. name, path to glif). This is
implemented with a loose backport of PEP678, to avoid adding a wrapper
error that would be less specific and would break API compatibility.

In addition, this commit adds a test to ensure that the new details are
present (specifically, in the case of parsing invalid XML).
Copy link
Member

@anthrotype anthrotype left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@anthrotype anthrotype merged commit 5d0432a into fonttools:main Mar 16, 2023
@Hoolean Hoolean deleted the wrap-glif-xml-errors branch March 16, 2023 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants