forked from tensorflow/tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfast_module_type_test.py
More file actions
71 lines (56 loc) · 2.6 KB
/
fast_module_type_test.py
File metadata and controls
71 lines (56 loc) · 2.6 KB
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
# Copyright 2021 The TensorFlow Authors. 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.
# ==============================================================================
"""Tests for tensorflow.python.util.fast_module_type."""
from tensorflow.python.platform import test
from tensorflow.python.util import fast_module_type
FastModuleType = fast_module_type.get_fast_module_type_class()
class ChildFastModule(FastModuleType):
def _getattribute1(self, name): # pylint: disable=unused-argument
return 2
def _getattribute2(self, name): # pylint: disable=unused-argument
raise AttributeError("Pass to getattr")
def _getattr(self, name): # pylint: disable=unused-argument
return 3
class FastModuleTypeTest(test.TestCase):
def testBaseGetattribute(self):
# Tests that the default attribute lookup works.
module = ChildFastModule("test")
module.foo = 1
self.assertEqual(1, module.foo)
def testGetattributeCallback(self):
# Tests that functionality of __getattribute__ can be set as a callback.
module = ChildFastModule("test")
FastModuleType.set_getattribute_callback(module,
ChildFastModule._getattribute1)
self.assertEqual(2, module.foo)
def testGetattrCallback(self):
# Tests that functionality of __getattr__ can be set as a callback.
module = ChildFastModule("test")
FastModuleType.set_getattribute_callback(module,
ChildFastModule._getattribute2)
FastModuleType.set_getattr_callback(module, ChildFastModule._getattr)
self.assertEqual(3, module.foo)
def testFastdictApis(self):
module = ChildFastModule("test")
# At first "bar" does not exist in the module's attributes
self.assertFalse(module._fastdict_key_in("bar"))
with self.assertRaisesRegex(KeyError, "module has no attribute 'bar'"):
module._fastdict_get("bar")
module._fastdict_insert("bar", 1)
# After _fastdict_insert() the attribute is added.
self.assertTrue(module._fastdict_key_in("bar"))
self.assertEqual(1, module.bar)
if __name__ == "__main__":
test.main()