This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 87
/
finding.py
74 lines (59 loc) · 2.38 KB
/
finding.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""Copyright 2013 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import inspect
import sys
ALL_IMPORTED_MODULES = object()
def find_classes(modules, classes):
if classes is not None:
all_classes = set(classes)
else:
all_classes = set()
for module in _get_explicit_or_default_modules(modules):
# TODO(kurts): how is a module getting to be None??
if module is not None:
all_classes |= _find_classes_in_module(module)
return all_classes
def _get_explicit_or_default_modules(modules):
if modules is ALL_IMPORTED_MODULES:
return list(sys.modules.values())
elif modules is None:
return []
return modules
def _find_classes_in_module(module):
classes = set()
try:
# Handle find_classes_in_module when module.__bases__ is not a tuple:
# https://github.com/google/pinject/pull/54
#
# modules, such tensorboard.compat.tensorflow_stub (2.2.2) return the
# __bases__ attribute as an integer, instead of the expected tuple
#
# when the inspect.getmembers() function attemps to iterate through it
# an error is raised
#
# this pre-empts this error, by assigning an empty tuple to avoid TypeError
if not isinstance(module.__bases__, tuple):
module.__bases__ = ()
except AttributeError:
pass
for member_name, member in inspect.getmembers(module):
try:
if inspect.isclass(member) and not member_name == '__class__':
classes.add(member)
except NameError:
# In Python 3 calling isinstance() on SWIG's global cvar property
# raises:
# "NameError: Unknown C global variable"
# In that case just continue, otherwise let the Error through.
if not member_name == 'cvar':
raise
return classes