Рівень дуже високого рівня — Python 3.15.2 documentation
Навігація індекс модулі | наступний | попередній | Python » 3.15.2 documentation » Python/C API reference manual » Рівень дуже високого рівня | Theme | Рівень дуже високого рівня ¶ Функції в цій главі дозволять вам виконувати вихідний код Python, поданий у файлі або буфері, але вони не дозволять вам більш детально взаємодіяти з інтерпретатором.
Several of these functions accept a start symbol from the grammar as a parameter. The available start symbols are Py_eval_input , Py_file_input , Py_single_input , and Py_func_type_input . These are described following the functions which accept them as parameters. Note also that several of these functions take FILE * parameters. One particular issue which needs to be handled carefully is that the FILE structure for different C libraries can be different and incompatible.
Under Windows (at least), it is possible for dynamically linked extensions to actually use different libraries, so care should be taken that FILE * parameters are only passed to these functions if it is certain that they were created by the same library that the Python runtime is using. int PyRun_AnyFile ( FILE * fp , const char * filename ) ¶ Це спрощений інтерфейс для PyRun_AnyFileExFlags() нижче, залишаючи closeit встановленим на 0 і flags встановленим на NULL . int PyRun_AnyFileFlags ( FILE * fp , const char * filename , PyCompilerFlags * flags ) ¶ Це спрощений інтерфейс для PyRun_AnyFileExFlags() нижче, залишаючи аргумент closeit встановленим на 0 . int PyRun_AnyFileEx ( FILE * fp , const char * filename , int closeit ) ¶ Це спрощений інтерфейс для PyRun_AnyFileExFlags() нижче, залишаючи для аргументу flags значення NULL . int PyRun_AnyFileExFlags ( FILE * fp , const char * filename , int closeit , PyCompilerFlags * flags ) ¶ Якщо fp посилається на файл, пов’язаний з інтерактивним пристроєм (консоль або термінал введення чи псевдотермінал Unix), повертає значення PyRun_InteractiveLoop() , інакше повертає результат PyRun_SimpleFile() .*ім’я файлу* розшифровується з кодування файлової системи ( sys.getfilesystemencoding() ).
Якщо ім’я файлу має значення NULL , ця функція використовує "???" як ім’я файлу. Якщо closeit має значення true, файл закривається до повернення PyRun_SimpleFileExFlags() . int PyRun_SimpleString ( const char * command ) ¶ This is a simplified interface to PyRun_SimpleStringFlags() below, leaving the PyCompilerFlags * argument set to NULL . int PyRun_SimpleStringFlags ( const char * command , PyCompilerFlags * flags ) ¶ Виконує вихідний код Python з команди в модулі __main__ відповідно до аргументу flags .
Якщо __main__ ще не існує, він буде створений. Повертає 0 у разі успіху або -1 , якщо було викликано виключення. Якщо сталася помилка, неможливо отримати інформацію про винятки. Про значення прапорів дивіться нижче. Note that if an otherwise unhandled SystemExit is raised, this function will not return -1 , but exit the process, as long as PyConfig.inspect is zero. int PyRun_SimpleFile ( FILE * fp , const char * filename ) ¶ Це спрощений інтерфейс для PyRun_SimpleFileExFlags() нижче, залишаючи closeit встановленим на 0 і flags встановленим на NULL . int PyRun_SimpleFileEx ( FILE * fp , const char * filename , int closeit ) ¶ Це спрощений інтерфейс для PyRun_SimpleFileExFlags() нижче, залишаючи flags встановленими на NULL . int PyRun_SimpleFileExFlags ( FILE * fp , const char * filename , int closeit , PyCompilerFlags * flags ) ¶ Подібно до PyRun_SimpleStringFlags() , але вихідний код Python читається з fp замість рядка в пам’яті. filename має бути назвою файлу, воно розшифровується з filesystem encoding and error handler .
Якщо closeit має значення true, файл закривається до повернення PyRun_SimpleFileExFlags() . Примітка У Windows fp слід відкривати як бінарний режим (наприклад, fopen(filename, "rb") ). Інакше Python може неправильно обробити файл сценарію з закінченням рядка LF. int PyRun_InteractiveOneObject ( FILE * fp , PyObject * filename , PyCompilerFlags * flags ) ¶ Read and execute a single statement from a file associated with an interactive device according to the flags argument.
The user will be prompted using sys.ps1 and sys.ps2 . filename must be a Python str object. Повертає 0 , якщо введення було виконано успішно, -1 , якщо стався виняток, або код помилки з файлу включення errcode.h , який поширюється як частина Python, якщо був помилка аналізу. (Зауважте, що errcode.h не включено в Python.h , тому його потрібно включити спеціально, якщо це необхідно.) int PyRun_InteractiveOne ( FILE * fp , const char * filename ) ¶ Це спрощений інтерфейс для PyRun_InteractiveOneFlags() нижче, залишаючи flags встановленими на NULL . int PyRun_InteractiveOneFlags ( FILE * fp , const char * filename , PyCompilerFlags * flags ) ¶ Similar to PyRun_InteractiveOneObject() , but filename is a const char * , which is decoded from the filesystem encoding and error handler . int PyRun_InteractiveLoop ( FILE * fp , const char * filename ) ¶ Це спрощений інтерфейс для PyRun_InteractiveLoopFlags() нижче, залишаючи flags встановленими на NULL . int PyRun_InteractiveLoopFlags ( FILE * fp , const char * filename , PyCompilerFlags * flags ) ¶ Читати та виконувати оператори з файлу, пов’язаного з інтерактивним пристроєм, доки не буде досягнуто EOF.
Користувачеві буде запропоновано використовувати sys.ps1 і sys.ps2 . ім’я файлу розшифровується з filesystem encoding and error handler . Повертає 0 на EOF або від’ємне число у разі помилки. int ( * PyOS_InputHook ) ( void ) ¶ Part of the Stable ABI . Can be set to point to a function with the prototype int func(void) .
The function will be called when Python’s interpreter prompt is about to become idle and wait for user input from the terminal. The return value is ignored. Overriding this hook can be used to integrate the interpreter’s prompt with other event loops, as done in Modules/_tkinter.c in the Python source code. The function is called without an attached thread state . Змінено в версії 3.12: This function is only called from the main interpreter . char * ( * PyOS_ReadlineFunctionPointer ) ( FILE * , FILE * , const char * ) ¶ Can be set to point to a function with the prototype char *func(FILE *stdin, FILE *stdo
The rest of this page is on the live site.