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