New submission from junyixie <xie.ju...@outlook.com>:
_PyRun_SimpleFileObject load __main__ module at cache. Call this function multiple times, the attributes stored in the module dict will affect eachother. for example. if we run test_A.py, call _PyRun_SimpleFileObject will create __main__ module, test_A.py add some attribute in __main__ module dict. now we run test_B.py. call _PyRun_SimpleFileObject will load cached __main__ module. now in __main__ module dict, we can get test_A's attribute. in unittest, if we execute test, and don't exit. (unittest main.py TestProgram), set exit=False. ``` def __init__(self, module='__main__', defaultTest=None, argv=None, testRunner=None, testLoader=loader.defaultTestLoader, exit=True, verbosity=1, failfast=None, catchbreak=None, buffer=None, warnings=None, *, tb_locals=False): ``` dir(module), We got unexpected results ``` for name in dir(module): ... ``` then when unittest load tests. if we use _PyRun_SimpleFileObject to run unittest, it will Repeated load test cases ``` for name in dir(module): obj = getattr(module, name) if isinstance(obj, type) and issubclass(obj, case.TestCase): tests.append(self.loadTestsFromTestCase(obj)) ``` ``` int _PyRun_SimpleFileObject(FILE *fp, PyObject *filename, int closeit, PyCompilerFlags *flags) { PyObject *m, *d, *v; int set_file_name = 0, ret = -1; m = PyImport_AddModule("__main__"); if (m == NULL) return -1; Py_INCREF(m); d = PyModule_GetDict(m); ``` ---------- messages: 389540 nosy: JunyiXie priority: normal severity: normal status: open title: unittest use dir(module) to load test cases. Run unittest file by _PyRun_SimpleFileObject, have bug _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue43630> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com