1

I can check AST in python file:

python3 -m ast some_file.py

But, when I compile it with nuitka:

nuitka3 --module some_file.py

I get some_file.so extension module and when I run

python3 -m ast some_file.so

I get error.

So, question my is:

is there abstract syntax tree (AST) in python extension module?

Manualmsdos
  • 1,505
  • 3
  • 11
  • 22
  • 1
    The AST is a product of the *source code*; `nuitka` is producing some *other* product, and not retaining the source. – chepner Nov 22 '22 at 13:34
  • 1
    It might also help to know what makes the AST abstract. After parsing the code, things like comments and arbitrary whitespace is discarded. While you could reconstruct *valid* Python source code from an AST, you can't necessarily reconstruct the original source that was parsed. For that, you would need a *concrete* syntax tree, which isn't something that the standard library provides tools to create or manipulate, but is what things like code formatters and syntax highlighters can use to create alternate forms of the original source code. – chepner Nov 22 '22 at 13:48

1 Answers1

1

A .so is almost certainly a Linux or MacOSX Shared Object (as the tag indicates). It almost certainly does not contain Python byte code, the usual content is raw binary instructions in the format that your CPU understands.

Viewing the symbols in a .so file

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • 1
    Byte code is independent of the AST. (The AST may be used to help *generate* byte code for CPython, but isn't strictly dependent on CPython itself.) – chepner Nov 22 '22 at 13:42
  • 1
    @chepner: true. The contrast here is between "Python byte codes" and "CPU instructions in binary". Once it's clear that the binary numbers in a .so are unrelated to Python, it's also logical why you can't get an Python AST for it. – MSalters Nov 22 '22 at 13:46
  • 2
    You can't necessarily get an AST from byte code, either. (Or at least, it might be a *different* AST than what the original source code would produce.) (Consider `x = 8` vs `x = 3 + 5`. The optimization CPython uses to create identical byte code from each occurs on two distinct ASTs: the AST preserves the `+` operator where the byte code does not.) – chepner Nov 22 '22 at 13:55