From d049ef0b234268d8048a0f414910f3fe5ff121ca Mon Sep 17 00:00:00 2001 From: Louis-Pujol Date: Tue, 14 Nov 2023 16:06:00 +0100 Subject: [PATCH 1/8] Don't reinitialize all sys.path between blocks --- src/mkdocs_gallery/gen_single.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/mkdocs_gallery/gen_single.py b/src/mkdocs_gallery/gen_single.py index bbb36524..fcd98b72 100644 --- a/src/mkdocs_gallery/gen_single.py +++ b/src/mkdocs_gallery/gen_single.py @@ -728,10 +728,12 @@ def _get_code_output(is_last_expr, script: GalleryScript, logging_tee, images_md return code_output -def _reset_cwd_syspath(cwd, sys_path): +def _reset_cwd_syspath(cwd, new_path): """Reset cwd and sys.path.""" + if new_path in sys.path: + sys.path.remove(new_path) os.chdir(cwd) - sys.path = sys_path + # sys.path = sys_path def execute_code_block(compiler, block, script: GalleryScript): @@ -774,7 +776,8 @@ def execute_code_block(compiler, block, script: GalleryScript): os.chdir(src_file.parent) sys_path = copy.deepcopy(sys.path) - sys.path.append(os.getcwd()) + new_path = os.getcwd() + sys.path.append(new_path) # Save figures unless there is a `mkdocs_gallery_defer_figures` flag match = re.search(r"^[\ \t]*#\s*mkdocs_gallery_defer_figures[\ \t]*\n?", bcontent, re.MULTILINE) @@ -818,11 +821,11 @@ def execute_code_block(compiler, block, script: GalleryScript): if need_save_figures: save_figures(block, script) else: - _reset_cwd_syspath(cwd, sys_path) + _reset_cwd_syspath(cwd, new_path) code_output = _get_code_output(is_last_expr, script, logging_tee, images_md) finally: - _reset_cwd_syspath(cwd, sys_path) + _reset_cwd_syspath(cwd, new_path) logging_tee.restore_std() # Sanitize ANSI escape characters from MD output From 7a982c3a2ff8a56bbf7af3ef8e309445a0c23fa3 Mon Sep 17 00:00:00 2001 From: Louis-Pujol Date: Tue, 14 Nov 2023 16:07:11 +0100 Subject: [PATCH 2/8] clean --- src/mkdocs_gallery/gen_single.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/mkdocs_gallery/gen_single.py b/src/mkdocs_gallery/gen_single.py index fcd98b72..84beebf7 100644 --- a/src/mkdocs_gallery/gen_single.py +++ b/src/mkdocs_gallery/gen_single.py @@ -733,7 +733,6 @@ def _reset_cwd_syspath(cwd, new_path): if new_path in sys.path: sys.path.remove(new_path) os.chdir(cwd) - # sys.path = sys_path def execute_code_block(compiler, block, script: GalleryScript): @@ -775,7 +774,6 @@ def execute_code_block(compiler, block, script: GalleryScript): # created by the example get created in this directory os.chdir(src_file.parent) - sys_path = copy.deepcopy(sys.path) new_path = os.getcwd() sys.path.append(new_path) From 6c299965d3003a9d7b6e329075b7b829a041a925 Mon Sep 17 00:00:00 2001 From: Louis Pujol <52413616+Louis-Pujol@users.noreply.github.com> Date: Mon, 20 Nov 2023 10:30:53 +0100 Subject: [PATCH 3/8] rename new_path to path_to_remove MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sylvain Marié --- src/mkdocs_gallery/gen_single.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mkdocs_gallery/gen_single.py b/src/mkdocs_gallery/gen_single.py index 84beebf7..6d02faa8 100644 --- a/src/mkdocs_gallery/gen_single.py +++ b/src/mkdocs_gallery/gen_single.py @@ -728,7 +728,7 @@ def _get_code_output(is_last_expr, script: GalleryScript, logging_tee, images_md return code_output -def _reset_cwd_syspath(cwd, new_path): +def _reset_cwd_syspath(cwd, path_to_remove): """Reset cwd and sys.path.""" if new_path in sys.path: sys.path.remove(new_path) From 2ab646a21eae222ced21ea0d4e587cb9a9fd03cf Mon Sep 17 00:00:00 2001 From: Louis Pujol <52413616+Louis-Pujol@users.noreply.github.com> Date: Mon, 20 Nov 2023 10:31:56 +0100 Subject: [PATCH 4/8] add comment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sylvain Marié --- src/mkdocs_gallery/gen_single.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mkdocs_gallery/gen_single.py b/src/mkdocs_gallery/gen_single.py index 6d02faa8..d4bbc164 100644 --- a/src/mkdocs_gallery/gen_single.py +++ b/src/mkdocs_gallery/gen_single.py @@ -774,6 +774,7 @@ def execute_code_block(compiler, block, script: GalleryScript): # created by the example get created in this directory os.chdir(src_file.parent) + # Add the example dir to the path temporarily (will be removed after execution) new_path = os.getcwd() sys.path.append(new_path) From 94d279de2b22fd261359bade7587d18c083222d5 Mon Sep 17 00:00:00 2001 From: Louis Pujol <52413616+Louis-Pujol@users.noreply.github.com> Date: Mon, 20 Nov 2023 10:36:00 +0100 Subject: [PATCH 5/8] Update gen_single.py --- src/mkdocs_gallery/gen_single.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mkdocs_gallery/gen_single.py b/src/mkdocs_gallery/gen_single.py index d4bbc164..1bd6eb16 100644 --- a/src/mkdocs_gallery/gen_single.py +++ b/src/mkdocs_gallery/gen_single.py @@ -729,9 +729,9 @@ def _get_code_output(is_last_expr, script: GalleryScript, logging_tee, images_md def _reset_cwd_syspath(cwd, path_to_remove): - """Reset cwd and sys.path.""" - if new_path in sys.path: - sys.path.remove(new_path) + """Reset current working directory to `cwd` and remove `path_to_remove` from `sys.path`.""" + if path_to_remove in sys.path: + sys.path.remove(path_to_remove) os.chdir(cwd) From 34e32f1b094d933f615660c095a5b697661a1052 Mon Sep 17 00:00:00 2001 From: Louis Pujol <52413616+Louis-Pujol@users.noreply.github.com> Date: Tue, 21 Nov 2023 09:34:34 +0100 Subject: [PATCH 6/8] Update changelog.md --- docs/changelog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/changelog.md b/docs/changelog.md index 76fe8dfd..c85bee15 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -3,6 +3,7 @@ ### 0.7.9 - (In Progress) - Swapped from deprecated `disutils.version` to `packaging.version`. PR [#79](https://github.com/smarie/mkdocs-gallery/pull/79) by [Samreay](https://github.com/Samreay) + - Rewrited `gen_single._reset_cwd_syspath` function to let `sys.path` modifications persist accross blocks. PR [#82](https://github.com/smarie/mkdocs-gallery/pull/82) by [Louis-Pujol](https://github.com/Louis-Pujol). ### 0.7.8 - Bugfixes From eafe8b10354a639d3ed7d0efc5eeb97b306250d8 Mon Sep 17 00:00:00 2001 From: Louis-Pujol Date: Tue, 21 Nov 2023 14:21:41 +0100 Subject: [PATCH 7/8] `sys.path` resetted after example --- docs/changelog.md | 2 +- src/mkdocs_gallery/gen_single.py | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/docs/changelog.md b/docs/changelog.md index c85bee15..9c5a177c 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -3,7 +3,7 @@ ### 0.7.9 - (In Progress) - Swapped from deprecated `disutils.version` to `packaging.version`. PR [#79](https://github.com/smarie/mkdocs-gallery/pull/79) by [Samreay](https://github.com/Samreay) - - Rewrited `gen_single._reset_cwd_syspath` function to let `sys.path` modifications persist accross blocks. PR [#82](https://github.com/smarie/mkdocs-gallery/pull/82) by [Louis-Pujol](https://github.com/Louis-Pujol). + - `sys.path` modifications now persist accross blocks of an example. `sys.path` is still reset after each example. PR [#82](https://github.com/smarie/mkdocs-gallery/pull/82) by [Louis-Pujol](https://github.com/Louis-Pujol). ### 0.7.8 - Bugfixes diff --git a/src/mkdocs_gallery/gen_single.py b/src/mkdocs_gallery/gen_single.py index 1bd6eb16..8dbdbb15 100644 --- a/src/mkdocs_gallery/gen_single.py +++ b/src/mkdocs_gallery/gen_single.py @@ -22,6 +22,7 @@ import sys import traceback import warnings +from copy import deepcopy from functools import partial from io import StringIO from pathlib import Path @@ -891,6 +892,9 @@ def parse_and_execute(script: GalleryScript, script_blocks): # Remember the original argv so that we can put them back after run argv_orig = sys.argv[:] + # Remember the original sys.path so that we can reset it after run + sys_path_orig = deepcopy(sys.path) + # Python file is the original one (not the copy for download) sys.argv[0] = script.src_py_file.as_posix() @@ -920,6 +924,9 @@ def parse_and_execute(script: GalleryScript, script_blocks): # Set back the sys argv sys.argv = argv_orig + # Set back the sys path + sys.path = sys_path_orig + # Write md5 checksum if the example was meant to run (no-plot shall not cache md5sum) and has built correctly script.write_final_md5_file() From bad96b0adcb937e54c81dbbf4f533ec697e4d9bb Mon Sep 17 00:00:00 2001 From: Louis-Pujol Date: Tue, 21 Nov 2023 14:23:29 +0100 Subject: [PATCH 8/8] solving changelog conflict --- docs/changelog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/changelog.md b/docs/changelog.md index 9c5a177c..295a750a 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -3,6 +3,7 @@ ### 0.7.9 - (In Progress) - Swapped from deprecated `disutils.version` to `packaging.version`. PR [#79](https://github.com/smarie/mkdocs-gallery/pull/79) by [Samreay](https://github.com/Samreay) + - Re-raise errors for better ExtensionError messages, so users have full details about the original problem. PR [#58](https://github.com/smarie/mkdocs-gallery/pull/58) by [GenevieveBuckley](https://github.com/GenevieveBuckley) - `sys.path` modifications now persist accross blocks of an example. `sys.path` is still reset after each example. PR [#82](https://github.com/smarie/mkdocs-gallery/pull/82) by [Louis-Pujol](https://github.com/Louis-Pujol). ### 0.7.8 - Bugfixes