Skip to content

escape names interpolated into source-matching patterns - #772

Open
alhudz wants to merge 1 commit into
uqfoundation:masterfrom
alhudz:source-escape-name-patterns
Open

escape names interpolated into source-matching patterns#772
alhudz wants to merge 1 commit into
uqfoundation:masterfrom
alhudz:source-escape-name-patterns

Conversation

@alhudz

@alhudz alhudz commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Repro: set Baz.__name__ = r'\w+' on any class and call dill.source.getsource(Baz). It returns the source of a different class defined later in the same file. With Baz.__name__ = 'Baz((' it raises re.error rather than the IOError the function documents.

Cause: findsource concatenates __name__ straight into the class-definition pattern, so a name that is not an identifier is matched as a pattern instead of literally. __name__ is freely assignable and is not required to be an identifier. _closuredimport builds its two candidate patterns from getname() the same way, and getname() can return a repr containing brackets even for ordinary objects.

Fix: re.escape the interpolated names at the three sites. re.escape leaves identifiers untouched, so behaviour for ordinary names is unchanged. Test added covers both the wrong-match and the uncompilable name.

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.

1 participant