Debian Codegraph @

hypothesis-python/tests/ghostwriter/try-writing-for-installed.py

raw
1 # This file is part of Hypothesis, which may be found at
2 # https://github.com/HypothesisWorks/hypothesis/
3 #
4 # Copyright the Hypothesis Authors.
5 # Individual contributors are listed in AUTHORS.rst and the git log.
6 #
7 # This Source Code Form is subject to the terms of the Mozilla Public License,
8 # v. 2.0. If a copy of the MPL was not distributed with this file, You can
9 # obtain one at https://mozilla.org/MPL/2.0/.
10
11 """Try running `hypothesis write ...` on all available modules.
12
13 Prints a list of module names which caused some kind of internal error.
14
15 The idea here is to check that we at least don't crash on anything that
16 people actually ship, or at least only on the cases we know and don't
17 really care about - there are a lot of strange things in a python install.
18 Some have import-time side effects or errors so we skip them; others
19 just have such weird semantics that we don't _want_ to support them.
20 """
21
22 import multiprocessing
23 import os
24 import subprocess
25 import sysconfig
26
27 skip = (
28 "idlelib curses antigravity pip prompt_toolkit IPython .popen_ django. .test. "
29 "execnet.script lib2to3.pgen2.conv tests. Cython. ~ - ._ libcst.codemod. "
30 "modernize flask. sphinx. pyasn1 dbm.ndbm doctest"
31 ).split()
32
33
34 def getmodules():
35 std_lib = sysconfig.get_python_lib(standard_lib=True)
36 for top, _, files in os.walk(std_lib):
37 for nm in files:
38 if nm.endswith(".py") and nm not in ("__init__.py", "__main__.py"):
39 modname = (
40 os.path.join(top, nm)[len(std_lib) + 1 : -3]
41 .replace(os.sep, ".")
42 .replace("site-packages.", "")
43 )
44 if not any(bad in modname for bad in skip):
45 yield modname
46
47
48 def write_for(mod):
49 try:
50 subprocess.run(
51 ["hypothesis", "write", mod],
52 check=True,
53 capture_output=True,
54 timeout=10,
55 text=True,
56 encoding="utf-8",
57 )
58 except subprocess.SubprocessError as e:
59 # Only report the error if we could load _but not process_ the module
60 if (
61 "Error: Found the '" not in e.stderr
62 and "Error: Failed to import" not in e.stderr
63 ):
64 return mod
65
66
67 if __name__ == "__main__":
68 print("# prints the names of modules for which `hypothesis write` errors out")
69 with multiprocessing.Pool() as pool:
70 for name in pool.imap(write_for, getmodules()):
71 if name is not None:
72 print(name, flush=True)