markdown-exec 1.8.1__py3-none-any.whl → 1.8.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- markdown_exec/formatters/_exec_python.py +8 -0
- markdown_exec/formatters/python.py +14 -2
- {markdown_exec-1.8.1.dist-info → markdown_exec-1.8.3.dist-info}/METADATA +1 -1
- {markdown_exec-1.8.1.dist-info → markdown_exec-1.8.3.dist-info}/RECORD +7 -6
- {markdown_exec-1.8.1.dist-info → markdown_exec-1.8.3.dist-info}/WHEEL +1 -1
- {markdown_exec-1.8.1.dist-info → markdown_exec-1.8.3.dist-info}/entry_points.txt +0 -0
- {markdown_exec-1.8.1.dist-info → markdown_exec-1.8.3.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,8 @@
|
|
1
|
+
"""Special module without future annotations for executing Python code."""
|
2
|
+
|
3
|
+
from typing import Any, Dict, Optional
|
4
|
+
|
5
|
+
|
6
|
+
def exec_python(code: str, filename: str, exec_globals: Optional[Dict[str, Any]] = None) -> None:
|
7
|
+
compiled = compile(code, filename=filename, mode="exec")
|
8
|
+
exec(compiled, exec_globals) # noqa: S102
|
@@ -2,12 +2,16 @@
|
|
2
2
|
|
3
3
|
from __future__ import annotations
|
4
4
|
|
5
|
+
import re
|
6
|
+
import sys
|
5
7
|
import traceback
|
6
8
|
from collections import defaultdict
|
7
9
|
from functools import partial
|
8
10
|
from io import StringIO
|
11
|
+
from types import ModuleType
|
9
12
|
from typing import Any
|
10
13
|
|
14
|
+
from markdown_exec.formatters._exec_python import exec_python
|
11
15
|
from markdown_exec.formatters.base import ExecutionError, base_format
|
12
16
|
from markdown_exec.rendering import code_block
|
13
17
|
|
@@ -51,12 +55,20 @@ def _run_python(
|
|
51
55
|
_code_blocks[code_block_id] = code.split("\n")
|
52
56
|
exec_globals = _sessions_globals[session] if session else {}
|
53
57
|
|
58
|
+
# Other libraries expect functions to have a valid `__module__` attribute.
|
59
|
+
# To achieve this, we need to add a `__name__` attribute to the globals.
|
60
|
+
# We compute the name from the code block ID, replacing invalid characters with `_`.
|
61
|
+
# We also create a module object with the same name and add it to `sys.modules`,
|
62
|
+
# because that's what yet other libraries expect (`dataclasses` for example).
|
63
|
+
module_name = re.sub(r"[^a-zA-Z\d]+", "_", code_block_id)
|
64
|
+
exec_globals["__name__"] = module_name
|
65
|
+
sys.modules[module_name] = ModuleType(module_name)
|
66
|
+
|
54
67
|
buffer = StringIO()
|
55
68
|
exec_globals["print"] = partial(_buffer_print, buffer)
|
56
69
|
|
57
70
|
try:
|
58
|
-
|
59
|
-
exec(compiled, exec_globals) # noqa: S102
|
71
|
+
exec_python(code, code_block_id, exec_globals)
|
60
72
|
except Exception as error: # noqa: BLE001
|
61
73
|
trace = traceback.TracebackException.from_exception(error)
|
62
74
|
for frame in trace.stack:
|
@@ -1,18 +1,19 @@
|
|
1
|
-
markdown_exec-1.8.
|
2
|
-
markdown_exec-1.8.
|
3
|
-
markdown_exec-1.8.
|
4
|
-
markdown_exec-1.8.
|
1
|
+
markdown_exec-1.8.3.dist-info/METADATA,sha256=Zb4ucOswqJdnlb39c6CjEuzb-J-9TssLja6SpsRKcz8,4954
|
2
|
+
markdown_exec-1.8.3.dist-info/WHEEL,sha256=vnE8JVcI2Wz7GRKorsPArnBdnW2SWKWGow5gu5tHlRU,90
|
3
|
+
markdown_exec-1.8.3.dist-info/entry_points.txt,sha256=W-JWRoZzS5TXRcyVnxYmsaVBV4HSl7B_7uTgtaZ-Pms,81
|
4
|
+
markdown_exec-1.8.3.dist-info/licenses/LICENSE,sha256=eZQBcJKqlN0QepmOi0u09hlqKMPFdzWjY6NUWYeJGZs,754
|
5
5
|
markdown_exec/__init__.py,sha256=cdQDNiSeWnQ8NK3hqNOfyxKT8uoxLkKm4VI_8E7ztcM,4366
|
6
6
|
markdown_exec/ansi.css,sha256=6PTJxTSsExVgPbMySCuKjih0gr-fdfx2aZ9--u7zWf0,8090
|
7
7
|
markdown_exec/debug.py,sha256=dy0bTd9mTTyTaWuUGNkH0UUdMp2ZThsmGh5q9wt9O0c,2840
|
8
8
|
markdown_exec/formatters/__init__.py,sha256=w8ui1JaGUA5SCyWd2pPAAopQ5y6QYtDvOUHUa7KW2Wg,51
|
9
|
+
markdown_exec/formatters/_exec_python.py,sha256=eVomRZQQl2DySCZIrtqipmBme3kgwIjjRIu8dl22JdE,323
|
9
10
|
markdown_exec/formatters/base.py,sha256=frtAFV1mxi0JMmgESrbmLk5XD-MMAgc6SrmpwLcS3gQ,4761
|
10
11
|
markdown_exec/formatters/bash.py,sha256=qVn6nPqitUuFX72ll00w_x343zl_bMuXrr-Xyox4dJY,886
|
11
12
|
markdown_exec/formatters/console.py,sha256=D2JBIC4MU0ct2MiRCkBO-qo4MiH6Hjy6LXjYAsRmuRA,815
|
12
13
|
markdown_exec/formatters/markdown.py,sha256=pd0akvFGUXrc41NABcHUTTkKFA3k5Ju8im-b3kzOvIw,276
|
13
14
|
markdown_exec/formatters/pycon.py,sha256=F9xpSRKFWsVpGu5XXybtCkMMJ_PAfyd48Qqo1SWl6RA,854
|
14
15
|
markdown_exec/formatters/pyodide.py,sha256=fsAkEq9cgI_-AipV4tS4iXbaOcqQVi-HjJ-VcfMv6aE,2898
|
15
|
-
markdown_exec/formatters/python.py,sha256=
|
16
|
+
markdown_exec/formatters/python.py,sha256=RMNOPBpTbcF6esxpQ6aW1hpU6YH5OAKq7dcsanUAhiw,3025
|
16
17
|
markdown_exec/formatters/sh.py,sha256=41kmwFjkyisWYmfJeBMC40kqhEe7sI2E0vvbO9ixjCg,876
|
17
18
|
markdown_exec/formatters/tree.py,sha256=4XU1KaNqChkkNxMYanwy6glTE2uwcY8Mz9jBZiIf3zY,2046
|
18
19
|
markdown_exec/logger.py,sha256=e06Ih9EonG3KCAPbwraCEYmS0Nt9yRytyMjVJMAREIQ,2109
|
@@ -22,4 +23,4 @@ markdown_exec/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
22
23
|
markdown_exec/pyodide.css,sha256=rJ-HW-ymcgUfrZ9Mm6QB1qo6eE-I3YQcqSxTmG1NhvM,849
|
23
24
|
markdown_exec/pyodide.js,sha256=6iL-9xA4b9UeZgsxVYq_BiCE-Bwu58NfYwYCzz9C9j0,3845
|
24
25
|
markdown_exec/rendering.py,sha256=ssWg6U-7Ez45N6sLak-oGD2e55qtEkPY8T8l7h1RbDM,9459
|
25
|
-
markdown_exec-1.8.
|
26
|
+
markdown_exec-1.8.3.dist-info/RECORD,,
|
File without changes
|
File without changes
|