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

Infinite Recursion Inside strlist.grow #69

Open
AlecRosenbaum opened this issue Dec 4, 2019 · 0 comments
Open

Infinite Recursion Inside strlist.grow #69

AlecRosenbaum opened this issue Dec 4, 2019 · 0 comments

Comments

@AlecRosenbaum
Copy link

If you give strlist.grow a subclass of str, strlist.grow will infinitely recurse.

This has come up because of changes using futurize.builtins.str made in an effort to make sourcecode compatible with python 2 and python 3.

It looks like the very strict type checks here are to blame:

pybars3/pybars/_compiler.py

Lines 191 to 197 in 7a8bd6d

if type(thing) == str_class:
self.append(thing)
# This will only ever match in Python 2 since str_class is str in
# Python 3.
elif type(thing) == str:
self.append(unicode(thing)) # noqa: F821 undefined name 'unicode'

In a pdb shell (running latest python 2.7) I get the following output:

(Pdb) ll
 204         def grow(self, thing):
 205             """Make the list longer, appending for unicode, extending otherwise."""
 206             if type(thing) == str_class:
 207                 self.append(thing)
 208
 209             # This will only ever match in Python 2 since str_class is str in
 210             # Python 3.
 211             elif type(thing) == str:
 212                 self.append(unicode(thing))  # noqa: F821 undefined name 'unicode'
 213
 214             else:
 215                 # Recursively expand to a flat list; may deserve a C accelerator at
 216                 # some point.
 217                 for element in thing:
 218  ->                 self.grow(element)
(Pdb) str
<type 'str'>
(Pdb) str_class
<type 'unicode'>
(Pdb) type(thing)
<class 'future.types.newstr.newstr'>
(Pdb) isinstance(thing, str)
False
(Pdb) isinstance(thing, str_class)
True

I suspect just changing the type(thing) == str_class lines to isinstance(type, str_class) will fix the issue.

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

No branches or pull requests

1 participant