Skip to content

Commit aabe338

Browse files
authored
Merge branch 'main' into 132413-take2
2 parents c0a27eb + 09b624b commit aabe338

File tree

19 files changed

+437
-67
lines changed

19 files changed

+437
-67
lines changed

Doc/data/stable_abi.dat

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Doc/library/dis.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,7 +1667,7 @@ iterations of the loop.
16671667
* ``oparg == 2``: call :func:`repr` on *value*
16681668
* ``oparg == 3``: call :func:`ascii` on *value*
16691669

1670-
Used for implementing formatted literal strings (f-strings).
1670+
Used for implementing formatted string literals (f-strings).
16711671

16721672
.. versionadded:: 3.13
16731673

@@ -1680,7 +1680,7 @@ iterations of the loop.
16801680
result = value.__format__("")
16811681
STACK.append(result)
16821682

1683-
Used for implementing formatted literal strings (f-strings).
1683+
Used for implementing formatted string literals (f-strings).
16841684

16851685
.. versionadded:: 3.13
16861686

@@ -1693,7 +1693,7 @@ iterations of the loop.
16931693
result = value.__format__(spec)
16941694
STACK.append(result)
16951695

1696-
Used for implementing formatted literal strings (f-strings).
1696+
Used for implementing formatted string literals (f-strings).
16971697

16981698
.. versionadded:: 3.13
16991699

Doc/library/stdtypes.rst

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2456,6 +2456,146 @@ expression support in the :mod:`re` module).
24562456
'-0042'
24572457

24582458

2459+
.. index::
2460+
single: ! formatted string literal
2461+
single: formatted string literals
2462+
single: ! f-string
2463+
single: f-strings
2464+
single: fstring
2465+
single: interpolated string literal
2466+
single: string; formatted literal
2467+
single: string; interpolated literal
2468+
single: {} (curly brackets); in formatted string literal
2469+
single: ! (exclamation mark); in formatted string literal
2470+
single: : (colon); in formatted string literal
2471+
single: = (equals); for help in debugging using string literals
2472+
2473+
Formatted String Literals (f-strings)
2474+
-------------------------------------
2475+
2476+
.. versionadded:: 3.6
2477+
.. versionchanged:: 3.7
2478+
The :keyword:`await` and :keyword:`async for` can be used in expressions
2479+
within f-strings.
2480+
.. versionchanged:: 3.8
2481+
Added the debugging operator (``=``)
2482+
.. versionchanged:: 3.12
2483+
Many restrictions on expressions within f-strings have been removed.
2484+
Notably, nested strings, comments, and backslashes are now permitted.
2485+
2486+
An :dfn:`f-string` (formally a :dfn:`formatted string literal`) is
2487+
a string literal that is prefixed with ``f`` or ``F``.
2488+
This type of string literal allows embedding arbitrary Python expressions
2489+
within *replacement fields*, which are delimited by curly brackets (``{}``).
2490+
These expressions are evaluated at runtime, similarly to :meth:`str.format`,
2491+
and are converted into regular :class:`str` objects.
2492+
For example:
2493+
2494+
.. doctest::
2495+
2496+
>>> who = 'nobody'
2497+
>>> nationality = 'Spanish'
2498+
>>> f'{who.title()} expects the {nationality} Inquisition!'
2499+
'Nobody expects the Spanish Inquisition!'
2500+
2501+
It is also possible to use a multi line f-string:
2502+
2503+
.. doctest::
2504+
2505+
>>> f'''This is a string
2506+
... on two lines'''
2507+
'This is a string\non two lines'
2508+
2509+
A single opening curly bracket, ``'{'``, marks a *replacement field* that
2510+
can contain any Python expression:
2511+
2512+
.. doctest::
2513+
2514+
>>> nationality = 'Spanish'
2515+
>>> f'The {nationality} Inquisition!'
2516+
'The Spanish Inquisition!'
2517+
2518+
To include a literal ``{`` or ``}``, use a double bracket:
2519+
2520+
.. doctest::
2521+
2522+
>>> x = 42
2523+
>>> f'{{x}} is {x}'
2524+
'{x} is 42'
2525+
2526+
Functions can also be used, and :ref:`format specifiers <formatstrings>`:
2527+
2528+
.. doctest::
2529+
2530+
>>> from math import sqrt
2531+
>>> f'√2 \N{ALMOST EQUAL TO} {sqrt(2):.5f}'
2532+
'√2 ≈ 1.41421'
2533+
2534+
Any non-string expression is converted using :func:`str`, by default:
2535+
2536+
.. doctest::
2537+
2538+
>>> from fractions import Fraction
2539+
>>> f'{Fraction(1, 3)}'
2540+
'1/3'
2541+
2542+
To use an explicit conversion, use the ``!`` (exclamation mark) operator,
2543+
followed by any of the valid formats, which are:
2544+
2545+
========== ==============
2546+
Conversion Meaning
2547+
========== ==============
2548+
``!a`` :func:`ascii`
2549+
``!r`` :func:`repr`
2550+
``!s`` :func:`str`
2551+
========== ==============
2552+
2553+
For example:
2554+
2555+
.. doctest::
2556+
2557+
>>> from fractions import Fraction
2558+
>>> f'{Fraction(1, 3)!s}'
2559+
'1/3'
2560+
>>> f'{Fraction(1, 3)!r}'
2561+
'Fraction(1, 3)'
2562+
>>> question = '¿Dónde está el Presidente?'
2563+
>>> print(f'{question!a}')
2564+
'\xbfD\xf3nde est\xe1 el Presidente?'
2565+
2566+
While debugging it may be helpful to see both the expression and its value,
2567+
by using the equals sign (``=``) after the expression.
2568+
This preserves spaces within the brackets, and can be used with a converter.
2569+
By default, the debugging operator uses the :func:`repr` (``!r``) conversion.
2570+
For example:
2571+
2572+
.. doctest::
2573+
2574+
>>> from fractions import Fraction
2575+
>>> calculation = Fraction(1, 3)
2576+
>>> f'{calculation=}'
2577+
'calculation=Fraction(1, 3)'
2578+
>>> f'{calculation = }'
2579+
'calculation = Fraction(1, 3)'
2580+
>>> f'{calculation = !s}'
2581+
'calculation = 1/3'
2582+
2583+
Once the output has been evaluated, it can be formatted using a
2584+
:ref:`format specifier <formatstrings>` following a colon (``':'``).
2585+
After the expression has been evaluated, and possibly converted to a string,
2586+
the :meth:`!__format__` method of the result is called with the format specifier,
2587+
or the empty string if no format specifier is given.
2588+
The formatted result is then used as the final value for the replacement field.
2589+
For example:
2590+
2591+
.. doctest::
2592+
2593+
>>> from fractions import Fraction
2594+
>>> f'{Fraction(1, 7):.6f}'
2595+
'0.142857'
2596+
>>> f'{Fraction(1, 7):_^+10}'
2597+
'___+1/7___'
2598+
24592599

24602600
.. _old-string-formatting:
24612601

Doc/whatsnew/3.14.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,10 @@ mimetypes
910910

911911
(Contributed by Hugo van Kemenade in :gh:`129965`.)
912912

913+
* Add :rfc:`9512` ``application/yaml`` MIME type for YAML files (``.yaml``
914+
and ``.yml``). (Contributed by Sasha "Nelie" Chernykh and Hugo van Kemenade
915+
in :gh:`132056`.)
916+
913917

914918
multiprocessing
915919
---------------

Include/cpython/longobject.h

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,57 +7,6 @@
77

88
PyAPI_FUNC(PyObject*) PyLong_FromUnicodeObject(PyObject *u, int base);
99

10-
#define Py_ASNATIVEBYTES_DEFAULTS -1
11-
#define Py_ASNATIVEBYTES_BIG_ENDIAN 0
12-
#define Py_ASNATIVEBYTES_LITTLE_ENDIAN 1
13-
#define Py_ASNATIVEBYTES_NATIVE_ENDIAN 3
14-
#define Py_ASNATIVEBYTES_UNSIGNED_BUFFER 4
15-
#define Py_ASNATIVEBYTES_REJECT_NEGATIVE 8
16-
#define Py_ASNATIVEBYTES_ALLOW_INDEX 16
17-
18-
/* PyLong_AsNativeBytes: Copy the integer value to a native variable.
19-
buffer points to the first byte of the variable.
20-
n_bytes is the number of bytes available in the buffer. Pass 0 to request
21-
the required size for the value.
22-
flags is a bitfield of the following flags:
23-
* 1 - little endian
24-
* 2 - native endian
25-
* 4 - unsigned destination (e.g. don't reject copying 255 into one byte)
26-
* 8 - raise an exception for negative inputs
27-
* 16 - call __index__ on non-int types
28-
If flags is -1 (all bits set), native endian is used, value truncation
29-
behaves most like C (allows negative inputs and allow MSB set), and non-int
30-
objects will raise a TypeError.
31-
Big endian mode will write the most significant byte into the address
32-
directly referenced by buffer; little endian will write the least significant
33-
byte into that address.
34-
35-
If an exception is raised, returns a negative value.
36-
Otherwise, returns the number of bytes that are required to store the value.
37-
To check that the full value is represented, ensure that the return value is
38-
equal or less than n_bytes.
39-
All n_bytes are guaranteed to be written (unless an exception occurs), and
40-
so ignoring a positive return value is the equivalent of a downcast in C.
41-
In cases where the full value could not be represented, the returned value
42-
may be larger than necessary - this function is not an accurate way to
43-
calculate the bit length of an integer object.
44-
*/
45-
PyAPI_FUNC(Py_ssize_t) PyLong_AsNativeBytes(PyObject* v, void* buffer,
46-
Py_ssize_t n_bytes, int flags);
47-
48-
/* PyLong_FromNativeBytes: Create an int value from a native integer
49-
n_bytes is the number of bytes to read from the buffer. Passing 0 will
50-
always produce the zero int.
51-
PyLong_FromUnsignedNativeBytes always produces a non-negative int.
52-
flags is the same as for PyLong_AsNativeBytes, but only supports selecting
53-
the endianness or forcing an unsigned buffer.
54-
55-
Returns the int object, or NULL with an exception set. */
56-
PyAPI_FUNC(PyObject*) PyLong_FromNativeBytes(const void* buffer, size_t n_bytes,
57-
int flags);
58-
PyAPI_FUNC(PyObject*) PyLong_FromUnsignedNativeBytes(const void* buffer,
59-
size_t n_bytes, int flags);
60-
6110
PyAPI_FUNC(int) PyUnstable_Long_IsCompact(const PyLongObject* op);
6211
PyAPI_FUNC(Py_ssize_t) PyUnstable_Long_CompactValue(const PyLongObject* op);
6312

Include/longobject.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,58 @@ PyAPI_FUNC(int) PyLong_AsInt32(PyObject *obj, int32_t *value);
4040
PyAPI_FUNC(int) PyLong_AsUInt32(PyObject *obj, uint32_t *value);
4141
PyAPI_FUNC(int) PyLong_AsInt64(PyObject *obj, int64_t *value);
4242
PyAPI_FUNC(int) PyLong_AsUInt64(PyObject *obj, uint64_t *value);
43+
44+
#define Py_ASNATIVEBYTES_DEFAULTS -1
45+
#define Py_ASNATIVEBYTES_BIG_ENDIAN 0
46+
#define Py_ASNATIVEBYTES_LITTLE_ENDIAN 1
47+
#define Py_ASNATIVEBYTES_NATIVE_ENDIAN 3
48+
#define Py_ASNATIVEBYTES_UNSIGNED_BUFFER 4
49+
#define Py_ASNATIVEBYTES_REJECT_NEGATIVE 8
50+
#define Py_ASNATIVEBYTES_ALLOW_INDEX 16
51+
52+
/* PyLong_AsNativeBytes: Copy the integer value to a native variable.
53+
buffer points to the first byte of the variable.
54+
n_bytes is the number of bytes available in the buffer. Pass 0 to request
55+
the required size for the value.
56+
flags is a bitfield of the following flags:
57+
* 1 - little endian
58+
* 2 - native endian
59+
* 4 - unsigned destination (e.g. don't reject copying 255 into one byte)
60+
* 8 - raise an exception for negative inputs
61+
* 16 - call __index__ on non-int types
62+
If flags is -1 (all bits set), native endian is used, value truncation
63+
behaves most like C (allows negative inputs and allow MSB set), and non-int
64+
objects will raise a TypeError.
65+
Big endian mode will write the most significant byte into the address
66+
directly referenced by buffer; little endian will write the least significant
67+
byte into that address.
68+
69+
If an exception is raised, returns a negative value.
70+
Otherwise, returns the number of bytes that are required to store the value.
71+
To check that the full value is represented, ensure that the return value is
72+
equal or less than n_bytes.
73+
All n_bytes are guaranteed to be written (unless an exception occurs), and
74+
so ignoring a positive return value is the equivalent of a downcast in C.
75+
In cases where the full value could not be represented, the returned value
76+
may be larger than necessary - this function is not an accurate way to
77+
calculate the bit length of an integer object.
78+
*/
79+
PyAPI_FUNC(Py_ssize_t) PyLong_AsNativeBytes(PyObject* v, void* buffer,
80+
Py_ssize_t n_bytes, int flags);
81+
82+
/* PyLong_FromNativeBytes: Create an int value from a native integer
83+
n_bytes is the number of bytes to read from the buffer. Passing 0 will
84+
always produce the zero int.
85+
PyLong_FromUnsignedNativeBytes always produces a non-negative int.
86+
flags is the same as for PyLong_AsNativeBytes, but only supports selecting
87+
the endianness or forcing an unsigned buffer.
88+
89+
Returns the int object, or NULL with an exception set. */
90+
PyAPI_FUNC(PyObject*) PyLong_FromNativeBytes(const void* buffer, size_t n_bytes,
91+
int flags);
92+
PyAPI_FUNC(PyObject*) PyLong_FromUnsignedNativeBytes(const void* buffer,
93+
size_t n_bytes, int flags);
94+
4395
#endif
4496

4597
PyAPI_FUNC(PyObject *) PyLong_GetInfo(void);

Lib/mimetypes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,8 @@ def _default_mime_types():
544544
'.rdf' : 'application/xml',
545545
'.wsdl' : 'application/xml',
546546
'.xpdl' : 'application/xml',
547+
'.yaml' : 'application/yaml',
548+
'.yml' : 'application/yaml',
547549
'.zip' : 'application/zip',
548550
'.3gp' : 'audio/3gpp',
549551
'.3gpp' : 'audio/3gpp',

Lib/test/test_clinic.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3032,6 +3032,11 @@ def test_bool_converter(self):
30323032
self.assertEqual(ac_tester.bool_converter('', [], 5), (False, False, True))
30333033
self.assertEqual(ac_tester.bool_converter(('not empty',), {1: 2}, 0), (True, True, False))
30343034

3035+
def test_bool_converter_c_default(self):
3036+
self.assertEqual(ac_tester.bool_converter_c_default(), (1, 0, -2, -3))
3037+
self.assertEqual(ac_tester.bool_converter_c_default(False, True, False, True),
3038+
(0, 1, 0, 1))
3039+
30353040
def test_char_converter(self):
30363041
with self.assertRaises(TypeError):
30373042
ac_tester.char_converter(1)

0 commit comments

Comments
 (0)