توسعه و تعبیه مفسر پایتون¶
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.
این سند دانش پایهای درباره C و Python را فرض میکند. برای مقدمهی غیررسمی به Python، به راهنمای پایتون مراجعه کنید. مرجع زبان پایتون تعریفی رسمیتر از زبان ارائه میدهد. ارجاع توکارهای پایتون توابع و انواع شیء توکار را مستند میکند، و کتابخانهی استاندارد پایتون ماژولها (هر دو توکار و نوشته شده در Python) را که به زبان طیف وسیعی از کاربردها میدهند، مستند میکند.
برای شرح دقیق کل Python/C API، به راهنمای مرجع Python/C API جداگانه مراجعه کنید.
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".
توجه
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.
ابزارهای شخص ثالث توصیهشده¶
This document only covers the basic tools for creating extensions provided as part of this version of CPython. Some third party tools offer both simpler and more sophisticated approaches to creating C and C++ extensions for Python.
While this document is aimed at extension authors, it should also be helpful to the authors of such tools. For example, the tutorial module can serve as a simple test case for a build tool or sample expected output of a code generator.
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.
Guides for intermediate topics¶
این بخش از راهنما به ایجاد توسعههای C و C++ بدون کمک گرفتن از ابزارهای شخص ثالث میپردازد. این بخش در درجهی اول برای سازندگان آن ابزارها در نظر گرفته شده است، نه اینکه روش توصیهشدهای برای ایجاد توسعههای C خودتان باشد.
تعبیه رانتایم سیپایتون در برنامهای بزرگتر¶
گاهی، بهجای ایجاد افزونهای که درون مفسر پایتون بهعنوان برنامه اصلی اجرا میشود، مطلوب است که رانتایم سیپایتون درون برنامهای بزرگتر تعبیه شود. این بخش برخی از جزئیات مربوط به انجام موفقیتآمیز این کار را در بر میگیرد.