Skip to content

Commit

Permalink
Merge pull request #3047 from charris/2to3-callable
Browse files Browse the repository at this point in the history
2to3: Fix callable.
  • Loading branch information
njsmith committed Feb 28, 2013
2 parents e1c7c4d + dd13084 commit 4b361f6
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 14 deletions.
3 changes: 2 additions & 1 deletion doc/sphinxext/docscrape.py
Expand Up @@ -8,6 +8,7 @@
import pydoc import pydoc
from StringIO import StringIO from StringIO import StringIO
from warnings import warn from warnings import warn
import collections


class Reader(object): class Reader(object):
"""A line-based string reader. """A line-based string reader.
Expand Down Expand Up @@ -495,7 +496,7 @@ def methods(self):
return [name for name,func in inspect.getmembers(self._cls) return [name for name,func in inspect.getmembers(self._cls)
if ((not name.startswith('_') if ((not name.startswith('_')
or name in self.extra_public_methods) or name in self.extra_public_methods)
and callable(func))] and isinstance(func, collections.Callable))]


@property @property
def properties(self): def properties(self):
Expand Down
3 changes: 2 additions & 1 deletion doc/sphinxext/docscrape_sphinx.py
@@ -1,6 +1,7 @@
import re, inspect, textwrap, pydoc import re, inspect, textwrap, pydoc
import sphinx import sphinx
from docscrape import NumpyDocString, FunctionDoc, ClassDoc from docscrape import NumpyDocString, FunctionDoc, ClassDoc
import collections


class SphinxDocString(NumpyDocString): class SphinxDocString(NumpyDocString):
def __init__(self, docstring, config={}): def __init__(self, docstring, config={}):
Expand Down Expand Up @@ -212,7 +213,7 @@ def get_doc_object(obj, what=None, doc=None, config={}):
what = 'class' what = 'class'
elif inspect.ismodule(obj): elif inspect.ismodule(obj):
what = 'module' what = 'module'
elif callable(obj): elif isinstance(obj, collections.Callable):
what = 'function' what = 'function'
else: else:
what = 'object' what = 'object'
Expand Down
3 changes: 2 additions & 1 deletion doc/sphinxext/linkcode.py
Expand Up @@ -10,6 +10,7 @@
""" """


import warnings import warnings
import collections
warnings.warn("This extension has been submitted to Sphinx upstream. " warnings.warn("This extension has been submitted to Sphinx upstream. "
"Use the version from there if it is accepted " "Use the version from there if it is accepted "
"https://bitbucket.org/birkenfeld/sphinx/pull-request/47/sphinxextlinkcode", "https://bitbucket.org/birkenfeld/sphinx/pull-request/47/sphinxextlinkcode",
Expand All @@ -29,7 +30,7 @@ def doctree_read(app, doctree):
env = app.builder.env env = app.builder.env


resolve_target = getattr(env.config, 'linkcode_resolve', None) resolve_target = getattr(env.config, 'linkcode_resolve', None)
if not callable(env.config.linkcode_resolve): if not isinstance(env.config.linkcode_resolve, collections.Callable):
raise LinkcodeError( raise LinkcodeError(
"Function `linkcode_resolve` is not given in conf.py") "Function `linkcode_resolve` is not given in conf.py")


Expand Down
3 changes: 2 additions & 1 deletion doc/sphinxext/numpydoc.py
Expand Up @@ -17,6 +17,7 @@
""" """


import sphinx import sphinx
import collections


if sphinx.__version__ < '1.0.1': if sphinx.__version__ < '1.0.1':
raise RuntimeError("Sphinx 1.0.1 or newer is required") raise RuntimeError("Sphinx 1.0.1 or newer is required")
Expand Down Expand Up @@ -82,7 +83,7 @@ def mangle_signature(app, what, name, obj, options, sig, retann):
'initializes x; see ' in pydoc.getdoc(obj.__init__))): 'initializes x; see ' in pydoc.getdoc(obj.__init__))):
return '', '' return '', ''


if not (callable(obj) or hasattr(obj, '__argspec_is_invalid_')): return if not (isinstance(obj, collections.Callable) or hasattr(obj, '__argspec_is_invalid_')): return
if not hasattr(obj, '__doc__'): return if not hasattr(obj, '__doc__'): return


doc = SphinxDocString(pydoc.getdoc(obj)) doc = SphinxDocString(pydoc.getdoc(obj))
Expand Down
3 changes: 2 additions & 1 deletion doc/sphinxext/traitsdoc.py
Expand Up @@ -25,6 +25,7 @@
import numpydoc import numpydoc


import comment_eater import comment_eater
import collections


class SphinxTraitsDoc(SphinxClassDoc): class SphinxTraitsDoc(SphinxClassDoc):
def __init__(self, cls, modulename='', func_doc=SphinxFunctionDoc): def __init__(self, cls, modulename='', func_doc=SphinxFunctionDoc):
Expand Down Expand Up @@ -117,7 +118,7 @@ def get_doc_object(obj, what=None, config=None):
what = 'class' what = 'class'
elif inspect.ismodule(obj): elif inspect.ismodule(obj):
what = 'module' what = 'module'
elif callable(obj): elif isinstance(obj, collections.Callable):
what = 'function' what = 'function'
else: else:
what = 'object' what = 'object'
Expand Down
3 changes: 2 additions & 1 deletion doc/summarize.py
Expand Up @@ -7,6 +7,7 @@
""" """


import os, glob, re, sys, inspect, optparse import os, glob, re, sys, inspect, optparse
import collections
sys.path.append(os.path.join(os.path.dirname(__file__), 'sphinxext')) sys.path.append(os.path.join(os.path.dirname(__file__), 'sphinxext'))
from sphinxext.phantom_import import import_phantom_module from sphinxext.phantom_import import import_phantom_module


Expand Down Expand Up @@ -134,7 +135,7 @@ def get_undocumented(documented, module, module_name=None, skip=[]):


if full_name in skip: continue if full_name in skip: continue
if full_name.startswith('numpy.') and full_name[6:] in skip: continue if full_name.startswith('numpy.') and full_name[6:] in skip: continue
if not (inspect.ismodule(obj) or callable(obj) or inspect.isclass(obj)): if not (inspect.ismodule(obj) or isinstance(obj, collections.Callable) or inspect.isclass(obj)):
continue continue


if full_name not in documented: if full_name not in documented:
Expand Down
5 changes: 3 additions & 2 deletions numpy/core/numeric.py
Expand Up @@ -29,6 +29,7 @@
from umath import * from umath import *
import numerictypes import numerictypes
from numerictypes import * from numerictypes import *
import collections




if sys.version_info[0] < 3: if sys.version_info[0] < 3:
Expand Down Expand Up @@ -2445,8 +2446,8 @@ def seterrcall(func):
{'over': 'log', 'divide': 'log', 'invalid': 'log', 'under': 'log'} {'over': 'log', 'divide': 'log', 'invalid': 'log', 'under': 'log'}
""" """
if func is not None and not callable(func): if func is not None and not isinstance(func, collections.Callable):
if not hasattr(func, 'write') or not callable(func.write): if not hasattr(func, 'write') or not isinstance(func.write, collections.Callable):
raise ValueError("Only callable can be used as callback") raise ValueError("Only callable can be used as callback")
pyvals = umath.geterrobj() pyvals = umath.geterrobj()
old = geterrcall() old = geterrcall()
Expand Down
3 changes: 2 additions & 1 deletion numpy/core/tests/test_records.py
Expand Up @@ -4,6 +4,7 @@
from numpy.compat import asbytes, asunicode from numpy.compat import asbytes, asunicode


import warnings import warnings
import collections




class TestFromrecords(TestCase): class TestFromrecords(TestCase):
Expand Down Expand Up @@ -94,7 +95,7 @@ def test_recarray_conflict_fields(self):
assert_array_equal(ra['shape'], [['A', 'B', 'C']]) assert_array_equal(ra['shape'], [['A', 'B', 'C']])
ra.field = 5 ra.field = 5
assert_array_equal(ra['field'], [[5, 5, 5]]) assert_array_equal(ra['field'], [[5, 5, 5]])
assert_(callable(ra.field)) assert_(isinstance(ra.field, collections.Callable))


def test_fromrecords_with_explicit_dtype(self): def test_fromrecords_with_explicit_dtype(self):
a = np.rec.fromrecords([(1, 'a'), (2, 'bbb')], a = np.rec.fromrecords([(1, 'a'), (2, 'bbb')],
Expand Down
3 changes: 2 additions & 1 deletion numpy/lib/function_base.py
Expand Up @@ -30,6 +30,7 @@
from utils import deprecate from utils import deprecate
from _compiled_base import add_newdoc_ufunc from _compiled_base import add_newdoc_ufunc
import numpy as np import numpy as np
import collections




def iterable(y): def iterable(y):
Expand Down Expand Up @@ -707,7 +708,7 @@ def piecewise(x, condlist, funclist, *args, **kw):
y = zeros(x.shape, x.dtype) y = zeros(x.shape, x.dtype)
for k in range(n): for k in range(n):
item = funclist[k] item = funclist[k]
if not callable(item): if not isinstance(item, collections.Callable):
y[condlist[k]] = item y[condlist[k]] = item
else: else:
vals = x[condlist[k]] vals = x[condlist[k]]
Expand Down
3 changes: 2 additions & 1 deletion numpy/matrixlib/tests/test_defmatrix.py
Expand Up @@ -4,6 +4,7 @@
from numpy.matrixlib.defmatrix import matrix_power from numpy.matrixlib.defmatrix import matrix_power
from numpy.matrixlib import mat from numpy.matrixlib import mat
import numpy as np import numpy as np
import collections


class TestCtor(TestCase): class TestCtor(TestCase):
def test_basic(self): def test_basic(self):
Expand Down Expand Up @@ -285,7 +286,7 @@ def test_instance_methods(self):
if attrib.startswith('_') or attrib in excluded_methods: if attrib.startswith('_') or attrib in excluded_methods:
continue continue
f = getattr(a, attrib) f = getattr(a, attrib)
if callable(f): if isinstance(f, collections.Callable):
# reset contents of a # reset contents of a
a.astype('f8') a.astype('f8')
a.fill(1.0) a.fill(1.0)
Expand Down
7 changes: 4 additions & 3 deletions numpy/testing/decorators.py
Expand Up @@ -18,6 +18,7 @@


from numpy.testing.utils import \ from numpy.testing.utils import \
WarningManager, WarningMessage WarningManager, WarningMessage
import collections


def slow(t): def slow(t):
""" """
Expand Down Expand Up @@ -122,7 +123,7 @@ def skip_decorator(f):
import nose import nose


# Allow for both boolean or callable skip conditions. # Allow for both boolean or callable skip conditions.
if callable(skip_condition): if isinstance(skip_condition, collections.Callable):
skip_val = lambda : skip_condition() skip_val = lambda : skip_condition()
else: else:
skip_val = lambda : skip_condition skip_val = lambda : skip_condition
Expand Down Expand Up @@ -198,7 +199,7 @@ def knownfailureif(fail_condition, msg=None):
msg = 'Test skipped due to known failure' msg = 'Test skipped due to known failure'


# Allow for both boolean or callable known failure conditions. # Allow for both boolean or callable known failure conditions.
if callable(fail_condition): if isinstance(fail_condition, collections.Callable):
fail_val = lambda : fail_condition() fail_val = lambda : fail_condition()
else: else:
fail_val = lambda : fail_condition fail_val = lambda : fail_condition
Expand Down Expand Up @@ -264,7 +265,7 @@ def _deprecated_imp(*args, **kwargs):
finally: finally:
ctx.__exit__() ctx.__exit__()


if callable(conditional): if isinstance(conditional, collections.Callable):
cond = conditional() cond = conditional()
else: else:
cond = conditional cond = conditional
Expand Down

0 comments on commit 4b361f6

Please sign in to comment.