6 Initialization of the Python Interpreter
6.1 The Big Picture
In order to use the functions in pyffi you need to start a Python interpreter. The call (initialize) does just that. A standard approach would be to make the call in your "main" module of your program.
That the Python interpreter isn’t available until the main module has been instantiated leads to a few complications. The problem is the interpreter instance is not available when the modules required by "main" is instantiated.
As an example: The module pyffi/numpy contains bindings for NumPy. If the main module looks like this:
#lang racket
(require pyffi pyffi/numpy)
(initialize)
Then pyffi/numpy is instantiated before the interpreter is started. This means pyffi/numpy can’t inspect the Python module numpy to get the function signatures it needs.
To solve this problem pyffi/numpy registers a number of initialization thunks to be run after the interpreter has started. The function post-initialize runs these initialization thunks.
To sum up, the typical main module for a program that uses pyffi starts:
#lang racket
(require pyffi pyffi/numpy)
(initialize)
(post-initialize)
6.2 Reference
procedure
(initialize) → void?
The precise steps taken are:
The locations of libpython and the folder containing the standard library is fetched from the preferences (keys 'pyffi:libdir and 'pyffi:data).
Calls Py_Initialize which starts an Python interpreter. Initializes the table of of loaded modules and creates the modules builtins, __main__ and sys.
Imports __main__, builtins, operator, traceback and inspect.
Creates instances of True, False, and, None.
procedure
(post-initialize) → void?
procedure
(diagnostics) → void?