markdown-exec 1.8.1__tar.gz → 1.8.3__tar.gz

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.
Files changed (34) hide show
  1. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/PKG-INFO +1 -1
  2. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/pyproject.toml +1 -1
  3. markdown_exec-1.8.3/src/markdown_exec/formatters/_exec_python.py +8 -0
  4. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/src/markdown_exec/formatters/python.py +14 -2
  5. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/tests/test_python.py +47 -0
  6. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/LICENSE +0 -0
  7. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/README.md +0 -0
  8. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/src/markdown_exec/__init__.py +0 -0
  9. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/src/markdown_exec/ansi.css +0 -0
  10. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/src/markdown_exec/debug.py +0 -0
  11. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/src/markdown_exec/formatters/__init__.py +0 -0
  12. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/src/markdown_exec/formatters/base.py +0 -0
  13. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/src/markdown_exec/formatters/bash.py +0 -0
  14. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/src/markdown_exec/formatters/console.py +0 -0
  15. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/src/markdown_exec/formatters/markdown.py +0 -0
  16. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/src/markdown_exec/formatters/pycon.py +0 -0
  17. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/src/markdown_exec/formatters/pyodide.py +0 -0
  18. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/src/markdown_exec/formatters/sh.py +0 -0
  19. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/src/markdown_exec/formatters/tree.py +0 -0
  20. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/src/markdown_exec/logger.py +0 -0
  21. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/src/markdown_exec/mkdocs_plugin.py +0 -0
  22. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/src/markdown_exec/processors.py +0 -0
  23. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/src/markdown_exec/py.typed +0 -0
  24. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/src/markdown_exec/pyodide.css +0 -0
  25. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/src/markdown_exec/pyodide.js +0 -0
  26. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/src/markdown_exec/rendering.py +0 -0
  27. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/tests/__init__.py +0 -0
  28. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/tests/conftest.py +0 -0
  29. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/tests/test_base_formatter.py +0 -0
  30. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/tests/test_converter.py +0 -0
  31. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/tests/test_shell.py +0 -0
  32. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/tests/test_toc.py +0 -0
  33. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/tests/test_tree.py +0 -0
  34. {markdown_exec-1.8.1 → markdown_exec-1.8.3}/tests/test_validator.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: markdown-exec
3
- Version: 1.8.1
3
+ Version: 1.8.3
4
4
  Summary: Utilities to execute code blocks in Markdown files.
5
5
  Keywords: markdown,python,exec,shell,bash,mkdocs
6
6
  Author-Email: =?utf-8?q?Timoth=C3=A9e_Mazzucotelli?= <dev@pawamoy.fr>
@@ -40,7 +40,7 @@ classifiers = [
40
40
  dependencies = [
41
41
  "pymdown-extensions>=9",
42
42
  ]
43
- version = "1.8.1"
43
+ version = "1.8.3"
44
44
 
45
45
  [project.license]
46
46
  text = "ISC"
@@ -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
- compiled = compile(code, filename=code_block_id, mode="exec")
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:
@@ -2,6 +2,7 @@
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
+ import re
5
6
  from textwrap import dedent
6
7
  from typing import TYPE_CHECKING
7
8
 
@@ -182,3 +183,49 @@ def test_removing_output_from_pycon_code(md: Markdown) -> None:
182
183
  )
183
184
  assert "ok" in html
184
185
  assert "ko" not in html
186
+
187
+
188
+ def test_functions_have_a_module_attribute(md: Markdown) -> None:
189
+ """Assert functions have a `__module__` attribute.
190
+
191
+ Parameters:
192
+ md: A Markdown instance (fixture).
193
+ """
194
+ html = md.convert(
195
+ dedent(
196
+ """
197
+ ```python exec="1"
198
+ def func():
199
+ pass
200
+
201
+ print(f"`{func.__module__}`")
202
+ ```
203
+ """,
204
+ ),
205
+ )
206
+ assert "_code_block_n" in html
207
+
208
+
209
+ def test_future_annotations_do_not_leak_into_user_code(md: Markdown) -> None:
210
+ """Assert future annotations do not leak into user code.
211
+
212
+ Parameters:
213
+ md: A Markdown instance (fixture).
214
+ """
215
+ html = md.convert(
216
+ dedent(
217
+ """
218
+ ```python exec="1"
219
+ class Int:
220
+ ...
221
+
222
+ def f(x: Int) -> None:
223
+ return x + 1.0
224
+
225
+ print(f"`{f.__annotations__['x']}`")
226
+ ```
227
+ """,
228
+ ),
229
+ )
230
+ assert "<code>Int</code>" not in html
231
+ assert re.search(r"class '_code_block_n\d+_\.Int'", html)
File without changes
File without changes