Home - Summaries: (main) : (py3.11) : Everything - Nightly builds - Benchmarks - RPython - Builders - About

pypy/interpreter/test/test_compiler.py::AppTestOptimizer::()::test_folding_of_binops_on_constants

self = <pypy.interpreter.typedef.W_ObjectObjectUserDictWeakrefable object at 0x7fdfac197750>

    def test_folding_of_binops_on_constants(self):
            def disassemble(func):
                from io import StringIO
                import sys, dis
                f = StringIO()
                tmp = sys.stdout
                sys.stdout = f
                dis.dis(func)
                sys.stdout = tmp
                result = f.getvalue()
                f.close()
                return result
    
            def dis_single(line):
                return disassemble(compile(line, '', 'single'))
    
            for line, elem in (
                ('a = 2+3+4', '(9)'),                   # chained fold
                ('"@"*4', "('@@@@')"),                  # check string ops
                ('a="abc" + "def"', "('abcdef')"),      # check string ops
                ('a = 3**4', '(81)'),                   # binary power
                ('a = 3*4', '(12)'),                    # binary multiply
                ('a = 13//4', '(3)'),                   # binary floor divide
                ('a = 14%4', '(2)'),                    # binary modulo
                ('a = 2+3', '(5)'),                     # binary add
                ('a = 13-4', '(9)'),                    # binary subtract
                ('a = (12,13)[1]', '(13)'),             # binary subscr
                ('a = 13 << 2', '(52)'),                # binary lshift
                ('a = 13 >> 2', '(3)'),                 # binary rshift
                ('a = 13 & 7', '(5)'),                  # binary and
                ('a = 13 ^ 7', '(10)'),                 # binary xor
                ('a = 13 | 7', '(15)'),                 # binary or
                ):
>               asm = dis_single(line)

[/build_dir/own-linux-x86-64/build/pypy/interpreter/test/test_compiler.py:1351]:34: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

line = W_UnicodeObject('a = 2+3+4')

    def dis_single(line):
>       return disassemble(compile(line, '', 'single'))

[/build_dir/own-linux-x86-64/build/pypy/interpreter/test/test_compiler.py:1351]:15: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

func = <code object <module>, file "", line 1>

    def disassemble(func):
        from io import StringIO
        import sys, dis
        f = StringIO()
        tmp = sys.stdout
        sys.stdout = f
>       dis.dis(func)

[/build_dir/own-linux-x86-64/build/pypy/interpreter/test/test_compiler.py:1351]:8: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

x = <code object <module>, file "", line 1>

    def dis(x=None, *, file=None, depth=None, show_caches=False, adaptive=False):
        """Disassemble classes, methods, functions, and other compiled objects.
    
        With no argument, disassemble the last traceback.
    
        Compiled objects currently include generator objects, async generator
        objects, and coroutine objects, all of which store their code object
        in a special attribute.
        """
        if x is None:
            distb(file=file, show_caches=show_caches, adaptive=adaptive)
            return
        # Extract functions from methods.
        if hasattr(x, '__func__'):
            x = x.__func__
        # Extract compiled code objects from...
        if hasattr(x, '__code__'):  # ...a function, or
            x = x.__code__
        elif hasattr(x, 'gi_code'):  #...a generator object, or
            x = x.gi_code
        elif hasattr(x, 'ag_code'):  #...an asynchronous generator object, or
            x = x.ag_code
        elif hasattr(x, 'cr_code'):  #...a coroutine.
            x = x.cr_code
        # Perform the disassembly.
        if hasattr(x, '__dict__'):  # Class or module
            items = sorted(x.__dict__.items())
            for name, x1 in items:
                if isinstance(x1, _have_code):
                    print("Disassembly of %s:" % name, file=file)
                    try:
                        dis(x1, file=file, depth=depth, show_caches=show_caches, adaptive=adaptive)
                    except TypeError as msg:
                        print("Sorry:", msg, file=file)
                    print(file=file)
        elif hasattr(x, 'co_code'): # Code object
>           _disassemble_recursive(x, file=file, depth=depth, show_caches=show_caches, adaptive=adaptive)

../lib-python/3/dis.py:114: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

co = <code object <module>, file "", line 1>

    def _disassemble_recursive(co, *, file=None, depth=None, show_caches=False, adaptive=False):
>       disassemble(co, file=file, show_caches=show_caches, adaptive=adaptive)

../lib-python/3/dis.py:555: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

co = <code object <module>, file "", line 1>, lasti = W_IntObject(-1)

    def disassemble(co, lasti=-1, *, file=None, show_caches=False, adaptive=False):
        """Disassemble a code object."""
        linestarts = dict(findlinestarts(co))
        exception_entries = _parse_exception_table(co)
>       _disassemble_bytes(_get_code_array(co, adaptive),

../lib-python/3/dis.py:548: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

code = W_BytesObject('d\x00Z\x00d\x01S\x00'), lasti = W_IntObject(-1)
varname_from_oparg = bound method _varname_from_oparg
names = W_TupleObject(W_UnicodeObject('a'))
co_consts = W_TupleObject(W_IntObject(9), <pypy.objspace.std.noneobject.W_NoneObject object at 0x7fdfb211dad0>)
linestarts = W_DictObject(<pypy.objspace.std.dictmultiobject.IntDictStrategy object at 0x7fdfb152f1d0>)

    def _disassemble_bytes(code, lasti=-1, varname_from_oparg=None,
                           names=None, co_consts=None, linestarts=None,
                           *, file=None, line_offset=0, exception_entries=(),
                           co_positions=None, show_caches=False):
        # Omit the line number column entirely if we have no line number info
        show_lineno = bool(linestarts)
        if show_lineno:
            maxlineno = max(linestarts.values()) + line_offset
            if maxlineno >= 1000:
                lineno_width = len(str(maxlineno))
            else:
                lineno_width = 3
        else:
            lineno_width = 0
        maxoffset = len(code) - 2
        if maxoffset >= 10000:
            offset_width = len(str(maxoffset))
        else:
            offset_width = 4
>       for instr in _get_instructions_bytes(code, varname_from_oparg, names,

../lib-python/3/dis.py:586: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

code = W_BytesObject('d\x00Z\x00d\x01S\x00')
varname_from_oparg = bound method _varname_from_oparg
names = W_TupleObject(W_UnicodeObject('a'))
co_consts = W_TupleObject(W_IntObject(9), <pypy.objspace.std.noneobject.W_NoneObject object at 0x7fdfb211dad0>)
linestarts = W_DictObject(<pypy.objspace.std.dictmultiobject.IntDictStrategy object at 0x7fdfb152f1d0>)
line_offset = W_IntObject(0)
exception_entries = W_ListObject(<pypy.objspace.std.listobject.EmptyListStrategy object at 0x7fdfb00bb310>, None)
co_positions = <pypy.objspace.std.iterobject.W_FastListIterObject object at 0x7fdfad4c1bd0>
show_caches = W_BoolObject(False)

    def _get_instructions_bytes(code, varname_from_oparg=None,
                                names=None, co_consts=None,
                                linestarts=None, line_offset=0,
                                exception_entries=(), co_positions=None,
                                show_caches=False):
        """Iterate over the instructions in a bytecode string.
    
        Generates a sequence of Instruction namedtuples giving the details of each
        opcode.  Additional information about the code's runtime environment
        (e.g. variable names, co_consts) can be specified using optional
        arguments.
    
        """
        co_positions = co_positions or iter(())
        get_name = None if names is None else names.__getitem__
        labels = set(findlabels(code))
        for start, end, target, _, _ in exception_entries:
            for i in range(start, end):
                labels.add(target)
        starts_line = None
        for offset, op, arg in _unpack_opargs(code):
            if linestarts is not None:
                starts_line = linestarts.get(offset, None)
                if starts_line is not None:
                    starts_line += line_offset
            is_jump_target = offset in labels
            argval = None
            argrepr = ''
            positions = Positions(*next(co_positions, ()))
            deop = _deoptop(op)
            caches = _inline_cache_entries[deop]
            if arg is not None:
                #  Set argval to the dereferenced value of the argument when
                #  available, and argrepr to the string representation of argval.
                #    _disassemble_bytes needs the string repr of the
                #    raw name index for LOAD_GLOBAL, LOAD_CONST, etc.
                argval = arg
                if deop in hasconst:
                    argval, argrepr = _get_const_info(deop, arg, co_consts)
                elif deop in hasname:
                    if deop == LOAD_GLOBAL:
                        argval, argrepr = _get_name_info(arg//2, get_name)
                        if (arg & 1) and argrepr:
                            argrepr = "NULL + " + argrepr
                    elif deop == LOAD_ATTR:
                        argval, argrepr = _get_name_info(arg//2, get_name)
                        if (arg & 1) and argrepr:
                            argrepr = "NULL|self + " + argrepr
>                   elif deop == LOAD_SUPER_ATTR:
E                   (application-level) NameError: name 'LOAD_SUPER_ATTR' is not defined

../lib-python/3/dis.py:483: NameError
builder: own-linux-x86-64 build #10880+
test: pypy/interpreter/test/test_compiler.py::AppTestOptimizer::()::test_folding_of_binops_on_constants