@@ -40,79 +40,305 @@ import CPython.Internal
40
40
41
41
#include <hscpython-shim.h>
42
42
43
+ -- | Initialize the Python interpreter. In an application embedding Python,
44
+ -- this should be called before using any other Python/C API computations;
45
+ -- with the exception of 'setProgramName', 'initThreads',
46
+ -- 'releaseLock', and 'acquireLock'. This initializes the table
47
+ -- of loaded modules (@sys.modules@), and creates the fundamental modules
48
+ -- @builtins@, @__main__@ and @sys@. It also initializes the module search
49
+ -- path (@sys.path@). It does not set @sys.argv@; use 'setArgv' for that. This
50
+ -- is a no-op when called for a second time (without calling 'finalize'
51
+ -- first). There is no return value; it is a fatal error if the initialization
52
+ -- fails.
53
+ --
43
54
{# fun Py_Initialize as initialize
44
55
{} -> `() ' id # }
45
56
57
+ -- | Return 'True' when the Python interpreter has been initialized, 'False'
58
+ -- if not. After 'finalize' is called, this returns 'False' until
59
+ -- 'initialize' is called again.
60
+ --
46
61
{# fun Py_IsInitialized as isInitialized
47
62
{} -> `Bool' # }
48
63
64
+ -- | Undo all initializations made by 'initialize' and subsequent use of
65
+ -- Python/C API computations, and destroy all sub-interpreters (see
66
+ -- 'newInterpreter' below) that were created and not yet destroyed since the
67
+ -- last call to 'initialize'. Ideally, this frees all memory allocated by the
68
+ -- Python interpreter. This is a no-op when called for a second time (without
69
+ -- calling 'initialize' again first). There is no return value; errors during
70
+ -- finalization are ignored.
71
+
72
+ -- This computation is provided for a number of reasons. An embedding
73
+ -- application might want to restart Python without having to restart the
74
+ -- application itself. An application that has loaded the Python interpreter
75
+ -- from a dynamically loadable library (or DLL) might want to free all memory
76
+ -- allocated by Python before unloading the DLL. During a hunt for memory
77
+ -- leaks in an application a developer might want to free all memory
78
+ -- allocated by Python before exiting from the application.
79
+
80
+ -- /Bugs and caveats/: The destruction of modules and objects in modules is
81
+ -- done in arbitrary order; this may cause destructors (@__del__()@ methods)
82
+ -- to fail when they depend on other objects (even functions) or modules.
83
+ -- Dynamically loaded extension modules loaded by Python are not unloaded.
84
+ -- Small amounts of memory allocated by the Python interpreter may not be
85
+ -- freed (if you find a leak, please report it). Memory tied up in circular
86
+ -- references between objects is not freed. Some memory allocated by extension
87
+ -- modules may not be freed. Some extensions may not work properly if their
88
+ -- initialization routine is called more than once; this can happen if an
89
+ -- application calls 'initialize' and 'finalize' more than once.
90
+ --
49
91
{# fun Py_Finalize as finalize
50
92
{} -> `() ' id # }
51
93
52
94
newtype ThreadState = ThreadState (Ptr ThreadState )
53
95
96
+ -- | Create a new sub-interpreter. This is an (almost) totally separate
97
+ -- environment for the execution of Python code. In particular, the new
98
+ -- interpreter has separate, independent versions of all imported modules,
99
+ -- including the fundamental modules @builtins@, @__main__@ and @sys@. The
100
+ -- table of loaded modules (@sys.modules@) and the module search path
101
+ -- (@sys.path@) are also separate. The new environment has no @sys.argv@
102
+ -- variable. It has new standard I/O stream file objects @sys.stdin@,
103
+ -- @sys.stdout@ and @sys.stderr@ (however these refer to the same underlying
104
+ -- @FILE@ structures in the C library).
105
+ --
106
+ -- The return value points to the first thread state created in the new
107
+ -- sub-interpreter. This thread state is made in the current thread state.
108
+ -- Note that no actual thread is created; see the discussion of thread states
109
+ -- below. If creation of the new interpreter is unsuccessful, 'Nothing' is
110
+ -- returned; no exception is set since the exception state is stored in the
111
+ -- current thread state and there may not be a current thread state. (Like
112
+ -- all other Python/C API computations, the global interpreter lock must be
113
+ -- held before calling this computation and is still held when it returns;
114
+ -- however, unlike most other Python/C API computations, there
115
+ -- needn’t be a current thread state on entry.)
116
+ --
117
+ -- Extension modules are shared between (sub-)interpreters as follows: the
118
+ -- first time a particular extension is imported, it is initialized normally,
119
+ -- and a (shallow) copy of its module’s dictionary is squirreled away.
120
+ -- When the same extension is imported by another (sub-)interpreter, a new
121
+ -- module is initialized and filled with the contents of this copy; the
122
+ -- extension’s @init@ procedure is not called. Note that this is
123
+ -- different from what happens when an extension is imported after the
124
+ -- interpreter has been completely re-initialized by calling 'finalize' and
125
+ -- 'initialize'; in that case, the extension’s @init/module/@
126
+ -- procedure is called again.
127
+ --
128
+ -- /Bugs and caveats/: Because sub-interpreters (and the main interpreter)
129
+ -- are part of the same process, the insulation between them isn’t
130
+ -- perfect — for example, using low-level file operations like
131
+ -- @os.close()@ they can (accidentally or maliciously) affect each
132
+ -- other’s open files. Because of the way extensions are shared
133
+ -- between (sub-)interpreters, some extensions may not work properly; this
134
+ -- is especially likely when the extension makes use of (static) global
135
+ -- variables, or when the extension manipulates its module’s
136
+ -- dictionary after its initialization. It is possible to insert objects
137
+ -- created in one sub-interpreter into a namespace of another
138
+ -- sub-interpreter; this should be done with great care to avoid sharing
139
+ -- user-defined functions, methods, instances or classes between
140
+ -- sub-interpreters, since import operations executed by such objects may
141
+ -- affect the wrong (sub-)interpreter’s dictionary of loaded modules.
142
+ -- (XXX This is a hard-to-fix bug that will be addressed in a future release.)
143
+ --
144
+ -- Also note that the use of this functionality is incompatible with
145
+ -- extension modules such as PyObjC and ctypes that use the @PyGILState_*()@
146
+ -- APIs (and this is inherent in the way the @PyGILState_*()@ procedures
147
+ -- work). Simple things may work, but confusing behavior will always be near.
148
+ --
54
149
newInterpreter :: IO (Maybe ThreadState )
55
150
newInterpreter = do
56
151
ptr <- {# call Py_NewInterpreter as ^ # }
57
152
return $ if ptr == nullPtr
58
153
then Nothing
59
154
else Just $ ThreadState $ castPtr ptr
60
155
156
+ -- | Destroy the (sub-)interpreter represented by the given thread state.
157
+ -- The given thread state must be the current thread state. See the
158
+ -- discussion of thread states below. When the call returns, the current
159
+ -- thread state is @NULL@. All thread states associated with this
160
+ -- interpreter are destroyed. (The global interpreter lock must be held
161
+ -- before calling this computation and is still held when it returns.)
162
+ -- 'finalize' will destroy all sub-interpreters that haven’t been
163
+ -- explicitly destroyed at that point.
164
+ --
61
165
endInterpreter :: ThreadState -> IO ()
62
166
endInterpreter (ThreadState ptr) =
63
167
{# call Py_EndInterpreter as ^ # } $ castPtr ptr
64
168
169
+ -- | Return the program name set with 'setProgramName', or the default.
170
+ --
65
171
getProgramName :: IO Text
66
172
getProgramName = pyGetProgramName >>= peekTextW
67
173
68
174
foreign import ccall safe " hscpython-shim.h Py_GetProgramName"
69
175
pyGetProgramName :: IO CWString
70
176
177
+ -- | This computation should be called before 'initialize' is called for the
178
+ -- first time, if it is called at all. It tells the interpreter the value of
179
+ -- the @argv[0]@ argument to the @main@ procedure of the program. This is
180
+ -- used by 'getPath' and some other computations below to find the Python
181
+ -- run-time libraries relative to the interpreter executable. The default
182
+ -- value is @"python"@. No code in the Python interpreter will change the
183
+ -- program name.
184
+ --
71
185
setProgramName :: Text -> IO ()
72
186
setProgramName name = withTextW name cSetProgramName
73
187
74
188
foreign import ccall safe " hscpython-shim.h hscpython_SetProgramName"
75
189
cSetProgramName :: CWString -> IO ()
76
190
191
+ -- | Return the prefix for installed platform-independent files. This is
192
+ -- derived through a number of complicated rules from the program name set
193
+ -- with 'setProgramName' and some environment variables; for example, if the
194
+ -- program name is @\"\/usr\/local\/bin\/python\"@, the prefix is
195
+ -- @\"\/usr\/local\"@. This corresponds to the @prefix@ variable in the
196
+ -- top-level Makefile and the /--prefix/ argument to the @configure@ script
197
+ -- at build time. The value is available to Python code as @sys.prefix@. It
198
+ -- is only useful on UNIX. See also 'getExecPrefix'.
199
+ --
77
200
getPrefix :: IO Text
78
201
getPrefix = pyGetPrefix >>= peekTextW
79
202
80
203
foreign import ccall safe " hscpython-shim.h Py_GetPrefix"
81
204
pyGetPrefix :: IO CWString
82
205
206
+ -- | Return the /exec-prefix/ for installed platform-/dependent/ files. This
207
+ -- is derived through a number of complicated rules from the program name
208
+ -- set with setProgramName' and some environment variables; for example, if
209
+ -- the program name is @\"\/usr\/local\/bin\/python\"@, the exec-prefix is
210
+ -- @\"\/usr\/local\"@. This corresponds to the @exec_prefix@ variable in the
211
+ -- top-level Makefile and the /--exec-prefix/ argument to the @configure@
212
+ -- script at build time. The value is available to Python code as
213
+ -- @sys.exec_prefix@. It is only useful on UNIX.
214
+ --
215
+ -- Background: The exec-prefix differs from the prefix when platform
216
+ -- dependent files (such as executables and shared libraries) are installed
217
+ -- in a different directory tree. In a typical installation, platform
218
+ -- dependent files may be installed in the @\/usr\/local\/plat@ subtree while
219
+ -- platform independent may be installed in @\/usr\/local@.
220
+ --
221
+ -- Generally speaking, a platform is a combination of hardware and software
222
+ -- families, e.g. Sparc machines running the Solaris 2.x operating system
223
+ -- are considered the same platform, but Intel machines running Solaris
224
+ -- 2.x are another platform, and Intel machines running Linux are yet
225
+ -- another platform. Different major revisions of the same operating system
226
+ -- generally also form different platforms. Non-UNIX operating systems are a
227
+ -- different story; the installation strategies on those systems are so
228
+ -- different that the prefix and exec-prefix are meaningless, and set to the
229
+ -- empty string. Note that compiled Python bytecode files are platform
230
+ -- independent (but not independent from the Python version by which they
231
+ -- were compiled!).
232
+ --
233
+ -- System administrators will know how to configure the @mount@ or @automount@
234
+ -- programs to share @\/usr\/local@ between platforms while having
235
+ -- @\/usr\/local\/plat@ be a different filesystem for each platform.
236
+ --
83
237
getExecPrefix :: IO Text
84
238
getExecPrefix = pyGetExecPrefix >>= peekTextW
85
239
86
240
foreign import ccall safe " hscpython-shim.h Py_GetExecPrefix"
87
241
pyGetExecPrefix :: IO CWString
88
242
243
+ -- | Return the full program name of the Python executable; this is computed
244
+ -- as a side-effect of deriving the default module search path from the
245
+ -- program name (set by 'setProgramName' above). The value is available to
246
+ -- Python code as @sys.executable@.
247
+ --
89
248
getProgramFullPath :: IO Text
90
249
getProgramFullPath = pyGetProgramFullPath >>= peekTextW
91
250
92
251
foreign import ccall safe " hscpython-shim.h Py_GetProgramFullPath"
93
252
pyGetProgramFullPath :: IO CWString
94
253
254
+ -- | Return the default module search path; this is computed from the
255
+ -- program name (set by 'setProgramName' above) and some environment
256
+ -- variables. The returned string consists of a series of directory names
257
+ -- separated by a platform dependent delimiter character. The delimiter
258
+ -- character is @\':\'@ on Unix and Mac OS X, @\';\'@ on Windows. The value
259
+ -- is available to Python code as the list @sys.path@, which may be modified
260
+ -- to change the future search path for loaded modules.
261
+ --
95
262
getPath :: IO Text
96
263
getPath = pyGetPath >>= peekTextW
97
264
98
265
foreign import ccall safe " hscpython-shim.h Py_GetPath"
99
266
pyGetPath :: IO CWString
100
267
268
+ -- | Return the version of this Python interpreter. This is a string that
269
+ -- looks something like
270
+ --
271
+ -- @
272
+ -- \"3.0a5+ (py3k:63103M, May 12 2008, 00:53:55) \\n[GCC 4.2.3]\"
273
+ -- @
274
+ --
275
+ -- The first word (up to the first space character) is the current Python
276
+ -- version; the first three characters are the major and minor version
277
+ -- separated by a period. The value is available to Python code as
278
+ -- @sys.version@.
279
+ --
101
280
{# fun Py_GetVersion as getVersion
102
281
{} -> `Text' peekText* # }
103
282
283
+ -- | Return the platform identifier for the current platform. On Unix, this
284
+ -- is formed from the “official” name of the operating system,
285
+ -- converted to lower case, followed by the major revision number; e.g., for
286
+ -- Solaris 2.x, which is also known as SunOS 5.x, the value is @\"sunos5\"@.
287
+ -- On Mac OS X, it is @\"darwin\"@. On Windows, it is @\"win\"@. The value
288
+ -- is available to Python code as @sys.platform@.
289
+ --
104
290
{# fun Py_GetPlatform as getPlatform
105
291
{} -> `Text' peekText* # }
106
292
293
+ -- | Return the official copyright string for the current Python version,
294
+ -- for example
295
+ --
296
+ -- @
297
+ -- \"Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam\"
298
+ -- @
299
+ --
300
+ -- The value is available to Python code as @sys.copyright@.
301
+ --
107
302
{# fun Py_GetCopyright as getCopyright
108
303
{} -> `Text' peekText* # }
109
304
305
+ -- | Return an indication of the compiler used to build the current Python
306
+ -- version, in square brackets, for example:
307
+ --
308
+ -- @
309
+ -- \"[GCC 2.7.2.2]\"
310
+ -- @
311
+ --
312
+ -- The value is available to Python code as part of the variable
313
+ -- @sys.version@.
314
+ --
110
315
{# fun Py_GetCompiler as getCompiler
111
316
{} -> `Text' peekText* # }
112
317
318
+ -- | Return information about the sequence number and build date and time of
319
+ -- the current Python interpreter instance, for example
320
+ --
321
+ -- @
322
+ -- \"#67, Aug 1 1997, 22:34:28\"
323
+ -- @
324
+ --
325
+ -- The value is available to Python code as part of the variable
326
+ -- @sys.version@.
327
+ --
113
328
{# fun Py_GetBuildInfo as getBuildInfo
114
329
{} -> `Text' peekText* # }
115
330
331
+ -- | Set @sys.argv@. The first parameter is similar to the result of
332
+ -- 'getProgName', with the difference that it should refer to the script
333
+ -- file to be executed rather than the executable hosting the Python
334
+ -- interpreter. If there isn’t a script that will be run, the first
335
+ -- parameter can be an empty string. If this function fails to initialize
336
+ -- @sys.argv@, a fatal condition is signalled using @Py_FatalError()@.
337
+ --
338
+ -- This function also prepends the executed script’s path to
339
+ -- @sys.path@. If no script is executed (in the case of calling @python -c@
340
+ -- or just the interactive interpreter), the empty string is used instead.
341
+ --
116
342
setArgv :: Text -> [Text ] -> IO ()
117
343
setArgv argv0 argv =
118
344
mapWith withTextW (argv0 : argv) $ \ textPtrs ->
@@ -122,12 +348,21 @@ setArgv argv0 argv =
122
348
foreign import ccall safe " hscpython-shim.h PySys_SetArgv"
123
349
pySetArgv :: CInt -> Ptr CWString -> IO ()
124
350
351
+ -- | Return the default “home”, that is, the value set by a
352
+ -- previous call to 'setPythonHome', or the value of the @PYTHONHOME@
353
+ -- environment variable if it is set.
354
+ --
125
355
getPythonHome :: IO Text
126
356
getPythonHome = pyGetPythonHome >>= peekTextW
127
357
128
358
foreign import ccall safe " hscpython-shim.h Py_GetPythonHome"
129
359
pyGetPythonHome :: IO CWString
130
360
361
+ -- | Set the default “home” directory, that is, the location
362
+ -- of the standard Python libraries. The libraries are searched in
363
+ -- @/home/\/lib\//python version/@ and @/home/\/lib\//python version/@. No
364
+ -- code in the Python interpreter will change the Python home.
365
+ --
131
366
setPythonHome :: Text -> IO ()
132
367
setPythonHome name = withTextW name cSetPythonHome
133
368
0 commit comments