MCPcopy Index your code
hub / github.com/rocky/python-uncompyle6 / code_deparse_around_offset

Function code_deparse_around_offset

uncompyle6/semantics/fragments.py:2188–2220  ·  view source on GitHub ↗

Like deparse_code(), but given a function/module name and offset, finds the node closest to offset. If offset is not an instruction boundary, we raise an IndexError.

(
    name,
    offset,
    co,
    out=StringIO(),
    version: Optional[tuple] = None,
    is_pypy: bool = False,
    debug_opts=DEFAULT_DEBUG_OPTS,
)

Source from the content-addressed store, hash-verified

2186
2187
2188def code_deparse_around_offset(
2189 name,
2190 offset,
2191 co,
2192 out=StringIO(),
2193 version: Optional[tuple] = None,
2194 is_pypy: bool = False,
2195 debug_opts=DEFAULT_DEBUG_OPTS,
2196):
2197 """
2198 Like deparse_code(), but given a function/module name and
2199 offset, finds the node closest to offset. If offset is not an instruction boundary,
2200 we raise an IndexError.
2201 """
2202 assert iscode(co)
2203
2204 if version is None:
2205 version = PYTHON_VERSION_TRIPLE
2206 if is_pypy is None:
2207 is_pypy = IS_PYPY
2208
2209 deparsed = code_deparse(co, out, version, is_pypy, debug_opts)
2210 if (name, offset) in deparsed.offsets.keys():
2211 # This is the easy case
2212 return deparsed
2213
2214 valid_offsets = [t for t in deparsed.offsets if isinstance(t[1], int)]
2215 offset_list = sorted([t[1] for t in valid_offsets if t[0] == name])
2216
2217 # FIXME: should check for branching?
2218 found_offset = find_gt(offset_list, offset)
2219 deparsed.offsets[name, offset] = deparsed.offsets[name, found_offset]
2220 return deparsed
2221
2222
2223# Deprecated. Here still for compatibility

Callers

nothing calls this directly

Calls 4

StringIOClass · 0.85
find_gtFunction · 0.85
code_deparseFunction · 0.70
keysMethod · 0.45

Tested by

no test coverage detected