|
| 1 | +# Copyright 1999-2021 Alibaba Group Holding Ltd. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import sys |
| 16 | +import types |
| 17 | +import warnings |
| 18 | +import pkg_resources |
| 19 | + |
| 20 | + |
| 21 | +class _DummyClass(object): |
| 22 | + def __init__(self, value): |
| 23 | + self.value = value |
| 24 | + |
| 25 | + def __repr__(self): |
| 26 | + return "_DummyClass(%f, %f)" % self.value |
| 27 | + |
| 28 | + |
| 29 | +def test_init_entrypoint(): |
| 30 | + # FIXME: Python 2 workaround because nonlocal doesn't exist |
| 31 | + counters = {"init": 0} |
| 32 | + |
| 33 | + def init_function(): |
| 34 | + counters["init"] += 1 |
| 35 | + |
| 36 | + mod = types.ModuleType("_test_mars_extension") |
| 37 | + mod.init_func = init_function |
| 38 | + |
| 39 | + try: |
| 40 | + # will remove this module at the end of the test |
| 41 | + sys.modules[mod.__name__] = mod |
| 42 | + |
| 43 | + # We are registering an entry point using the "mars" package |
| 44 | + # ("distribution" in pkg_resources-speak) itself, though these are |
| 45 | + # normally registered by other packages. |
| 46 | + dist = "pymars" |
| 47 | + entrypoints = pkg_resources.get_entry_map(dist) |
| 48 | + my_entrypoint = pkg_resources.EntryPoint( |
| 49 | + "init", # name of entry point |
| 50 | + mod.__name__, # module with entry point object |
| 51 | + attrs=["init_func"], # name of entry point object |
| 52 | + dist=pkg_resources.get_distribution(dist), |
| 53 | + ) |
| 54 | + entrypoints.setdefault("mars_extensions", {})["init"] = my_entrypoint |
| 55 | + |
| 56 | + from .. import entrypoints |
| 57 | + |
| 58 | + # Allow reinitialization |
| 59 | + entrypoints.init_extension_entrypoints.cache_clear() |
| 60 | + |
| 61 | + entrypoints.init_extension_entrypoints() |
| 62 | + |
| 63 | + # was our init function called? |
| 64 | + assert counters["init"] == 1 |
| 65 | + |
| 66 | + # ensure we do not initialize twice |
| 67 | + entrypoints.init_extension_entrypoints() |
| 68 | + assert counters["init"] == 1 |
| 69 | + finally: |
| 70 | + # remove fake module |
| 71 | + if mod.__name__ in sys.modules: |
| 72 | + del sys.modules[mod.__name__] |
| 73 | + |
| 74 | + |
| 75 | +def test_entrypoint_tolerance(): |
| 76 | + # FIXME: Python 2 workaround because nonlocal doesn't exist |
| 77 | + counters = {"init": 0} |
| 78 | + |
| 79 | + def init_function(): |
| 80 | + counters["init"] += 1 |
| 81 | + raise ValueError("broken") |
| 82 | + |
| 83 | + mod = types.ModuleType("_test_mars_bad_extension") |
| 84 | + mod.init_func = init_function |
| 85 | + |
| 86 | + try: |
| 87 | + # will remove this module at the end of the test |
| 88 | + sys.modules[mod.__name__] = mod |
| 89 | + |
| 90 | + # We are registering an entry point using the "mars" package |
| 91 | + # ("distribution" in pkg_resources-speak) itself, though these are |
| 92 | + # normally registered by other packages. |
| 93 | + dist = "pymars" |
| 94 | + entrypoints = pkg_resources.get_entry_map(dist) |
| 95 | + my_entrypoint = pkg_resources.EntryPoint( |
| 96 | + "init", # name of entry point |
| 97 | + mod.__name__, # module with entry point object |
| 98 | + attrs=["init_func"], # name of entry point object |
| 99 | + dist=pkg_resources.get_distribution(dist), |
| 100 | + ) |
| 101 | + entrypoints.setdefault("mars_extensions", {})["init"] = my_entrypoint |
| 102 | + |
| 103 | + from .. import entrypoints |
| 104 | + |
| 105 | + # Allow reinitialization |
| 106 | + entrypoints.init_extension_entrypoints.cache_clear() |
| 107 | + |
| 108 | + with warnings.catch_warnings(record=True) as w: |
| 109 | + entrypoints.init_extension_entrypoints() |
| 110 | + |
| 111 | + bad_str = "Mars extension module '_test_mars_bad_extension'" |
| 112 | + for x in w: |
| 113 | + if bad_str in str(x): |
| 114 | + break |
| 115 | + else: |
| 116 | + raise ValueError("Expected warning message not found") |
| 117 | + |
| 118 | + # was our init function called? |
| 119 | + assert counters["init"] == 1 |
| 120 | + |
| 121 | + finally: |
| 122 | + # remove fake module |
| 123 | + if mod.__name__ in sys.modules: |
| 124 | + del sys.modules[mod.__name__] |
0 commit comments