1 /**
2   Mirror _pythonrun.h
3 
4   Interfaces to parse and execute pieces of python code
5 
6 See_Also:
7 <a href="http://docs.python.org/c-api/veryhigh.html"> The Very High Level Layer </a>
8   */
9 module deimos.python.pythonrun;
10 
11 import core.stdc.stdio;
12 import deimos.python.pyport;
13 import deimos.python.object;
14 import deimos.python.code;
15 import deimos.python.compile;
16 import deimos.python.pyarena;
17 import deimos.python.pystate;
18 import deimos.python.node;
19 import deimos.python.symtable;
20 
21 extern(C):
22 // Python-header-file: Include/pythonrun.h:
23 
24 version(Python_3_7_Or_Later){
25     // moved to compile.d
26 }else version(Python_3_2_Or_Later) {
27     /// _
28     enum PyCF_MASK = (CO_FUTURE_DIVISION | CO_FUTURE_ABSOLUTE_IMPORT |
29             CO_FUTURE_WITH_STATEMENT | CO_FUTURE_PRINT_FUNCTION |
30             CO_FUTURE_UNICODE_LITERALS | CO_FUTURE_BARRY_AS_BDFL);
31 }else version(Python_2_6_Or_Later) {
32     /// _
33     enum PyCF_MASK = (CO_FUTURE_DIVISION | CO_FUTURE_ABSOLUTE_IMPORT |
34             CO_FUTURE_WITH_STATEMENT | CO_FUTURE_PRINT_FUNCTION |
35             CO_FUTURE_UNICODE_LITERALS);
36 }else version(Python_2_5_Or_Later) {
37     /// _
38     enum PyCF_MASK = (CO_FUTURE_DIVISION | CO_FUTURE_ABSOLUTE_IMPORT |
39             CO_FUTURE_WITH_STATEMENT );
40 }else {
41     /// _
42     enum PyCF_MASK = CO_FUTURE_DIVISION;
43 }
44 
45 version(Python_3_7_Or_Later) {
46     // moved to compile.d
47     import deimos.python.compile;
48 }else{
49     /// _
50     enum PyCF_SOURCE_IS_UTF8 =  0x0100;
51     /// _
52     enum PyCF_DONT_IMPLY_DEDENT = 0x0200;
53 
54     version(Python_2_5_Or_Later) {
55         /// Availability: >= 2.5
56         enum PyCF_ONLY_AST = 0x0400;
57     }
58     version(Python_3_2_Or_Later) {
59         /// Availability: >= 3.2
60         enum PyCF_IGNORE_COOKIE = 0x0800;
61     }
62 
63     /// _
64     struct PyCompilerFlags {
65         /// _
66         int cf_flags;
67     }
68 }
69 
70 
71 version(Python_3_2_Or_Later) {
72     /// Availability: >= 3.2
73     void Py_SetProgramName(wchar*);
74     /// Availability: >= 3.2
75     wchar* Py_GetProgramName();
76 
77     /// Availability: >= 3.2
78     void Py_SetPythonHome(wchar*);
79     /// Availability: >= 3.2
80     wchar* Py_GetPythonHome();
81 }else{
82     /// Availability: <= 2.7
83     void Py_SetProgramName(char*);
84     /// Availability: <= 2.7
85     char* Py_GetProgramName();
86 
87     /// Availability: <= 2.7
88     void Py_SetPythonHome(char*);
89     /// Availability: <= 2.7
90     char* Py_GetPythonHome();
91 }
92 
93 /**
94   Initialize the Python interpreter. For embedding python, this should
95   be called before accessing other Python/C API functions, with the
96   following exceptions:
97 
98   For Python 3, PyImport_AppendInittab and PyImport_ExtendInittab should
99   be called before Py_Initialize.
100   */
101 void Py_Initialize();
102 /// _
103 void Py_InitializeEx(int);
104 /// _
105 void Py_Finalize();
106 /// _
107 int Py_IsInitialized();
108 /// _
109 PyThreadState* Py_NewInterpreter();
110 /// _
111 void Py_EndInterpreter(PyThreadState*);
112 
113 version(Python_2_5_Or_Later){
114     /// _
115     int PyRun_AnyFile()(FILE* fp, const(char)* name) {
116         return PyRun_AnyFileExFlags(fp, name, 0, null);
117     }
118     /// _
119     int PyRun_AnyFileEx()(FILE* fp, const(char)* name, int closeit) {
120         return PyRun_AnyFileExFlags(fp, name, closeit, null);
121     }
122     /// _
123     int PyRun_AnyFileFlags()(FILE* fp, const(char)* name, PyCompilerFlags* flags) {
124         return PyRun_AnyFileExFlags(fp, name, 0, flags);
125     }
126     /// _
127     int PyRun_SimpleString()(const(char)* s) {
128         return PyRun_SimpleStringFlags(s, null);
129     }
130     /// _
131     int PyRun_SimpleFile()(FILE* f, const(char)* p) {
132         return PyRun_SimpleFileExFlags(f, p, 0, null);
133     }
134     /// _
135     int PyRun_SimpleFileEx()(FILE* f, const(char)* p, int c) {
136         return PyRun_SimpleFileExFlags(f, p, c, null);
137     }
138     /// _
139     int PyRun_InteractiveOne()(FILE* f, const(char)* p) {
140         return PyRun_InteractiveOneFlags(f, p, null);
141     }
142     /// _
143     int PyRun_InteractiveLoop()(FILE* f, const(char)* p) {
144         return PyRun_InteractiveLoopFlags(f, p, null);
145     }
146 }else{
147     /// _
148     int PyRun_AnyFile(FILE*, const(char)*);
149     /// _
150     int PyRun_AnyFileEx(FILE*, const(char)*,int);
151 
152     /// _
153     int PyRun_AnyFileFlags(FILE*, const(char)*, PyCompilerFlags *);
154     /// _
155     int PyRun_SimpleString(const(char)*);
156     /// _
157     int PyRun_SimpleFile(FILE*, const(char)*);
158     /// _
159     int PyRun_SimpleFileEx(FILE*, const(char)*, int);
160     /// _
161     int PyRun_InteractiveOne(FILE*, const(char)*);
162     /// _
163     int PyRun_InteractiveLoop(FILE*, const(char)*);
164 }
165 
166 /// _
167 int PyRun_AnyFileExFlags(
168         FILE* fp,
169         const(char)* filename,
170         int closeit,
171         PyCompilerFlags* flags);
172 
173 /// _
174 int PyRun_SimpleStringFlags(const(char)*, PyCompilerFlags*);
175 
176 /// _
177 int PyRun_SimpleFileExFlags(
178         FILE* fp,
179         const(char)* filename,
180         int closeit,
181         PyCompilerFlags* flags);
182 
183 /// _
184 int PyRun_InteractiveOneFlags(
185         FILE* fp,
186         const(char)* filename,
187         PyCompilerFlags* flags);
188 /// _
189 int PyRun_InteractiveLoopFlags(
190         FILE* fp,
191         const(char)* filename,
192         PyCompilerFlags* flags);
193 
194 version(Python_2_5_Or_Later) {
195     /// Availability: >= 2.5
196     _mod* PyParser_ASTFromString(
197             const(char)* s,
198             const(char)* filename,
199             int start,
200             PyCompilerFlags* flags,
201             PyArena* arena);
202     version(Python_3_2_Or_Later) {
203         /// Availability: >= 3.2
204         _mod* PyParser_ASTFromFile(
205                 FILE* fp,
206                 const(char)* filename,
207                 const(char)* enc,
208                 int start,
209                 char* ps1,
210                 char* ps2,
211                 PyCompilerFlags* flags,
212                 int* errcode,
213                 PyArena* arena);
214     }else{
215         /// Availability: <= 2.7
216         _mod* PyParser_ASTFromFile(
217                 FILE* fp,
218                 const(char)* filename,
219                 int start,
220                 char* ps1,
221                 char* ps2,
222                 PyCompilerFlags* flags,
223                 int* errcode,
224                 PyArena* arena);
225     }
226     /// _
227     node* PyParser_SimpleParseString()(const(char)* s, int b) {
228         return PyParser_SimpleParseStringFlags(s, b, 0);
229     }
230     /// _
231     node* PyParser_SimpleParseFile()(FILE* f, const(char)* s, int b) {
232         return PyParser_SimpleParseFileFlags(f, s, b, 0);
233     }
234 }else{
235     /// _
236     node* PyParser_SimpleParseString(const(char)*, int);
237     /// _
238     node* PyParser_SimpleParseFile(FILE*, const(char)*, int);
239     /// Availability: 2.4
240     node* PyParser_SimpleParseStringFlagsFilename(const(char)*, const(char)*, int, int);
241 }
242 
243 /// _
244 node* PyParser_SimpleParseStringFlags(const(char)*, int, int);
245 /// _
246 node* PyParser_SimpleParseFileFlags(FILE*, const(char)*,int, int);
247 
248     /**
249 Params:
250 str = python code to run
251 s = start symbol. one of Py_eval_input, Py_file_input, Py_single_input.
252 g = globals variables. should be a dict.
253 l = local variables. should be a dict.
254 flags = compilation flags (modified by `from __future__ import xx`).
255 
256 Returns:
257 result of executing code, or null if an exception was raised.
258 */
259 PyObject* PyRun_StringFlags(
260         const(char)* str,
261         int s,
262         PyObject* g,
263         PyObject* g,
264         PyCompilerFlags* flags);
265 
266 version(Python_2_5_Or_Later){
267     /**
268 Params:
269 str = python code to run
270 s = start symbol. one of Py_eval_input, Py_file_input, Py_single_input.
271 g = globals variables. should be a dict.
272 l = local variables. should be a dict.
273 
274 Returns:
275 result of executing code, or null if an exception was raised.
276      */
277     PyObject* PyRun_String()(
278             const(char)* str,
279             int s,
280             PyObject* g,
281             PyObject* l) {
282         return PyRun_StringFlags(str, s, g, l, null);
283     }
284     /// _
285     PyObject* PyRun_File()(FILE* fp, const(char)* p, int s, PyObject* g, PyObject* l) {
286         return PyRun_FileExFlags(fp, p, s, g, l, 0, null);
287     }
288     /// _
289     PyObject* PyRun_FileEx()(FILE* fp, const(char)* p, int s, PyObject* g, PyObject* l, int c) {
290         return PyRun_FileExFlags(fp, p, s, g, l, c, null);
291     }
292     /// _
293     PyObject* PyRun_FileFlags()(FILE* fp, const(char)* p, int s, PyObject* g,
294             PyObject* l, PyCompilerFlags *flags) {
295         return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
296     }
297     /// _
298     PyObject* Py_CompileString()(const(char)* str, const(char)* p, int s) {
299         return Py_CompileStringFlags(str, p, s, null);
300     }
301 }else{
302     /**
303 Params:
304 str = python code to run
305 s = start symbol. one of Py_eval_input, Py_file_input, Py_single_input.
306 g = globals variables. should be a dict.
307 l = local variables. should be a dict.
308 
309 Returns:
310 result of executing code, or null if an exception was raised.
311 */
312     PyObject* PyRun_String(const(char)* str, int s, PyObject* g, PyObject* l);
313     /// _
314     PyObject* PyRun_File(FILE*, const(char)*, int, PyObject*, PyObject*);
315     /// _
316     PyObject* PyRun_FileEx(FILE*, const(char)*, int, PyObject*, PyObject*, int);
317     /// _
318     PyObject* PyRun_FileFlags(FILE*, const(char)*, int, PyObject*, PyObject*,
319             PyCompilerFlags *);
320     /// _
321     PyObject* Py_CompileString(const(char)*, const(char)*, int);
322 }
323 
324 /// _
325 PyObject* PyRun_FileExFlags(
326         FILE* fp,
327         const(char)* filename,
328         int start,
329         PyObject* globals,
330         PyObject* locals,
331         int closeit,
332         PyCompilerFlags* flags);
333 
334 version(Python_3_2_Or_Later) {
335     /// _
336     auto Py_CompileStringFlags()(const(char)* str, const(char)* p,
337             int s, PyCompilerFlags* f) {
338         return Py_CompileStringExFlags(str, p, s, f, -1);
339     }
340     /// Availability: >= 3.2
341     PyObject* Py_CompileStringExFlags(
342             const(char)* str,
343             const(char)* filename,
344             int start,
345             PyCompilerFlags* flags,
346             int optimize);
347 }else{
348     /// _
349     PyObject* Py_CompileStringFlags(
350             const(char)* str,
351             const(char)* filename,
352             int,
353             PyCompilerFlags* flags);
354 }
355 
356 /// _
357 symtable* Py_SymtableString(
358     const(char)* str,
359     const(char)* filename,
360     int start);
361 
362 /// _
363 void PyErr_Print();
364 /// _
365 void PyErr_PrintEx(int);
366 /// _
367 void PyErr_Display(PyObject*, PyObject*, PyObject*);
368 version(Python_3_2_Or_Later) {
369     /// Availability: >= 3.2
370     void _Py_PyAtExit(void function() func);
371 }
372 
373 /// _
374 int Py_AtExit(void function() func);
375 
376 /// _
377 void Py_Exit(int);
378 
379 version(Python_3_2_Or_Later) {
380     /// Availability: >= 3.2
381     void _Py_RestoreSignals();
382 }
383 
384 /// _
385 int Py_FdIsInteractive(FILE*, const(char)*);
386 
387 version(Python_3_2_Or_Later) {
388     /// Availability: >= 3.2
389     int Py_Main(int argc, wchar** argv);
390 }else{
391     /// Availability: <= 2.7
392     int Py_Main(int argc, char** argv);
393 }
394 
395 /* In getpath.c */
396 version(Python_3_0_Or_Later) {
397     /// Availability: >= 3.0
398     wchar* Py_GetProgramFullPath();
399     /// Availability: >= 3.0
400     wchar* Py_GetPrefix();
401     /// Availability: >= 3.0
402     wchar* Py_GetExecPrefix();
403     /// Availability: >= 3.0
404     wchar* Py_GetPath();
405     version(Python_3_2_Or_Later) {
406         /// Availability: >= 3.2
407         void Py_SetPath(const(wchar)*);
408     }
409     version(Windows) {
410         /// Availability: >= 3.0, Windows only
411         int _Py_CheckPython3();
412     }
413 }else{
414     char* Py_GetProgramFullPath();
415     char* Py_GetPrefix();
416     char* Py_GetExecPrefix();
417     char* Py_GetPath();
418 }
419 
420 /* In their own files */
421 /// _
422 const(char)* Py_GetVersion();
423 /// _
424 const(char)* Py_GetPlatform();
425 /// _
426 const(char)* Py_GetCopyright();
427 /// _
428 const(char)* Py_GetCompiler();
429 /// _
430 const(char)* Py_GetBuildInfo();
431 
432 /* Various internal finalizers */
433 /// _
434 void _PyExc_Fini();
435 /// _
436 void _PyImport_Fini();
437 /// _
438 void PyMethod_Fini();
439 /// _
440 void PyFrame_Fini();
441 /// _
442 void PyCFunction_Fini();
443 /// _
444 void PyDict_Fini();
445 /// _
446 void PyTuple_Fini();
447 /// _
448 void PyList_Fini();
449 /// _
450 void PySet_Fini();
451 version(Python_3_0_Or_Later) {
452     /// Availability: 3.*
453     void PyBytes_Fini();
454     /// Availability: 3.*
455     void PyByteArray_Fini();
456 }else{
457     /// Availability: 2.*
458     void PyString_Fini();
459     /// Availability: 2.*
460     void PyInt_Fini();
461 }
462 /// _
463 void PyFloat_Fini();
464 /// _
465 void PyOS_FiniInterrupts();
466 /// _
467 void PyByteArray_Fini();
468 version(Python_3_2_Or_Later) {
469     /// Availability: >= 3.2
470     void PySlice_Fini();
471     /// Availability: >= 3.2
472     mixin(PyAPI_DATA!"PyThreadState* _Py_Finalizing");
473 }
474 
475 
476 /// _
477 char* PyOS_Readline(FILE*, FILE*, char*);
478 
479 /// _
480 mixin(PyAPI_DATA!"int function() PyOS_InputHook");
481 /// _
482 mixin(PyAPI_DATA!"char* function(FILE*, FILE*, char*)
483     PyOS_ReadlineFunctionPointer");
484 /// _
485 mixin(PyAPI_DATA!"PyThreadState* _PyOS_ReadlineTState");
486