Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views2 pages

Chapter 1 Python

The document introduces the Python/C API, which allows C and C++ programmers to access the Python interpreter for writing extension modules or embedding Python in larger applications. It emphasizes the importance of following coding standards outlined in PEP 7 for contributions to CPython and provides guidelines for including necessary headers in the code. Additionally, it highlights the distinction between user-visible names and internal names within the API.

Uploaded by

teens6388
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Chapter 1 Python

The document introduces the Python/C API, which allows C and C++ programmers to access the Python interpreter for writing extension modules or embedding Python in larger applications. It emphasizes the importance of following coding standards outlined in PEP 7 for contributions to CPython and provides guidelines for including necessary headers in the code. Additionally, it highlights the distinction between user-visible names and internal names within the API.

Uploaded by

teens6388
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 2

CHAPTER

ONE
INTRODUCTION

The Application Programmer’s Interface to Python gives C and C++ programmers access to the Python
interpreter
at a variety of levels. The API is equally usable from C++, but for brevity it is generally referred to as
the Python/C
API. There are two fundamentally different reasons for using the Python/C API. The first reason is to
write extension
modules for specific purposes; these are C modules that extend the Python interpreter. This is probably
the most
common use. The second reason is to use Python as a component in a larger application; this technique
is generally
referred to as embedding Python in an application.
Writing an extension module is a relatively well-understood process, where a “cookbook” approach
works well. There
are several tools that automate the process to some extent. While people have embedded Python in
other applications
since its early existence, the process of embedding Python is less straightforward than writing an
extension.
Many API functions are useful independent of whether you’re embedding or extending Python;
moreover, most
applications that embed Python will need to provide a custom extension as well, so it’s probably a good
idea to
become familiar with writing an extension before attempting to embed Python in a real application.
1.1 Coding standards
If you’re writing C code for inclusion in CPython, you must follow the guidelines and standards
defined in PEP 7.
These guidelines apply regardless of the version of Python you are contributing to. Following these
conventions is
not necessary for your own third party extension modules, unless you eventually expect to contribute
them to Python.
1.2 Include Files
All function, type and macro definitions needed to use the Python/C API are included in your code by
the following
line:

#define PY_SSIZE_T_CLEAN
#include <Python.h>
This implies inclusion of the following standard headers: <stdio.h>, <string.h>, <errno.h>, <limits.h>,
<assert.h> and <stdlib.h> (if available).

® Note
Since Python may define some pre-processor definitions which affect the standard headers on some
systems, you
must include Python.h before any standard headers are included.
It is recommended to always define PY_SSIZE_T_CLEAN before including Python.h. See Parsing
arguments
and building values for a description of this macro.
All user visible names defined by Python.h (except those defined by the included standard headers)
have one of the
prefixes Py or _Py. Names beginning with _Py are for internal use by the Python implementation and
should not be
used by extension writers. Structure member names do not have a reserved prefix.

You might also like