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