Where is the entry-point of the C code generating by pypy - python

I'm using PyPy to translate some python code to C code. I wrote a very simple script as below:
def main():
print "hello world!"
def entry_point(argv):
main()
return 0
def target(*args):
return entry_point, None
Then I used translate.py --source test.py. It did generate C code successful. When I make those code, it generated a executable file test-c. However I cannot find the main function in those code using grep, so I'm wondering where is the entry-point of the code generating by pypy.
Thank you for your reading.

that's incorrect. Grep for pypy_g_entry_point. main() function is likely to be inlined in this example, so you won't get it. If you want it to be rendered use --inline-threshold=0 as a translation parameter.

PyPy is probably not giving you a "main" function because you actually don't have an entry point in your Python code. You should probably just add
main()
at the end of the file.

Related

Why main() function is called at the end of the program in python?

Lets consider the following piece of code in Python:
def main():
abc()
def abc():
.
.
.
<statements....>
.
.
.
main()
#Why the above line of code is used in python???
Lets consider other programming languages like C, C++ or Java or may be an interpreted language like BASIC. In BASIC, the code goes like this:
if the name of the module is module1 then:
Module Module1
Sub Main()
Console.WriteLine("Hello World")
Console.ReadKey()
End Sub
End Module
This is in vbnet.
No Main() is called.
The above code is just for illustration.
So why is main() called at the end of the program in Python?
TL;DR
Because the creator of Python decided that was the way to use the language.
Details
Very simply put, it's a similar convention in those compiled languages you mentioned, like Visual Basic, C or C++ (or also Java, C#, and many other), that a program is started by launching the Main() function (with some variation, like in a module that is defined as the startup when you compile it).
So, basically, when you compile, in the binary .exe. the compiler adds a call to this Main() function, even if you haven't written it in your code.
Whereas in a Python program, it's simply the file itself that is run, line by line. There is no convention that any function would be called. Python is not a compiled program, not much happens "behind the scenes" (actually, not entirely true, some global variables are set by the interpreter Python.exe).
So, in your Python program, if there wasn't a line main() at the end, you would just have defined 2 functions abc and main, but nothing would be actually called and your program would stop after the definition, nothing inside the two functions would be executed.
In the end, it's just that Python language has a different rule than those languages you mentioned. It's by design of the language.
Of course, it's not "rnadom", this design is more "natural" depending on the type - compiled or interpreted - of the language. For instance, you have a similar behavior in JavaScript, there is no "main()" function either, a JavaScript code is also executed starting from the top, line by line, with potential functions defined in the main code.
(Simplified explanation, some special case may apply).
Its because you have to define the function before calling it in Python. If you call it before the definition, it may give you an error. A VB program launches the Main() function automatically inside the startup module.
The general rule in Python is that a function should be defined before its usage, which does not necessarily mean it needs to be higher in the code.
lol()
def lol():
print("Hello")
Should not work, but if you try
def test():
lol()
def lol():
print("Hello")
test()
It works.

Importing code from a file as a function in python

Essentially I have a script in one file which I would like to import and run as a function in another file. Here is the catch, the contents of the first file CANNOT be written as function definition it just needs to be a plain old script (I'm writing a simulator for my robotics kit so user experience is important). I have no idea how to go about this.
Adam
Anything can be written as a function.
If you additionally need the ability to call your script directly, you just use the __name__ == '__main__' trick:
def my_function():
... code goes here ...
if __name__ == '__main__':
my_function()
Now you can import my_function from the rest of your code, but still execute the file directly since the block at the end will call the function.
Assuming that the code in the file you need to import is a well bounded script - then you can read in as a text variable and use the "execfile" function to create a function from that script.
By well bounded I mean that you understand all the data it needs and you are able to provide all of it from your program.
An alternative would be to use the "system" call, or the subprocess module to call the script as if it was an external program (depending if you need the script output).
A final approach will be to use exec to create a function - see approach 3.
The approach you use determines what you need your other script to do ..
examples :
hello.py (your file you want to run, but can't change):
# Silly example to illustrate a script which does something.
fp = open("hello.txt", "a")
fp.write("Hello World !!!\n")
fp.close()
Three approaches to use hello.py without importing hello.py
import os
print "approach 1 - using system"
os.system("python hello.py")
print "approach 2 - using execfile"
execfile("hello.py", globals(), locals())
print "approach 3 - exec to create a function"
# read script into string and indent
with open("hello.py","r") as hfp:
hsrc = [" " + line for line in hfp]
# insert def line
hsrc.insert(0, "def func_hello():")
# execute our function definition
exec "\n".join( hsrc) in globals(), locals()
# you now have a function called func_hello, which you can call just like a normal function
func_hello()
func_hello()
print "My original script is still running"

Sublime tries to compile Python-script as a Haskell-script [duplicate]

This question already has answers here:
How do I run Python code from Sublime Text 2?
(16 answers)
Closed 6 years ago.
I am newbie, pls don't be to harsh with me.
I am trying to setup Sublime Text with Python (for next Semester). Before that I used Haskell in SublimeText, where I could run my skript with "ctrl+b" in Sublime.
When I try doing the same with a file named "test.py".
def add(a,b):
return a+b
main = print(add(2,3))
I get the error-message:
/home/nayooti/Desktop/test.py:1:1:
**Parse error: naked expression at top level**
[Finished in 0.2s with exit code 1]
[shell_cmd: runhaskell "/home/nayooti/Desktop/test.py"]
[dir: /home/nayooti/Desktop]
[path: /usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games]
The "naked expression at top level"-part looks familiar, since it is very Haskell specific. Indeed, when I search the Web for exactly this message, it brings me to Haskell related stuff only. So apparently Sublime or the compiler thinks, I am trying to run a Haskell-script, even though I named the file ~.py .
For these, who are not familiar with Haskell and Python:
you can usually run a script by:
Python: main = print(method(x,y))
Haskell: main = print(function x y)
I am using Ubuntu 12.04. What am I doing wrong here? Help is very appreciated. Tx.
Go to Tools -> Build System and make sure that Python is selected. Also, that is not how you write main in Python, it should be more like
def add(a, b):
return a + b
def main():
print(add(2, 3))
if __name__ == '__main__':
main()
When you write
main = print(add(2, 3))
And load your script, that line gets executed since it is an assignment, much the same if I did
a = 1
Or
a = print(add(2, 3))
There is nothing special about the name main in Python. You still see your output because print has side effects, but rather than defining main you are just executing a script. The difference comes in if you were to try
add.py:
def add(a, b):
return a + b
main = print(add(2, 3))
subtract.py:
from add import add
def subtract(a, b):
return add(a, -b)
If you ran python subtract.py, you would still see 5 printed to the screen, even though there is no main defined in subtract.py. Obviously, this is not desired behavior, your main function in add.py should not be executing if you did not run it as the main script. When you run a Python script directly as python scriptname.py, there is a variable set global to that file called __name__ that gets assigned the string "__main__". If you import it, then __name__ is set to its qualified module name, something like "scriptname". This gives you the ability to define different behavior if the file is executed compared to if the file is imported by another script.

How to make an executable to use in a shell - Python

I have a Python script and I was wondering how I can make it executable; in other words how can I run it by using a shell like bash.
I know the first thing is to stick on the first line #! /usr/bin/env python but then do I need for example the functions to be in a specific order (i.e., the main one at the top or the bottom). What's more do I need to keep the extension .py for my python file (can I just call the function Dosomething?).
To be short, could you provide a simple guide, the important points someone has to take into account to make a Python file executable?
This is how I make an executable script. It doesn't take eggs or anything like that into account. It's just a simple script that I want to be able to execute. I'm assuming you are using linux.
#! /usr/bin/env python
import sys
def main():
#
# Do something ... Whatever processing you need to do, make it happen here.
# Don't shove everything into main, break it up into testable functions!
#
# Whatever this function returns, is what the exit code of the interpreter,
# i.e. your script, will be. Because main is called by sys.exit(), it will
# behave differently depending on what you return.
#
# So, if you return None, 0 is returned. If you return integer, that
# return code is used. Anything else is printed to the console and 1 (error)
# is returned.
#
if an_error_occurred:
return 'I\'m returning a string, it will be printed and 1 returned'
# Otherwise 0, success is returned.
return 0
# This is true if the script is run by the interpreter, not imported by another
# module.
if __name__ == '__main__':
# main should return 0 for success, something else (usually 1) for error.
sys.exit(main())
Now, if you're permissions are set correctly, you can execute this script.
One thing to realize is as your script is processed each line is executed in the interpreter. This is true, regardless of how the processor "gets it". That is importing a script as a module and executing it as a script essentially both work the same, in that they both execute each line of the module.
Once you realize your script is simply executing as it runs, you realize that the order of functions don't matter. A function declaration is a function declaration. It's when you call the function that matters.
So, in general, the layout of your script looks like this
def func1():
pass
def func2():
pass
def main():
return 0
if __name__ == '__main__':
sys.exit(main())
You create the functions you want to use first, then you use them. Hope it helps.
Delete the first space. That is,
#!/usr/bin/env python
Should be the very first line of your file. Then, make sure you make set the permisions for the the file to executable with:
chmod u+x your_script.py
Python scripts execute in sequential order. If you have a file filled with functions, common practice is to have something that looks like this at the very end of your file:
if __name__ == '__main__':
main()
Where main() starts execution of your script. The reason we do it this way, instead of a bare call to main() is that this allows you to make your script act like a module without any modification; if you just had a single line that called main(), your module would would execute the script's main. The if statement just checks if your script is running in the __main__ namespace, i.e., it is running as a script.
The only thing (like you said it) is to include:
#! /bin/env python
on the first line. And is not even mandatory, but recommended.
After that, you can just call it writing:
python [filename].py
in a terminal or in a bash file.
You'll also have to give it execution rights:
chmod u+x yourfile.py
Your code should follow the template
# any functions I want to define, and will be accessible when imported as module
# or run from command line
...
if __name__ == '__main__':
# things I want to do only when I run it from the command line
...
If you want to be able to run it without having to use python fileName.py but rather just ./fileName.py then you will want to make the first line of your file
#!/usr/bin/env python
And make the file executable by the user at least
chmod u+x fileName.py
If you do not add a .py extension to your file then it will still be runnable from the command line ... but not importable by other modules.
Place #!/usr/bin/python in the first line
You can name your python script anything, like: myPythonScript (No, you do not need to keep .py extension)
chmod +x myPythonScript
Run it: ./myPythonScript
Example: myPythonScript
#!/usr/bin/python
print "hello, world!"
You need to add sha bang as you described, e.g.
#!/usr/bin/python
or
#!/usr/bin/env python
as the first line in your file and you need to make it executable by running
chmod +x Dosomething
You do not need to do anything else, in particular file name may be anything including Dosomething. Your PATH probably doesn't include the directory where the file resides, so you should run it like this (assuming your current working directory is where the file is):
./Dosomething

What will happen if I modify a Python script while it's running?

Imagine a python script that will take a long time to run, what will happen if I modify it while it's running? Will the result be different?
Nothing, because Python precompiles your script into a PYC file and launches that.
However, if some kind of exception occurs, you may get a slightly misleading explanation, because line X may have different code than before you started the script.
When you run a python program and the interpreter is started up, the first thing that happens is the following:
the module sys and builtins is initialized
the __main__ module is initialized, which is the file you gave as an argument to the interpreter; this causes your code to execute
When a module is initialized, it's code is run, defining classes, variables, and functions in the process. The first step of your module (i.e. main file) will probably be to import other modules, which will again be initialized in just the same way; their resulting namespaces are then made available for your module to use. The result of an importing process is in part a module (python-) object in memory. This object does have fields that point to the .py and .pyc content, but these are not evaluated anymore: module objects are cached and their source never run twice. Hence, modifying the module afterwards on disk has no effect on the execution. It can have an effect when the source is read for introspective purposes, such as when exceptions are thrown, or via the module inspect.
This is why the check if __name__ == "__main__" is necessary when adding code that is not intended to run when the module is imported. Running the file as main is equivalent to that file being imported, with the exception of __name__ having a different value.
Sources:
What happens when a module is imported: The import system
What happens when the interpreter starts: Top Level Components
What's the __main__ module: __main__- Top-level code environment
This is a fun question. The answer is that "it depends".
Consider the following code:
"Example script showing bad things you can do with python."
import os
print('this is a good script')
with open(__file__, 'w') as fdesc:
fdesc.write('print("this is a bad script")')
import bad
Try saving the above as "/tmp/bad.py" then do "cd /tmp" and finally "python3 bad.py" and see what happens.
On my ubuntu 20 system I see the output:
this is a good script
this is a bad script
So again, the answer to your question is "it depends". If you don't do anything funky then the script is in memory and you are fine. But python is a pretty dynamic language so there are a variety of ways to modify your "script" and have it affect the output.
If you aren't trying to do anything funky, then probably one of the things to watch out for are imports inside functions.
Below is another example which illustrates the idea (save as "/tmp/modify.py" and do "cd /tmp" and then "python3 modify.py" to run). The fiddle function defined below simulates you modifying the script while it is running (if desired, you could remove the fiddle function, put in a time.sleep(300) at the second to last line, and modify the file yourself).
The point is that since the show function is doing an import inside the function instead of at the top of the module, the import won't happen until the function is called. If you have modified the script before you call show, then your modified version of the script will be used.
If you are seeing surprising or unexpected behavior from modifying a running script, I would suggest looking for import statements inside functions. There are sometimes good reasons to do that sort of thing so you will see it in people's code as well as some libraries from time to time.
Below is the demonstration of how an import inside a function can cause strange effects. You can try this as is vs commenting out the call to the fiddle function to see the effect of modifying a script while it is running.
"Example showing import in a function"
import time
def yell(msg):
"Yell a msg"
return f'#{msg}#'
def show(msg):
"Print a message nicely"
import modify
print(modify.yell(msg))
def fiddle():
orig = open(__file__).read()
with open(__file__, 'w') as fdesc:
modified = orig.replace('{' + 'msg' + '}', '{msg.upper()}')
fdesc.write(modified)
fiddle()
show('What do you think?')
No, the result will not reflect the changes once saved. The result will not change when running regular python files. You will have to save your changes and re-run your program.
If you run the following script:
from time import sleep
print("Printing hello world in: ")
for i in range(10, 0, -1):
print(f"{i}...")
sleep(1)
print("Hello World!")
Then change "Hello World!" to "Hello StackOverflow!" while it's counting down, it will still output "Hello World".
Nothing, as this answer. Besides, I did experiment when multiprocessing is involved. Save the script below as x.py:
import multiprocessing
import time
def f(x):
print(x)
time.sleep(10)
if __name__ == '__main__':
with multiprocessing.Pool(2) as pool:
for _ in pool.imap(f, ['hello'] * 5):
pass
After python3 x.py and after the first two 'hello' being printed out, I modified ['hello'] to ['world'] and observed what happend. Nothing interesting happened. The result was still:
hello
hello
hello
hello
hello
It happens nothing. Once the script is loaded in memory and running it will keep like this.
An "auto-reloading" feature can be implemented anyway in your code, like Flask and other frameworks does.
This is slightly different from what you describe in your question, but it works:
my_string = "Hello World!"
line = input(">>> ")
exec(line)
print(my_string)
Test run:
>>> print("Hey")
Hey
Hello World!
>>> my_string = "Goodbye, World"
Goodbye, World
See, you can change the behavior of your "loaded" code dynamically.
depending. if a python script links to other modified file, then will load newer version ofcourse. but if source doesnt point to any other file it'll just run all script from cache as long as its run. changes will be visible next time...
and if about auto-applying changes when they're made - yes, #pcbacterio was correct. its possible to do thar but script which does it just remembers last action/thing what was doing and checks when the file is modified to rerun it (so its almost invisible)
=]

Categories