This article is mainly to track the execution of a relatively simple Python program, to briefly discuss the introduction of the basic framework and structure of Python implementation. The following describes the specific application of the actual operation solution for Python program execution.
To execute the Python program as follows, the function is very simple: Add 1 to 10 and print it out.
- # test program
- sum = 0
- for i in range(1, 11):
- sumsum = sum + i
- print sum
If you want to debug Python with VS 2005 in Windows, you can set the Startup Project to Python through the following steps: Right-click the Python Project through F5, select Properties. In the dialog box
- Configuration Properties->Debugging
Next, SET Command Arguments to-d test. py. Test. py is the name of the program to be debugged. -D indicates that the debug switch is enabled and additional debugging information is displayed.
Well, after setting, you can directly press F10 to track the execution of the program in a single step.
First, F10, start the program, and you can see that there is nothing in the main function of Python, just a simple call to Py_Main. Py_Main, as its name implies, is naturally the main function, which is divided into several parts:
Analyze command lines and Environment Variables
Call Py_Initialize for initialization
Enter different execution modes based on the command line content.
- if (command) {
- sts = PyRun_SimpleStringFlags(command, &cf) != 0;
- free(command);
- } else if (module) {
- sts = RunModule(module);
- free(module);
- }
- else {
- if (filename == NULL && stdin_is_interactive) {
- RunStartupFile(&cf);
- }
- /* XXX */
- sts = PyRun_AnyFileExFlags(
- fp,
- filename == NULL ? "<stdin>" : filename,
-
- filename != NULL, &cf) != 0;
-
- }
The above is an introduction to the execution process of a simple Python program.