Is possibile to get the AST from a python code object? - python

I know that's possible from a snippet of code in python to get its AST or its code object via compile().
I was wondering if it is possible to do the opposite: I have the code object(extracted by a .pyc file) and I was looking for the its AST.

No, unfortunately it is not possible to do by conventional methods unless the file that the code object created is still available in the path. There are tools like uncompyle6, which might help though.

Related

Not able to understand the implementation of the os module in cpython

So I wanted to check out some implementations of standard libraries. I started with the os library with the code being here on github.
I took one method for example os.listdir() and I have absolutely no idea how it is implemented even after looking at the code ( pardon this noob ). I have following questions:
os.__all__ do not list this method but I think it is definitely a method as print(type(os.listdir)) listed <class 'builtin_function_or_method'> and I searched on google to find all the builtin functions which I found on this doc page and this is not one of them.
There is not such exclusive function named listdir defined in the module. In the code, from my limited understanding, the function is taken from globals() and put into a support_fd set. How this method is being called I do not understand.
I think the main problem I have is how that module is designed and I was not able to find any resources online to explain in simpler terms hence I am asking here for pointers.
EDIT: For those who are asking, I tried the following code in onlinegdb
import os
if "listdir" in os.__all__:
print("Yes")
print(os.listdir())
The result is only main.py, it should also print Yes, maybe the platform onlinegdb is the problem but it clearly shows the output of listdir as main.py.
After having discussion in the comments I see now that this is more of a online python version problem and not an issue with python or the module itself.

inspect python object source code without comments

A library I'd like to use seems to get confused by triple quote comments when introspecting python code. It appears that Python's inspect doesn't give access to de-commented code though?
This code in Hyperas breaks and looks un-pythonic but I can't figure out how it could be done better. Is AST the way forward?

How to find out application is using object reference in cache?

I find it hard to explain my problem as the problem is not within the code itself. If you need more input to understand the following better, please let me know.
I'm using ArcGIS to call a bunch of python scripts, one after another. The scripts use the same class by importing it, or inherit from it. To initialize the class I often use the same name. I have no idea how the communication between ArcGIS and python is implemented. But I noticed that my scripts are not always behaving like they should. I suppose that the reference to the object still exists (even though the script the object was created with has finished) when I call the second script.
First of all: How can I make sure my suspicion is true?
Secondly, if this is the case: is it a good idea to destroy all references to all objects using del or __del__? What is the best way to do this?
Trace tracing your code and walking through it with a debugger? Quickest way to tell if it's accessing correct code. Python Debugger
If you read the documentation, what you want to do is use a break point to make sure code reaches that point in code.
This is the syntax with the debugger.
b(reak) [[filename:]lineno | function[, condition]]

PyDoc on a shared object

Small simple question, but I cannot find the answer!
Is it possible to use PyDoc on a shared object used as a module in python ? Let say that I declare all the help in that *.so and I'm able to use them with help(module or class name). But I'd like to use PyDoc to create an HTML from these. So this way, I won't have to write the doc two times!
Let me know if it's possible, or if there's a small hack :)
Also, in the C files, I'm able to put the description of each function! But when I type help(my_class) I don't have a description of the class and then the list of function. What am I doing wrong ?
So, what I want is when I do this in the C files:
return PyObject_NEW(my_class, &my_class_type);
Later, in Python, after I loaded the module, I can do:
help(my_class)
and see the help, like it would do for help(int)!!
Thanks!
So, I partially solved my problem.
I still can't use pydoc on an *.so file. But now, I can add modules, which is nice. Everything is available here: http://docs.python.org/extending/newtypes.html
But I'm still looking for the pydoc solution!

Find statements in a python file that have a chance to get executed

I am working on a opensource project where i implementing searching of "print" statements
and such other statements that are unnecessary in a live production enviroment and can
create a error.
But i dont want to trouble the user if there is a print statement commmented out or "print"
word in a docstring or a comment. So i am trying to find out those portion of the python
script that have a chance to get executed. How can i do that ?
Use ast module and NodeVisitor to analyze statically program's code. This way you will have no problems with docstrings or comments.
For Python 2.6 or later you could probably use the ast module. Read in the code (as a string, use ast.parse() to create an abstract syntax tree of that code, and then walk over the code looking for the ast.Print objects and then translate those back into filename, line number tuples.
A similar thread is here:
Check Python code for certain statements
You can get some more ideas.

Categories