Skip to content

Commit 0a772f2

Browse files
authored
gh-141004: Document remaining PyCF_* compiler flag macros (GH-153958)
1 parent c89b5ef commit 0a772f2

2 files changed

Lines changed: 114 additions & 8 deletions

File tree

Doc/c-api/veryhigh.rst

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,125 @@ the same library that the Python runtime is using.
343343
:py:mod:`!ast` Python module, which exports these constants under
344344
the same names.
345345
346+
.. rubric:: Low-level flags
347+
348+
The following flags and masks serve narrow needs of the standard
349+
library and interactive interpreters. Code outside the standard
350+
library rarely has a reason to use them. They are considered
351+
implementation details and may change at any time.
352+
353+
.. c:macro:: PyCF_ALLOW_INCOMPLETE_INPUT
354+
355+
This flag is a private interface between the compiler and the
356+
:mod:`codeop` module. Do not use it; its behavior is unsupported
357+
and may change without warning.
358+
359+
With this flag set, when compilation fails because the source text
360+
ends where more input is expected, for example in the middle of an
361+
indented block or an unterminated string literal, the error raised
362+
is the undocumented ``_IncompleteInputError``, a subclass of
363+
:exc:`SyntaxError`. The :mod:`codeop` module sets this flag,
364+
together with :c:macro:`PyCF_DONT_IMPLY_DEDENT`, to tell input
365+
that is incomplete apart from input with a real syntax error, so
366+
that interactive interpreters know when to prompt for another
367+
line instead of reporting an error.
368+
369+
.. versionadded:: 3.11
370+
371+
.. c:macro:: PyCF_DONT_IMPLY_DEDENT
372+
373+
By default, when compiling with the :c:var:`Py_single_input` start
374+
symbol, reaching the end of the source text implicitly closes any
375+
open indented blocks. With this flag set, open blocks are only
376+
closed if the last line of the source ends with a newline; otherwise,
377+
compilation fails with a :exc:`SyntaxError`:
378+
379+
.. code-block:: c
380+
381+
PyCompilerFlags flags = {
382+
.cf_flags = 0,
383+
.cf_feature_version = PY_MINOR_VERSION,
384+
};
385+
const char *source = "if a:\n pass";
386+
387+
/* The "if" block is closed implicitly;
388+
this returns a code object: */
389+
Py_CompileStringFlags(source, "<input>", Py_single_input, &flags);
390+
391+
/* With the flag, this fails with a SyntaxError,
392+
because the last line does not end with a newline: */
393+
flags.cf_flags = PyCF_DONT_IMPLY_DEDENT;
394+
Py_CompileStringFlags(source, "<input>", Py_single_input, &flags);
395+
396+
The :mod:`codeop` module uses this flag to detect incomplete
397+
interactive input. While the user is still typing inside an
398+
indented block, the source does not yet end with a newline, so it
399+
fails to compile and the user is prompted for another line.
400+
401+
.. c:macro:: PyCF_IGNORE_COOKIE
402+
403+
Read the source text as UTF-8, ignoring its :pep:`263` encoding
404+
declaration ("coding cookie"), if any:
405+
406+
.. code-block:: c
407+
408+
PyCompilerFlags flags = {
409+
.cf_flags = 0,
410+
.cf_feature_version = PY_MINOR_VERSION,
411+
};
412+
const char *source = "# coding: latin-1\ns = '\xe9'\n";
413+
414+
/* The coding cookie is honored: byte 0xE9 is decoded as
415+
Latin-1, and this returns a code object that sets s to "é": */
416+
Py_CompileStringFlags(source, "<input>", Py_file_input, &flags);
417+
418+
/* With the flag, the cookie is ignored and compilation fails
419+
with a SyntaxError, because 0xE9 is not valid UTF-8: */
420+
flags.cf_flags = PyCF_IGNORE_COOKIE;
421+
Py_CompileStringFlags(source, "<input>", Py_file_input, &flags);
422+
423+
The :func:`compile`, :func:`eval` and :func:`exec` built-in functions
424+
set this flag when the source is a :class:`str` object, because they
425+
pass the text to the parser encoded as UTF-8.
426+
427+
.. c:macro:: PyCF_SOURCE_IS_UTF8
428+
429+
Mark the source text as known to be UTF-8 encoded.
430+
The :func:`compile`, :func:`eval` and :func:`exec` built-in functions
431+
set this flag, but it currently has no effect.
432+
346433
The "``PyCF``" flags above can be combined with "``CO_FUTURE``" flags such
347434
as :c:macro:`CO_FUTURE_ANNOTATIONS` to enable features normally
348435
selectable using :ref:`future statements <future>`.
349436
See :ref:`c_codeobject_flags` for a complete list.
350437
438+
The following masks combine several flags:
439+
440+
.. c:macro:: PyCF_MASK
441+
442+
Bitmask of all ``CO_FUTURE`` flags (see :ref:`c_codeobject_flags`),
443+
which select features normally enabled by
444+
:ref:`future statements <future>`.
445+
When code compiled with a ``PyCompilerFlags *flags`` argument
446+
contains a ``from __future__ import`` statement, the flag for the
447+
imported feature is added to *flags*, so that code executed later
448+
in the same context inherits it.
449+
450+
.. c:macro:: PyCF_MASK_OBSOLETE
451+
452+
Do not use this mask in new code. It is kept only so that old
453+
code passing its flags to :func:`compile` keeps working.
454+
455+
Bitmask of flags for obsolete future features that no longer
456+
have any effect.
457+
458+
.. c:macro:: PyCF_COMPILE_MASK
459+
460+
Bitmask of all ``PyCF`` flags that change how the source is
461+
compiled, such as :c:macro:`PyCF_ONLY_AST`.
462+
The :func:`compile` built-in function uses this mask to validate
463+
its *flags* argument.
464+
351465
352466
.. _start-symbols:
353467

Tools/check-c-api-docs/ignored_c_api.txt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,6 @@ PY_DWORD_MAX
2929
PY_BIG_ENDIAN
3030
# cpython/methodobject.h
3131
PyCFunction_GET_CLASS
32-
# cpython/compile.h
33-
PyCF_ALLOW_INCOMPLETE_INPUT
34-
PyCF_COMPILE_MASK
35-
PyCF_DONT_IMPLY_DEDENT
36-
PyCF_IGNORE_COOKIE
37-
PyCF_MASK
38-
PyCF_MASK_OBSOLETE
39-
PyCF_SOURCE_IS_UTF8
4032
# cpython/descrobject.h
4133
PyDescr_NAME
4234
PyDescr_TYPE

0 commit comments

Comments
 (0)