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

Can I get a call stack for a variable? #1854

Closed
richtier opened this issue Apr 29, 2022 · 3 comments
Closed

Can I get a call stack for a variable? #1854

richtier opened this issue Apr 29, 2022 · 3 comments

Comments

@richtier
Copy link

richtier commented Apr 29, 2022

Hello :) Very cool tool.

I would like to determine all the places a variable is passed to, practically I mean:

def a(x):
    return x.format(bar=1')

def b(y):
    value = 'hello {bar}'
    return a(value)

How to see all the places __main__.b.value was used? Making the list manually I see it's used on line 5, 6, and 2:

value = 'hello {bar}'  # stored
a(value)  #  accessed
x.format(bar=1')  # passed into function a and then accessed

I have tried get_refernces but it does not follow the variable to a, so I think I'm misunderstanding what that function does:

In [2]: script = jedi.Script("""
   ...: def a(x):
   ...:     return x.format(bar=1')
   ...: 
   ...: def b(y):
   ...:     value = 'hello {bar}'
   ...:     return a(value)
   ...: """)

In [3]: script.get_references(6, 4)
Out[3]: 
[<Name full_name='__main__.b.value', description="value = 'hello {bar}'">,
 <Name full_name='__main__.b.value', description='value'>]

Is what I am asking possible?

@davidhalter
Copy link
Owner

get_references calculates the places where a name is used.

I feel like it's a bit weird to follow the params in different functions. What's are your goals?

@richtier
Copy link
Author

richtier commented May 1, 2022

Thanks for the explanation :)

My goal is to get a list of all the methods that were called on that particular string.

That's would be easy to do if the string is only used in the function scope it was defined in: that can be done via simple ast library using a visitor.

My problem is I'm having trouble finding a static analysis tool that statically "follows" a variable around the codebase as it's passed as arguments into other functions or returned by a function.

@davidhalter
Copy link
Owner

davidhalter commented May 1, 2022

This will not be supported in Jedi, for simple performance reasons. It's just extremely slow with the current design.

Most static analysis tools choose not to do this, because it gets endlessly complicated and you cannot guarantee anyway that you caught everything. Jedi tries its best with get_references on methods as well, but I'm pretty sure it has its limits as well. You could try calling get_references on all the methods you want to check.

#1059 might eventually help.

Closing, because there's nothing to do here. Feel free to ask further questions, though.

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