From 4d2955bb777628800a01666a6b1506a2ddd00fa5 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Mon, 18 Oct 2021 16:24:43 -0700 Subject: [PATCH 1/3] only call read on sync responses in ContentDecodePolicy --- .../azure-core/azure/core/pipeline/policies/_universal.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sdk/core/azure-core/azure/core/pipeline/policies/_universal.py b/sdk/core/azure-core/azure/core/pipeline/policies/_universal.py index 178db95a7dd7..d4d257ffb979 100644 --- a/sdk/core/azure-core/azure/core/pipeline/policies/_universal.py +++ b/sdk/core/azure-core/azure/core/pipeline/policies/_universal.py @@ -28,6 +28,7 @@ """ from __future__ import absolute_import # we have a "requests" module that conflicts with "requests" on Py2.7 import json +import inspect import logging import os import platform @@ -617,10 +618,10 @@ def deserialize_from_http_generics( mime_type = "application/json" # Rely on transport implementation to give me "text()" decoded correctly - if hasattr(response, "read"): + if hasattr(response, "read") and not inspect.iscoroutinefunction(response.read): # since users can call deserialize_from_http_generics by themselves # we want to make sure our new responses are read before we try to - # deserialize + # deserialize. Only read sync responses since we're in a sync function response.read() return cls.deserialize_from_text(response.text(encoding), mime_type, response=response) From b518af8d786a6d8d3d8a6ed26619a73a7aa1d86d Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Mon, 18 Oct 2021 16:27:59 -0700 Subject: [PATCH 2/3] update changelog --- sdk/core/azure-core/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/sdk/core/azure-core/CHANGELOG.md b/sdk/core/azure-core/CHANGELOG.md index f17c5a6d40ee..974a5a3c5e7d 100644 --- a/sdk/core/azure-core/CHANGELOG.md +++ b/sdk/core/azure-core/CHANGELOG.md @@ -9,6 +9,7 @@ ### Bugs Fixed - respect text encoding specified in argument (thanks to @ryohji for the contribution) #20796 +- Fix "coroutine x.read() was never awaited" warning from `ContentDecodePolicy` #21318 ### Other Changes From b208403a7821fc9163ac38a97041fc3fa91517f8 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Tue, 26 Oct 2021 14:18:41 -0700 Subject: [PATCH 3/3] allow to work in 2.7 --- .../azure/core/pipeline/policies/_universal.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/sdk/core/azure-core/azure/core/pipeline/policies/_universal.py b/sdk/core/azure-core/azure/core/pipeline/policies/_universal.py index d4d257ffb979..f329dc9914f2 100644 --- a/sdk/core/azure-core/azure/core/pipeline/policies/_universal.py +++ b/sdk/core/azure-core/azure/core/pipeline/policies/_universal.py @@ -618,11 +618,18 @@ def deserialize_from_http_generics( mime_type = "application/json" # Rely on transport implementation to give me "text()" decoded correctly - if hasattr(response, "read") and not inspect.iscoroutinefunction(response.read): - # since users can call deserialize_from_http_generics by themselves - # we want to make sure our new responses are read before we try to - # deserialize. Only read sync responses since we're in a sync function - response.read() + if hasattr(response, "read"): + try: + # since users can call deserialize_from_http_generics by themselves + # we want to make sure our new responses are read before we try to + # deserialize. Only read sync responses since we're in a sync function + if not inspect.iscoroutinefunction(response.read): + response.read() + except AttributeError: + # raises an AttributeError in 2.7 bc inspect.iscoroutinefunction was added in 3.5 + # Entering here means it's 2.7 and that the response has a read method, so we read + # bc it will be sync. + response.read() return cls.deserialize_from_text(response.text(encoding), mime_type, response=response) def on_request(self, request):