Skip to content
This repository was archived by the owner on Feb 17, 2019. It is now read-only.

Commit c5a3492

Browse files
committed
Copied documentation blurbs from the main Python docs into Haddock-style comments.
1 parent 0f7d3df commit c5a3492

23 files changed

+904
-50
lines changed

CPython.chs

+235
Original file line numberDiff line numberDiff line change
@@ -40,79 +40,305 @@ import CPython.Internal
4040

4141
#include <hscpython-shim.h>
4242

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+
--
4354
{# fun Py_Initialize as initialize
4455
{} -> `()' id #}
4556

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+
--
4661
{# fun Py_IsInitialized as isInitialized
4762
{} -> `Bool' #}
4863

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+
--
4991
{# fun Py_Finalize as finalize
5092
{} -> `()' id #}
5193

5294
newtype ThreadState = ThreadState (Ptr ThreadState)
5395

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&#x2019;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&#x2019;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&#x2019;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&#x2019;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&#x2019;t
130+
-- perfect &#x2014; for example, using low-level file operations like
131+
-- @os.close()@ they can (accidentally or maliciously) affect each
132+
-- other&#x2019;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&#x2019;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&#x2019;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+
--
54149
newInterpreter :: IO (Maybe ThreadState)
55150
newInterpreter = do
56151
ptr <- {# call Py_NewInterpreter as ^ #}
57152
return $ if ptr == nullPtr
58153
then Nothing
59154
else Just $ ThreadState $ castPtr ptr
60155

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&#x2019;t been
163+
-- explicitly destroyed at that point.
164+
--
61165
endInterpreter :: ThreadState -> IO ()
62166
endInterpreter (ThreadState ptr) =
63167
{# call Py_EndInterpreter as ^ #} $ castPtr ptr
64168

169+
-- | Return the program name set with 'setProgramName', or the default.
170+
--
65171
getProgramName :: IO Text
66172
getProgramName = pyGetProgramName >>= peekTextW
67173

68174
foreign import ccall safe "hscpython-shim.h Py_GetProgramName"
69175
pyGetProgramName :: IO CWString
70176

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+
--
71185
setProgramName :: Text -> IO ()
72186
setProgramName name = withTextW name cSetProgramName
73187

74188
foreign import ccall safe "hscpython-shim.h hscpython_SetProgramName"
75189
cSetProgramName :: CWString -> IO ()
76190

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+
--
77200
getPrefix :: IO Text
78201
getPrefix = pyGetPrefix >>= peekTextW
79202

80203
foreign import ccall safe "hscpython-shim.h Py_GetPrefix"
81204
pyGetPrefix :: IO CWString
82205

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+
--
83237
getExecPrefix :: IO Text
84238
getExecPrefix = pyGetExecPrefix >>= peekTextW
85239

86240
foreign import ccall safe "hscpython-shim.h Py_GetExecPrefix"
87241
pyGetExecPrefix :: IO CWString
88242

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+
--
89248
getProgramFullPath :: IO Text
90249
getProgramFullPath = pyGetProgramFullPath >>= peekTextW
91250

92251
foreign import ccall safe "hscpython-shim.h Py_GetProgramFullPath"
93252
pyGetProgramFullPath :: IO CWString
94253

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+
--
95262
getPath :: IO Text
96263
getPath = pyGetPath >>= peekTextW
97264

98265
foreign import ccall safe "hscpython-shim.h Py_GetPath"
99266
pyGetPath :: IO CWString
100267

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+
--
101280
{# fun Py_GetVersion as getVersion
102281
{} -> `Text' peekText* #}
103282

283+
-- | Return the platform identifier for the current platform. On Unix, this
284+
-- is formed from the &#x201c;official&#x201d; 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+
--
104290
{# fun Py_GetPlatform as getPlatform
105291
{} -> `Text' peekText* #}
106292

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+
--
107302
{# fun Py_GetCopyright as getCopyright
108303
{} -> `Text' peekText* #}
109304

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+
--
110315
{# fun Py_GetCompiler as getCompiler
111316
{} -> `Text' peekText* #}
112317

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+
--
113328
{# fun Py_GetBuildInfo as getBuildInfo
114329
{} -> `Text' peekText* #}
115330

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&#x2019;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&#x2019;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+
--
116342
setArgv :: Text -> [Text] -> IO ()
117343
setArgv argv0 argv =
118344
mapWith withTextW (argv0 : argv) $ \textPtrs ->
@@ -122,12 +348,21 @@ setArgv argv0 argv =
122348
foreign import ccall safe "hscpython-shim.h PySys_SetArgv"
123349
pySetArgv :: CInt -> Ptr CWString -> IO ()
124350

351+
-- | Return the default &#x201c;home&#x201d;, 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+
--
125355
getPythonHome :: IO Text
126356
getPythonHome = pyGetPythonHome >>= peekTextW
127357

128358
foreign import ccall safe "hscpython-shim.h Py_GetPythonHome"
129359
pyGetPythonHome :: IO CWString
130360

361+
-- | Set the default &#x201c;home&#x201d; 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+
--
131366
setPythonHome :: Text -> IO ()
132367
setPythonHome name = withTextW name cSetPythonHome
133368

CPython/Constants.chs

+6
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,18 @@ import CPython.Internal
2626

2727
#include <hscpython-shim.h>
2828

29+
-- | The Python @None@ object, denoting lack of value.
30+
--
2931
{# fun hscpython_Py_None as none
3032
{} -> `SomeObject' peekObject* #}
3133

34+
-- | The Python @True@ object.
35+
--
3236
{# fun hscpython_Py_True as true
3337
{} -> `SomeObject' peekObject* #}
3438

39+
-- | The Python @False@ object.
40+
--
3541
{# fun hscpython_Py_False as false
3642
{} -> `SomeObject' peekObject* #}
3743

0 commit comments

Comments
 (0)