python / python/cpython

Extract common bytecode retrieval logic from `_PyFrame_GetBytecode` and `_PyFrame_SafeGetLasti`

Open
#142,929 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core type-refactor
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Both _PyFrame_GetBytecode and _PyFrame_SafeGetLasti inline functions currently duplicate the same logic for retrieving the bytecode, differing only in whether they call _PyFrame_GetCode or _PyFrame_SafeGetCode.

static inline _Py_CODEUNIT *
_PyFrame_GetBytecode(_PyInterpreterFrame *f)
{
#ifdef Py_GIL_DISABLED
    PyCodeObject *co = _PyFrame_GetCode(f);
    _PyCodeArray *tlbc = _PyCode_GetTLBCArray(co);
    assert(f->tlbc_index >= 0 && f->tlbc_index < tlbc->size);
    return (_Py_CODEUNIT *)tlbc->entries[f->tlbc_index];
#else
    return _PyCode_CODE(_PyFrame_GetCode(f));
#endif
}

static inline int
_PyFrame_SafeGetLasti(struct _PyInterpreterFrame *f)
{
    // Code based on _PyFrame_GetBytecode() but replace _PyFrame_GetCode()
    // with _PyFrame_SafeGetCode().
    PyCodeObject *co = _PyFrame_SafeGetCode(f);
    if (co == NULL) {
        return -1;
    }

    _Py_CODEUNIT *bytecode;
#ifdef Py_GIL_DISABLED
    _PyCodeArray *tlbc = _PyCode_GetTLBCArray(co);
    assert(f->tlbc_index >= 0 && f->tlbc_index < tlbc->size);
    bytecode = (_Py_CODEUNIT *)tlbc->entries[f->tlbc_index];
#else
    bytecode = _PyCode_CODE(co);
#endif

    return (int)(f->instr_ptr - bytecode) * sizeof(_Py_CODEUNIT);
}

However, I think we should extract the bytecode retrieving logic into another helper, let's say, _PyFrame_GetBytecodeFromCodeObject, as:

static inline _Py_CODEUNIT *
_PyFrame_GetBytecodeFromCodeObject(_PyInterpreterFrame *f, PyCodeObject *co)
{
#ifdef Py_GIL_DISABLED
     _PyCodeArray *tlbc = _PyCode_GetTLBCArray(co);
    assert(f->tlbc_index >= 0 && f->tlbc_index < tlbc->size);
    return (_Py_CODEUNIT *)tlbc->entries[f->tlbc_index];
#else
    return _PyCode_CODE(co);
#endif
}

So we can use that helper as:

static inline _Py_CODEUNIT *
_PyFrame_GetBytecode(_PyInterpreterFrame *f)
{
    PyCodeObject *co = _PyFrame_GetCode(f);
    return _PyFrame_GetBytecodeFromCodeObject(f, co);
}

static inline int
_PyFrame_SafeGetLasti(_PyInterpreterFrame *f)
{
    PyCodeObject *co = _PyFrame_SafGetCode(f);
    if(co == NULL){
        return -1;
    }

    _Py_CODEUNIT *bytecode = _PyFrame_GetBytecodeFromCodeObject(f, co);
    return (int)(f->insert_ptr - bytecode) * sizeof(_Py_CODEUNIT);
}

However, we can use different types of helpers such as inline helper with a function pointer or a macro wrapper. My point here is not about the implementation of the helper itself, but about extracting the bytecode retrieving logic into another helper regardless of the helper name and the method used to implement that helper.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating _PyFrame_GetBytecode and _PyFrame_SafeGetLasti, then compare their callers and the existing _PyFrame_GetCode and _PyFrame_SafeGetCode paths. Extract their shared bytecode retrieval logic while preserving both GIL-enabled and GIL-disabled behavior, and verify that the relevant CPython tests pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
backend
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.