6d11ee5
From 8462d7cc42f64c4584661188966b14f905aa3da1 Mon Sep 17 00:00:00 2001
6d11ee5
From: =?UTF-8?q?Robert-Andr=C3=A9=20Mauchin?= <zebob.m@gmail.com>
6d11ee5
Date: Mon, 14 Feb 2022 00:40:44 +0100
6d11ee5
Subject: [PATCH] Use CodeType.replace() in copycode for Python > 3.8
6d11ee5
6d11ee5
Fix #135
6d11ee5
---
6d11ee5
 automat/_introspection.py | 34 ++++++++++++++++++----------------
6d11ee5
 1 file changed, 18 insertions(+), 16 deletions(-)
6d11ee5
6d11ee5
diff --git a/automat/_introspection.py b/automat/_introspection.py
6d11ee5
index 3f7307d..0ce9037 100644
6d11ee5
--- a/automat/_introspection.py
6d11ee5
+++ b/automat/_introspection.py
6d11ee5
@@ -6,22 +6,24 @@ from types import CodeType as code, FunctionType as function
6d11ee5
 
6d11ee5
 
6d11ee5
 def copycode(template, changes):
6d11ee5
-    names = [
6d11ee5
-        "argcount", "nlocals", "stacksize", "flags", "code", "consts",
6d11ee5
-        "names", "varnames", "filename", "name", "firstlineno", "lnotab",
6d11ee5
-        "freevars", "cellvars"
6d11ee5
-    ]
6d11ee5
-    if hasattr(code, "co_kwonlyargcount"):
6d11ee5
-        names.insert(1, "kwonlyargcount")
6d11ee5
-    if hasattr(code, "co_posonlyargcount"):
6d11ee5
-        # PEP 570 added "positional only arguments"
6d11ee5
-        names.insert(1, "posonlyargcount")
6d11ee5
-    values = [
6d11ee5
-        changes.get(name, getattr(template, "co_" + name))
6d11ee5
-        for name in names
6d11ee5
-    ]
6d11ee5
-    return code(*values)
6d11ee5
-
6d11ee5
+    if hasattr(code, "replace"):
6d11ee5
+        return template.replace(**{"co_" + k : v for k, v in changes.items()})
6d11ee5
+    else:
6d11ee5
+        names = [
6d11ee5
+            "argcount", "nlocals", "stacksize", "flags", "code", "consts",
6d11ee5
+            "names", "varnames", "filename", "name", "firstlineno", "lnotab",
6d11ee5
+            "freevars", "cellvars"
6d11ee5
+        ]
6d11ee5
+        if hasattr(code, "co_kwonlyargcount"):
6d11ee5
+            names.insert(1, "kwonlyargcount")
6d11ee5
+        if hasattr(code, "co_posonlyargcount"):
6d11ee5
+            # PEP 570 added "positional only arguments"
6d11ee5
+            names.insert(1, "posonlyargcount")
6d11ee5
+        values = [
6d11ee5
+            changes.get(name, getattr(template, "co_" + name))
6d11ee5
+            for name in names
6d11ee5
+        ]
6d11ee5
+        return code(*values)
6d11ee5
 
6d11ee5
 
6d11ee5
 def copyfunction(template, funcchanges, codechanges):
6d11ee5
-- 
6d11ee5
2.34.1
6d11ee5