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
95 changes: 58 additions & 37 deletions Doc/howto/curses.rst
Original file line number Diff line number Diff line change
Expand Up @@ -295,17 +295,21 @@ underline, reverse code, or in color. They'll be explained in more detail in
the next subsection.


The :meth:`~curses.window.addstr` method takes a Python string or
bytestring as the value to be displayed. The contents of bytestrings
are sent to the terminal as-is. Strings are encoded to bytes using
the value of the window's :attr:`~window.encoding` attribute; this defaults to
the default system encoding as returned by :func:`locale.getencoding`.
The :meth:`~curses.window.addstr` method takes a Python string, bytestring
or :class:`~curses.complexstr` as the value to be displayed. The contents
of bytestrings are sent to the terminal as-is.
On a build without wide-character support strings are encoded
using the value of the window's :attr:`~window.encoding` attribute;
this defaults to the default system encoding
as returned by :func:`locale.getencoding`.

The :meth:`~curses.window.addch` methods take a character, which can be
either a string of length 1, a bytestring of length 1, or an integer.
either a string of length 1, a bytestring of length 1, an integer, or a
:class:`~curses.complexchar`.

Constants are provided for extension characters; these constants are
integers greater than 255. For example, :const:`ACS_PLMINUS` is a +/-
Constants are provided for the characters of the terminal's alternate
character set.
For example, :const:`ACS_PLMINUS` is a +/-
symbol, and :const:`ACS_ULCORNER` is the upper left corner of a box
(handy for drawing borders). You can also use the appropriate Unicode
character.
Expand All @@ -319,11 +323,11 @@ won't be distracting; it can be confusing to have the cursor blinking at some
apparently random location.

If your application doesn't need a blinking cursor at all, you can
call ``curs_set(False)`` to make it invisible. For compatibility
with older curses versions, there's a ``leaveok(bool)`` function
that's a synonym for :func:`~curses.curs_set`. When *bool* is true, the
curses library will attempt to suppress the flashing cursor, and you
won't need to worry about leaving it in odd locations.
call ``curs_set(False)`` to make it invisible.
The window method :meth:`~curses.window.leaveok` does something different:
when its argument is true,
curses leaves the cursor wherever the last update put it,
instead of moving it back to the window's cursor position.


Attributes and Color
Expand Down Expand Up @@ -364,6 +368,14 @@ could code::
curses.A_REVERSE)
stdscr.refresh()

A :class:`~curses.complexchar` carries its attributes and color pair
together with the text of one character cell,
and a :class:`~curses.complexstr` is a run of such cells.
They are what :meth:`~curses.window.in_wch` and
:meth:`~curses.window.in_wchstr` return,
so a part of the screen can be read and written back
with its appearance intact.

The curses library also supports color on those terminals that provide it. The
most common such terminal is probably the Linux console, followed by color
xterms.
Expand Down Expand Up @@ -429,40 +441,48 @@ The C curses library offers only very simple input mechanisms. Python's
:mod:`curses` module adds a basic text-input widget. (Other libraries
such as :pypi:`Urwid` have more extensive collections of widgets.)

There are two methods for getting input from a window:
There are three methods for getting input from a window:

* :meth:`~curses.window.getch` refreshes the screen and then waits for
* :meth:`~curses.window.get_wch` refreshes the screen and then waits for
the user to hit a key, displaying the key if :func:`~curses.echo` has been
called earlier. You can optionally specify a coordinate to which
the cursor should be moved before pausing.

* :meth:`~curses.window.getkey` does the same thing but converts the
integer to a string. Individual characters are returned as
1-character strings, and special keys such as function keys return
longer strings containing a key name such as ``KEY_UP`` or ``^G``.
* :meth:`~curses.window.getch` does the same thing but returns the code of
the key instead of a character.
With ncurses this is a single byte of the key's encoding in the current
locale, so a character encoded with several bytes takes several calls,
one byte per call.

* :meth:`~curses.window.getkey` does the same as :meth:`!getch` but returns
a string:
an ordinary key as a 1-character string,
and a special key as its name, such as ``KEY_UP``.

It's possible to not wait for the user using the
:meth:`~curses.window.nodelay` window method. After ``nodelay(True)``,
:meth:`!getch` and :meth:`!getkey` for the window become
non-blocking. To signal that no input is ready, :meth:`!getch` returns
``curses.ERR`` (a value of -1) and :meth:`!getkey` raises an exception.
the reads for the window become non-blocking.
To signal that no input is ready,
:meth:`!get_wch` and :meth:`!getkey` raise an exception,
and :meth:`!getch` returns ``-1``.
There's also a :func:`~curses.halfdelay` function, which can be used to (in
effect) set a timer on each :meth:`!getch`; if no input becomes
effect) set a timer on each read; if no input becomes
available within a specified delay (measured in tenths of a second),
curses raises an exception.
the read fails the same way.

The :meth:`!getch` method returns an integer; if it's between 0 and 255, it
represents the ASCII code of the key pressed. Values greater than 255 are
special keys such as Page Up, Home, or the cursor keys. You can compare the
value returned to constants such as :const:`curses.KEY_PPAGE`,
Special keys such as Page Up, Home, or the cursor keys are returned by all
three as one of the :ref:`KEY_* constants <curses-key-constants>`,
all larger than 255.
You can compare the value returned to constants such as
:const:`curses.KEY_PPAGE`,
:const:`curses.KEY_HOME`, or :const:`curses.KEY_LEFT`. The main loop of
your program may look something like this::

while True:
c = stdscr.getch()
if c == ord('p'):
c = stdscr.get_wch()
if c == 'p':
PrintDocument()
elif c == ord('q'):
elif c == 'q':
break # Exit the while loop
elif c == curses.KEY_HOME:
x = y = 0
Expand All @@ -474,16 +494,17 @@ conversion functions that take either integer or 1-character-string arguments
and return the same type. For example, :func:`curses.ascii.ctrl` returns the
control character corresponding to its argument.

There's also a method to retrieve an entire string,
:meth:`~curses.window.getstr`. It isn't used very often, because its
There's also a method to retrieve an entire line,
:meth:`~curses.window.get_wstr`. It isn't used very often, because its
functionality is quite limited; the only editing keys available are
the backspace key and the Enter key, which terminates the string. It
can optionally be limited to a fixed number of characters. ::
the erase and kill characters, and the Enter key, which terminates the line.
It can optionally be limited to a fixed number of characters;
:meth:`~curses.window.getstr` returns a bytes object instead. ::

curses.echo() # Enable echoing of characters

# Get a 15-character string, with the cursor on the top line
s = stdscr.getstr(0,0, 15)
# Get a line of at most 15 characters, with the cursor on the top line
s = stdscr.get_wstr(0,0, 15)

The :mod:`curses.textpad` module supplies a text box that supports an
Emacs-like set of keybindings. Various methods of the
Expand Down
84 changes: 47 additions & 37 deletions Doc/library/atexit.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,69 @@

--------------

The :mod:`!atexit` module defines functions to register and unregister cleanup
functions. Functions thus registered are automatically executed upon normal
interpreter termination. :mod:`!atexit` runs these functions in the *reverse*
order in which they were registered; if you register ``A``, ``B``, and ``C``,
at interpreter termination time they will be run in the order ``C``, ``B``,
``A``.

**Note:** The functions registered via this module are not called when the
The :mod:`!atexit` module defines functions to register and unregister
:dfn:`exit handlers`: functions that are automatically executed
"at exit", that is, upon normal program termination (for instance,
if :func:`sys.exit` is called or the main module's execution completes)
or, more generally, upon :term:`interpreter shutdown`.

At exit, all registered exit handlers are called
in the *reverse* order in which they were registered.
If you register ``A``, ``B``, and ``C``, at interpreter shutdown time they
will be run in the order ``C``, ``B``, ``A``.
The assumption is that lower level modules will normally be imported before
higher level modules and thus must be cleaned up later.

If an exception is raised during execution of an exit handler, a traceback is
printed (unless :exc:`SystemExit` is raised) and the exception information is
saved. After all exit handlers have had a chance to run, the last exception to
be raised is re-raised.

In programs that use multiple interpreters, each interpreter has its own stack
of exit handlers, which are executed when the interpreter shuts down
(for example, with :meth:`concurrent.interpreters.Interpreter.close` or the
C API :c:func:`Py_EndInterpreter`).
Registration functions in this module only affect the interpreter they are
called from.

**Note:** Exit handlers are not called when the
program is killed by a signal not handled by Python, when a Python fatal
internal error is detected, or when :func:`os._exit` is called.

**Note:** The effect of registering or unregistering functions from within
a cleanup function is undefined.

.. versionchanged:: 3.7
When used with C-API subinterpreters, registered functions
are local to the interpreter they were registered in.
.. warning::
When writing exit handlers, especially in C API extensions, keep in mind
that other exit handlers may still run arbitrary Python code after you
clean up.
Such code should succeed or fail with an exception, rather than crash.

.. function:: register(func, *args, **kwargs)
.. versionchanged:: 3.12
Attempts to start a new thread or :func:`os.fork` a new process
in an exit handler now leads to :exc:`RuntimeError`.
Previously, this could cause race conditions between the main Python
runtime thread freeing thread states while internal :mod:`threading`
routines or the new process try to use that state, which could lead to
crashes rather than clean shutdown.

Register *func* as a function to be executed at termination. Any optional
arguments that are to be passed to *func* must be passed as arguments to
:func:`register`. It is possible to register the same function and arguments
more than once.
.. versionchanged:: 3.7
When used with subinterpreters, registered functions
are local to the interpreter they were registered in.

At normal program termination (for instance, if :func:`sys.exit` is called or
the main module's execution completes), all functions registered are called in
last in, first out order. The assumption is that lower level modules will
normally be imported before higher level modules and thus must be cleaned up
later.
.. function:: register(func, *args, **kwargs)

If an exception is raised during execution of the exit handlers, a traceback is
printed (unless :exc:`SystemExit` is raised) and the exception information is
saved. After all exit handlers have had a chance to run, the last exception to
be raised is re-raised.
Register *func* as an exit handler.
Any optional arguments that are to be passed to *func* must be passed as
arguments to :func:`register`.
It is possible to register the same function and arguments more than once.

This function returns *func*, which makes it possible to use it as a
decorator.

.. warning::
Starting new threads or calling :func:`os.fork` from a registered
function can lead to race condition between the main Python
runtime thread freeing thread states while internal :mod:`threading`
routines or the new process try to use that state. This can lead to
crashes rather than clean shutdown.

.. versionchanged:: 3.12
Attempts to start a new thread or :func:`os.fork` a new process
in a registered function now leads to :exc:`RuntimeError`.

.. function:: unregister(func)

Remove *func* from the list of functions to be run at interpreter shutdown.
Remove *func* from the list of exit handlers.
:func:`unregister` silently does nothing if *func* was not previously
registered. If *func* has been registered more than once, every occurrence
of that function in the :mod:`!atexit` call stack will be removed. Equality
Expand Down
3 changes: 2 additions & 1 deletion Doc/library/curses.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2002,7 +2002,8 @@ Other

.. attribute:: window.encoding

Encoding used to encode method arguments (Unicode strings and characters).
Encoding used to encode the string arguments of the methods and to decode
their results on a build without wide-character support.
The encoding attribute is inherited from the parent window when a subwindow
is created, for example with :meth:`window.subwin`.
By default, current locale encoding is used (see :func:`locale.getencoding`).
Expand Down
12 changes: 12 additions & 0 deletions Doc/library/inspect.rst
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,10 @@ attributes (see :ref:`import-mod-attrs` for module attributes):
Functions wrapped in :func:`functools.partial` now return ``True`` if the
wrapped function is a Python generator function.

.. versionchanged:: 3.10.6
:term:`Duck-typed <duck-typing>` function-like objects now return
``True`` if their code object has the :data:`CO_GENERATOR` flag.

.. versionchanged:: 3.13
Functions wrapped in :func:`functools.partialmethod` now return ``True``
if the wrapped function is a Python generator function.
Expand All @@ -507,6 +511,10 @@ attributes (see :ref:`import-mod-attrs` for module attributes):
Functions wrapped in :func:`functools.partial` now return ``True`` if the
wrapped function is a :term:`coroutine function`.

.. versionchanged:: 3.10.6
:term:`Duck-typed <duck-typing>` function-like objects now return
``True`` if their code object has the :data:`CO_COROUTINE` flag.

.. versionchanged:: 3.12
Sync functions marked with :func:`markcoroutinefunction` now return
``True``.
Expand Down Expand Up @@ -581,6 +589,10 @@ attributes (see :ref:`import-mod-attrs` for module attributes):
Functions wrapped in :func:`functools.partial` now return ``True`` if the
wrapped function is an :term:`asynchronous generator` function.

.. versionchanged:: 3.10.6
:term:`Duck-typed <duck-typing>` function-like objects now return
``True`` if their code object has the :data:`CO_ASYNC_GENERATOR` flag.

.. versionchanged:: 3.13
Functions wrapped in :func:`functools.partialmethod` now return ``True``
if the wrapped function is a :term:`asynchronous generator` function.
Expand Down
13 changes: 7 additions & 6 deletions Doc/library/weakref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,10 @@ same issues as the :meth:`WeakKeyDictionary.keyrefs` method.
from an object's :meth:`~object.__del__` method or a weak reference's
callback.

When the program exits, each remaining live finalizer is called
unless its :attr:`atexit` attribute has been set to false. They
are called in reverse order of creation.
When the program exits (or more generally, at :term:`interpreter shutdown`),
each remaining live finalizer is called unless its :attr:`atexit` attribute
has been set to false.
They are called in reverse order of creation.

A finalizer will never invoke its callback during the later part of
the :term:`interpreter shutdown` when module globals are liable to have
Expand Down Expand Up @@ -321,9 +322,9 @@ same issues as the :meth:`WeakKeyDictionary.keyrefs` method.

.. attribute:: atexit

A writable boolean property which by default is true. When the
program exits, it calls all remaining live finalizers for which
:attr:`.atexit` is true. They are called in reverse order of
A writable boolean property which by default is true. At
:term:`interpreter shutdown`, all remaining live finalizers for which
:attr:`.atexit` is true are called in reverse order of
creation.

.. note::
Expand Down
2 changes: 1 addition & 1 deletion Doc/library/xml.etree.elementtree.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ for parsing and creating XML data.
This module will use a fast implementation whenever available.

.. deprecated:: 3.3
The :mod:`!xml.etree.cElementTree` module is deprecated.
The :mod:`!xml.etree.cElementTree` alias of this module is deprecated.


.. note::
Expand Down
6 changes: 4 additions & 2 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ def _has_code_flag(f, flag):
f = functools._unwrap_partial(f)
if not (isfunction(f) or _signature_is_functionlike(f)):
return False
# If it's a pure Python function, or an object that is duck type
# of a Python function (Cython and Mock functions, for instance), then:
return bool(f.__code__.co_flags & flag)

def isgeneratorfunction(obj):
Expand Down Expand Up @@ -2338,7 +2340,7 @@ def _signature_from_function(cls, func, skip_bound_arg=True,
is_duck_function = True
else:
# If it's not a pure Python function, and not a duck type
# of pure function:
# of pure function (Cython and Mock functions, for instance), then:
raise TypeError('{!r} is not a Python function'.format(func))

s = getattr(func, "__text_signature__", None)
Expand Down Expand Up @@ -2531,7 +2533,7 @@ def _signature_from_callable(obj, *,

if isfunction(obj) or _signature_is_functionlike(obj):
# If it's a pure Python function, or an object that is duck type
# of a Python function (Cython functions, for instance), then:
# of a Python function (Cython and Mock functions, for instance), then:
return _signature_from_function(sigcls, obj,
skip_bound_arg=skip_bound_arg,
globals=globals, locals=locals, eval_str=eval_str,
Expand Down
Loading
Loading