Questions tagged [python-ast]

The ast module helps Python applications to process trees of the Python abstract syntax grammar.

19 questions
13
votes
2 answers

Generate .pyc from Python AST?

How would I generate a .pyc file from a Python AST such that I could import the file from Python? I've used compile to create a code object, then written the co_code attribute to a file, but when I try to import the file from Python, I get an…
exupero
  • 9,136
  • 8
  • 47
  • 63
2
votes
1 answer

Adding visit function to AST visitor prevents other methods from firing

Using the ast module, I run into a weird issue: Code import ast from _ast import Try code = """ def my_add(a, b): try: return a + b except TypeError: return a + b """ class Visitor(ast.NodeVisitor): def visit_Try(self,…
Dorian Turba
  • 3,260
  • 3
  • 23
  • 67
2
votes
1 answer

ast nodes not preserving some properties (lineno or/and col_offset)

I'm trying to convert every break statement with exec('break') in a code. So far I've got this: import ast source = '''some_list = [2, 3, 4, 5] for i in some_list: if i == 4: p = 0 break exec('d = 9')''' tree =…
musava_ribica
  • 476
  • 1
  • 5
  • 18
2
votes
1 answer

Find end_lineno in ast.ImportFrom

In python 3.8 while doing ast.parse you get a end_lineno variable: import ast code_example = 'from typing import List, Dict' parsed_tree = ast.parse(code_example) for item in parsed_tree.body: print(item.__dict__) Results in: { 'module':…
ArcLight_Slavik
  • 113
  • 1
  • 10
2
votes
1 answer

Replacing Python Call AST node with Try node

The system I work with allows users to specify a Python Boolean expression as a string (in a configuration file). The system takes the string, converts it to a Python AST object and then evaluates the expression based on input data. There are…
KyleKnoepfel
  • 1,426
  • 8
  • 24
2
votes
1 answer

Python: what is the ast.FunctionType in python ast?

I don't pretend to have understood everything in python ast, but FunctionType is something that bothered me. mod = Module(stmt* body, type_ignore *type_ignores) | Interactive(stmt* body) | Expression(expr body) |…
1
vote
1 answer

Print source of lambda without surrounding call

If I define a function def foo(function): import inspect return inspect.getsource(function) and then call it, I get: In [11]: foo(lambda x: x[0] + x[1]*2) Out[11]: 'foo(lambda x: x[0] + x[1]*2)\n' Note how it printed the entire line,…
ignoring_gravity
  • 6,677
  • 4
  • 32
  • 65
1
vote
1 answer

Visiting a assert statement within a functiondef using Python ast

I am currently doing some code manipulation and I am unable to visit an "Assert" statement within a "FunctionDef". How I visit an internal Assert node within a Function node? Here is a sample code I am modifying assert True == True def test_code(): …
1
vote
1 answer

Using Python AST Module, parse callables used as arguments

I'm writing a script that will walk through various Python modules, and hunt for instances of calls to fx.bind(). The first argument to this function is a string representing the hotkey, and the second argument is a function that will be run when…
Corin
  • 27
  • 4
1
vote
2 answers

How do I find the line and column offsets for imported Python modules?

I have a class (based on this answer) that uses ast.NodeVisitor to get a list of modules imported by a Python file. However, I also want to return the line and column offsets for where the module names are located in the file. Code: import…
Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
1
vote
1 answer

Importing AST modification

I'm attempting dynamic rewrite of pywin32 win32com.client module prior to import, the below seems to work - but I'm not happy with the 6 lines of code for importing (after the last comment). Can anyone recommend a more concise way of creating /…
1
vote
1 answer

How does python interpret variable attributes in strings

Suppose I have a string like this "Style.BRIGHT + 'BRIGHT' + '\\n' + Style.DIM + 'DIM' + '\\n' + Style.NORMAL + 'NORMAL'" I try to print it directly from colorama import Style s = "Style.BRIGHT + 'BRIGHT' + '\\n' + Style.DIM + 'DIM' + '\\n' +…
pppig
  • 1,215
  • 1
  • 6
  • 12
1
vote
1 answer

ast - Get the list of only the variable names in an expression

I'm using the Python ast module to obtain a list of variable names in a Python expression. For example, the expression [int(s[i:i + 3], 2) for i in range(0, len(s), 3)] should return a singleton list with the variable name s like [s]. I've tried the…
1
vote
1 answer

Override functions in exec function

I'm working on a mobile app that can execute Python code easily, and unlike other execution apps I am going to have it run with actual Python. I'm using a Flask Webserver and requests to accomplish this. This is my code: @app.route('/exec') def…
UCYT5040
  • 367
  • 3
  • 15
0
votes
1 answer

Trying to run current script again causes stack overflow or parserror?

I'm writing a script that will modify the code in the script and rerun it automatically but whichever way I do it, it always goes into repeated output until the kernel restarts and if I keyboard interrupt sometimes I get: ParserError: Error…
1
2