Skip to content

Commit d1e02c4

Browse files
committed
Add version number to code object for better versioning of functions.
1 parent 82ccbf6 commit d1e02c4

File tree

5 files changed

+14
-4
lines changed

5 files changed

+14
-4
lines changed

Include/cpython/code.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ typedef struct {
8888
int co_nplaincellvars; /* number of non-arg cell variables */ \
8989
int co_ncellvars; /* total number of cell variables */ \
9090
int co_nfreevars; /* number of free variables */ \
91+
uint32_t co_version; /* version number */ \
9192
\
9293
PyObject *co_localsplusnames; /* tuple mapping offsets to names */ \
9394
PyObject *co_localspluskinds; /* Bytes mapping to local kinds (one byte \

Objects/codeobject.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "pycore_tuple.h" // _PyTuple_ITEMS()
1212
#include "clinic/codeobject.c.h"
1313

14+
extern uint32_t _Py_next_func_version;
1415

1516
/******************
1617
* generic helpers
@@ -347,7 +348,10 @@ init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
347348
co->co_nplaincellvars = nplaincellvars;
348349
co->co_ncellvars = ncellvars;
349350
co->co_nfreevars = nfreevars;
350-
351+
co->co_version = _Py_next_func_version;
352+
if (_Py_next_func_version != 0) {
353+
_Py_next_func_version++;
354+
}
351355
/* not set */
352356
co->co_weakreflist = NULL;
353357
co->co_extra = NULL;

Objects/funcobject.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "pycore_pyerrors.h" // _PyErr_Occurred()
88
#include "structmember.h" // PyMemberDef
99

10-
static uint32_t next_func_version = 1;
10+
uint32_t _Py_next_func_version = 10000; /* Leave plenty of space for deep frozen code objects */
1111

1212
PyFunctionObject *
1313
_PyFunction_FromConstructor(PyFrameConstructor *constr)
@@ -137,10 +137,10 @@ uint32_t _PyFunction_GetVersionForCurrentState(PyFunctionObject *func)
137137
if (func->vectorcall != _PyFunction_Vectorcall) {
138138
return 0;
139139
}
140-
if (next_func_version == 0) {
140+
if (_Py_next_func_version == 0) {
141141
return 0;
142142
}
143-
uint32_t v = next_func_version++;
143+
uint32_t v = _Py_next_func_version++;
144144
func->func_version = v;
145145
return v;
146146
}

Python/ceval.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4814,6 +4814,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
48144814
assert(PyTuple_CheckExact(TOP()));
48154815
func->func_defaults = POP();
48164816
}
4817+
func->func_version = ((PyCodeObject *)codeobj)->co_version;
48174818

48184819
PUSH((PyObject *)func);
48194820
DISPATCH();

Tools/build/deepfreeze.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def make_string_literal(b: bytes) -> str:
4444
CO_FAST_CELL = 0x40
4545
CO_FAST_FREE = 0x80
4646

47+
next_code_version = 1
4748

4849
def get_localsplus(code: types.CodeType):
4950
a = collections.defaultdict(int)
@@ -228,6 +229,7 @@ def generate_unicode(self, name: str, s: str) -> str:
228229

229230

230231
def generate_code(self, name: str, code: types.CodeType) -> str:
232+
global next_code_version
231233
# The ordering here matches PyCode_NewWithPosOnlyArgs()
232234
# (but see below).
233235
co_consts = self.generate(name + "_consts", code.co_consts)
@@ -270,6 +272,8 @@ def generate_code(self, name: str, code: types.CodeType) -> str:
270272
self.write(f".co_nplaincellvars = {nplaincellvars},")
271273
self.write(f".co_ncellvars = {ncellvars},")
272274
self.write(f".co_nfreevars = {nfreevars},")
275+
self.write(f".co_version = {next_code_version},")
276+
next_code_version += 1
273277
self.write(f".co_localsplusnames = {co_localsplusnames},")
274278
self.write(f".co_localspluskinds = {co_localspluskinds},")
275279
self.write(f".co_filename = {co_filename},")

0 commit comments

Comments
 (0)