fix unicode handling with new monkeypatch.setattr(import_path, value)

API.  Thanks Rob Dennis.  Fixes issue371.
This commit is contained in:
holger krekel 2013-10-21 14:01:02 +02:00
parent 47d2d20d81
commit 0d8392bc45
3 changed files with 13 additions and 3 deletions

View File

@ -1,6 +1,9 @@
Changes between 2.4.2 and 2.4.3 Changes between 2.4.2 and 2.4.3
----------------------------------- -----------------------------------
- fix unicode handling with new monkeypatch.setattr(import_path, value)
API. Thanks Rob Dennis. Fixes issue371.
- In assertion rewriting mode on Python 2, fix the detection of coding - In assertion rewriting mode on Python 2, fix the detection of coding
cookies. See issue #330. cookies. See issue #330.

View File

@ -1,6 +1,7 @@
""" monkeypatching and mocking functionality. """ """ monkeypatching and mocking functionality. """
import os, sys import os, sys
from py.builtin import _basestring
def pytest_funcarg__monkeypatch(request): def pytest_funcarg__monkeypatch(request):
"""The returned ``monkeypatch`` funcarg provides these """The returned ``monkeypatch`` funcarg provides these
@ -28,7 +29,7 @@ def pytest_funcarg__monkeypatch(request):
def derive_importpath(import_path): def derive_importpath(import_path):
import pytest import pytest
if not isinstance(import_path, str) 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" %
(import_path,)) (import_path,))
rest = [] rest = []
@ -85,7 +86,7 @@ class monkeypatch:
import inspect import inspect
if value is notset: if value is notset:
if not isinstance(target, str): if not isinstance(target, _basestring):
raise TypeError("use setattr(target, name, value) or " raise TypeError("use setattr(target, name, value) or "
"setattr(target, value) with target being a dotted " "setattr(target, value) with target being a dotted "
"import string") "import string")
@ -115,7 +116,7 @@ class monkeypatch:
""" """
__tracebackhide__ = True __tracebackhide__ = True
if name is notset: if name is notset:
if not isinstance(target, str): if not isinstance(target, basestring):
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")

View File

@ -45,6 +45,12 @@ class TestSetattrWithImportPath:
import _pytest import _pytest
assert _pytest.config.Config == 42 assert _pytest.config.Config == 42
def test_unicode_string(self, monkeypatch):
monkeypatch.setattr(u"_pytest.config.Config", 42)
import _pytest
assert _pytest.config.Config == 42
monkeypatch.delattr(u"_pytest.config.Config")
def test_wrong_target(self, monkeypatch): def test_wrong_target(self, monkeypatch):
pytest.raises(TypeError, lambda: monkeypatch.setattr(None, None)) pytest.raises(TypeError, lambda: monkeypatch.setattr(None, None))