From 4379a97592fb282c6e6acc9185a68953e7c46e3d Mon Sep 17 00:00:00 2001 From: Christian Tismer Date: Thu, 30 Nov 2017 19:25:59 +0100 Subject: Fix signature registry on Python2 with .pyc files, part 2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There is unfortunately another bad side effect with .pyc files. I had to make sure that not the __file__ attribute is used, but the correct filename is computed, because __file__ can refer to the .pyc file under certain circumstances. Improved error handling, more file type checks and short filenames relative to the project path added for convenience. Task-number: PYSIDE-510 Change-Id: Ia0002fdfb382b7d3681156b1aef42739eb22dcc9 Reviewed-by: Simo Fält --- sources/pyside2/tests/registry/existence_test.py | 48 ++++++++++++++++-------- sources/pyside2/tests/registry/init_platform.py | 40 ++++---------------- sources/pyside2/tests/registry/util.py | 6 +++ 3 files changed, 45 insertions(+), 49 deletions(-) (limited to 'sources') diff --git a/sources/pyside2/tests/registry/existence_test.py b/sources/pyside2/tests/registry/existence_test.py index 7627eeab2..cdb6f362f 100644 --- a/sources/pyside2/tests/registry/existence_test.py +++ b/sources/pyside2/tests/registry/existence_test.py @@ -43,25 +43,37 @@ import os import sys import unittest import warnings -from init_platform import enum_all, generate_all, is_ci, outname, outpath -from util import isolate_warnings, check_warnings +from textwrap import dedent +from init_platform import enum_all, generate_all, is_ci, module, refpath +from util import isolate_warnings, check_warnings, suppress_warnings from PySide2 import * from PySide2.QtCore import __version__ -refmodule_name = outname[:-3] # no .py -pyc = os.path.splitext(outpath)[0] + ".pyc" -if os.path.exists(pyc) and not os.path.exists(outname): +pyc = os.path.splitext(refpath)[0] + ".pyc" +if os.path.exists(pyc) and not os.path.exists(refpath): # on Python2 the pyc file would be imported os.unlink(pyc) -sys.path.insert(0, os.path.dirname(__file__)) +home_dir = refpath +for _ in "abcde": + home_dir = os.path.dirname(home_dir) +shortpath = os.path.relpath(refpath, home_dir) try: - exec("import {} as sig_exists".format(refmodule_name)) - print("found:", refmodule_name) + exec("import {} as sig_exists".format(module)) + print("found:", shortpath) have_refmodule = True except ImportError: - print("*** not found:", refmodule_name) + print("*** not found:", shortpath) have_refmodule = False +except SyntaxError: + print("*** not a python file, removed:", shortpath) + os.unlink(refpath) + have_refmodule = False +if have_refmodule and not hasattr(sig_exists, "dict"): + print("*** wrong module without 'dict', removed:", shortpath) + os.unlink(refpath) + have_refmodule = False + @unittest.skipIf(not have_refmodule, "not activated for this platform or version") @@ -87,12 +99,12 @@ class TestSignaturesExists(unittest.TestCase): found_sigs = enum_all() # make sure that errors are actually raised found_sigs.pop(list(found_sigs.keys())[42]) - with isolate_warnings(): + with isolate_warnings(), suppress_warnings(): for key, value in sig_exists.dict.items(): if key not in found_sigs: - warnings.warn("ignore missing key: '{}'".format(key), RuntimeWarning) + warnings.warn("missing key: '{}'".format(key), RuntimeWarning) elif isinstance(value, list) and len(value) != len(found_sigs[key]): - warnings.warn("ignore different sig length: '{}'".format(key), RuntimeWarning) + warnings.warn("different sig length: '{}'".format(key), RuntimeWarning) self.assertTrue(check_warnings()) version = tuple(map(int, __version__.split("."))) @@ -107,12 +119,16 @@ if not have_refmodule and is_ci and version[:2] in tested_versions: """ generate_all() sys.stderr.flush() - print("BEGIN", outpath, file=sys.stderr) - with open(outpath) as f: + print("BEGIN_FILE", shortpath, file=sys.stderr) + with open(refpath) as f: print(f.read(), file=sys.stderr) - print("END", outpath, file=sys.stderr) + print("END_FILE", shortpath, file=sys.stderr) sys.stderr.flush() - raise RuntimeError("This is the initial call. You should check this file in.") + raise RuntimeError(dedent(""" + {line} + ** This is the initial call. You should check this file in: + ** {} + **""").format(shortpath, line=79 * "*")) if __name__ == '__main__': unittest.main() diff --git a/sources/pyside2/tests/registry/init_platform.py b/sources/pyside2/tests/registry/init_platform.py index ea8eb2af2..5b6558806 100644 --- a/sources/pyside2/tests/registry/init_platform.py +++ b/sources/pyside2/tests/registry/init_platform.py @@ -59,10 +59,11 @@ version_id = __version__.replace(".", "_") is_ci = os.environ.get("QTEST_ENVIRONMENT", "") == "ci" # Python2 legacy: Correct 'linux2' to 'linux', recommended way. platform = 'linux' if sys.platform.startswith('linux') else sys.platform -outname = "exists_{}_{}{}.py".format(platform, version_id, +module = "exists_{}_{}{}".format(platform, version_id, "_ci" if is_ci else "") -outpath = os.path.join(os.path.dirname(__file__), outname) +refpath = os.path.join(os.path.dirname(__file__), module + ".py") outfile = None +sourcepath = os.path.splitext(__file__)[0] + ".py" # make sure not to get .pyc def xprint(*args, **kw): if outfile: @@ -144,9 +145,8 @@ def enum_module(mod_name): def generate_all(): global outfile - with open(outpath, "w") as outfile: - with open(__file__) as f: - lines = f.readlines() + with open(refpath, "w") as outfile, open(sourcepath) as f: + lines = f.readlines() license_line = next((lno for lno, line in enumerate(lines) if "$QT_END_LICENSE$" in line)) xprint("".join(lines[:license_line + 3])) @@ -165,35 +165,9 @@ def enum_all(): ret.update(enum_module(mod_name)) return ret -# This function exists because I forgot to sort the files in the first place. -def sort_dict(fname): - with open(fname) as f: - lines = f.readlines() - out = [] - while lines: - line = lines.pop(0) - if not line.lstrip().startswith('"'): - out.append(line) - continue - out.append(line) - buf = [] # leave __init__ in place - line = lines.pop(0) - while line.lstrip().startswith('"'): - buf.append(line) - line = lines.pop(0) - buf.sort() - out.extend(buf) - out.append(line) - with open(fname, "w") as f: - f.writelines(out) - def __main__(): - if sys.argv[1:]: - fname = sys.argv[1] - print("we are just sorting", fname) - sort_dict(fname) - sys.exit(0) - print("+++ generating {}. You should check this file in.".format(outname)) + print("+++ generating {}. You should probably check this file in." + .format(refpath)) generate_all() if __name__ == "__main__": diff --git a/sources/pyside2/tests/registry/util.py b/sources/pyside2/tests/registry/util.py index 5d0602b2a..d873a7d66 100644 --- a/sources/pyside2/tests/registry/util.py +++ b/sources/pyside2/tests/registry/util.py @@ -72,6 +72,12 @@ def isolate_warnings(): if warn is None: delattr(mod, warn_name) +@contextmanager +def suppress_warnings(): + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + yield + def check_warnings(): for name, mod in sys.modules.items(): if mod: -- cgit v1.2.3