Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Doc/c-api/concrete.rst
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,13 @@ Other Objects
gen.rst
coro.rst
contextvars.rst
datetime.rst
typehints.rst


C API for extension modules
===========================

.. toctree::

curses.rst
datetime.rst
138 changes: 138 additions & 0 deletions Doc/c-api/curses.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
.. highlight:: c

Curses C API
------------

:mod:`curses` exposes a small C interface for extension modules.
Consumers must include the header file :file:`py_curses.h` (which is not
included by default by :file:`Python.h`) and :c:func:`import_curses` must
be invoked, usually as part of the module initialisation function, to populate
:c:var:`PyCurses_API`.

.. warning::

Neither the C API nor the pure Python :mod:`curses` module are compatible
with subinterpreters.

.. c:macro:: import_curses()

Import the curses C API. The macro does not need a semi-colon to be called.

On success, populate the :c:var:`PyCurses_API` pointer.

On failure, set :c:var:`PyCurses_API` to NULL and set an exception.
The caller must check if an error occurred via :c:func:`PyErr_Occurred`:

.. code-block::

import_curses(); // semi-colon is optional but recommended
if (PyErr_Occurred()) { /* cleanup */ }


.. c:var:: void **PyCurses_API

Dynamically allocated object containing the curses C API.
This variable is only available once :c:macro:`import_curses` succeeds.

``PyCurses_API[0]`` corresponds to :c:data:`PyCursesWindow_Type`.

``PyCurses_API[1]``, ``PyCurses_API[2]``, and ``PyCurses_API[3]``
are pointers to predicate functions of type ``int (*)(void)``.

When called, these predicates return whether :func:`curses.setupterm`,
:func:`curses.initscr`, and :func:`curses.start_color` have been called
respectively.

See also the convenience macros :c:macro:`PyCursesSetupTermCalled`,
:c:macro:`PyCursesInitialised`, and :c:macro:`PyCursesInitialisedColor`.

.. note::

The number of entries in this structure is subject to changes.
Consider using :c:macro:`PyCurses_API_pointers` to check if
new fields are available or not.


.. c:macro:: PyCurses_API_pointers

The number of accessible fields (``4``) in :c:var:`PyCurses_API`.
This number is incremented whenever new fields are added.


.. c:var:: PyTypeObject PyCursesWindow_Type

The :ref:`heap type <heap-types>` corresponding to :class:`curses.window`.


.. c:function:: int PyCursesWindow_Check(PyObject *op)

Return true if *op* is a :class:`curses.window` instance, false otherwise.


The following macros are convenience macros expanding into C statements.
In particular, they can only be used as ``macro;`` or ``macro``, but not
``macro()`` or ``macro();``.

.. c:macro:: PyCursesSetupTermCalled

Macro checking if :func:`curses.setupterm` has been called.

The macro expansion is roughly equivalent to:

.. code-block::

{
typedef int (*predicate_t)(void);
predicate_t was_setupterm_called = (predicate_t)PyCurses_API[1];
if (!was_setupterm_called()) {
return NULL;
}
}


.. c:macro:: PyCursesInitialised

Macro checking if :func:`curses.initscr` has been called.

The macro expansion is roughly equivalent to:

.. code-block::

{
typedef int (*predicate_t)(void);
predicate_t was_initscr_called = (predicate_t)PyCurses_API[2];
if (!was_initscr_called()) {
return NULL;
}
}


.. c:macro:: PyCursesInitialisedColor

Macro checking if :func:`curses.start_color` has been called.

The macro expansion is roughly equivalent to:

.. code-block::

{
typedef int (*predicate_t)(void);
predicate_t was_start_color_called = (predicate_t)PyCurses_API[3];
if (!was_start_color_called()) {
return NULL;
}
}


Internal data
-------------

The following objects are exposed by the C API but should be considered
internal-only.

.. c:macro:: PyCurses_CAPSULE_NAME

Name of the curses capsule to pass to :c:func:`PyCapsule_Import`.

Internal usage only. Use :c:macro:`import_curses` instead.

42 changes: 42 additions & 0 deletions Doc/c-api/descriptor.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,45 @@ found in the dictionary of type objects.


.. c:function:: PyObject* PyWrapper_New(PyObject *, PyObject *)


Built-in descriptors
^^^^^^^^^^^^^^^^^^^^

.. c:var:: PyTypeObject PySuper_Type

The type object for super objects. This is the same object as
:class:`super` in the Python layer.


.. c:var:: PyTypeObject PyClassMethod_Type

The type of class method objects. This is the same object as
:class:`classmethod` in the Python layer.


.. c:function:: PyObject *PyClassMethod_New(PyObject *callable)

Create a new :class:`classmethod` object wrapping *callable*.
*callable* must be a callable object and must not be ``NULL``.

On success, this function returns a :term:`strong reference` to a new class
method descriptor. On failure, this function returns ``NULL`` with an
exception set.


.. c:var:: PyTypeObject PyStaticMethod_Type

The type of static method objects. This is the same object as
:class:`staticmethod` in the Python layer.


.. c:function:: PyObject *PyStaticMethod_New(PyObject *callable)

Create a new :class:`staticmethod` object wrapping *callable*.
*callable* must be a callable object and must not be ``NULL``.

On success, this function returns a :term:`strong reference` to a new static
method descriptor. On failure, this function returns ``NULL`` with an
exception set.

11 changes: 11 additions & 0 deletions Doc/c-api/exceptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,17 @@ Exception Classes
Exception Objects
=================

.. c:function:: int PyExceptionInstance_Check(PyObject *op)

Return true if *op* is an instance of :class:`BaseException`, false
otherwise. This function always succeeds.


.. c:macro:: PyExceptionInstance_Class(op)

Equivalent to :c:func:`Py_TYPE(op) <Py_TYPE>`.


.. c:function:: PyObject* PyException_GetTraceback(PyObject *ex)

Return the traceback associated with the exception as a new reference, as
Expand Down
9 changes: 9 additions & 0 deletions Doc/c-api/function.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ There are a few functions specific to Python functions.
dictionary of arguments or ``NULL``.


.. c:function:: int PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults)

Set the keyword-only argument default values of the function object *op*.
*defaults* must be a dictionary of keyword-only arguments or ``Py_None``.

This function returns ``0`` on success, and returns ``-1`` with an exception
set on failure.


.. c:function:: PyObject* PyFunction_GetClosure(PyObject *op)

Return the closure associated with the function object *op*. This can be ``NULL``
Expand Down
87 changes: 55 additions & 32 deletions Doc/c-api/veryhigh.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ the interpreter.

Several of these functions accept a start symbol from the grammar as a
parameter. The available start symbols are :c:data:`Py_eval_input`,
:c:data:`Py_file_input`, and :c:data:`Py_single_input`. These are described
following the functions which accept them as parameters.
:c:data:`Py_file_input`, :c:data:`Py_single_input`, and
:c:data:`Py_func_type_input`. These are described following the functions
which accept them as parameters.

Note also that several of these functions take :c:expr:`FILE*` parameters. One
particular issue which needs to be handled carefully is that the :c:type:`FILE`
Expand Down Expand Up @@ -183,8 +184,7 @@ the same library that the Python runtime is using.
objects *globals* and *locals* with the compiler flags specified by
*flags*. *globals* must be a dictionary; *locals* can be any object
that implements the mapping protocol. The parameter *start* specifies
the start symbol and must one of the following:
:c:data:`Py_eval_input`, :c:data:`Py_file_input`, or :c:data:`Py_single_input`.
the start symbol and must one of the :ref:`available start symbols <start-symbols>`.

Returns the result of executing the code as a Python object, or ``NULL`` if an
exception was raised.
Expand Down Expand Up @@ -233,8 +233,8 @@ the same library that the Python runtime is using.

Parse and compile the Python source code in *str*, returning the resulting code
object. The start symbol is given by *start*; this can be used to constrain the
code which can be compiled and should be :c:data:`Py_eval_input`,
:c:data:`Py_file_input`, or :c:data:`Py_single_input`. The filename specified by
code which can be compiled and should be :ref:`available start symbols
<start-symbols>`. The filename specified by
*filename* is used to construct the code object and may appear in tracebacks or
:exc:`SyntaxError` exception messages. This returns ``NULL`` if the code
cannot be parsed or compiled.
Expand Down Expand Up @@ -297,32 +297,6 @@ the same library that the Python runtime is using.
true on success, false on failure.


.. c:var:: int Py_eval_input

.. index:: single: Py_CompileString (C function)

The start symbol from the Python grammar for isolated expressions; for use with
:c:func:`Py_CompileString`.


.. c:var:: int Py_file_input

.. index:: single: Py_CompileString (C function)

The start symbol from the Python grammar for sequences of statements as read
from a file or other source; for use with :c:func:`Py_CompileString`. This is
the symbol to use when compiling arbitrarily long Python source code.


.. c:var:: int Py_single_input

.. index:: single: Py_CompileString (C function)

The start symbol from the Python grammar for a single statement; for use with
:c:func:`Py_CompileString`. This is the symbol used for the interactive
interpreter loop.


.. c:struct:: PyCompilerFlags

This is the structure used to hold compiler flags. In cases where code is only
Expand Down Expand Up @@ -366,3 +340,52 @@ the same library that the Python runtime is using.
as :c:macro:`CO_FUTURE_ANNOTATIONS` to enable features normally
selectable using :ref:`future statements <future>`.
See :ref:`c_codeobject_flags` for a complete list.


.. _start-symbols:

Available start symbols
^^^^^^^^^^^^^^^^^^^^^^^


.. c:var:: int Py_eval_input

.. index:: single: Py_CompileString (C function)

The start symbol from the Python grammar for isolated expressions; for use with
:c:func:`Py_CompileString`.


.. c:var:: int Py_file_input

.. index:: single: Py_CompileString (C function)

The start symbol from the Python grammar for sequences of statements as read
from a file or other source; for use with :c:func:`Py_CompileString`. This is
the symbol to use when compiling arbitrarily long Python source code.


.. c:var:: int Py_single_input

.. index:: single: Py_CompileString (C function)

The start symbol from the Python grammar for a single statement; for use with
:c:func:`Py_CompileString`. This is the symbol used for the interactive
interpreter loop.


.. c:var:: int Py_func_type_input

.. index:: single: Py_CompileString (C function)

The start symbol from the Python grammar for a function type; for use with
:c:func:`Py_CompileString`. This is used to parse "signature type comments"
from :pep:`484`.

This requires the :c:macro:`PyCF_ONLY_AST` flag to be set.

.. seealso::
* :py:class:`ast.FunctionType`
* :pep:`484`

.. versionadded:: 3.8
4 changes: 4 additions & 0 deletions Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1673,9 +1673,13 @@ iterations of the loop.
* ``0x02`` a dictionary of keyword-only parameters' default values
* ``0x04`` a tuple of strings containing parameters' annotations
* ``0x08`` a tuple containing cells for free variables, making a closure
* ``0x10`` the :term:`annotate function` for the function object

.. versionadded:: 3.13

.. versionchanged:: 3.14
Added ``0x10`` to indicate the annotate function for the function object.


.. opcode:: BUILD_SLICE (argc)

Expand Down
Loading