File: _py2.py

package info (click to toggle)
python-mitogen 0.3.36-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,708 kB
  • sloc: python: 24,457; sh: 198; makefile: 74; perl: 19; ansic: 18
file content (54 lines) | stat: -rw-r--r-- 1,431 bytes parent folder | download
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
# SPDX-FileCopyrightText: 2025 Mitogen authors <https://github.com/mitogen-hq>
# SPDX-License-Identifier: BSD-3-Clause
# !mitogen: minify_safe

import array
import itertools
import opcode


IMPORT_NAME = opcode.opmap['IMPORT_NAME']
LOAD_CONST = opcode.opmap['LOAD_CONST']


def _opargs(code, _have_arg=opcode.HAVE_ARGUMENT):
    it = iter(array.array('B', code))
    nexti = it.next
    for i in it:
        if i >= _have_arg:
            yield (i, nexti() | (nexti() << 8))
        else:
            yield (i, None)


def _code_imports_py25(code, consts, names):
    it1, it2, it3 = itertools.tee(_opargs(code), 3)
    try:
        next(it2)
        next(it3)
        next(it3)
    except StopIteration:
        return
    for oparg1, oparg2, (op3, arg3) in itertools.izip(it1, it2, it3):
        if op3 != IMPORT_NAME:
            continue
        op1, arg1 = oparg1
        op2, arg2 = oparg2
        if op1 != LOAD_CONST or op2 != LOAD_CONST:
            continue
        yield (consts[arg1], names[arg3], consts[arg2] or ())


def _code_imports_py24(code, consts, names):
    it1, it2 = itertools.tee(_opargs(code), 2)
    try:
        next(it2)
    except StopIteration:
        return
    for oparg1, (op2, arg2) in itertools.izip(it1, it2):
        if op2 != IMPORT_NAME:
            continue
        op1, arg1 = oparg1
        if op1 != LOAD_CONST:
            continue
        yield (-1, names[arg2], consts[arg1] or ())