1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
From 4436f5d6c950bb1fe214ff38354e90d012bbc4f0 Mon Sep 17 00:00:00 2001
Origin: https://github.com/numba/numba/pull/8590
From: Siu Kwan Lam <1929845+sklam@users.noreply.github.com>
Date: Thu, 17 Nov 2022 16:16:24 -0600
Subject: [PATCH 08/14] Remove duplicated handling of CodeType.
Make new module for codetype handling across different python versions.
---
numba/core/overload_glue.py | 29 +++++++------------------
numba/misc/codetype.py | 41 ++++++++++++++++++++++++++++++++++++
numba/tests/test_analysis.py | 28 ++----------------------
numba/tests/test_parfors.py | 30 ++++----------------------
4 files changed, 55 insertions(+), 73 deletions(-)
create mode 100644 numba/misc/codetype.py
--- a/numba/core/overload_glue.py
+++ b/numba/core/overload_glue.py
@@ -42,27 +42,14 @@
co_args = [co_argcount]
additional_co_nlocals = len(varnames)
- from numba.core import utils
- if utils.PYVERSION >= (3, 8):
- co_args.append(stub_code.co_posonlyargcount)
- co_args.append(stub_code.co_kwonlyargcount)
- co_args.extend([stub_code.co_nlocals + additional_co_nlocals,
- stub_code.co_stacksize,
- stub_code.co_flags,
- stub_code.co_code,
- stub_code.co_consts,
- stub_code.co_names,
- tuple(new_varnames),
- stub_code.co_filename,
- stub_code.co_name,
- stub_code.co_firstlineno,
- stub_code.co_lnotab,
- stub_code.co_freevars,
- stub_code.co_cellvars
- ])
-
- new_code = pytypes.CodeType(*co_args)
+ from numba.misc import codetype
+ new_code = codetype.copy_code_type(
+ stub_code,
+ argcount=co_argcount,
+ nlocals=stub_code.co_nlocals + additional_co_nlocals,
+ varnames=tuple(new_varnames),
+ )
# get function
new_func = pytypes.FunctionType(new_code, {'body': body_func})
return new_func
--- /dev/null
+++ b/numba/misc/codetype.py
@@ -0,0 +1,41 @@
+import types
+
+from numba.core import utils
+
+def copy_code_type(code: types.CodeType, **kwargs) -> types.CodeType:
+ """Copy CodeType with mutations.
+
+ Parameters
+ ----------
+ code: CodeType
+ **kwargs:
+ Attributes to mutate. Only support "argcount", "nlocals", "codestring",
+ "constants", "names", and "varnames". These names matches argument in
+ CodeType. See CodeType docstring for details.
+ """
+ co_args = [kwargs.get('argcount', code.co_argcount)]
+ if utils.PYVERSION >= (3, 8):
+ co_args.append(code.co_posonlyargcount)
+ co_args.append(code.co_kwonlyargcount)
+ co_args.extend([
+ kwargs.get('nlocals', code.co_nlocals),
+ code.co_stacksize,
+ code.co_flags,
+ kwargs.get('codestring', code.co_code),
+ kwargs.get('constants', code.co_consts),
+ kwargs.get('names', code.co_names),
+ kwargs.get('varnames', code.co_varnames),
+ code.co_filename,
+ code.co_name,
+ ])
+ if utils.PYVERSION >= (3, 11):
+ co_args.append(code.co_qualname)
+ co_args.extend([code.co_firstlineno,
+ code.co_lnotab])
+ if utils.PYVERSION >= (3, 11):
+ co_args.append(code.co_exceptiontable)
+ co_args.extend([
+ code.co_freevars,
+ code.co_cellvars,
+ ])
+ return types.CodeType(*co_args)
--- a/numba/tests/test_analysis.py
+++ b/numba/tests/test_analysis.py
@@ -15,6 +15,7 @@
IRProcessing, DeadBranchPrune,
PreserveIR)
from numba.core.compiler import DefaultPassBuilder, CompilerBase, PassManager
+from numba.misc import codetype
_GLOBAL = 123
@@ -661,33 +662,8 @@
new_consts = tuple([v for _, v in sorted(co_consts.items())])
# create new code parts
- co_args = [pyfunc_code.co_argcount]
-
- if utils.PYVERSION >= (3, 8):
- co_args.append(pyfunc_code.co_posonlyargcount)
- co_args.append(pyfunc_code.co_kwonlyargcount)
- co_args.extend([pyfunc_code.co_nlocals,
- pyfunc_code.co_stacksize,
- pyfunc_code.co_flags,
- pyfunc_code.co_code,
- new_consts,
- pyfunc_code.co_names,
- pyfunc_code.co_varnames,
- pyfunc_code.co_filename,
- pyfunc_code.co_name])
-
- if utils.PYVERSION >= (3, 11):
- co_args.append(pyfunc_code.co_qualname)
- co_args.extend([pyfunc_code.co_firstlineno,
- pyfunc_code.co_lnotab])
- if utils.PYVERSION >= (3, 11):
- co_args.append(pyfunc_code.co_exceptiontable)
- co_args.extend([pyfunc_code.co_freevars,
- pyfunc_code.co_cellvars
- ])
-
# create code object with mutation
- new_code = pytypes.CodeType(*co_args)
+ new_code = codetype.copy_code_type(pyfunc_code, constants=new_consts)
# get function
return pytypes.FunctionType(new_code, globals())
--- a/numba/tests/test_parfors.py
+++ b/numba/tests/test_parfors.py
@@ -49,6 +49,8 @@
needs_subprocess)
from numba.core.extending import register_jitable
from numba.core.bytecode import _fix_LOAD_GLOBAL_arg
+from numba.misc import codetype
+
import cmath
import unittest
@@ -3272,33 +3274,9 @@
new_code = bytes(new_code)
# create new code parts
- co_args = [pyfunc_code.co_argcount]
-
- if utils.PYVERSION >= (3, 8):
- co_args.append(pyfunc_code.co_posonlyargcount)
- co_args.append(pyfunc_code.co_kwonlyargcount)
- co_args.extend([pyfunc_code.co_nlocals,
- pyfunc_code.co_stacksize,
- pyfunc_code.co_flags,
- new_code,
- pyfunc_code.co_consts,
- prange_names,
- pyfunc_code.co_varnames,
- pyfunc_code.co_filename,
- pyfunc_code.co_name])
-
- if utils.PYVERSION >= (3, 11):
- co_args.append(pyfunc_code.co_qualname)
- co_args.extend([pyfunc_code.co_firstlineno,
- pyfunc_code.co_lnotab])
- if utils.PYVERSION >= (3, 11):
- co_args.append(pyfunc_code.co_exceptiontable)
- co_args.extend([pyfunc_code.co_freevars,
- pyfunc_code.co_cellvars
- ])
-
# create code object with prange mutation
- prange_code = pytypes.CodeType(*co_args)
+ prange_code = codetype.copy_code_type(pyfunc_code, codestring=new_code,
+ names=prange_names)
# get function
pfunc = pytypes.FunctionType(prange_code, globals())
|