File 99366-patch.dict-can-decorate-async.patch of Package python3.41872

From 1c57ada66c9f0b74f2f8082d95a387639a5f9e68 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mat=C4=9Bj=20Cepl?= <mcepl@cepl.eu>
Date: Thu, 4 Apr 2024 01:05:40 +0200
Subject: [PATCH] make sure ``patch.dict()`` can be applied on async functions

Code is from gh#python/cpython@88b101ff520, it was released
upstream in 3.10.9.

Fixes: gh#98086
Patch: 99366-patch.dict-can-decorate-async.patch
---
 Lib/unittest/mock.py                                                   |   18 ++++++++++
 Misc/NEWS.d/next/Library/2022-10-08-19-39-27.gh-issue-98086.y---WC.rst |    1 
 2 files changed, 19 insertions(+)
 create mode 100644 Misc/NEWS.d/next/Library/2022-10-08-19-39-27.gh-issue-98086.y---WC.rst

Index: Python-3.6.15/Lib/unittest/mock.py
===================================================================
--- Python-3.6.15.orig/Lib/unittest/mock.py	2021-09-04 05:49:41.000000000 +0200
+++ Python-3.6.15/Lib/unittest/mock.py	2025-11-14 00:50:38.726652198 +0100
@@ -1595,6 +1595,12 @@
     def __call__(self, f):
         if isinstance(f, type):
             return self.decorate_class(f)
+        if inspect.iscoroutinefunction(f):
+            return self.decorate_async_callable(f)
+        return self.decorate_callable(f)
+
+
+    def decorate_callable(self, f):
         @wraps(f)
         def _inner(*args, **kw):
             self._patch_dict()
@@ -1603,6 +1609,18 @@
             finally:
                 self._unpatch_dict()
 
+        return _inner
+
+
+    def decorate_async_callable(self, f):
+        @wraps(f)
+        async def _inner(*args, **kw):
+            self._patch_dict()
+            try:
+                return await f(*args, **kw)
+            finally:
+                self._unpatch_dict()
+
         return _inner
 
 
Index: Python-3.6.15/Misc/NEWS.d/next/Library/2022-10-08-19-39-27.gh-issue-98086.y---WC.rst
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ Python-3.6.15/Misc/NEWS.d/next/Library/2022-10-08-19-39-27.gh-issue-98086.y---WC.rst	2025-11-14 00:50:38.726974659 +0100
@@ -0,0 +1 @@
+Make sure ``patch.dict()`` can be applied on async functions.
openSUSE Build Service is sponsored by