@@ -307,6 +307,86 @@ Other language changes
307307 not only integers or floats, although this does not improve precision.
308308 (Contributed by Serhiy Storchaka in :gh: `67795 `.)
309309
310+ .. _whatsnew315-bytearray-take-bytes :
311+
312+ * Added :meth: `bytearray.take_bytes(n=None, /) <bytearray.take_bytes> ` to take
313+ bytes out of a :class: `bytearray ` without copying. This enables optimizing code
314+ which must return :class: `bytes ` after working with a mutable buffer of bytes
315+ such as data buffering, network protocol parsing, encoding, decoding,
316+ and compression. Common code patterns which can be optimized with
317+ :func: `~bytearray.take_bytes ` are listed below.
318+
319+ (Contributed by Cody Maloney in :gh: `139871 `.)
320+
321+ .. list-table :: Suggested Optimizing Refactors
322+ :header-rows: 1
323+
324+ * - Description
325+ - Old
326+ - New
327+
328+ * - Return :class: `bytes ` after working with :class: `bytearray `
329+ - .. code:: python
330+
331+ def read() -> bytes:
332+ buffer = bytearray(1024)
333+ ...
334+ return bytes(buffer)
335+
336+ - .. code:: python
337+
338+ def read() -> bytes:
339+ buffer = bytearray(1024)
340+ ...
341+ return buffer.take_bytes()
342+
343+ * - Empty a buffer getting the bytes
344+ - .. code:: python
345+
346+ buffer = bytearray(1024)
347+ ...
348+ data = bytes(buffer)
349+ buffer.clear()
350+
351+ - .. code:: python
352+
353+ buffer = bytearray(1024)
354+ ...
355+ data = buffer.take_bytes()
356+
357+ * - Split a buffer at a specific separator
358+ - .. code:: python
359+
360+ buffer = bytearray(b'abc\n def')
361+ n = buffer.find(b'\n ')
362+ data = bytes(buffer[:n + 1])
363+ del buffer[:n + 1]
364+ assert data == b'abc'
365+ assert buffer == bytearray(b'def')
366+
367+ - .. code:: python
368+
369+ buffer = bytearray(b'abc\n def')
370+ n = buffer.find(b'\n ')
371+ data = buffer.take_bytes(n + 1)
372+
373+ * - Split a buffer at a specific separator; discard after the separator
374+ - .. code:: python
375+
376+ buffer = bytearray(b'abc\n def')
377+ n = buffer.find(b'\n ')
378+ data = bytes(buffer[:n])
379+ buffer.clear()
380+ assert data == b'abc'
381+ assert len(buffer) == 0
382+
383+ - .. code:: python
384+
385+ buffer = bytearray(b'abc\n def')
386+ n = buffer.find(b'\n ')
387+ buffer.resize(n)
388+ data = buffer.take_bytes()
389+
310390* Many functions related to compiling or parsing Python code, such as
311391 :func: `compile `, :func: `ast.parse `, :func: `symtable.symtable `,
312392 and :func: `importlib.abc.InspectLoader.source_to_code `, now allow to pass
@@ -418,6 +498,14 @@ difflib
418498 (Contributed by Jiahao Li in :gh: `134580 `.)
419499
420500
501+ functools
502+ ---------
503+
504+ * :func: `~functools.singledispatchmethod ` now supports non-:term: `descriptor `
505+ callables.
506+ (Contributed by Serhiy Storchaka in :gh: `140873 `.)
507+
508+
421509hashlib
422510-------
423511
@@ -986,6 +1074,16 @@ New features
9861074* Add :c:func: `PyTuple_FromArray ` to create a :class: `tuple ` from an array.
9871075 (Contributed by Victor Stinner in :gh: `111489 `.)
9881076
1077+ * Add :c:func: `PyUnstable_ThreadState_SetStackProtection ` and
1078+ :c:func: `PyUnstable_ThreadState_ResetStackProtection ` functions to set
1079+ the stack protection base address and stack protection size of a Python
1080+ thread state.
1081+ (Contributed by Victor Stinner in :gh: `139653 `.)
1082+
1083+ * Add a new :c:func: `PyImport_CreateModuleFromInitfunc ` C-API for creating
1084+ a module from a *spec * and *initfunc *.
1085+ (Contributed by Itamar Oren in :gh: `116146 `.)
1086+
9891087
9901088Changed C APIs
9911089--------------
@@ -1150,3 +1248,9 @@ that may require changes to your code.
11501248
11511249* :meth: `~mmap.mmap.resize ` has been removed on platforms that don't support the
11521250 underlying syscall, instead of raising a :exc: `SystemError `.
1251+
1252+ * Resource warning is now emitted for unclosed
1253+ :func: `xml.etree.ElementTree.iterparse ` iterator if it opened a file.
1254+ Use its :meth: `!close ` method or the :func: `contextlib.closing ` context
1255+ manager to close it.
1256+ (Contributed by Osama Abdelkader and Serhiy Storchaka in :gh: `140601 `.)
0 commit comments