From 0d8392bc45c812c7fe90ab38792238ac7a2af3c9 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Mon, 21 Oct 2013 14:01:02 +0200 Subject: [PATCH] fix unicode handling with new monkeypatch.setattr(import_path, value) API. Thanks Rob Dennis. Fixes issue371. --- CHANGELOG | 3 +++ _pytest/monkeypatch.py | 7 ++++--- testing/test_monkeypatch.py | 6 ++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 6d2217ff2..8401f361d 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,9 @@ 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 cookies. See issue #330. diff --git a/_pytest/monkeypatch.py b/_pytest/monkeypatch.py index 4d3abc0dc..0444a5404 100644 --- a/_pytest/monkeypatch.py +++ b/_pytest/monkeypatch.py @@ -1,6 +1,7 @@ """ monkeypatching and mocking functionality. """ import os, sys +from py.builtin import _basestring def pytest_funcarg__monkeypatch(request): """The returned ``monkeypatch`` funcarg provides these @@ -28,7 +29,7 @@ def pytest_funcarg__monkeypatch(request): def derive_importpath(import_path): 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" % (import_path,)) rest = [] @@ -85,7 +86,7 @@ class monkeypatch: import inspect if value is notset: - if not isinstance(target, str): + if not isinstance(target, _basestring): raise TypeError("use setattr(target, name, value) or " "setattr(target, value) with target being a dotted " "import string") @@ -115,7 +116,7 @@ class monkeypatch: """ __tracebackhide__ = True if name is notset: - if not isinstance(target, str): + if not isinstance(target, basestring): raise TypeError("use delattr(target, name) or " "delattr(target) with target being a dotted " "import string") diff --git a/testing/test_monkeypatch.py b/testing/test_monkeypatch.py index b45698fef..61a397a9c 100644 --- a/testing/test_monkeypatch.py +++ b/testing/test_monkeypatch.py @@ -45,6 +45,12 @@ class TestSetattrWithImportPath: import _pytest 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): pytest.raises(TypeError, lambda: monkeypatch.setattr(None, None))