Debian Codegraph @

tests/what.py

raw symbols SCIP
1 """Creates a version of known.rst to insert in the documentation.
2 """
3
4 import builtins
5 import os
6 import sys
7 import platform
8 from contextlib import redirect_stderr
9 this_dir = os.path.dirname(__file__)
10 sys.path.append(os.path.join(this_dir, ".."))
11
12 import friendly_traceback
13
14 from friendly_traceback import console_helpers
15
16
17 # Make it possible to find docs
18 docs_root_dir = os.path.abspath(
19 os.path.join(this_dir, "..", "..", "friendly-docs")
20 )
21 assert os.path.isdir(docs_root_dir), "Separate docs repo need to exist"
22
23 target = os.path.normpath(
24 os.path.join(
25 docs_root_dir, "source/known.rst"
26 )
27 )
28
29 LANG = "en"
30 friendly_traceback.install()
31 friendly_traceback.set_lang(LANG)
32 friendly_traceback.set_formatter("docs")
33 friendly_traceback.debug_helper.DEBUG = False # silence message about new cases
34
35 sys.path.insert(0, this_dir)
36 py_version = f"{sys.version_info.major}.{sys.version_info.minor}"
37
38
39 intro_text = """
40 Generic information about various exceptions
41 ==============================================
42
43 .. note::
44
45 The content of this page is generated by running
46 what.py located in the ``tests/`` directory.
47 This needs to be done explicitly, independently of updating the
48 documentation using Sphinx.
49
50 This file contains the generic information provided by
51 Friendly-traceback about built-in exceptions.
52 By "generic information" we mean the information provided using
53 ``what()`` in a friendly console.
54
55 Some exceptions will never be seen by users of Friendly-traceback.
56
57 For example, ``SystemExit`` and ``KeyboardInterrupt`` are never
58 intercepted by Friendly-traceback. Furthermore, exceptions such as
59 ``GeneratorExit``, ``FloatingPointError``, and
60 ``StopAsyncIteration``, would likely never be seen.
61
62 ``FloatingPointError`` is actually
63 `not used by Python <https://docs.python.org/3.7/library/exceptions.html#FloatingPointError>`_.
64
65 ``BaseException``, ``Exception``, and ``ArithmeticError`` are base classes which
66 are also not normally seen: some derived classes are normally used instead.
67
68 Information compiled using Friendly version: {friendly},
69 Python version: {python}
70
71 """.format(
72 friendly=friendly_traceback.__version__, python=platform.python_version()
73 )
74
75
76 def write(text):
77 sys.stderr.write(text + "\n")
78
79
80 def make_title(text):
81 write("\n" + text)
82 write("~" * len(text) + "\n")
83 write(".. code-block:: none\n")
84
85
86 with open(target, "w", encoding="utf8") as out, redirect_stderr(out):
87 write(intro_text)
88
89 write("\n")
90 write("Exceptions")
91 write("----------")
92
93 for item in dir(builtins):
94 try:
95 exc = eval(item) # skipcq PYL-W0123
96 except Exception: # noqa
97 continue
98 try:
99 if not issubclass(exc, BaseException) or issubclass(exc, Warning):
100 continue
101 except Exception: # noqa
102 continue
103 make_title(item)
104 console_helpers.what(item, pre=True)
105
106 write("\n")
107 write("Warnings")
108 write("----------")
109
110 for item in dir(builtins):
111 try:
112 exc = eval(item) # skipcq PYL-W0123
113 except Exception: # noqa
114 continue
115 try:
116 if not issubclass(exc, Warning):
117 continue
118 except Exception: # noqa
119 continue
120 make_title(item)
121 console_helpers.what(item, pre=True)