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