From 6fe57d459418f83e8f5fa047246715728cb6f816 Mon Sep 17 00:00:00 2001 From: u214892 <u214892@sbb.ch> Date: Thu, 4 Jul 2019 10:11:14 +0200 Subject: [PATCH] fix error by copying file from unresolved package --- .gitlab-ci.yml | 4 +--- benchmarks/benchmark_utils.py | 35 ++++++++++++++++++++++++++++++ benchmarks/profile_all_examples.py | 8 +++---- 3 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 benchmarks/benchmark_utils.py diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 3a91f0c..f3bb42a 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -26,9 +26,7 @@ tests: - apt update - apt install -y libgl1-mesa-glx xvfb graphviz xdg-utils libcairo2-dev libjpeg-dev libgif-dev - pip install tox - - which python - - ls /usr/lib/python3.7/ - - python -c 'from test.support import swap_attr2' + - xvfb-run tox -e benchmarks,profiling -v --recreate - xvfb-run tox -v --recreate build_and_deploy_docs: diff --git a/benchmarks/benchmark_utils.py b/benchmarks/benchmark_utils.py new file mode 100644 index 0000000..720590b --- /dev/null +++ b/benchmarks/benchmark_utils.py @@ -0,0 +1,35 @@ +import contextlib + + +# Copied from https://docs.python.org/3/library/test.html + +@contextlib.contextmanager +def swap_attr(obj, attr, new_val): + """Temporary swap out an attribute with a new object. + + Usage: + with swap_attr(obj, "attr", 5): + ... + + This will set obj.attr to 5 for the duration of the with: block, + restoring the old value at the end of the block. If `attr` doesn't + exist on `obj`, it will be created and then deleted at the end of the + block. + + The old value (or None if it doesn't exist) will be assigned to the + target of the "as" clause, if there is one. + """ + if hasattr(obj, attr): + real_val = getattr(obj, attr) + setattr(obj, attr, new_val) + try: + yield real_val + finally: + setattr(obj, attr, real_val) + else: + setattr(obj, attr, new_val) + try: + yield + finally: + if hasattr(obj, attr): + delattr(obj, attr) diff --git a/benchmarks/profile_all_examples.py b/benchmarks/profile_all_examples.py index 0b6db57..f0280f1 100644 --- a/benchmarks/profile_all_examples.py +++ b/benchmarks/profile_all_examples.py @@ -2,19 +2,17 @@ import cProfile import runpy import sys from io import StringIO -from test.support import swap_attr import importlib_resources import pkg_resources from importlib_resources import path +from benchmarks.benchmark_utils import swap_attr + def profile(resource, entry): with path(resource, entry) as file_in: - # we use the test package, which is meant for internal use by Python only internal and - # Any use of this package outside of Python’s standard library is discouraged as code (..) - # can change or be removed without notice between releases of Python. - # https://docs.python.org/3/library/test.html + # TODO remove input() from examples print("*****************************************************************") print("Profiling {}".format(entry)) -- GitLab