Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into pythongh-109595
Browse files Browse the repository at this point in the history
  • Loading branch information
corona10 committed Oct 7, 2023
2 parents a0cfb21 + de2a403 commit 8daa3b7
Show file tree
Hide file tree
Showing 201 changed files with 5,735 additions and 1,837 deletions.
13 changes: 13 additions & 0 deletions Doc/c-api/init.rst
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,19 @@ code, or when embedding the Python interpreter:
When the current thread state is ``NULL``, this issues a fatal error (so that
the caller needn't check for ``NULL``).
See also :c:func:`PyThreadState_GetUnchecked`.
.. c:function:: PyThreadState* PyThreadState_GetUnchecked()
Similar to :c:func:`PyThreadState_Get`, but don't kill the process with a
fatal error if it is NULL. The caller is responsible to check if the result
is NULL.
.. versionadded:: 3.13
In Python 3.5 to 3.12, the function was private and known as
``_PyThreadState_UncheckedGet()``.
.. c:function:: PyThreadState* PyThreadState_Swap(PyThreadState *tstate)
Expand Down
21 changes: 17 additions & 4 deletions Doc/c-api/sys.rst
Original file line number Diff line number Diff line change
Expand Up @@ -291,19 +291,24 @@ accessible to C code. They all work with the current interpreter thread's
Raise an auditing event with any active hooks. Return zero for success
and non-zero with an exception set on failure.
The *event* string argument must not be *NULL*.
If any hooks have been added, *format* and other arguments will be used
to construct a tuple to pass. Apart from ``N``, the same format characters
as used in :c:func:`Py_BuildValue` are available. If the built value is not
a tuple, it will be added into a single-element tuple. (The ``N`` format
option consumes a reference, but since there is no way to know whether
arguments to this function will be consumed, using it may cause reference
leaks.)
a tuple, it will be added into a single-element tuple.
The ``N`` format option must not be used. It consumes a reference, but since
there is no way to know whether arguments to this function will be consumed,
using it may cause reference leaks.
Note that ``#`` format characters should always be treated as
:c:type:`Py_ssize_t`, regardless of whether ``PY_SSIZE_T_CLEAN`` was defined.
:func:`sys.audit` performs the same function from Python code.
See also :c:func:`PySys_AuditTuple`.
.. versionadded:: 3.8
.. versionchanged:: 3.8.2
Expand All @@ -312,6 +317,14 @@ accessible to C code. They all work with the current interpreter thread's
unavoidable deprecation warning was raised.
.. c:function:: int PySys_AuditTuple(const char *event, PyObject *args)
Similar to :c:func:`PySys_Audit`, but pass arguments as a Python object.
*args* must be a :class:`tuple`. To pass no arguments, *args* can be *NULL*.
.. versionadded:: 3.13
.. c:function:: int PySys_AddAuditHook(Py_AuditHookFunction hook, void *userData)
Append the callable *hook* to the list of active auditing hooks.
Expand Down
1 change: 1 addition & 0 deletions Doc/data/stable_abi.dat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Doc/howto/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ Currently, the HOWTOs are:
perf_profiling.rst
annotations.rst
isolating-extensions.rst
timerfd.rst

3 changes: 1 addition & 2 deletions Doc/howto/perf_profiling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ the :option:`!-X` option takes precedence over the environment variable.

Example, using the environment variable::

$ PYTHONPERFSUPPORT=1
$ python script.py
$ PYTHONPERFSUPPORT=1 python script.py
$ perf report -g -i perf.data

Example, using the :option:`!-X` option::
Expand Down
230 changes: 230 additions & 0 deletions Doc/howto/timerfd.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
.. _timerfd-howto:

*****************************
timer file descriptor HOWTO
*****************************

:Release: 1.13

This HOWTO discusses Python's support for the linux timer file descriptor.


Examples
========

The following example shows how to use a timer file descriptor
to execute a function twice a second:

.. code-block:: python
# Practical scripts should use really use a non-blocking timer,
# we use a blocking timer here for simplicity.
import os, time
# Create the timer file descriptor
fd = os.timerfd_create(time.CLOCK_REALTIME)
# Start the timer in 1 second, with an interval of half a second
os.timerfd_settime(fd, initial=1, interval=0.5)
try:
# Process timer events four times.
for _ in range(4):
# read() will block until the timer expires
_ = os.read(fd, 8)
print("Timer expired")
finally:
# Remember to close the timer file descriptor!
os.close(fd)
To avoid the precision loss caused by the :class:`float` type,
timer file descriptors allow specifying initial expiration and interval
in integer nanoseconds with ``_ns`` variants of the functions.

This example shows how :func:`~select.epoll` can be used with timer file
descriptors to wait until the file descriptor is ready for reading:

.. code-block:: python
import os, time, select, socket, sys
# Create an epoll object
ep = select.epoll()
# In this example, use loopback address to send "stop" command to the server.
#
# $ telnet 127.0.0.1 1234
# Trying 127.0.0.1...
# Connected to 127.0.0.1.
# Escape character is '^]'.
# stop
# Connection closed by foreign host.
#
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("127.0.0.1", 1234))
sock.setblocking(False)
sock.listen(1)
ep.register(sock, select.EPOLLIN)
# Create timer file descriptors in non-blocking mode.
num = 3
fds = []
for _ in range(num):
fd = os.timerfd_create(time.CLOCK_REALTIME, flags=os.TFD_NONBLOCK)
fds.append(fd)
# Register the timer file descriptor for read events
ep.register(fd, select.EPOLLIN)
# Start the timer with os.timerfd_settime_ns() in nanoseconds.
# Timer 1 fires every 0.25 seconds; timer 2 every 0.5 seconds; etc
for i, fd in enumerate(fds, start=1):
one_sec_in_nsec = 10**9
i = i * one_sec_in_nsec
os.timerfd_settime_ns(fd, initial=i//4, interval=i//4)
timeout = 3
try:
conn = None
is_active = True
while is_active:
# Wait for the timer to expire for 3 seconds.
# epoll.poll() returns a list of (fd, event) pairs.
# fd is a file descriptor.
# sock and conn[=returned value of socket.accept()] are socket objects, not file descriptors.
# So use sock.fileno() and conn.fileno() to get the file descriptors.
events = ep.poll(timeout)
# If more than one timer file descriptors are ready for reading at once,
# epoll.poll() returns a list of (fd, event) pairs.
#
# In this example settings,
# 1st timer fires every 0.25 seconds in 0.25 seconds. (0.25, 0.5, 0.75, 1.0, ...)
# 2nd timer every 0.5 seconds in 0.5 seconds. (0.5, 1.0, 1.5, 2.0, ...)
# 3rd timer every 0.75 seconds in 0.75 seconds. (0.75, 1.5, 2.25, 3.0, ...)
#
# In 0.25 seconds, only 1st timer fires.
# In 0.5 seconds, 1st timer and 2nd timer fires at once.
# In 0.75 seconds, 1st timer and 3rd timer fires at once.
# In 1.5 seconds, 1st timer, 2nd timer and 3rd timer fires at once.
#
# If a timer file descriptor is signaled more than once since
# the last os.read() call, os.read() returns the nubmer of signaled
# as host order of class bytes.
print(f"Signaled events={events}")
for fd, event in events:
if event & select.EPOLLIN:
if fd == sock.fileno():
# Check if there is a connection request.
print(f"Accepting connection {fd}")
conn, addr = sock.accept()
conn.setblocking(False)
print(f"Accepted connection {conn} from {addr}")
ep.register(conn, select.EPOLLIN)
elif conn and fd == conn.fileno():
# Check if there is data to read.
print(f"Reading data {fd}")
data = conn.recv(1024)
if data:
# You should catch UnicodeDecodeError exception for safety.
cmd = data.decode()
if cmd.startswith("stop"):
print(f"Stopping server")
is_active = False
else:
print(f"Unknown command: {cmd}")
else:
# No more data, close connection
print(f"Closing connection {fd}")
ep.unregister(conn)
conn.close()
conn = None
elif fd in fds:
print(f"Reading timer {fd}")
count = int.from_bytes(os.read(fd, 8), byteorder=sys.byteorder)
print(f"Timer {fds.index(fd) + 1} expired {count} times")
else:
print(f"Unknown file descriptor {fd}")
finally:
for fd in fds:
ep.unregister(fd)
os.close(fd)
ep.close()
This example shows how :func:`~select.select` can be used with timer file
descriptors to wait until the file descriptor is ready for reading:

.. code-block:: python
import os, time, select, socket, sys
# In this example, use loopback address to send "stop" command to the server.
#
# $ telnet 127.0.0.1 1234
# Trying 127.0.0.1...
# Connected to 127.0.0.1.
# Escape character is '^]'.
# stop
# Connection closed by foreign host.
#
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("127.0.0.1", 1234))
sock.setblocking(False)
sock.listen(1)
# Create timer file descriptors in non-blocking mode.
num = 3
fds = [os.timerfd_create(time.CLOCK_REALTIME, flags=os.TFD_NONBLOCK)
for _ in range(num)]
select_fds = fds + [sock]
# Start the timers with os.timerfd_settime() in seconds.
# Timer 1 fires every 0.25 seconds; timer 2 every 0.5 seconds; etc
for i, fd in enumerate(fds, start=1):
os.timerfd_settime(fd, initial=i/4, interval=i/4)
timeout = 3
try:
conn = None
is_active = True
while is_active:
# Wait for the timer to expire for 3 seconds.
# select.select() returns a list of file descriptors or objects.
rfd, wfd, xfd = select.select(select_fds, select_fds, select_fds, timeout)
for fd in rfd:
if fd == sock:
# Check if there is a connection request.
print(f"Accepting connection {fd}")
conn, addr = sock.accept()
conn.setblocking(False)
print(f"Accepted connection {conn} from {addr}")
select_fds.append(conn)
elif conn and fd == conn:
# Check if there is data to read.
print(f"Reading data {fd}")
data = conn.recv(1024)
if data:
# You should catch UnicodeDecodeError exception for safety.
cmd = data.decode()
if cmd.startswith("stop"):
print(f"Stopping server")
is_active = False
else:
print(f"Unknown command: {cmd}")
else:
# No more data, close connection
print(f"Closing connection {fd}")
select_fds.remove(conn)
conn.close()
conn = None
elif fd in fds:
print(f"Reading timer {fd}")
count = int.from_bytes(os.read(fd, 8), byteorder=sys.byteorder)
print(f"Timer {fds.index(fd) + 1} expired {count} times")
else:
print(f"Unknown file descriptor {fd}")
finally:
for fd in fds:
os.close(fd)
sock.close()
sock = None
14 changes: 7 additions & 7 deletions Doc/library/ast.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2483,26 +2483,26 @@ The following options are accepted:

.. program:: ast

.. cmdoption:: -h, --help
.. option:: -h, --help

Show the help message and exit.

.. cmdoption:: -m <mode>
--mode <mode>
.. option:: -m <mode>
--mode <mode>

Specify what kind of code must be compiled, like the *mode* argument
in :func:`parse`.

.. cmdoption:: --no-type-comments
.. option:: --no-type-comments

Don't parse type comments.

.. cmdoption:: -a, --include-attributes
.. option:: -a, --include-attributes

Include attributes such as line numbers and column offsets.

.. cmdoption:: -i <indent>
--indent <indent>
.. option:: -i <indent>
--indent <indent>

Indentation of nodes in AST (number of spaces).

Expand Down

0 comments on commit 8daa3b7

Please sign in to comment.