From ec5e279f935370a505e68b6d1259cb8b5a2d73fb Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sun, 20 Jan 2019 11:59:48 -0800 Subject: [PATCH] Remove and ban use of py.builtin --- .pre-commit-config.yaml | 5 +++++ src/_pytest/_code/source.py | 4 +--- src/_pytest/assertion/rewrite.py | 2 +- testing/python/collect.py | 4 ++-- testing/test_assertion.py | 10 ++++++---- 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ecfc004ba..80e78ab50 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -51,3 +51,8 @@ repos: entry: 'changelog files must be named ####.(feature|bugfix|doc|deprecation|removal|vendor|trivial).rst' exclude: changelog/(\d+\.(feature|bugfix|doc|deprecation|removal|vendor|trivial).rst|README.rst|_template.rst) files: ^changelog/ + - id: py-deprecated + name: py library is deprecated + language: pygrep + entry: '\bpy\.(builtin\.|code\.|std\.)' + types: [python] diff --git a/src/_pytest/_code/source.py b/src/_pytest/_code/source.py index b74ecf88e..887f323f9 100644 --- a/src/_pytest/_code/source.py +++ b/src/_pytest/_code/source.py @@ -237,9 +237,7 @@ def getfslineno(obj): def findsource(obj): try: sourcelines, lineno = inspect.findsource(obj) - except py.builtin._sysex: - raise - except: # noqa + except Exception: return None, -1 source = Source() source.lines = [line.rstrip() for line in sourcelines] diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py index df8b9becb..80f182723 100644 --- a/src/_pytest/assertion/rewrite.py +++ b/src/_pytest/assertion/rewrite.py @@ -678,7 +678,7 @@ class AssertionRewriter(ast.NodeVisitor): # Insert some special imports at the top of the module but after any # docstrings and __future__ imports. aliases = [ - ast.alias(py.builtin.builtins.__name__, "@py_builtins"), + ast.alias(six.moves.builtins.__name__, "@py_builtins"), ast.alias("_pytest.assertion.rewrite", "@pytest_ar"), ] doc = getattr(mod, "docstring", None) diff --git a/testing/python/collect.py b/testing/python/collect.py index 3147ee9e2..dcb4e6474 100644 --- a/testing/python/collect.py +++ b/testing/python/collect.py @@ -963,7 +963,7 @@ class TestTracebackCutting(object): def test_filter_traceback_generated_code(self): """test that filter_traceback() works with the fact that - py.code.Code.path attribute might return an str object. + _pytest._code.code.Code.path attribute might return an str object. In this case, one of the entries on the traceback was produced by dynamically generated code. See: https://bitbucket.org/pytest-dev/py/issues/71 @@ -984,7 +984,7 @@ class TestTracebackCutting(object): def test_filter_traceback_path_no_longer_valid(self, testdir): """test that filter_traceback() works with the fact that - py.code.Code.path attribute might return an str object. + _pytest._code.code.Code.path attribute might return an str object. In this case, one of the files in the traceback no longer exists. This fixes #1133. """ diff --git a/testing/test_assertion.py b/testing/test_assertion.py index cbd0d9068..b659233eb 100644 --- a/testing/test_assertion.py +++ b/testing/test_assertion.py @@ -7,7 +7,6 @@ import sys import textwrap import attr -import py import six import _pytest.assertion as plugin @@ -455,10 +454,13 @@ class TestAssert_reprcompare(object): assert len(expl) > 1 def test_Sequence(self): - col = py.builtin._tryimport("collections.abc", "collections", "sys") - if not hasattr(col, "MutableSequence"): + if sys.version_info >= (3, 3): + import collections.abc as collections_abc + else: + import collections as collections_abc + if not hasattr(collections_abc, "MutableSequence"): pytest.skip("cannot import MutableSequence") - MutableSequence = col.MutableSequence + MutableSequence = collections_abc.MutableSequence class TestSequence(MutableSequence): # works with a Sequence subclass def __init__(self, iterable):