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
31 changes: 24 additions & 7 deletions Doc/library/faulthandler.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ tracebacks:
* Each string is limited to 500 characters.
* Only the filename, the function name and the line number are
displayed. (no source code)
* It is limited to 100 frames and 100 threads.
* It is limited to 100 frames per thread, and 100 threads
(configurable via *max_threads*).
* The order is reversed: the most recent call is shown first.

By default, the Python traceback is written to :data:`sys.stderr`. To see
Expand All @@ -55,16 +56,20 @@ at Python startup.
Dumping the traceback
---------------------

.. function:: dump_traceback(file=sys.stderr, all_threads=True)
.. function:: dump_traceback(file=sys.stderr, all_threads=True, *, max_threads=100)

Dump the tracebacks of all threads into *file*. If *all_threads* is
``False``, dump only the current thread.
``False``, dump only the current thread. *max_threads* caps the number
of threads dumped.

.. seealso:: :func:`traceback.print_tb`, which can be used to print a traceback object.

.. versionchanged:: 3.5
Added support for passing file descriptor to this function.

.. versionchanged:: next
Added the *max_threads* keyword argument.


Dumping the C stack
-------------------
Expand Down Expand Up @@ -100,7 +105,7 @@ instead of the stack, even if the operating system supports dumping stacks.
Fault handler state
-------------------

.. function:: enable(file=sys.stderr, all_threads=True, c_stack=True)
.. function:: enable(file=sys.stderr, all_threads=True, c_stack=True, *, max_threads=100)

Enable the fault handler: install handlers for the :const:`~signal.SIGSEGV`,
:const:`~signal.SIGFPE`, :const:`~signal.SIGABRT`, :const:`~signal.SIGBUS`
Expand All @@ -116,6 +121,8 @@ Fault handler state
traceback, unless the system does not support it. See :func:`dump_c_stack` for
more information on compatibility.

*max_threads* caps the number of threads dumped when a fatal signal fires.

.. versionchanged:: 3.5
Added support for passing file descriptor to this function.

Expand All @@ -133,6 +140,9 @@ Fault handler state
.. versionchanged:: 3.14
The dump now displays the C stack trace if *c_stack* is true.

.. versionchanged:: next
Added the *max_threads* keyword argument.

.. function:: disable()

Disable the fault handler: uninstall the signal handlers installed by
Expand All @@ -146,15 +156,15 @@ Fault handler state
Dumping the tracebacks after a timeout
--------------------------------------

.. function:: dump_traceback_later(timeout, repeat=False, file=sys.stderr, exit=False)
.. function:: dump_traceback_later(timeout, repeat=False, file=sys.stderr, exit=False, *, max_threads=100)

Dump the tracebacks of all threads, after a timeout of *timeout* seconds, or
every *timeout* seconds if *repeat* is ``True``. If *exit* is ``True``, call
:c:func:`!_exit` with status=1 after dumping the tracebacks. (Note
:c:func:`!_exit` exits the process immediately, which means it doesn't do any
cleanup like flushing file buffers.) If the function is called twice, the new
call replaces previous parameters and resets the timeout. The timer has a
sub-second resolution.
sub-second resolution. *max_threads* caps the number of threads dumped.

The *file* must be kept open until the traceback is dumped or
:func:`cancel_dump_traceback_later` is called: see :ref:`issue with file
Expand All @@ -168,6 +178,9 @@ Dumping the tracebacks after a timeout
.. versionchanged:: 3.7
This function is now always available.

.. versionchanged:: next
Added the *max_threads* keyword argument.

.. function:: cancel_dump_traceback_later()

Cancel the last call to :func:`dump_traceback_later`.
Expand All @@ -176,11 +189,12 @@ Dumping the tracebacks after a timeout
Dumping the traceback on a user signal
--------------------------------------

.. function:: register(signum, file=sys.stderr, all_threads=True, chain=False)
.. function:: register(signum, file=sys.stderr, all_threads=True, chain=False, *, max_threads=100)

Register a user signal: install a handler for the *signum* signal to dump
the traceback of all threads, or of the current thread if *all_threads* is
``False``, into *file*. Call the previous handler if chain is ``True``.
*max_threads* caps the number of threads dumped.

The *file* must be kept open until the signal is unregistered by
:func:`unregister`: see :ref:`issue with file descriptors <faulthandler-fd>`.
Expand All @@ -190,6 +204,9 @@ Dumping the traceback on a user signal
.. versionchanged:: 3.5
Added support for passing file descriptor to this function.

.. versionchanged:: next
Added the *max_threads* keyword argument.

.. function:: unregister(signum)

Unregister a user signal: uninstall the handler of the *signum* signal
Expand Down
2 changes: 1 addition & 1 deletion Doc/library/functools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ The :mod:`!functools` module defines the following functions:

Roughly equivalent to::

initial_missing = object()
initial_missing = sentinel('initial_missing')

def reduce(function, iterable, /, initial=initial_missing):
it = iter(iterable)
Expand Down
20 changes: 20 additions & 0 deletions Doc/library/http.server.rst
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,14 @@ instantiation, of which this module provides three different variants:
This will be ``"SimpleHTTP/" + __version__``, where ``__version__`` is
defined at the module level.

.. attribute:: default_content_type

Specifies the Content-Type header value sent when the MIME type
cannot be guessed from the file extension of the requested URL.
By default, it is set to ``'application/octet-stream'``.

.. versionadded:: next

.. attribute:: extensions_map

A dictionary mapping suffixes into MIME types, contains custom overrides
Expand Down Expand Up @@ -528,6 +536,18 @@ The following options are accepted:

.. versionadded:: 3.11

.. option:: --content-type <content_type>

Specifies the default Content-Type HTTP header used when the MIME type
cannot be guessed from the URL's file extension. By default, the server
uses ``'application/octet-stream'``:

.. code-block:: bash

python -m http.server --content-type text/html

.. versionadded:: next

.. option:: --tls-cert

Specifies a TLS certificate chain for HTTPS connections:
Expand Down
6 changes: 6 additions & 0 deletions Doc/library/tomllib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ support writing TOML.
Added TOML 1.1.0 support.
See the :ref:`What's New <whatsnew315-tomllib-1-1-0>` for details.

.. warning::

Be cautious when parsing data from untrusted sources.
A malicious TOML string may cause the decoder to consume considerable
CPU and memory resources.
Limiting the size of data to be parsed is recommended.

.. seealso::

Expand Down
15 changes: 15 additions & 0 deletions Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,15 @@ difflib
(Contributed by Jiahao Li in :gh:`134580`.)


faulthandler
------------

* Added the *max_threads* parameter in :func:`faulthandler.enable`,
:func:`faulthandler.dump_traceback`, :func:`faulthandler.dump_traceback_later`,
and :func:`faulthandler.register`.
(Contributed by Eric Froemling in :gh:`149085`.)


functools
---------

Expand Down Expand Up @@ -955,6 +964,12 @@ http.server
<using-on-controlling-color>`.
(Contributed by Hugo van Kemenade in :gh:`146292`.)

* Added :attr:`~http.server.SimpleHTTPRequestHandler.default_content_type`
and the :option:`--content-type <http.server --content-type>` command-line
option to allow customizing the default ``Content-Type`` header
for files with unknown extensions.
(Contributed by John Comeau and Hugo van Kemenade in :gh:`113471`.)


inspect
-------
Expand Down
2 changes: 1 addition & 1 deletion Include/Python.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ __pragma(warning(disable: 4201))
#include "cpython/genobject.h"
#include "descrobject.h"
#include "genericaliasobject.h"
#include "sentinelobject.h"
#include "cpython/sentinelobject.h"
#include "warnings.h"
#include "weakrefobject.h"
#include "structseq.h"
Expand Down
4 changes: 2 additions & 2 deletions Include/sentinelobject.h → Include/cpython/sentinelobject.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
/* Sentinel object interface */

#ifndef Py_LIMITED_API
#ifndef Py_SENTINELOBJECT_H
#define Py_SENTINELOBJECT_H
#ifdef __cplusplus
extern "C" {
#endif

#ifndef Py_LIMITED_API
PyAPI_DATA(PyTypeObject) PySentinel_Type;

#define PySentinel_Check(op) Py_IS_TYPE((op), &PySentinel_Type)

PyAPI_FUNC(PyObject *) PySentinel_New(
const char *name,
const char *module_name);
#endif

#ifdef __cplusplus
}
#endif
#endif /* !Py_SENTINELOBJECT_H */
#endif /* !Py_LIMITED_API */
3 changes: 3 additions & 0 deletions Include/internal/pycore_faulthandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct faulthandler_user_signal {
int chain;
_Py_sighandler_t previous;
PyInterpreterState *interp;
Py_ssize_t max_threads;
};
#endif /* FAULTHANDLER_USER */

Expand All @@ -57,6 +58,7 @@ struct _faulthandler_runtime_state {
void *exc_handler;
#endif
int c_stack;
Py_ssize_t max_threads;
} fatal_error;

struct {
Expand All @@ -68,6 +70,7 @@ struct _faulthandler_runtime_state {
int exit;
char *header;
size_t header_len;
Py_ssize_t max_threads;
/* The main thread always holds this lock. It is only released when
faulthandler_thread() is interrupted before this thread exits, or at
Python exit. */
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_global_objects_fini_generated.h

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

1 change: 1 addition & 0 deletions Include/internal/pycore_global_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ struct _Py_global_strings {
STRUCT_FOR_ID(mask)
STRUCT_FOR_ID(match)
STRUCT_FOR_ID(max_length)
STRUCT_FOR_ID(max_threads)
STRUCT_FOR_ID(maxdigits)
STRUCT_FOR_ID(maxevents)
STRUCT_FOR_ID(maxlen)
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_runtime_init_generated.h

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

3 changes: 2 additions & 1 deletion Include/internal/pycore_traceback.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ extern void _Py_DumpTraceback(
extern const char* _Py_DumpTracebackThreads(
int fd,
PyInterpreterState *interp,
PyThreadState *current_tstate);
PyThreadState *current_tstate,
Py_ssize_t max_threads);

/* Write a Unicode object into the file descriptor fd. Encode the string to
ASCII using the backslashreplace error handler.
Expand Down
4 changes: 4 additions & 0 deletions Include/internal/pycore_unicodeobject_generated.h

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

8 changes: 3 additions & 5 deletions Lib/email/_header_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2063,12 +2063,10 @@ def get_address_list(value):
address_list.defects.append(errors.InvalidHeaderDefect(
"invalid address in address-list"))
if value and value[0] != ',':
# Crap after address; treat it as an invalid mailbox.
# The mailbox info will still be available.
mailbox = address_list[-1][0]
mailbox.token_type = 'invalid-mailbox'
# Crap after address: add it to the address list
# as an invalid mailbox
token, value = get_invalid_mailbox(value, ',')
mailbox.extend(token)
address_list.append(Address([token]))
address_list.defects.append(errors.InvalidHeaderDefect(
"invalid address in address-list"))
if value: # Must be a , at this point.
Expand Down
12 changes: 10 additions & 2 deletions Lib/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,7 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
"""

server_version = "SimpleHTTP"
default_content_type = "application/octet-stream"
index_pages = ("index.html", "index.htm")
extensions_map = _encodings_map_default = {
'.gz': 'application/gzip',
Expand Down Expand Up @@ -974,7 +975,7 @@ def guess_type(self, path):
guess, _ = mimetypes.guess_file_type(path)
if guess:
return guess
return 'application/octet-stream'
return self.default_content_type


nobody = None
Expand Down Expand Up @@ -1010,9 +1011,10 @@ def _get_best_family(*address):
return family, sockaddr


def test(HandlerClass=BaseHTTPRequestHandler,
def test(HandlerClass=SimpleHTTPRequestHandler,
ServerClass=ThreadingHTTPServer,
protocol="HTTP/1.0", port=8000, bind=None,
content_type=SimpleHTTPRequestHandler.default_content_type,
tls_cert=None, tls_key=None, tls_password=None):
"""Test the HTTP request handler class.

Expand All @@ -1021,6 +1023,7 @@ def test(HandlerClass=BaseHTTPRequestHandler,
"""
ServerClass.address_family, addr = _get_best_family(bind, port)
HandlerClass.protocol_version = protocol
HandlerClass.default_content_type = content_type

if tls_cert:
server = ServerClass(addr, HandlerClass, certfile=tls_cert,
Expand Down Expand Up @@ -1060,6 +1063,10 @@ def _main(args=None):
default='HTTP/1.0',
help='conform to this HTTP version '
'(default: %(default)s)')
parser.add_argument('--content-type',
default=SimpleHTTPRequestHandler.default_content_type,
help='default content type for unknown extensions '
'(default: %(default)s)')
parser.add_argument('--tls-cert', metavar='PATH',
help='path to the TLS certificate chain file')
parser.add_argument('--tls-key', metavar='PATH',
Expand Down Expand Up @@ -1112,6 +1119,7 @@ class HTTPSDualStackServer(DualStackServerMixin, ThreadingHTTPSServer):
port=args.port,
bind=args.bind,
protocol=args.protocol,
content_type=args.content_type,
tls_cert=args.tls_cert,
tls_key=args.tls_key,
tls_password=tls_key_password,
Expand Down
Loading
Loading