def test_callbacks_leak_refcycle(self): n = atexit._ncallbacks() code = """if 1: import atexit def f(): pass atexit.register(f) atexit.__atexit = atexit """ ret = support.run_in_subinterp(code) self.assertEqual(ret, 0) self.assertEqual(atexit._ncallbacks(), n)
def test_callbacks_leak_refcycle(self): # Similar to the above, but with a refcycle through the atexit # module. n = atexit._ncallbacks() code = textwrap.dedent(r""" import atexit def f(): pass atexit.register(f) atexit.__atexit = atexit """) ret = support.run_in_subinterp(code) self.assertEqual(ret, 0) self.assertEqual(atexit._ncallbacks(), n)
def test_callbacks_leak_refcycle(self): # Similar to the above, but with a refcycle through the atexit # module. n = atexit._ncallbacks() code = r"""if 1: import atexit def f(): pass atexit.register(f) atexit.__atexit = atexit """ ret = support.run_in_subinterp(code) self.assertEqual(ret, 0) self.assertEqual(atexit._ncallbacks(), n)
def test_callbacks_leak(self): # This test shows a leak in refleak mode if atexit doesn't # take care to free callbacks in its per-subinterpreter module # state. n = atexit._ncallbacks() code = textwrap.dedent(r""" import atexit def f(): pass atexit.register(f) del atexit """) ret = support.run_in_subinterp(code) self.assertEqual(ret, 0) self.assertEqual(atexit._ncallbacks(), n)
def test_callbacks_leak(self): # This test shows a leak in refleak mode if atexit doesn't # take care to free callbacks in its per-subinterpreter module # state. n = atexit._ncallbacks() code = r"""if 1: import atexit def f(): pass atexit.register(f) del atexit """ ret = support.run_in_subinterp(code) self.assertEqual(ret, 0) self.assertEqual(atexit._ncallbacks(), n)
def _predictor_server(path, qi, qo): # Using os.fork() (called from multiprocessing) and allowing cleanups to be run like # normal is dangerous because some filesystem-related cleanups might be called # twice. That's why we remove them first, without executing them in this process. atexit._clear() from allennlp.predictors.predictor import Predictor from allennlp.predictors.semantic_role_labeler import SemanticRoleLabelerPredictor from allennlp.data.tokenizers.word_splitter import SpacyWordSplitter # Use en_core_web_md for tokenizer instead # TODO: make optional SemanticRoleLabelerPredictor.__init__ = Predictor.__init__ predictor = Predictor.from_path(path) predictor._tokenizer = SpacyWordSplitter(language="en_core_web_md", pos_tags=True) while True: s = qi.get() if s is None: break qo.put_nowait(predictor.predict(s)) # We need to manually call atexit callbacks here because the multiprocessing module # doesn't call them: # https://stackoverflow.com/a/34507557/ # https://github.com/python/cpython/blob/49fd6dd887df6ea18dbb1a3c0f599239ccd1cb42/Lib/multiprocessing/popen_fork.py#L75 # But if we don't call them, allennlp leaves extracted archives in the $TMPDIR: # https://github.com/allenai/allennlp/blob/fefc439035df87e3d2484eb2f53ca921c4c2e2fe/allennlp/models/archival.py#L176-L178 logger.debug("atexit should call %d callbacks", atexit._ncallbacks()) atexit._run_exitfuncs()
def test(): for _ in range(999): args = [fargs() for _ in range(fint())] for f, a, k in args[fint():fint()]: atexit.register(f, *a, **k) if fint() == 0: atexit._clear() if fint() == 0: atexit._run_exitfuncs() for f, a, k in args[fint():fint()]: atexit.unregister(f) if fint() == 0: atexit._clear() if fint() == 0: atexit._run_exitfuncs() atexit._ncallbacks()
def add_state_plan(cls, function): cls.state_plan.append(function) if atexit._ncallbacks() <= 1: def f(l): print("atexit command:") for element in l: print(element.action()) command = input() print("registered" + command) atexit.register(f, cls.state_plan)
def test_args(self): import atexit import io import sys stdout, stderr = sys.stdout, sys.stderr try: sys.stdout = sys.stderr = capture = io.StringIO() def h1(): print("h1") def h2(): print("h2") atexit.register(h1) atexit.register(h2) assert atexit._ncallbacks() == 2 atexit._run_exitfuncs() assert atexit._ncallbacks() == 0 assert capture.getvalue() == 'h2\nh1\n' finally: sys.stdout = stdout sys.stderr = stderr