매우 고수준 계층

이 장의 함수들은 파일이나 버퍼에 제공된 파이썬 소스 코드를 실행할 수 있도록 하지만, 인터프리터와 더 세밀한 방식으로 상호 작용하도록 하지는 않습니다.

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, and Py_single_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 Py_Main(int argc, wchar_t **argv)
Part of the Stable ABI.

표준 인터프리터의 메인 프로그램. 이것은 파이썬을 내장하는 프로그램을 위해 제공됩니다. argcargv 매개 변수는 C 프로그램의 main() 함수에 전달되는 것과 정확히 일치하도록 준비해야 합니다 (사용자의 로케일에 따라 wchar_t로 변환됩니다). 인자 목록이 수정될 수 있음에 유의해야 합니다 (하지만 인자 목록이 가리키는 문자열의 내용은 수정되지 않습니다). 인터프리터가 정상적으로 (즉, 예외 없이) 종료되면 반환 값은 0, 예외로 인해 인터프리터가 종료되면 1, 매개 변수 목록이 유효한 파이썬 명령 줄을 나타내지 않으면 2가 됩니다.

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 Py_BytesMain(int argc, char **argv)
Part of the Stable ABI since version 3.8.

Py_Main()과 유사하지만 argv는 바이트 문자열의 배열입니다.

버전 3.8에 추가.

int PyRun_AnyFile(FILE *fp, const char *filename)

아래 PyRun_AnyFileExFlags() 의 단순화된 인터페이스입니다. closeit0으로 flagsNULL로 설정된 상태로 남겨둡니다.

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)

If fp refers to a file associated with an interactive device (console or terminal input or Unix pseudo-terminal), return the value of PyRun_InteractiveLoop(), otherwise return the result of PyRun_SimpleFile(). filename is decoded from the filesystem encoding (sys.getfilesystemencoding()). If filename is NULL, this function uses "???" as the filename. If closeit is true, the file is closed before PyRun_SimpleFileExFlags() returns.

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)

flags 인자에 따라 __main__ 모듈에서 command에 있는 파이썬 소스 코드를 실행합니다. __main__이 존재하지 않으면 만듭니다. 성공하면 0을, 예외가 발생하면 -1을 반환합니다. 에러가 있으면, 예외 정보를 얻을 방법이 없습니다. flags의 의미는 아래를 참조하십시오.

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() 의 단순화된 인터페이스입니다. closeit0으로, flagsNULL로 설정된 상태로 남겨둡니다.

int PyRun_SimpleFileEx(FILE *fp, const char *filename, int closeit)

아래 PyRun_SimpleFileExFlags() 의 단순화된 인터페이스입니다. flagsNULL로 설정된 상태로 남겨둡니다.

int PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, PyCompilerFlags *flags)

Similar to PyRun_SimpleStringFlags(), but the Python source code is read from fp instead of an in-memory string. filename should be the name of the file, it is decoded from filesystem encoding and error handler. If closeit is true, the file is closed before PyRun_SimpleFileExFlags() returns.

참고

윈도우에서, fp는 바이너리 모드로 열어야 합니다 (예를 들어 fopen(filename, "rb")). 그렇지 않으면, 파이썬은 LF 줄 종료가 있는 스크립트 파일을 올바르게 처리하지 못할 수 있습니다.

int PyRun_InteractiveOne(FILE *fp, const char *filename)

아래 PyRun_InteractiveOneFlags() 의 단순화된 인터페이스입니다. flagsNULL로 설정된 상태로 남겨둡니다.

int PyRun_InteractiveOneFlags(FILE *fp, const char *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 is decoded from the filesystem encoding and error handler.

입력이 성공적으로 실행될 때 0을, 예외가 있으면 -1을, 또는 구문 분석 에러가 있으면 파이썬의 일부로 배포된 errcode.h 인클루드 파일에 있는 에러 코드를 반환합니다. (errcode.hPython.h에서 인클루드하지 않기 때문에 필요하면 특별히 인클루드해야 함에 유의하십시오.)

int PyRun_InteractiveLoop(FILE *fp, const char *filename)

아래 PyRun_InteractiveLoopFlags() 의 단순화된 인터페이스입니다. flagsNULL로 설정된 상태로 남겨둡니다.

int PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)

Read and execute statements from a file associated with an interactive device until EOF is reached. The user will be prompted using sys.ps1 and sys.ps2. filename is decoded from the filesystem encoding and error handler. Returns 0 at EOF or a negative number upon failure.

int (*PyOS_InputHook)(void)
Part of the Stable ABI.

프로토타입 int func(void)인 함수를 가리키도록 설정할 수 있습니다. 이 함수는 파이썬의 인터프리터 프롬프트가 유휴 상태가 되고 터미널에서 사용자 입력을 기다리려고 할 때 호출됩니다. 반환 값은 무시됩니다. 이 훅을 재정의하는 것은 파이썬 소스 코드의 Modules/_tkinter.c에서 한 것처럼 인터프리터의 프롬프트를 다른 이벤트 루프와 통합하는 데 사용될 수 있습니다.

버전 3.12에서 변경: This function is only called from the main interpreter.

char *(*PyOS_ReadlineFunctionPointer)(FILE*, FILE*, const char*)

프로토타입 char *func(FILE *stdin, FILE *stdout, char *prompt)인 함수를 가리키도록 설정하여, 인터프리터의 프롬프트에서 단일 입력 줄을 읽는 데 사용되는 기본 함수를 재정의할 수 있습니다. 이 함수는 NULL이 아니면 문자열 prompt를 출력한 다음 제공된 표준 입력 파일에서 입력 줄을 읽고 결과 문자열을 반환할 것이라고 기대됩니다. 예를 들어, readline 모듈은 이 훅을 설정하여 줄 편집과 탭 완성 기능을 제공합니다.

결과는 PyMem_RawMalloc()이나 PyMem_RawRealloc()으로 할당된 문자열 이거나, 에러가 발생했으면 NULL이어야 합니다.

버전 3.4에서 변경: 결과는 PyMem_Malloc()이나 PyMem_Realloc()으로 할당하는 대신, PyMem_RawMalloc()이나 PyMem_RawRealloc()으로 할당해야 합니다.

버전 3.12에서 변경: This function is only called from the main interpreter.

PyObject *PyRun_String(const char *str, int start, PyObject *globals, PyObject *locals)
Return value: New reference.

아래 PyRun_StringFlags()의 단순화된 인터페이스입니다. 이것은 flagsNULL로 설정된 상태로 남겨둡니다.

PyObject *PyRun_StringFlags(const char *str, int start, PyObject *globals, PyObject *locals, PyCompilerFlags *flags)
Return value: New reference.

flags로 지정된 컴파일러 플래그를 사용하여 globalslocals 객체로 지정된 컨텍스트에서 str에서 파이썬 소스 코드를 실행합니다. globals는 딕셔너리이어야 합니다. locals는 매핑 프로토콜을 구현하는 모든 객체가 될 수 있습니다. 매개 변수 start는 소스 코드를 구문 분석하는 데 사용해야 하는 시작 토큰을 지정합니다.

코드를 실행한 결과를 파이썬 객체로 반환하거나, 예외가 발생하면 NULL을 반환합니다.

PyObject *PyRun_File(FILE *fp, const char *filename, int start, PyObject *globals, PyObject *locals)
Return value: New reference.

아래 PyRun_FileExFlags()의 단순화된 인터페이스입니다. closeit0으로, flagsNULL로 설정된 상태로 남겨둡니다.

PyObject *PyRun_FileEx(FILE *fp, const char *filename, int start, PyObject *globals, PyObject *locals, int closeit)
Return value: New reference.

아래 PyRun_FileExFlags()의 단순화된 인터페이스입니다. flagsNULL로 설정된 상태로 남겨둡니다.

PyObject *PyRun_FileFlags(FILE *fp, const char *filename, int start, PyObject *globals, PyObject *locals, PyCompilerFlags *flags)
Return value: New reference.

아래 PyRun_FileExFlags()의 단순화된 인터페이스입니다. closeit0으로 설정된 상태로 남겨둡니다.

PyObject *PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals, PyObject *locals, int closeit, PyCompilerFlags *flags)
Return value: New reference.

Similar to PyRun_StringFlags(), but the Python source code is read from fp instead of an in-memory string. filename should be the name of the file, it is decoded from the filesystem encoding and error handler. If closeit is true, the file is closed before PyRun_FileExFlags() returns.

PyObject *Py_CompileString(const char *str, const char *filename, int start)
Return value: New reference. Part of the Stable ABI.

아래 Py_CompileStringFlags() 의 단순화된 인터페이스입니다. flagsNULL로 설정된 상태로 유지합니다.

PyObject *Py_CompileStringFlags(const char *str, const char *filename, int start, PyCompilerFlags *flags)
Return value: New reference.

아래 Py_CompileStringExFlags() 의 단순화된 인터페이스입니다. optimize-1로 설정된 상태로 유지합니다.

PyObject *Py_CompileStringObject(const char *str, PyObject *filename, int start, PyCompilerFlags *flags, int optimize)
Return value: New reference.

Parse and compile the Python source code in str, returning the resulting code object. The start token is given by start; this can be used to constrain the code which can be compiled and should be Py_eval_input, Py_file_input, or Py_single_input. The filename specified by filename is used to construct the code object and may appear in tracebacks or SyntaxError exception messages. This returns NULL if the code cannot be parsed or compiled.

정수 optimize는 컴파일러의 최적화 수준을 지정합니다. -1 값은 -O 옵션으로 주어진 것처럼 인터프리터의 최적화 수준을 선택합니다. 명시적 수준은 0 (최적화 없음; __debug__가 참), 1 (어서션이 제거되고 __debug__가 거짓) 또는 2 (독스트링도 제거됩니다)입니다.

버전 3.4에 추가.

PyObject *Py_CompileStringExFlags(const char *str, const char *filename, int start, PyCompilerFlags *flags, int optimize)
Return value: New reference.

Like Py_CompileStringObject(), but filename is a byte string decoded from the filesystem encoding and error handler.

버전 3.2에 추가.

PyObject *PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Return value: New reference. Part of the Stable ABI.

이것은 코드 객체와 전역 변수 및 지역 변수만 있는, PyEval_EvalCodeEx()의 단순화된 인터페이스입니다. 다른 인자는 NULL로 설정됩니다.

PyObject *PyEval_EvalCodeEx(PyObject *co, PyObject *globals, PyObject *locals, PyObject *const *args, int argcount, PyObject *const *kws, int kwcount, PyObject *const *defs, int defcount, PyObject *kwdefs, PyObject *closure)
Return value: New reference. Part of the Stable ABI.

주어진 평가를 위한 특정 환경에서, 미리 컴파일된 코드 객체를 평가합니다. 이 환경은 전역 변수의 딕셔너리, 지역 변수의 매핑 객체, 인자의 배열, 키워드와 기본값, 키워드 전용 인자의 기본값 딕셔너리 및 셀의 클로저 튜플로 구성됩니다.

PyObject *PyEval_EvalFrame(PyFrameObject *f)
Return value: New reference. Part of the Stable ABI.

실행 프레임을 평가합니다. 이전 버전과의 호환성을 위한, PyEval_EvalFrameEx()의 단순화된 인터페이스입니다.

PyObject *PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Return value: New reference. Part of the Stable ABI.

이것은 파이썬 인터프리트의 메인, 꾸미지 않은 함수입니다. 실행 프레임 f와 연관된 코드 객체가 실행됩니다. 필요에 따라 바이트 코드를 해석하고 호출을 실행합니다. 추가 throwflag 매개 변수는 대체로 무시할 수 있습니다 - 참이면, 예외가 즉시 발생하도록 합니다; 제너레이터 객체의 throw() 메서드에 사용됩니다.

버전 3.4에서 변경: 이 함수는 이제 활성 예외를 조용히 버리지 않았는지 확인하도록 도우려고 디버그 어서션을 포함합니다.

int PyEval_MergeCompilerFlags(PyCompilerFlags *cf)

이 함수는 현재 평가 프레임의 플래그를 변경하고, 성공하면 참을, 실패하면 거짓을 반환합니다.

int Py_eval_input

격리된 표현식을 위한 파이썬 문법의 시작 기호; Py_CompileString()과 함께 사용합니다.

int Py_file_input

파일이나 다른 소스에서 읽은 문장의 시퀀스를 위한 파이썬 문법의 시작 기호; Py_CompileString()과 함께 사용합니다. 임의로 긴 파이썬 소스 코드를 컴파일할 때 사용하는 기호입니다.

int Py_single_input

단일 문장을 위한 파이썬 문법의 시작 기호; Py_CompileString()과 함께 사용합니다. 대화식 인터프리터 루프에 사용되는 기호입니다.

struct PyCompilerFlags

이것은 컴파일러 플래그를 담는 데 사용되는 구조체입니다. 코드가 컴파일되기만 하는 경우 int flags로 전달되고, 코드가 실행되는 경우 PyCompilerFlags *flags로 전달됩니다. 이 경우, from __future__ importflags를 수정할 수 있습니다.

Whenever PyCompilerFlags *flags is NULL, cf_flags is treated as equal to 0, and any modification due to from __future__ import is discarded.

int cf_flags

컴파일러 플래그.

int cf_feature_version

cf_feature_version은 부 파이썬 버전입니다. PY_MINOR_VERSION으로 초기화되어야 합니다.

The field is ignored by default, it is used if and only if PyCF_ONLY_AST flag is set in cf_flags.

버전 3.8에서 변경: cf_feature_version 필드를 추가했습니다.

int CO_FUTURE_DIVISION

flags에서 이 비트를 설정하면 PEP 238에 따라 나누기 연산자 /를 “실수 나누기(true division)”로 해석되도록 합니다.