From 3dead9d4724e7eedfd24da2bb79c0ceb5499766b Mon Sep 17 00:00:00 2001 From: Kevin Kirsche Date: Mon, 20 Jun 2022 09:00:59 -0400 Subject: [PATCH] Use yield from instead of looping yield This merge request updates the stream yield functionality to use `yield from` instead of `yield` within a for loop. Because this is yielding from an iterable, we can use `yield from` which is not only slightly shorter but also on average 15% more performant than using `yield` inside of a loop. This is a result of some of the optimizations included as part of [PEP 380](https://peps.python.org/pep-0380/) --- requests/models.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/requests/models.py b/requests/models.py index 7e1522837f..3cd49f5bba 100644 --- a/requests/models.py +++ b/requests/models.py @@ -813,8 +813,7 @@ def generate(): # Special case for urllib3. if hasattr(self.raw, "stream"): try: - for chunk in self.raw.stream(chunk_size, decode_content=True): - yield chunk + yield from self.raw.stream(chunk_size, decode_content=True) except ProtocolError as e: raise ChunkedEncodingError(e) except DecodeError as e: