def test_flag_packing(): random.seed(8025816322119661921) # ayy lmao nflags = len(Flag.__members__) cases = chain( random.sample(list(product((True, False), repeat=nflags)), 1000), [[True] * nflags], [[False] * nflags], ) for flagvals in cases: flags = dict(zip(Flag.__members__.keys(), flagvals)) assert Flag.unpack(Flag.pack(**flags)) == flags
def test_code_flags(sample_flags): attr_map = { 'CO_NESTED': 'is_nested', 'CO_GENERATOR': 'is_generator', 'CO_COROUTINE': 'is_coroutine', 'CO_ITERABLE_COROUTINE': 'is_iterable_coroutine', 'CO_NEWLOCALS': 'constructs_new_locals', } for flags in sample_flags: code = Code.from_pycode(pycode( argcount=0, kwonlyargcount=0, nlocals=2, stacksize=0, flags=Flag.pack(**flags), codestring=b'd\x00\x00S', # return None constants=(None,), names=(), varnames=('a', 'b'), filename='', name='', firstlineno=0, lnotab=b'', )) assert code.flags == flags for flag, attr in attr_map.items(): if flags[flag]: assert getattr(code, attr)
def test_flag_max(): assert Flag.pack( CO_OPTIMIZED=True, CO_NEWLOCALS=True, CO_VARARGS=True, CO_VARKEYWORDS=True, CO_NESTED=True, CO_GENERATOR=True, CO_NOFREE=True, CO_COROUTINE=True, CO_ITERABLE_COROUTINE=True, CO_FUTURE_DIVISION=True, CO_FUTURE_ABSOLUTE_IMPORT=True, CO_FUTURE_WITH_STATEMENT=True, CO_FUTURE_PRINT_FUNCTION=True, CO_FUTURE_UNICODE_LITERALS=True, CO_FUTURE_BARRY_AS_BDFL=True, CO_FUTURE_GENERATOR_STOP=True, ) == Flag.max
def test_flag_unpack_too_big(): assert all(Flag.unpack(Flag.max).values()) with pytest.raises(ValueError): Flag.unpack(Flag.max + 1)
def test_flag_packing(sample_flags): for flags in sample_flags: assert Flag.unpack(Flag.pack(**flags)) == flags