Skip to content

Commit

Permalink
Merge pull request #335 from rshest/getargspec-to-getfullargspec
Browse files Browse the repository at this point in the history
Change `getargspec` to `getfullargspec`
  • Loading branch information
little-dude authored Sep 5, 2017
2 parents 08a3855 + 2bc9d64 commit 26c1d12
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 33 deletions.
7 changes: 3 additions & 4 deletions nose2/plugins/loader/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ def teardown():


import sys
import inspect
import types

from nose2 import util
Expand Down Expand Up @@ -89,8 +88,8 @@ def loadTestsFromName(self, event):
parent, obj, name, index = result
if (isinstance(obj, types.FunctionType) and not
util.isgenerator(obj) and not
hasattr(obj, 'paramList') and not
inspect.getargspec(obj).args):
hasattr(obj, 'paramList') and
util.num_expected_args(obj) == 0):
suite = event.loader.suiteClass()
suite.addTests(self._createTests(obj))
event.handled = True
Expand All @@ -103,7 +102,7 @@ def loadTestsFromModule(self, event):
def is_test(obj):
if not obj.__name__.startswith(self.session.testMethodPrefix):
return False
if inspect.getargspec(obj).args:
if util.num_expected_args(obj) > 0:
return False
return True

Expand Down
4 changes: 1 addition & 3 deletions nose2/suite.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import sys
import inspect
import logging

from nose2 import util
Expand Down Expand Up @@ -147,8 +146,7 @@ def _allLayers(self, test, method, reverse=False):
def _inLayer(self, layer, test, method):
meth = self._getBoundClassmethod(layer, method)
if meth:
args, _, _, _ = inspect.getargspec(meth)
if len(args) > 1:
if util.num_expected_args(meth) > 1:
meth(test)
else:
meth()
Expand Down
32 changes: 6 additions & 26 deletions nose2/tools/such.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from contextlib import contextmanager
import inspect
import logging
import sys

Expand All @@ -9,6 +8,7 @@
from nose2 import util
from nose2.main import PluggableTestProgram


log = logging.getLogger(__name__)

__unittest = True
Expand Down Expand Up @@ -273,21 +273,13 @@ def _test(s, *args):
if setups:
def setUp(self):
for func in setups:
args, _, _, _ = inspect.getargspec(func)
if args:
func(self)
else:
func()
util.call_with_args_if_expected(func, self)
attr['setUp'] = setUp
teardowns = getattr(parent_layer, 'testTeardowns', []) + group._test_teardowns[:]
if teardowns:
def tearDown(self):
for func in teardowns:
args, _, _, _ = inspect.getargspec(func)
if args:
func(self)
else:
func()
util.call_with_args_if_expected(func, self)
attr['tearDown'] = tearDown

def methodDescription(self):
Expand All @@ -302,19 +294,11 @@ def _makeLayer(self, group, parent_layer=None, position=0):

def setUp(cls):
for func in cls.setups:
args, _, _, _ = inspect.getargspec(func)
if args:
func(self)
else:
func()
util.call_with_args_if_expected(func, self)

def tearDown(cls):
for func in cls.teardowns:
args, _, _, _ = inspect.getargspec(func)
if args:
func(self)
else:
func()
util.call_with_args_if_expected(func, self)

attr = {
'description': group.description,
Expand Down Expand Up @@ -410,11 +394,7 @@ def __init__(self, group, func, description):
def __call__(self, testcase, *args):
# ... only if it takes an arg
self._helper = testcase
funcargs, _, _, _ = inspect.getargspec(self.func)
if funcargs:
self.func(testcase, *args)
else:
self.func()
util.call_with_args_if_expected(self.func, testcase, *args)

def __getattr__(self, attr):
return getattr(self._helper, attr)
19 changes: 19 additions & 0 deletions nose2/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import traceback
import platform
import six
import inspect
from inspect import isgeneratorfunction # new in 2.6


Expand Down Expand Up @@ -373,3 +374,21 @@ def ancestry(layer):

def bases_and_mixins(layer):
return (layer.__bases__ + getattr(layer, 'mixins', ()))


def num_expected_args(func):
"""Return the number of arguments that :func: expects"""
if six.PY2:
return len(inspect.getargspec(func)[0])
else:
return len(inspect.getfullargspec(func)[0])


def call_with_args_if_expected(func, *args):
"""Take :func: and call it with supplied :args:, in case that signature expects any.
Otherwise call the function without any arguments.
"""
if num_expected_args(func) > 0:
func(*args)
else:
func()

0 comments on commit 26c1d12

Please sign in to comment.