Skip to content

Commit 83048bf

Browse files
Merge branch 'main' into SimpleHTTPRequestHandler-query-and-fragment
2 parents 4d6f677 + 4e9005d commit 83048bf

29 files changed

+180
-790
lines changed

Doc/c-api/init.rst

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -919,8 +919,36 @@ Note that the ``PyGILState_*`` functions assume there is only one global
919919
interpreter (created automatically by :c:func:`Py_Initialize`). Python
920920
supports the creation of additional interpreters (using
921921
:c:func:`Py_NewInterpreter`), but mixing multiple interpreters and the
922-
``PyGILState_*`` API is unsupported.
922+
``PyGILState_*`` API is unsupported. This is because :c:func:`PyGILState_Ensure`
923+
and similar functions default to :term:`attaching <attached thread state>` a
924+
:term:`thread state` for the main interpreter, meaning that the thread can't safely
925+
interact with the calling subinterpreter.
926+
927+
Supporting subinterpreters in non-Python threads
928+
------------------------------------------------
929+
930+
If you would like to support subinterpreters with non-Python created threads, you
931+
must use the ``PyThreadState_*`` API instead of the traditional ``PyGILState_*``
932+
API.
933+
934+
In particular, you must store the interpreter state from the calling
935+
function and pass it to :c:func:`PyThreadState_New`, which will ensure that
936+
the :term:`thread state` is targeting the correct interpreter::
937+
938+
/* The return value of PyInterpreterState_Get() from the
939+
function that created this thread. */
940+
PyInterpreterState *interp = ThreadData->interp;
941+
PyThreadState *tstate = PyThreadState_New(interp);
942+
PyThreadState_Swap(tstate);
943+
944+
/* GIL of the subinterpreter is now held.
945+
Perform Python actions here. */
946+
result = CallSomeFunction();
947+
/* evaluate result or handle exception */
923948
949+
/* Destroy the thread state. No Python API allowed beyond this point. */
950+
PyThreadState_Clear(tstate);
951+
PyThreadState_DeleteCurrent();
924952
925953
.. _fork-and-threads:
926954
@@ -1097,6 +1125,10 @@ code, or when embedding the Python interpreter:
10971125
.. seealso:
10981126
:c:func:`PyEval_ReleaseThread`
10991127
1128+
.. note::
1129+
Similar to :c:func:`PyGILState_Ensure`, this function will hang the
1130+
thread if the runtime is finalizing.
1131+
11001132
11011133
The following functions use thread-local storage, and are not compatible
11021134
with sub-interpreters:
@@ -1123,10 +1155,10 @@ with sub-interpreters:
11231155
When the function returns, there will be an :term:`attached thread state`
11241156
and the thread will be able to call arbitrary Python code. Failure is a fatal error.
11251157
1126-
.. note::
1127-
Calling this function from a thread when the runtime is finalizing will
1128-
hang the thread until the program exits, even if the thread was not
1129-
created by Python. Refer to
1158+
.. warning::
1159+
Calling this function when the runtime is finalizing is unsafe. Doing
1160+
so will either hang the thread until the program ends, or fully crash
1161+
the interpreter in rare cases. Refer to
11301162
:ref:`cautions-regarding-runtime-finalization` for more details.
11311163
11321164
.. versionchanged:: 3.14
@@ -1143,28 +1175,37 @@ with sub-interpreters:
11431175
Every call to :c:func:`PyGILState_Ensure` must be matched by a call to
11441176
:c:func:`PyGILState_Release` on the same thread.
11451177
1146-
11471178
.. c:function:: PyThreadState* PyGILState_GetThisThreadState()
11481179
11491180
Get the :term:`attached thread state` for this thread. May return ``NULL`` if no
11501181
GILState API has been used on the current thread. Note that the main thread
11511182
always has such a thread-state, even if no auto-thread-state call has been
11521183
made on the main thread. This is mainly a helper/diagnostic function.
11531184
1154-
.. seealso: :c:func:`PyThreadState_Get``
1185+
.. note::
1186+
This function does not account for :term:`thread states <thread state>` created
1187+
by something other than :c:func:`PyGILState_Ensure` (such as :c:func:`PyThreadState_New`).
1188+
Prefer :c:func:`PyThreadState_Get` or :c:func:`PyThreadState_GetUnchecked`
1189+
for most cases.
11551190
1191+
.. seealso: :c:func:`PyThreadState_Get``
11561192
11571193
.. c:function:: int PyGILState_Check()
11581194
11591195
Return ``1`` if the current thread is holding the :term:`GIL` and ``0`` otherwise.
11601196
This function can be called from any thread at any time.
1161-
Only if it has had its Python thread state initialized and currently is
1162-
holding the :term:`GIL` will it return ``1``.
1197+
Only if it has had its :term:`thread state <attached thread state>` initialized
1198+
via :c:func:`PyGILState_Ensure` will it return ``1``.
11631199
This is mainly a helper/diagnostic function. It can be useful
11641200
for example in callback contexts or memory allocation functions when
11651201
knowing that the :term:`GIL` is locked can allow the caller to perform sensitive
11661202
actions or otherwise behave differently.
11671203
1204+
.. note::
1205+
If the current Python process has ever created a subinterpreter, this
1206+
function will *always* return ``1``. Prefer :c:func:`PyThreadState_GetUnchecked`
1207+
for most cases.
1208+
11681209
.. versionadded:: 3.4
11691210
11701211

Doc/deprecations/pending-removal-in-3.15.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Pending removal in Python 3.15
2020

2121
* :mod:`http.server`:
2222

23-
* The obsolete and rarely used :class:`~http.server.CGIHTTPRequestHandler`
23+
* The obsolete and rarely used :class:`!CGIHTTPRequestHandler`
2424
has been deprecated since Python 3.13.
2525
No direct replacement exists.
2626
*Anything* is better than CGI to interface

Doc/library/dataclasses.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,9 @@ Module contents
304304

305305
.. versionadded:: 3.10
306306

307-
- ``doc``: optional docstring for this field.
307+
- *doc*: optional docstring for this field.
308308

309-
.. versionadded:: 3.13
309+
.. versionadded:: 3.14
310310

311311
If the default value of a field is specified by a call to
312312
:func:`!field`, then the class attribute for this field will be

Doc/library/http.server.rst

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -458,55 +458,6 @@ such as using different index file names by overriding the class attribute
458458
:attr:`index_pages`.
459459

460460

461-
.. class:: CGIHTTPRequestHandler(request, client_address, server)
462-
463-
This class is used to serve either files or output of CGI scripts from the
464-
current directory and below. Note that mapping HTTP hierarchic structure to
465-
local directory structure is exactly as in :class:`SimpleHTTPRequestHandler`.
466-
467-
.. note::
468-
469-
CGI scripts run by the :class:`CGIHTTPRequestHandler` class cannot execute
470-
redirects (HTTP code 302), because code 200 (script output follows) is
471-
sent prior to execution of the CGI script. This pre-empts the status
472-
code.
473-
474-
The class will however, run the CGI script, instead of serving it as a file,
475-
if it guesses it to be a CGI script. Only directory-based CGI are used ---
476-
the other common server configuration is to treat special extensions as
477-
denoting CGI scripts.
478-
479-
The :func:`do_GET` and :func:`do_HEAD` functions are modified to run CGI scripts
480-
and serve the output, instead of serving files, if the request leads to
481-
somewhere below the ``cgi_directories`` path.
482-
483-
The :class:`CGIHTTPRequestHandler` defines the following data member:
484-
485-
.. attribute:: cgi_directories
486-
487-
This defaults to ``['/cgi-bin', '/htbin']`` and describes directories to
488-
treat as containing CGI scripts.
489-
490-
The :class:`CGIHTTPRequestHandler` defines the following method:
491-
492-
.. method:: do_POST()
493-
494-
This method serves the ``'POST'`` request type, only allowed for CGI
495-
scripts. Error 501, "Can only POST to CGI scripts", is output when trying
496-
to POST to a non-CGI url.
497-
498-
Note that CGI scripts will be run with UID of user nobody, for security
499-
reasons. Problems with the CGI script will be translated to error 403.
500-
501-
.. deprecated-removed:: 3.13 3.15
502-
503-
:class:`CGIHTTPRequestHandler` is being removed in 3.15. CGI has not
504-
been considered a good way to do things for well over a decade. This code
505-
has been unmaintained for a while now and sees very little practical use.
506-
Retaining it could lead to further :ref:`security considerations
507-
<http.server-security>`.
508-
509-
510461
.. _http-server-cli:
511462

512463
Command-line interface
@@ -563,24 +514,6 @@ The following options are accepted:
563514

564515
.. versionadded:: 3.11
565516

566-
.. option:: --cgi
567-
568-
:class:`CGIHTTPRequestHandler` can be enabled in the command line by passing
569-
the ``--cgi`` option::
570-
571-
python -m http.server --cgi
572-
573-
.. deprecated-removed:: 3.13 3.15
574-
575-
:mod:`http.server` command line ``--cgi`` support is being removed
576-
because :class:`CGIHTTPRequestHandler` is being removed.
577-
578-
.. warning::
579-
580-
:class:`CGIHTTPRequestHandler` and the ``--cgi`` command-line option
581-
are not intended for use by untrusted clients and may be vulnerable
582-
to exploitation. Always use within a secure environment.
583-
584517
.. option:: --tls-cert
585518

586519
Specifies a TLS certificate chain for HTTPS connections::

Doc/library/json.rst

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@ is a lightweight data interchange format inspired by
1818
`JavaScript <https://en.wikipedia.org/wiki/JavaScript>`_ object literal syntax
1919
(although it is not a strict subset of JavaScript [#rfc-errata]_ ).
2020

21+
.. note::
22+
The term "object" in the context of JSON processing in Python can be
23+
ambiguous. All values in Python are objects. In JSON, an object refers to
24+
any data wrapped in curly braces, similar to a Python dictionary.
25+
2126
.. warning::
2227
Be cautious when parsing JSON data from untrusted sources. A malicious
2328
JSON string may cause the decoder to consume considerable CPU and memory
2429
resources. Limiting the size of data to be parsed is recommended.
2530

26-
:mod:`json` exposes an API familiar to users of the standard library
31+
This module exposes an API familiar to users of the standard library
2732
:mod:`marshal` and :mod:`pickle` modules.
2833

2934
Encoding basic Python object hierarchies::
@@ -60,7 +65,7 @@ Pretty printing::
6065
"6": 7
6166
}
6267

63-
Specializing JSON object encoding::
68+
Customizing JSON object encoding::
6469

6570
>>> import json
6671
>>> def custom_json(obj):
@@ -83,7 +88,7 @@ Decoding JSON::
8388
>>> json.load(io)
8489
['streaming API']
8590

86-
Specializing JSON object decoding::
91+
Customizing JSON object decoding::
8792

8893
>>> import json
8994
>>> def as_complex(dct):
@@ -279,7 +284,7 @@ Basic Usage
279284

280285
:param object_hook:
281286
If set, a function that is called with the result of
282-
any object literal decoded (a :class:`dict`).
287+
any JSON object literal decoded (a :class:`dict`).
283288
The return value of this function will be used
284289
instead of the :class:`dict`.
285290
This feature can be used to implement custom decoders,
@@ -289,7 +294,7 @@ Basic Usage
289294

290295
:param object_pairs_hook:
291296
If set, a function that is called with the result of
292-
any object literal decoded with an ordered list of pairs.
297+
any JSON object literal decoded with an ordered list of pairs.
293298
The return value of this function will be used
294299
instead of the :class:`dict`.
295300
This feature can be used to implement custom decoders.

Doc/library/unittest.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ Here is a short script to test three string methods::
109109
unittest.main()
110110

111111

112-
A testcase is created by subclassing :class:`unittest.TestCase`. The three
112+
A test case is created by subclassing :class:`unittest.TestCase`. The three
113113
individual tests are defined with methods whose names start with the letters
114114
``test``. This naming convention informs the test runner about which methods
115115
represent tests.

Doc/tutorial/controlflow.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,8 @@ scope::
999999
43
10001000

10011001
The above example uses a lambda expression to return a function. Another use
1002-
is to pass a small function as an argument::
1002+
is to pass a small function as an argument. For instance, :meth:`list.sort`
1003+
takes a sorting key function *key* which can be a lambda function::
10031004

10041005
>>> pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
10051006
>>> pairs.sort(key=lambda pair: pair[1])

Doc/whatsnew/3.10.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -551,11 +551,12 @@ Patterns and classes
551551
552552
If you are using classes to structure your data, you can use as a pattern
553553
the class name followed by an argument list resembling a constructor. This
554-
pattern has the ability to capture class attributes into variables::
554+
pattern has the ability to capture instance attributes into variables::
555555
556556
class Point:
557-
x: int
558-
y: int
557+
def __init__(self, x, y):
558+
self.x = x
559+
self.y = y
559560
560561
def location(point):
561562
match point:

Doc/whatsnew/3.13.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1871,7 +1871,7 @@ New Deprecations
18711871

18721872
* :mod:`http.server`:
18731873

1874-
* Deprecate :class:`~http.server.CGIHTTPRequestHandler`,
1874+
* Deprecate :class:`!CGIHTTPRequestHandler`,
18751875
to be removed in Python 3.15.
18761876
Process-based CGI HTTP servers have been out of favor for a very long time.
18771877
This code was outdated, unmaintained, and rarely used.

Doc/whatsnew/3.15.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,15 @@ Deprecated
121121
Removed
122122
=======
123123

124+
http.server
125+
-----------
126+
127+
* Removed the :class:`!CGIHTTPRequestHandler` class
128+
and the ``--cgi`` flag from the :program:`python -m http.server`
129+
command-line interface. They were deprecated in Python 3.13.
130+
(Contributed by Bénédikt Tran in :gh:`133810`.)
131+
132+
124133
platform
125134
--------
126135

0 commit comments

Comments
 (0)