Python2exe source code analysis

Source: Internet
Author: User

Working principle of python2exe

Python2exe uses python modualfinder to call the scripts to run and find all python modules and extension modules required to run it. Python-only modules are compiled into. pycdebug) and. pyooptimized files in the temporary directory. The compiled extension module pyd file can also be found.

Finally, generate a zip-compatible package that contains a dependency directory and your script. This package is provided to a python interpreter that comes with py2exe. Running the generated program will establish an important Association, so that when the script starts running, the necessary modules can be correctly imported from the zip package.

Because the extension modules cannot be included in or imported from the zip package, they are provided as a single read. In the dist \ myscript directory. Similar work: Gordon McMillan's installer is more mature)

Python2exe code analysis

Decoding process

Main and winmain call the start function.
Start:
1) Get the directory name and file name GetModuleFileName)
2) retrieve the data of the execution file MapExistingFile)
3) Get the script information GetScriptInfo)
4) use _ putenv to set the environment variables PYTHONHOME, PYTHONPATH, PYTHONOPTIMIZE, and PYTHONDEBUG)
5) set the scriptinfo information to the python internal variables Py_NoSiteFlag and Py_VerboseFlag)
6) Py_Initialize ()
7) PySys_SetArgv (argc, argv)
8) Call BuildToc to obtain the toc (dict object)
9) Call PyRun_SimpleString to execute "import sys; sys. path = [r '% s']"
10) Call Load_Module to call the imputil module and call PyRun_SimpleString to execute "import imputil"
11) Py_InitModule ("_ main _", methods );
12) Solve Scripts \ support. py and run it.
13) Solve Scripts \\__ main __. py and run it here _ main __. py is the python file to be packaged and renamed as _ main __. py) source \ start. c:
1. MapExistingFile: use the File image method to open the file and read and process the content.
2. GetScriptInfo: extract the script infooptimize, verbose, tag from the data in the central directory record format defined in archive. h to the global variable p_script_info. Detailed description in http://ww.pkware.com/appnote.html
3. buildToc: construct a new dict object, create a loop in the arch file, and retrieve all the names and put them in the dict PyDict_SetItem ), fixpath is called to solve the unified problem of "\" and "/", and dict is returned. Note that the name does not seem to have a suffix)
4. extract_data: Use the zstream structure defined in zlib. h to obtain data of the specified length from the archive structure.
5. GetContentsFromOffset: calculates the location and calls extract to obtain data.
6. GetContents: Use PyDict_GetItemString to obtain the location of the specified file from the toc, and call GetContentsFromOffset to obtain data.
7. Load_Module: Check p_script_info-> optimize to determine whether the file name suffix is ". pyo" or ". pyc". Call GetContents to call the file from the data.
8. get_code: specify the name to get the content of this file: GetContents + PyString_FromStringAndSize
Test
You can use winzip to open the generated exe file, indicating that this exe is actually a zip file.
--
Though hope is frail
It's hard to kill

(

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.