diff --git a/Doc/c-api/dict.rst b/Doc/c-api/dict.rst index 1d74140ea360ba..734462bc0051af 100644 --- a/Doc/c-api/dict.rst +++ b/Doc/c-api/dict.rst @@ -82,6 +82,9 @@ Dictionary objects Return a new dictionary that contains the same key-value pairs as *p*. + .. versionchanged:: next + If *p* is a subclass of :class:`frozendict`, the result will be a + :class:`frozendict` instance instead of a :class:`dict` instance. .. c:function:: int PyDict_SetItem(PyObject *p, PyObject *key, PyObject *val) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 21572d6dc71d84..4451d485884987 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -2504,6 +2504,19 @@ expression support in the :mod:`re` module). done using the specified *fillchar* (default is an ASCII space). The original string is returned if *width* is less than or equal to ``len(s)``. + For example: + + .. doctest:: + + >>> 'Python'.rjust(10) + ' Python' + >>> 'Python'.rjust(10, '.') + '....Python' + >>> 'Monty Python'.rjust(10, '.') + 'Monty Python' + + See also :meth:`ljust` and :meth:`zfill`. + .. method:: str.rpartition(sep, /) @@ -2828,13 +2841,17 @@ expression support in the :mod:`re` module). than before. The original string is returned if *width* is less than or equal to ``len(s)``. - For example:: + For example: + + .. doctest:: >>> "42".zfill(5) '00042' >>> "-42".zfill(5) '-0042' + See also :meth:`rjust`. + .. index:: single: ! formatted string literal diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index a0e396983885d2..ef44701bb251dd 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -3353,8 +3353,8 @@ Introspection helpers .. function:: get_type_hints(obj, globalns=None, localns=None, include_extras=False) - Return a dictionary containing type hints for a function, method, module - or class object. + Return a dictionary containing type hints for a function, method, module, + class object, or other callable object. This is often the same as ``obj.__annotations__``, but this function makes the following changes to the annotations dictionary: @@ -3395,6 +3395,13 @@ Introspection helpers :ref:`type aliases ` that include forward references, or with names imported under :data:`if TYPE_CHECKING `. + .. note:: + + Calling :func:`get_type_hints` on an instance is not supported. + To retrieve annotations for an instance, call + :func:`get_type_hints` on the instance's class instead + (for example, ``get_type_hints(type(obj))``). + .. versionchanged:: 3.9 Added ``include_extras`` parameter as part of :pep:`593`. See the documentation on :data:`Annotated` for more information. @@ -3404,6 +3411,11 @@ Introspection helpers if a default value equal to ``None`` was set. Now the annotation is returned unchanged. + .. versionchanged:: 3.14 + Calling :func:`get_type_hints` on instances is no longer supported. + Some instances were accepted in earlier versions as an undocumented + implementation detail. + .. function:: get_origin(tp) Get the unsubscripted version of a type: for a typing object of the form diff --git a/Lib/test/test_unicodedata.py b/Lib/test/test_unicodedata.py index 30a26751d3ac54..8ecb0df2f8e5dd 100644 --- a/Lib/test/test_unicodedata.py +++ b/Lib/test/test_unicodedata.py @@ -12,7 +12,9 @@ import sys import unicodedata import unittest +import weakref from test.support import ( + gc_collect, open_urlresource, requires_resource, script_helper, @@ -1338,6 +1340,28 @@ def run_grapheme_break_tests(self, testdata): self.assertEqual([x.start for x in result], breaks[i:-1], comment) self.assertEqual([x.end for x in result], breaks[i+1:], comment) + def test_reference_loops(self): + # Test that reference loops involving GraphemeBreakIterator or + # Segment can be broken by the garbage collector. + class S(str): + pass + + s = S('abc') + s.ref = unicodedata.iter_graphemes(s) + wr = weakref.ref(s) + del s + self.assertIsNotNone(wr()) + gc_collect() + self.assertIsNone(wr()) + + s = S('abc') + s.ref = next(unicodedata.iter_graphemes(s)) + wr = weakref.ref(s) + del s + self.assertIsNotNone(wr()) + gc_collect() + self.assertIsNone(wr()) + if __name__ == "__main__": unittest.main() diff --git a/Modules/unicodedata.c b/Modules/unicodedata.c index 401f64e7416944..2c67c23d98ed81 100644 --- a/Modules/unicodedata.c +++ b/Modules/unicodedata.c @@ -1925,13 +1925,6 @@ Segment_traverse(PyObject *self, visitproc visit, void *arg) return 0; } -static int -Segment_clear(PyObject *self) -{ - Py_CLEAR(((SegmentObject *)self)->string); - return 0; -} - static PyObject * Segment_str(PyObject *self) { @@ -1947,9 +1940,9 @@ Segment_repr(PyObject *self) } static PyMemberDef Segment_members[] = { - {"start", Py_T_PYSSIZET, offsetof(SegmentObject, start), 0, + {"start", Py_T_PYSSIZET, offsetof(SegmentObject, start), Py_READONLY, PyDoc_STR("grapheme start")}, - {"end", Py_T_PYSSIZET, offsetof(SegmentObject, end), 0, + {"end", Py_T_PYSSIZET, offsetof(SegmentObject, end), Py_READONLY, PyDoc_STR("grapheme end")}, {NULL} /* Sentinel */ }; @@ -1957,7 +1950,6 @@ static PyMemberDef Segment_members[] = { static PyType_Slot Segment_slots[] = { {Py_tp_dealloc, Segment_dealloc}, {Py_tp_traverse, Segment_traverse}, - {Py_tp_clear, Segment_clear}, {Py_tp_str, Segment_str}, {Py_tp_repr, Segment_repr}, {Py_tp_members, Segment_members}, @@ -2001,13 +1993,6 @@ GBI_traverse(PyObject *self, visitproc visit, void *arg) return 0; } -static int -GBI_clear(PyObject *self) -{ - Py_CLEAR(((GraphemeBreakIterator *)self)->iter.str); - return 0; -} - static PyObject * GBI_iternext(PyObject *self) { @@ -2038,7 +2023,6 @@ static PyType_Slot GraphemeBreakIterator_slots[] = { {Py_tp_iter, PyObject_SelfIter}, {Py_tp_iternext, GBI_iternext}, {Py_tp_traverse, GBI_traverse}, - {Py_tp_clear, GBI_clear}, {0, 0}, };