I am trying to modify the source code for the eval() function, in my own file, so I need the source code.
I have tried writing eval with an error, but that just says error in . I tried using the inspect function, but that said "TypeError: is a built-in class
".
This is what I did for inspect:
import inspect
print(inspect.getsource(eval))
Can you tell me how to find it, or even just give me a link to github file?
Even though some python functions are written in python, most core functions are actually implemented in C.
Here is the source code for the eval() function.
Related
I'm trying to extract all the "runnable" code given a function in a module. Right now I'm only able to extract the functions in any imported module that are called inside the starting function. However, some modules have "outside" expressions (i.e. some variables defined globally in the module, or functions called in the same level).
With inspect and dis I did the work to extract the functions, but, is there any way of extracting the "non-function" of a module?
If anybody wonders what am I doing, is a packer for python. What I want to achieve is that this tools only packs the required code given a starting function.
Also, if is there something already out there that does what I'm trying to achieve, I'd like to know.
importing a module as a code object
You can use importlib's get_code() method which returns the code object of the module and then you can modify or create a new code object extracting the required parts. exec method can be used for executing code object.
Alternatively, using the built-in compile function, you can directly compile source code into byte code then follow the same procedure as mentioned above.
References:
Modifying python bytecode
assembling-python-module-on-fly-dynamic-import
I am looking for a Neovim plugin that would be able to generate the initial template of docstring for a function based on the defined arguments and returns of this function. An example of what I want is illustrated below:
So, I wonder whether such a plugin exists. Or if it does not exist, how can I do this in Neovim for Python?
I'm currently giving https://github.com/heavenshell/vim-pydocstring a try. It doesn't seem to do much for classes, but it works fine for functions/methods!
I need to call a function written in VB.net as part of my COM automation program.
The VB program requires a byref parameter, defined as
Function GetBeamWidth(ByRef pfBeamWidth As Single) As Integer
I tested the function in VB codes shown below, no problem at all,
Dim y As Single
myObject.GetBeamWidth(x)
'myOject is an instance of the ActiveX server object, which implements function GetBeamWidth
however, I don't have a clue on how to call this function in python. Everything I tried gave me "type not match" error. I am sure that I loaded the ActiveX server, tried early and late binding, tried passing x with the definition of x = [0].
I also tried ctype.byref(x), not working. The example I found online is not that easy to understand. If ctype.byreg is the cure, can someone here paste an easy-to-follow example?
I guess this must be a common problem for yo all experienced programmer out there. I know python doesn't support pointer, but sadly in real life we have to interface with functions written by others/other languages which need passing function parameters by reference.
Thanks in advance!
ByRef is passing by pointer, you can look at ctypes. It also exports byref.
Finally, it turns out to be pretty much impossible to accomplish what I asked for. Alternatively, I wrapped my core functions in Python class and register it as COM server.
Then, I just built the most outside shell in VB.net, which loads all COM servers, provides UI, and simply calls COM functions to do the work.
I guess that is one of reasons for why we have programming platforms other than Python still alive.
Is it ok to use the ast module to parse and modify untrusted external Python code programatically?
I will just parse the source code, get some info from the source code (docstrings, function definitions, maybe, I don't know) and leave it there, not compile it or run it.
If you're using the ast.parse function then it should be safe. As the documentation says, this function will
Parse the source into an AST node. Equivalent to compile(source, filename, mode, ast.PyCF_ONLY_AST)
which simply parses the file even if it contains invalid Python code. It doesn't do any sort of evaluation.
If your aim is to evaluate expressions, then you can use ast.literal_eval, which is safer than the built-in eval statement
"Unsafe" implies something bad could happen controlled by the artifact you are engaging. Since parsing only builds ASTs, and (assuming there isn't something malicious in the parsing and AST building code), then parsing an arbitrary bit of text can't hurt you.
Typically to get malicious behaviour from the outside, something (controlled by you) must essentially execute some supplied code. Clearly building a parse tree doesn't execute the outside program. However, if you built an interpreter that interpreted the parse tree and ran it, you might have a problem.
I believe so. No code is executed. In fact, parsing the ast is exactly what ast.literal_eval does, and that's deemed safe.
Is there is a tool similar to dir() for modules that will tell me what parameters a given function takes? For instance, I would like to do something like dir(os.rename) and have it tell me what parameters are documented so that I can avoid checking the documentation online, and instead use only the Python scripting interface to do this.
I realize that you're more interested in help(thing) or thing.__doc__, but if you're trying to do programmatic introspection (instead of human-readable documentation) to find out about calling a function, then you can use the inspect module, as discussed in this question.
help(thing) pretty prints all the docstrings that are in the module, method, whatever ...