From 8daedaf063f20c635e2b47da500819c38e3c15fb Mon Sep 17 00:00:00 2001 From: rocky Date: Tue, 20 Aug 2019 18:23:52 -0400 Subject: [PATCH] Run black over validate.py --- pytest/validate.py | 69 ++++++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 33 deletions(-) diff --git a/pytest/validate.py b/pytest/validate.py index 84e6e4b2..29707e8e 100644 --- a/pytest/validate.py +++ b/pytest/validate.py @@ -1,16 +1,20 @@ # future from __future__ import print_function + # std import os import difflib import subprocess import tempfile import functools + # uncompyle6 / xdis from uncompyle6 import PYTHON_VERSION, PYTHON3, IS_PYPY, code_deparse + # TODO : I think we can get xdis to support the dis api (python 3 version) by doing something like this there from xdis.bytecode import Bytecode from xdis.main import get_opcode + opc = get_opcode(PYTHON_VERSION, IS_PYPY) Bytecode = functools.partial(Bytecode, opc=opc) import six @@ -20,6 +24,7 @@ if PYTHON3: else: from StringIO import StringIO + def _dis_to_text(co): return Bytecode(co).dis() @@ -33,36 +38,32 @@ def print_diff(original, uncompyled): :param original: Text describing the original code object. :param uncompyled: Text describing the uncompyled code object. """ - original_lines = original.split('\n') - uncompyled_lines = uncompyled.split('\n') - args = original_lines, uncompyled_lines, 'original', 'uncompyled' + original_lines = original.split("\n") + uncompyled_lines = uncompyled.split("\n") + args = original_lines, uncompyled_lines, "original", "uncompyled" try: from bs4 import BeautifulSoup + diff = difflib.HtmlDiff().make_file(*args) diff = BeautifulSoup(diff, "html.parser") diff.select_one('table[summary="Legends"]').extract() except ImportError: - print('\nTo display diff highlighting run:\n pip install BeautifulSoup4') + print("\nTo display diff highlighting run:\n pip install BeautifulSoup4") diff = difflib.HtmlDiff().make_table(*args) with tempfile.NamedTemporaryFile(delete=False) as f: - f.write(str(diff).encode('utf-8')) + f.write(str(diff).encode("utf-8")) try: print() - html = subprocess.check_output([ - 'elinks', - '-dump', - '-no-references', - '-dump-color-mode', - '1', - f.name, - ]).decode('utf-8') + html = subprocess.check_output( + ["elinks", "-dump", "-no-references", "-dump-color-mode", "1", f.name] + ).decode("utf-8") print(html) except: - print('\nFor side by side diff install elinks') + print("\nFor side by side diff install elinks") diff = difflib.Differ().compare(original_lines, uncompyled_lines) - print('\n'.join(diff)) + print("\n".join(diff)) finally: os.unlink(f.name) @@ -80,18 +81,19 @@ def are_instructions_equal(i1, i2): :return: True if the two instructions are approximately equal, otherwise False. """ - result = (1 == 1 + result = ( + 1 == 1 and i1.opname == i2.opname and i1.opcode == i2.opcode and i1.arg == i2.arg # ignore differences due to code objects # TODO : Better way of ignoring address - and (i1.argval == i2.argval or '', mode) + original_code = compile(text, "", mode) original_dis = _dis_to_text(original_code) original_text = text - deparsed = code_deparse(original_code, - out=six.StringIO(), - version=PYTHON_VERSION, - compile_mode=mode) + deparsed = code_deparse( + original_code, out=six.StringIO(), version=PYTHON_VERSION, compile_mode=mode + ) uncompyled_text = deparsed.text - uncompyled_code = compile(uncompyled_text, '', 'exec') + uncompyled_code = compile(uncompyled_text, "", "exec") if not are_code_objects_equal(uncompyled_code, original_code): @@ -138,15 +139,17 @@ def validate_uncompyle(text, mode='exec'): def output(text, dis): width = 60 - return '\n\n'.join([ - ' SOURCE CODE '.center(width, '#'), - text.strip(), - ' BYTECODE '.center(width, '#'), - dis - ]) + return "\n\n".join( + [ + " SOURCE CODE ".center(width, "#"), + text.strip(), + " BYTECODE ".center(width, "#"), + dis, + ] + ) original = output(original_text, original_dis) uncompyled = output(uncompyled_text, uncompyled_dis) print_diff(original, uncompyled) - assert 'original' == 'uncompyled' + assert "original" == "uncompyled"