[Pytest-commit] commit/pytest: hpk42: internally make varnames() deal with classes's __init__,
commits-noreply at bitbucket.org
commits-noreply at bitbucket.org
Tue Nov 19 15:33:58 CET 2013
1 new commit in pytest:
https://bitbucket.org/hpk42/pytest/commits/282e0cca851c/
Changeset: 282e0cca851c
User: hpk42
Date: 2013-11-19 15:33:52
Summary: internally make varnames() deal with classes's __init__,
although it's not needed by pytest itself atm. Also
fix caching. Fixes issue376.
Affected #: 3 files
diff -r f9e200cee8f7ec364af514d38f5f8865509fa227 -r 282e0cca851c58e64ddaeeb56907f4376927ac13 CHANGELOG
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -48,6 +48,10 @@
- xfail a test on pypy that checks wrong encoding/ascii (pypy does
not error out). fixes issue385.
+- internally make varnames() deal with classes's __init__,
+ although it's not needed by pytest itself atm. Also
+ fix caching. Fixes issue376.
+
Changes between 2.4.1 and 2.4.2
-----------------------------------
diff -r f9e200cee8f7ec364af514d38f5f8865509fa227 -r 282e0cca851c58e64ddaeeb56907f4376927ac13 _pytest/core.py
--- a/_pytest/core.py
+++ b/_pytest/core.py
@@ -304,19 +304,36 @@
return kwargs
def varnames(func):
+ """ return argument name tuple for a function, method, class or callable.
+
+ In case of a class, its "__init__" method is considered.
+ For methods the "self" parameter is not included unless you are passing
+ an unbound method with Python3 (which has no supports for unbound methods)
+ """
+ cache = getattr(func, "__dict__", {})
try:
- return func._varnames
- except AttributeError:
+ return cache["_varnames"]
+ except KeyError:
pass
- if not inspect.isfunction(func) and not inspect.ismethod(func):
- func = getattr(func, '__call__', func)
- ismethod = inspect.ismethod(func)
+ if inspect.isclass(func):
+ try:
+ func = func.__init__
+ except AttributeError:
+ return ()
+ ismethod = True
+ else:
+ if not inspect.isfunction(func) and not inspect.ismethod(func):
+ func = getattr(func, '__call__', func)
+ ismethod = inspect.ismethod(func)
rawcode = py.code.getrawcode(func)
try:
x = rawcode.co_varnames[ismethod:rawcode.co_argcount]
except AttributeError:
x = ()
- py.builtin._getfuncdict(func)['_varnames'] = x
+ try:
+ cache["_varnames"] = x
+ except TypeError:
+ pass
return x
class HookRelay:
diff -r f9e200cee8f7ec364af514d38f5f8865509fa227 -r 282e0cca851c58e64ddaeeb56907f4376927ac13 testing/test_core.py
--- a/testing/test_core.py
+++ b/testing/test_core.py
@@ -438,6 +438,15 @@
assert varnames(A().f) == ('y',)
assert varnames(B()) == ('z',)
+def test_varnames_class():
+ class C:
+ def __init__(self, x):
+ pass
+ class D:
+ pass
+ assert varnames(C) == ("x",)
+ assert varnames(D) == ()
+
class TestMultiCall:
def test_uses_copy_of_methods(self):
l = [lambda: 42]
@@ -654,8 +663,7 @@
def test_importplugin_issue375(testdir):
testdir.makepyfile(qwe="import aaaa")
- with pytest.raises(ImportError) as excinfo:
- importplugin("qwe")
+ excinfo = pytest.raises(ImportError, lambda: importplugin("qwe"))
assert "qwe" not in str(excinfo.value)
assert "aaaa" in str(excinfo.value)
Repository URL: https://bitbucket.org/hpk42/pytest/
--
This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.
More information about the pytest-commit
mailing list