From 8d7c2d60c9f30aaed48d27bfe93c01c2b085d8df Mon Sep 17 00:00:00 2001
From: Moul <moul@moul.re>
Date: Wed, 30 Oct 2024 18:26:18 +0100
Subject: [PATCH] Fix API.request_url() with Py3.13 (#208)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Since Python 3.13, the IOBase finalizer now logs
any errors raised by the close() method
https://docs.python.org/3/whatsnew/3.13.html#io

According to
https://docs.python.org/3.13/library/io.html#io.IOBase.close
if the file (descriptor) is accessed after being close,
`ValueError` is raised.

The `fd` is copied with `copy.copy()`.
Both gets automatically closed once the function ends,
the second can’t close a second time the same fd,
that’s why we get this error.

Not sure if there is a use case with the fd copy
Fix: remove fd copy
---
 duniterpy/api/client.py | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/duniterpy/api/client.py b/duniterpy/api/client.py
index ac59069..30ad5f1 100644
--- a/duniterpy/api/client.py
+++ b/duniterpy/api/client.py
@@ -13,7 +13,6 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-import copy
 import json
 import logging
 from http.client import HTTPResponse
@@ -269,7 +268,6 @@ class API:
                 raise ValueError(f"status code != 200 => {response.status} ({content})")
 
             # get response content
-            return_response = copy.copy(response)
             content = response.read().decode("utf-8")
 
         # if schema supplied...
@@ -278,7 +276,7 @@ class API:
             parse_response(content, schema)
 
         # return the chosen type
-        result = return_response  # type: Any
+        result = response  # type: Any
         if rtype == RESPONSE_TEXT:
             result = content
         elif rtype == RESPONSE_JSON:
-- 
GitLab