Skip to content

escape tuple labels in generate_gml - #8755

Merged
dschult merged 2 commits into
networkx:mainfrom
abdul-khaliq-khalid:gml-tuple-label-escape
Jul 14, 2026
Merged

dschult merged 2 commits into
networkx:mainfrom
abdul-khaliq-khalid:gml-tuple-label-escape

Conversation

@abdul-khaliq-khalid

Copy link
Copy Markdown
Contributor

When a node or edge label is a tuple, generate_gml joins repr() of the elements directly into a quoted GML string without running it through escape() the way every other string value goes. A tuple element whose repr contains a double quote closes the string early, so a crafted label injects extra nodes, edges, or attributes into the output that another reader then parses back. The fix builds the tuple text and escapes it before emitting, so legitimate tuple labels are unchanged and quotes become the usual " entities. Added a round-trip regression test alongside the existing tuple-label and quoting tests.

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for this! Looks like a case that slipped through -- just as you seem to have inferred. I have a minor suggestion that allows the expected string for one assert to avoid using escapes so it is easier to read. What do you think?

Comment thread networkx/readwrite/tests/test_gml.py Resolved

@amcandio amcandio left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good to me. It's worth evaluating using f strings rather than +, should be faster and nicer to read.

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I haven't looked in depth, but usually for cases like these the recommendation is to use the liter_stringizer/destringizer functions (see e.g. #6943). Does that work in this case?

@abdul-khaliq-khalid

Copy link
Copy Markdown
Contributor Author

@amcandio switched that line to an f-string, agreed it reads better.

@rossbar the literal_stringizer/destringizer path is a bit orthogonal here. Tuple labels are caught by this dedicated isinstance(value, tuple) and key == "label" branch (the default-path fallback added in #6943) before they ever reach a provided stringizer, and on read-back you'd need destringizer=literal_destringizer to recover an actual tuple, so in the default path (destringizer=None) it comes back as a string either way. The bug here was just that this one branch skipped escape() while every other string value in the default path goes through it, so a " in an element repr closed the GML string early. This PR only plugs that escape hole and leaves the output otherwise unchanged. Happy to look at routing tuple labels through the literal path as a separate, larger change if you'd prefer that direction.

@rossbar

rossbar commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

@rossbar the literal_stringizer/destringizer path is a bit orthogonal here. Tuple labels are caught by this dedicated isinstance(value, tuple) and key == "label" branch (the default-path fallback added in #6943) before they ever reach a provided stringizer, and on read-back you'd need destringizer=literal_destringizer to recover an actual tuple, so in the default path (destringizer=None) it comes back as a string either way. The bug here was just that this one branch skipped escape() while every other string value in the default path goes through it, so a " in an element repr closed the GML string early. This PR only plugs that escape hole and leaves the output otherwise unchanged. Happy to look at routing tuple labels through the literal path as a separate, larger change if you'd prefer that direction.

Right - my question is: if the user were to use the liter_stringizer / liter_destringizer functions for "stringize"-ing, would they get the expected answer? I understand this doesn't work with the default code paths, but lots of things don't serialize "correctly" with the defaults. The current policy is something like: if you have complex data to serialize, use the "stringizer" parameter to bring your own serialization method. Is this inadequate?

@abdul-khaliq-khalid

Copy link
Copy Markdown
Contributor Author

Right, and that's the part that caught me too. A supplied stringizer never actually sees a tuple label. The isinstance(value, tuple) and key == "label" branch sits above the else clause where the stringizer gets called, so it short-circuits first. I just checked with generate_gml(G, stringizer=literal_stringizer) on a tuple node label and it still comes out of that dedicated branch as label "('a','b')"; literal_stringizer is never consulted. So there's no opt-out here, any tuple used as a node label takes this path no matter what stringizer you pass.

And this isn't really about serializing complex data the 'right' way. The fix doesn't change how the tuple renders at all, same (...) text. It just runs that text through escape() like every other string value in stringize already does in the else branch. Without it a " inside an element closes the GML string early, so the output stops being valid round-trippable GML and injects extra nodes. So I'd read it as one branch that forgot to escape, orthogonal to the stringizer policy rather than a case the policy is meant to cover.

@dschult
dschult merged commit 304b3ae into networkx:main Jul 14, 2026
43 checks passed
@dschult

dschult commented Jul 14, 2026

Copy link
Copy Markdown
Member

Thanks @abdul-khaliq-khalid

@dschult dschult added this to the 3.7 milestone Jul 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

4 participants