summaryrefslogtreecommitdiffstats
path: root/Include
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2021-06-21 20:53:04 (GMT)
committerGitHub <noreply@github.com>2021-06-21 20:53:04 (GMT)
commit355f5dd36a0f53175517f35798aa874564d1113a (patch)
treecfc6c7e4f009afc772d4a9e0c909d6e3499cf2e9 /Include
parentc5d700f0e2e2921c6b54c72ffb0fca3c3d1ef06b (diff)
downloadcpython-355f5dd36a0f53175517f35798aa874564d1113a.zip
cpython-355f5dd36a0f53175517f35798aa874564d1113a.tar.gz
cpython-355f5dd36a0f53175517f35798aa874564d1113a.tar.bz2
bpo-43693: Turn localspluskinds into an object (GH-26749)
Managing it as a bare pointer to malloc'ed bytes is just too awkward in a few places.
Diffstat (limited to 'Include')
-rw-r--r--Include/cpython/code.h6
-rw-r--r--Include/internal/pycore_code.h39
2 files changed, 18 insertions, 27 deletions
diff --git a/Include/cpython/code.h b/Include/cpython/code.h
index 5bf4c8c..77801dc 100644
--- a/Include/cpython/code.h
+++ b/Include/cpython/code.h
@@ -26,9 +26,6 @@ typedef uint16_t _Py_CODEUNIT;
typedef struct _PyOpcache _PyOpcache;
-typedef unsigned char _PyLocalsPlusKind;
-typedef _PyLocalsPlusKind *_PyLocalsPlusKinds;
-
/* Bytecode object */
struct PyCodeObject {
PyObject_HEAD
@@ -75,7 +72,7 @@ struct PyCodeObject {
int co_firstlineno; /* first source line number */
PyObject *co_code; /* instruction opcodes */
PyObject *co_localsplusnames; /* tuple mapping offsets to names */
- _PyLocalsPlusKinds co_localspluskinds; /* array mapping to local kinds */
+ PyObject *co_localspluskinds; /* Bytes mapping to local kinds (one byte per variable) */
PyObject *co_filename; /* unicode (where it was loaded from) */
PyObject *co_name; /* unicode (name, for reference) */
PyObject *co_linetable; /* string (encoding addr<->lineno mapping) See
@@ -222,4 +219,3 @@ void PyLineTable_InitAddressRange(const char *linetable, Py_ssize_t length, int
int PyLineTable_NextAddressRange(PyCodeAddressRange *range);
int PyLineTable_PreviousAddressRange(PyCodeAddressRange *range);
-
diff --git a/Include/internal/pycore_code.h b/Include/internal/pycore_code.h
index 1e78b92..78969df 100644
--- a/Include/internal/pycore_code.h
+++ b/Include/internal/pycore_code.h
@@ -180,39 +180,34 @@ extern Py_ssize_t _Py_QuickenedCount;
* "free" kind is mutually exclusive with both.
*/
-// For now _PyLocalsPlusKind and _PyLocalsPlusKinds are defined
-// in Include/cpython/code.h.
-/* Note that these all fit within _PyLocalsPlusKind, as do combinations. */
+// Note that these all fit within a byte, as do combinations.
// Later, we will use the smaller numbers to differentiate the different
// kinds of locals (e.g. pos-only arg, varkwargs, local-only).
#define CO_FAST_LOCAL 0x20
#define CO_FAST_CELL 0x40
#define CO_FAST_FREE 0x80
-static inline int
-_PyCode_InitLocalsPlusKinds(int num, _PyLocalsPlusKinds *pkinds)
+typedef unsigned char _PyLocals_Kind;
+
+static inline _PyLocals_Kind
+_PyLocals_GetKind(PyObject *kinds, int i)
{
- if (num == 0) {
- *pkinds = NULL;
- return 0;
- }
- _PyLocalsPlusKinds kinds = PyMem_NEW(_PyLocalsPlusKind, num);
- if (kinds == NULL) {
- PyErr_NoMemory();
- return -1;
- }
- *pkinds = kinds;
- return 0;
+ assert(PyBytes_Check(kinds));
+ assert(0 <= i && i < PyBytes_GET_SIZE(kinds));
+ char *ptr = PyBytes_AS_STRING(kinds);
+ return (_PyLocals_Kind)(ptr[i]);
}
static inline void
-_PyCode_ClearLocalsPlusKinds(_PyLocalsPlusKinds kinds)
+_PyLocals_SetKind(PyObject *kinds, int i, _PyLocals_Kind kind)
{
- if (kinds != NULL) {
- PyMem_Free(kinds);
- }
+ assert(PyBytes_Check(kinds));
+ assert(0 <= i && i < PyBytes_GET_SIZE(kinds));
+ char *ptr = PyBytes_AS_STRING(kinds);
+ ptr[i] = (char) kind;
}
+
struct _PyCodeConstructor {
/* metadata */
PyObject *filename;
@@ -229,8 +224,8 @@ struct _PyCodeConstructor {
PyObject *names;
/* mapping frame offsets to information */
- PyObject *localsplusnames;
- _PyLocalsPlusKinds localspluskinds;
+ PyObject *localsplusnames; // Tuple of strings
+ PyObject *localspluskinds; // Bytes object, one byte per variable
/* args (within varnames) */
int argcount;