Skip to content

Commit 5d1155c

Browse files
committed
Closes #13258: Use callable() built-in in the standard library.
1 parent f99e4b5 commit 5d1155c

File tree

25 files changed

+48
-51
lines changed

25 files changed

+48
-51
lines changed

Lib/argparse.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,6 @@
9292
from gettext import gettext as _, ngettext
9393

9494

95-
def _callable(obj):
96-
return hasattr(obj, '__call__') or hasattr(obj, '__bases__')
97-
98-
9995
SUPPRESS = '==SUPPRESS=='
10096

10197
OPTIONAL = '?'
@@ -1286,13 +1282,13 @@ def add_argument(self, *args, **kwargs):
12861282

12871283
# create the action object, and add it to the parser
12881284
action_class = self._pop_action_class(kwargs)
1289-
if not _callable(action_class):
1285+
if not callable(action_class):
12901286
raise ValueError('unknown action "%s"' % (action_class,))
12911287
action = action_class(**kwargs)
12921288

12931289
# raise an error if the action type is not callable
12941290
type_func = self._registry_get('type', action.type, action.type)
1295-
if not _callable(type_func):
1291+
if not callable(type_func):
12961292
raise ValueError('%r is not callable' % (type_func,))
12971293

12981294
# raise an error if the metavar does not match the type
@@ -2240,7 +2236,7 @@ def _get_values(self, action, arg_strings):
22402236

22412237
def _get_value(self, action, arg_string):
22422238
type_func = self._registry_get('type', action.type, action.type)
2243-
if not _callable(type_func):
2239+
if not callable(type_func):
22442240
msg = _('%r is not callable')
22452241
raise ArgumentError(action, msg % type_func)
22462242

Lib/copyreg.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
dispatch_table = {}
1111

1212
def pickle(ob_type, pickle_function, constructor_ob=None):
13-
if not hasattr(pickle_function, '__call__'):
13+
if not callable(pickle_function):
1414
raise TypeError("reduction functions must be callable")
1515
dispatch_table[ob_type] = pickle_function
1616

@@ -20,7 +20,7 @@ def pickle(ob_type, pickle_function, constructor_ob=None):
2020
constructor(constructor_ob)
2121

2222
def constructor(object):
23-
if not hasattr(object, '__call__'):
23+
if not callable(object):
2424
raise TypeError("constructors must be callable")
2525

2626
# Example: provide pickling support for complex numbers.

Lib/distutils/dist.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ def _parse_command_opts(self, parser, args):
537537
for (help_option, short, desc, func) in cmd_class.help_options:
538538
if hasattr(opts, parser.get_attr_name(help_option)):
539539
help_option_found=1
540-
if hasattr(func, '__call__'):
540+
if callable(func):
541541
func()
542542
else:
543543
raise DistutilsClassError(

Lib/encodings/__init__.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,11 @@ def search_function(encoding):
120120
if not 4 <= len(entry) <= 7:
121121
raise CodecRegistryError('module "%s" (%s) failed to register'
122122
% (mod.__name__, mod.__file__))
123-
if not hasattr(entry[0], '__call__') or \
124-
not hasattr(entry[1], '__call__') or \
125-
(entry[2] is not None and not hasattr(entry[2], '__call__')) or \
126-
(entry[3] is not None and not hasattr(entry[3], '__call__')) or \
127-
(len(entry) > 4 and entry[4] is not None and not hasattr(entry[4], '__call__')) or \
128-
(len(entry) > 5 and entry[5] is not None and not hasattr(entry[5], '__call__')):
123+
if not callable(entry[0]) or not callable(entry[1]) or \
124+
(entry[2] is not None and not callable(entry[2])) or \
125+
(entry[3] is not None and not callable(entry[3])) or \
126+
(len(entry) > 4 and entry[4] is not None and not callable(entry[4])) or \
127+
(len(entry) > 5 and entry[5] is not None and not callable(entry[5])):
129128
raise CodecRegistryError('incompatible codecs in module "%s" (%s)'
130129
% (mod.__name__, mod.__file__))
131130
if len(entry)<7 or entry[6] is None:

Lib/fileinput.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,11 @@ def __init__(self, files=None, inplace=False, backup="", bufsize=0,
225225
raise ValueError("FileInput opening mode must be one of "
226226
"'r', 'rU', 'U' and 'rb'")
227227
self._mode = mode
228-
if inplace and openhook:
229-
raise ValueError("FileInput cannot use an opening hook in inplace mode")
230-
elif openhook and not hasattr(openhook, '__call__'):
231-
raise ValueError("FileInput openhook must be callable")
228+
if openhook:
229+
if inplace:
230+
raise ValueError("FileInput cannot use an opening hook in inplace mode")
231+
if not callable(openhook):
232+
raise ValueError("FileInput openhook must be callable")
232233
self._openhook = openhook
233234

234235
def __del__(self):

Lib/hmac.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def __init__(self, key, msg = None, digestmod = None):
3939
import hashlib
4040
digestmod = hashlib.md5
4141

42-
if hasattr(digestmod, '__call__'):
42+
if callable(digestmod):
4343
self.digest_cons = digestmod
4444
else:
4545
self.digest_cons = lambda d=b'': digestmod.new(d)

Lib/idlelib/rpc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ def _getmethods(obj, methods):
570570
# Adds names to dictionary argument 'methods'
571571
for name in dir(obj):
572572
attr = getattr(obj, name)
573-
if hasattr(attr, '__call__'):
573+
if callable(attr):
574574
methods[name] = 1
575575
if isinstance(obj, type):
576576
for super in obj.__bases__:
@@ -579,7 +579,7 @@ def _getmethods(obj, methods):
579579
def _getattributes(obj, attributes):
580580
for name in dir(obj):
581581
attr = getattr(obj, name)
582-
if not hasattr(attr, '__call__'):
582+
if not callable(attr):
583583
attributes[name] = 1
584584

585585
class MethodProxy(object):

Lib/logging/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ def convert(self, value):
471471
def configure_custom(self, config):
472472
"""Configure an object with a user-supplied factory."""
473473
c = config.pop('()')
474-
if not hasattr(c, '__call__'):
474+
if not callable(c):
475475
c = self.resolve(c)
476476
props = config.pop('.', None)
477477
# Check for valid identifiers
@@ -690,7 +690,7 @@ def configure_handler(self, config):
690690
filters = config.pop('filters', None)
691691
if '()' in config:
692692
c = config.pop('()')
693-
if not hasattr(c, '__call__') and hasattr(types, 'ClassType') and type(c) != types.ClassType:
693+
if not callable(c):
694694
c = self.resolve(c)
695695
factory = c
696696
else:

Lib/multiprocessing/managers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def all_methods(obj):
134134
temp = []
135135
for name in dir(obj):
136136
func = getattr(obj, name)
137-
if hasattr(func, '__call__'):
137+
if callable(func):
138138
temp.append(name)
139139
return temp
140140

@@ -510,7 +510,7 @@ def start(self, initializer=None, initargs=()):
510510
'''
511511
assert self._state.value == State.INITIAL
512512

513-
if initializer is not None and not hasattr(initializer, '__call__'):
513+
if initializer is not None and not callable(initializer):
514514
raise TypeError('initializer must be a callable')
515515

516516
# pipe over which we will retrieve address of server

Lib/multiprocessing/pool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def __init__(self, processes=None, initializer=None, initargs=(),
151151
if processes < 1:
152152
raise ValueError("Number of processes must be at least 1")
153153

154-
if initializer is not None and not hasattr(initializer, '__call__'):
154+
if initializer is not None and not callable(initializer):
155155
raise TypeError('initializer must be a callable')
156156

157157
self._processes = processes

0 commit comments

Comments
 (0)