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

parameterized.expand uses slow inspect.stack #107

Open
gatesn opened this issue Nov 3, 2020 · 1 comment
Open

parameterized.expand uses slow inspect.stack #107

gatesn opened this issue Nov 3, 2020 · 1 comment

Comments

@gatesn
Copy link

gatesn commented Nov 3, 2020

Almost half of my pytest collection time is spent inside parameterized.expand, specifically inside inspect.stack. I tried switching to inspect.currentframe and it's significantly faster as it doesn't have to assemble the entire stack just to look at the outer frame.

i.e.

stack = inspect.stack()
frame = stack[1]
frame_locals = frame[0].f_locals

to

frame_locals = inspect.currentframe().f_back.f_locals
@wolever
Copy link
Owner

wolever commented Jan 4, 2021

Hey! Thanks!

0.7.5 will fix the issue you called out. There's still one more use that may affect things:

    def _terrible_magic_get_defining_classes(self):
        """ Returns the set of parent classes of the class currently being defined.
            Will likely only work if called from the ``parameterized`` decorator.
            This function is entirely @brandon_rhodes's fault, as he suggested
            the implementation: http://stackoverflow.com/a/8793684/71522
            """
        stack = inspect.stack()
        if len(stack) <= 4:
            return []
        frame = stack[4]
        code_context = frame[4] and frame[4][0].strip()
        if not (code_context and code_context.startswith("class ")):
            return []
        _, _, parents = code_context.partition("(")
        parents, _, _ = parents.partition(")")
        return eval("[" + parents + "]", frame[0].f_globals, frame[0].f_locals)

But this will take a bit more work + testing to fix, so I'm going to hold off until I have the time (and, ofc, I'd love a PR)

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

2 participants