Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove pytest internal usage of the namespace hook #2315

Merged
merged 25 commits into from
Apr 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5818e65
remove pytest_namespace from _pytest/assertion
RonnyPfannschmidt Feb 28, 2017
fab9b99
remove pytest_namespace from _pytest.freeze_support
RonnyPfannschmidt Feb 28, 2017
794fd56
remove pytest_namespace from _pytest/debugging.py
RonnyPfannschmidt Feb 28, 2017
c74103f
remove pytest_namespace from recwarn and fixture decorators
RonnyPfannschmidt Feb 28, 2017
6a02cdb
remove pytest_namespace from _pytest/runner.py
RonnyPfannschmidt Feb 28, 2017
90788de
remove pytest_namespace from _pytest.mark and fix latent pytest nesti…
RonnyPfannschmidt Feb 28, 2017
9b755f6
remove pytest_namespace from _pytest.skipping
RonnyPfannschmidt Feb 28, 2017
7d797b7
add a note about the deprecation of the pytest_namespace hook
RonnyPfannschmidt Feb 28, 2017
839c936
_pytest.mark: fix unconfigure after bad configure, still potential bug
RonnyPfannschmidt Feb 28, 2017
9b58d6e
prepare a own pytest.collect fake module in oder to remove the nested…
RonnyPfannschmidt Feb 28, 2017
61f418a
hollow out pytest_namespace in _pytest.fixtures
RonnyPfannschmidt Feb 28, 2017
99c8f2d
remove pytest_namespace from _pytest.main
RonnyPfannschmidt Feb 28, 2017
ae23478
remove pytest_namespace from _pytest.python
RonnyPfannschmidt Feb 28, 2017
23bc981
remove pytest_namespace from _pytest.fixtures
RonnyPfannschmidt Feb 28, 2017
809c36e
add a changelog note for pytest_namespace
RonnyPfannschmidt Mar 1, 2017
92f6ab1
fix all singular internal module imports and add a test for them
RonnyPfannschmidt Mar 15, 2017
bb750a7
add missed file
RonnyPfannschmidt Mar 15, 2017
4d31ea8
add a comment explaining the modimport tests
RonnyPfannschmidt Mar 15, 2017
7cdefce
fix up oversights
RonnyPfannschmidt Mar 16, 2017
147bb8a
correct setting pytest.config
RonnyPfannschmidt Mar 16, 2017
c9ab421
fix python2 only import loop failure
RonnyPfannschmidt Mar 16, 2017
efe0340
fixup nose/pytest plugins
RonnyPfannschmidt Mar 16, 2017
6165939
fix rebase mistakes
RonnyPfannschmidt Mar 20, 2017
ebeba79
remove the namespace hook from mark after the param feature merge
RonnyPfannschmidt Mar 20, 2017
afb1778
put in a singular namespace hook to work around the strange issue
RonnyPfannschmidt Mar 28, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ New Features
Changes
-------

* remove all internal uses of pytest_namespace hooks,
this is to prepare the removal of preloadconfig in pytest 4.0
Thanks to `@RonnyPfannschmidt`_ for the PR.

* Old-style classes have been changed to new-style classes in order to improve
compatibility with Python 2. Thanks to `@MichalTHEDUDE`_ and `@mandeep`_ for the PR (`#2147`_).

Expand Down
3 changes: 0 additions & 3 deletions _pytest/assertion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ def pytest_addoption(parser):
expression information.""")


def pytest_namespace():
return {'register_assert_rewrite': register_assert_rewrite}


def register_assert_rewrite(*names):
"""Register one or more module names to be rewritten on import.
Expand Down
32 changes: 32 additions & 0 deletions _pytest/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,29 @@ def safe_str(v):
return v.encode('ascii', errors)


COLLECT_FAKEMODULE_ATTRIBUTES = (
'Collector',
'Module',
'Generator',
'Function',
'Instance',
'Session',
'Item',
'Class',
'File',
'_fillfuncargs',
)


def _setup_collect_fakemodule():
from types import ModuleType
import pytest
pytest.collect = ModuleType('pytest.collect')
pytest.collect.__all__ = [] # used for setns
for attr in COLLECT_FAKEMODULE_ATTRIBUTES:
setattr(pytest.collect, attr, getattr(pytest, attr))


if _PY2:
from py.io import TextIO as CaptureIO
else:
Expand All @@ -268,3 +291,12 @@ def __init__(self):

def getvalue(self):
return self.buffer.getvalue().decode('UTF-8')

class FuncargnamesCompatAttr(object):
""" helper class so that Metafunc, Function and FixtureRequest
don't need to each define the "funcargnames" compatibility attribute.
"""
@property
def funcargnames(self):
""" alias attribute for ``fixturenames`` for pre-2.3 compatibility"""
return self.fixturenames
1 change: 1 addition & 0 deletions _pytest/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def directory_arg(path, optname):
"junitxml resultlog doctest cacheprovider freeze_support "
"setuponly setupplan warnings").split()


builtin_plugins = set(default_plugins)
builtin_plugins.add("pytester")

Expand Down
21 changes: 10 additions & 11 deletions _pytest/debugging.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import pdb
import sys

import pytest


def pytest_addoption(parser):
Expand All @@ -16,8 +15,6 @@ def pytest_addoption(parser):
help="start a custom interactive Python debugger on errors. "
"For example: --pdbcls=IPython.terminal.debugger:TerminalPdb")

def pytest_namespace():
return {'set_trace': pytestPDB().set_trace}

def pytest_configure(config):
if config.getvalue("usepdb_cls"):
Expand All @@ -37,31 +34,33 @@ def fin():
pytestPDB._config = None
pytestPDB._pdb_cls = pdb.Pdb

pdb.set_trace = pytest.set_trace
pdb.set_trace = pytestPDB.set_trace
pytestPDB._pluginmanager = config.pluginmanager
pytestPDB._config = config
pytestPDB._pdb_cls = pdb_cls
config._cleanup.append(fin)


class pytestPDB(object):
""" Pseudo PDB that defers to the real pdb. """
_pluginmanager = None
_config = None
_pdb_cls = pdb.Pdb

def set_trace(self):
@classmethod
def set_trace(cls):
""" invoke PDB set_trace debugging, dropping any IO capturing. """
import _pytest.config
frame = sys._getframe().f_back
if self._pluginmanager is not None:
capman = self._pluginmanager.getplugin("capturemanager")
if cls._pluginmanager is not None:
capman = cls._pluginmanager.getplugin("capturemanager")
if capman:
capman.suspendcapture(in_=True)
tw = _pytest.config.create_terminal_writer(self._config)
tw = _pytest.config.create_terminal_writer(cls._config)
tw.line()
tw.sep(">", "PDB set_trace (IO-capturing turned off)")
self._pluginmanager.hook.pytest_enter_pdb(config=self._config)
self._pdb_cls().set_trace(frame)
cls._pluginmanager.hook.pytest_enter_pdb(config=cls._config)
cls._pdb_cls().set_trace(frame)


class PdbInvoke(object):
Expand All @@ -75,7 +74,7 @@ def pytest_exception_interact(self, node, call, report):

def pytest_internalerror(self, excrepr, excinfo):
for line in str(excrepr).split("\n"):
sys.stderr.write("INTERNALERROR> %s\n" %line)
sys.stderr.write("INTERNALERROR> %s\n" % line)
sys.stderr.flush()
tb = _postmortem_traceback(excinfo)
post_mortem(tb)
Expand Down
56 changes: 20 additions & 36 deletions _pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from py._code.code import FormattedExcinfo

import py
import pytest
import warnings

import inspect
Expand All @@ -17,8 +16,16 @@
getlocation, getfuncargnames,
safe_getattr,
)
from _pytest.runner import fail
from _pytest.compat import FuncargnamesCompatAttr

def pytest_sessionstart(session):
import _pytest.python
scopename2class.update({
'class': _pytest.python.Class,
'module': _pytest.python.Module,
'function': _pytest.main.Item,
})
session._fixturemanager = FixtureManager(session)


Expand All @@ -45,19 +52,6 @@ def provide(self):
return decoratescope


def pytest_namespace():
scopename2class.update({
'class': pytest.Class,
'module': pytest.Module,
'function': pytest.Item,
})
return {
'fixture': fixture,
'yield_fixture': yield_fixture,
'collect': {'_fillfuncargs': fillfixtures}
}


def get_scope_node(node, scope):
cls = scopename2class.get(scope)
if cls is None:
Expand Down Expand Up @@ -105,7 +99,7 @@ def add_funcarg_pseudo_fixture_def(collector, metafunc, fixturemanager):
if scope != "function":
node = get_scope_node(collector, scope)
if node is None:
assert scope == "class" and isinstance(collector, pytest.Module)
assert scope == "class" and isinstance(collector, _pytest.python.Module)
# use module-level collector for class-scope (for now)
node = collector
if node and argname in node._name2pseudofixturedef:
Expand Down Expand Up @@ -221,17 +215,6 @@ def slice_items(items, ignore, scoped_argkeys_cache):
return items, None, None, None



class FuncargnamesCompatAttr(object):
""" helper class so that Metafunc, Function and FixtureRequest
don't need to each define the "funcargnames" compatibility attribute.
"""
@property
def funcargnames(self):
""" alias attribute for ``fixturenames`` for pre-2.3 compatibility"""
return self.fixturenames


def fillfixtures(function):
""" fill missing funcargs for a test function. """
try:
Expand Down Expand Up @@ -327,7 +310,7 @@ def function(self):
@scopeproperty("class")
def cls(self):
""" class (can be None) where the test function was collected. """
clscol = self._pyfuncitem.getparent(pytest.Class)
clscol = self._pyfuncitem.getparent(_pytest.python.Class)
if clscol:
return clscol.obj

Expand All @@ -345,7 +328,7 @@ def instance(self):
@scopeproperty()
def module(self):
""" python module object where the test function was collected. """
return self._pyfuncitem.getparent(pytest.Module).obj
return self._pyfuncitem.getparent(_pytest.python.Module).obj

@scopeproperty()
def fspath(self):
Expand Down Expand Up @@ -508,7 +491,7 @@ def _getfixturevalue(self, fixturedef):
source_lineno,
)
)
pytest.fail(msg)
fail(msg)
else:
# indices might not be set if old-style metafunc.addcall() was used
param_index = funcitem.callspec.indices.get(argname, 0)
Expand Down Expand Up @@ -541,10 +524,10 @@ def _check_scope(self, argname, invoking_scope, requested_scope):
if scopemismatch(invoking_scope, requested_scope):
# try to report something helpful
lines = self._factorytraceback()
pytest.fail("ScopeMismatch: You tried to access the %r scoped "
"fixture %r with a %r scoped request object, "
"involved factories\n%s" %(
(requested_scope, argname, invoking_scope, "\n".join(lines))),
fail("ScopeMismatch: You tried to access the %r scoped "
"fixture %r with a %r scoped request object, "
"involved factories\n%s" % (
(requested_scope, argname, invoking_scope, "\n".join(lines))),
pytrace=False)

def _factorytraceback(self):
Expand All @@ -554,7 +537,7 @@ def _factorytraceback(self):
fs, lineno = getfslineno(factory)
p = self._pyfuncitem.session.fspath.bestrelpath(fs)
args = _format_args(factory)
lines.append("%s:%d: def %s%s" %(
lines.append("%s:%d: def %s%s" % (
p, lineno, factory.__name__, args))
return lines

Expand Down Expand Up @@ -699,8 +682,9 @@ def fail_fixturefunc(fixturefunc, msg):
fs, lineno = getfslineno(fixturefunc)
location = "%s:%s" % (fs, lineno+1)
source = _pytest._code.Source(fixturefunc)
pytest.fail(msg + ":\n\n" + str(source.indent()) + "\n" + location,
pytrace=False)
fail(msg + ":\n\n" + str(source.indent()) + "\n" + location,
pytrace=False)


def call_fixture_func(fixturefunc, request, kwargs):
yieldctx = is_generator(fixturefunc)
Expand Down
3 changes: 0 additions & 3 deletions _pytest/freeze_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
from __future__ import absolute_import, division, print_function


def pytest_namespace():
return {'freeze_includes': freeze_includes}


def freeze_includes():
"""
Expand Down
4 changes: 3 additions & 1 deletion _pytest/hookspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ def pytest_addhooks(pluginmanager):

@hookspec(historic=True)
def pytest_namespace():
"""return dict of name->object to be made globally available in
"""
DEPRECATED: this hook causes direct monkeypatching on pytest, its use is strongly discouraged
return dict of name->object to be made globally available in
the pytest namespace. This hook is called at plugin registration
time.
"""
Expand Down
Loading