parent
3401049dea
commit
ff8d787cd5
|
@ -27,7 +27,7 @@ def pytest_funcarg__monkeypatch(request):
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def derive_importpath(import_path):
|
def derive_importpath(import_path, raising):
|
||||||
import pytest
|
import pytest
|
||||||
if not isinstance(import_path, _basestring) or "." not in import_path:
|
if not isinstance(import_path, _basestring) or "." not in import_path:
|
||||||
raise TypeError("must be absolute import path string, not %r" %
|
raise TypeError("must be absolute import path string, not %r" %
|
||||||
|
@ -51,6 +51,7 @@ def derive_importpath(import_path):
|
||||||
attr = rest.pop()
|
attr = rest.pop()
|
||||||
obj = getattr(obj, attr)
|
obj = getattr(obj, attr)
|
||||||
attr = rest[0]
|
attr = rest[0]
|
||||||
|
if raising:
|
||||||
getattr(obj, attr)
|
getattr(obj, attr)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
__tracebackhide__ = True
|
__tracebackhide__ = True
|
||||||
|
@ -95,7 +96,7 @@ class monkeypatch:
|
||||||
"setattr(target, value) with target being a dotted "
|
"setattr(target, value) with target being a dotted "
|
||||||
"import string")
|
"import string")
|
||||||
value = name
|
value = name
|
||||||
name, target = derive_importpath(target)
|
name, target = derive_importpath(target, raising)
|
||||||
|
|
||||||
oldval = getattr(target, name, notset)
|
oldval = getattr(target, name, notset)
|
||||||
if raising and oldval is notset:
|
if raising and oldval is notset:
|
||||||
|
@ -124,7 +125,7 @@ class monkeypatch:
|
||||||
raise TypeError("use delattr(target, name) or "
|
raise TypeError("use delattr(target, name) or "
|
||||||
"delattr(target) with target being a dotted "
|
"delattr(target) with target being a dotted "
|
||||||
"import string")
|
"import string")
|
||||||
name, target = derive_importpath(target)
|
name, target = derive_importpath(target, raising)
|
||||||
|
|
||||||
if not hasattr(target, name):
|
if not hasattr(target, name):
|
||||||
if raising:
|
if raising:
|
||||||
|
|
|
@ -62,6 +62,11 @@ class TestSetattrWithImportPath:
|
||||||
pytest.raises(pytest.fail.Exception,
|
pytest.raises(pytest.fail.Exception,
|
||||||
lambda: monkeypatch.setattr("os.path.qweqwe", None))
|
lambda: monkeypatch.setattr("os.path.qweqwe", None))
|
||||||
|
|
||||||
|
def test_unknown_attr_non_raising(self, monkeypatch):
|
||||||
|
# https://bitbucket.org/pytest-dev/pytest/issue/746/
|
||||||
|
monkeypatch.setattr('os.path.qweqwe', 42, raising=False)
|
||||||
|
assert os.path.qweqwe == 42
|
||||||
|
|
||||||
def test_delattr(self, monkeypatch):
|
def test_delattr(self, monkeypatch):
|
||||||
monkeypatch.delattr("os.path.abspath")
|
monkeypatch.delattr("os.path.abspath")
|
||||||
assert not hasattr(os.path, "abspath")
|
assert not hasattr(os.path, "abspath")
|
||||||
|
|
Loading…
Reference in New Issue