Memperluas dan Menggabungkan Interpreter Python

This document describes how to write modules in C or C++ to extend the Python interpreter with new modules. Those modules can do what Python code does -- define functions, object types and methods -- but also interact with native libraries or achieve better performance by avoiding the overhead of an interpreter. The document also describes how to embed the Python interpreter in another application, for use as an extension language. Finally, it shows how to compile and link extension modules so that they can be loaded dynamically (at run time) into the interpreter, if the underlying operating system supports this feature.

This document assumes basic knowledge about C and Python. For an informal introduction to Python, see Tutorial Python. Referensi Bahasa Python gives a more formal definition of the language. Pustaka Standar Python documents the existing object types, functions and modules (both built-in and written in Python) that give the language its wide application range.

Untuk deskripsi terperinci dari keseluruhan API Python/C, lihat bagian terpisah Panduan Referensi Python/API C.

To support extensions, Python's C API (Application Programmers Interface) defines a set of functions, macros and variables that provide access to most aspects of the Python run-time system. The Python API is incorporated in a C source file by including the header "Python.h".

Catatan

The C extension interface is specific to CPython, and extension modules do not work on other Python implementations. In many cases, it is possible to avoid writing C extensions and preserve portability to other implementations. For example, if your use case is calling C library functions or system calls, you should consider using the ctypes module or the cffi library rather than writing custom C code. These modules let you write Python code to interface with C code and are more portable between implementations of Python than writing and compiling a C extension module.

C API Tutorial

This tutorial describes how to write a simple module in C or C++, using the Python C API -- that is, using the basic tools provided as part of this version of CPython.

  1. Your first C API extension module

Guides for intermediate topics

Bagian panduan ini mencakup pembuatan ekstensi C dan C++ tanpa bantuan dari alat pihak ketiga. Ini dimaksudkan terutama untuk pembuat alat-alat itu, daripada menjadi cara yang disarankan untuk membuat ekstensi C Anda sendiri.

Menggabungkan runtime CPython di aplikasi yang lebih besar

Terkadang, alih-alih membuat ekstensi yang berjalan di dalam interpreter Python sebagai aplikasi utama, lebih baik menanamkan runtime CPython di dalam aplikasi yang lebih besar. Bagian ini mencakup beberapa detail yang terlibat dalam melakukan hal itu dengan sukses.