Skip to content

Commit 702c86d

Browse files
authored
Respect Python bytecode generation settings (#1055)
Fixes #1054
1 parent ce3eb81 commit 702c86d

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
### Added
99
* Added a compiler metadata flag for suppressing warnings when Var indirection is unavoidable (#1052)
1010

11+
### Fixed
12+
* Basilisp now respects the value of Python's `sys.dont_write_bytecode` flag when generating bytecode (#1054)
13+
1114
## [v0.2.2]
1215
### Added
1316
* Added the `-p`/`--include-path` CLI command to prepend entries to the `sys.path` as an alternative to `PYTHONPATH` (#1027)

docs/compiler.rst

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,12 @@ Namespace Caching
9999

100100
The Basilisp compiler aggressively caches compiled namespace modules because compilation is relatively expensive and leads to significant slowdowns when starting Basilisp.
101101
Basilisp namespaces are cached using the `same mechanism as the Python compiler uses <https://docs.python.org/3/reference/import.html#cached-bytecode-invalidation>`_ -- namespaces are cached as bytecode and only recomputed when the ``mtime`` of the source file differs from the ``mtime`` stored in the header of the cached file.
102+
Cache files are stored with an ``.lpyc`` prefix and respect the Python ``PYTHONCACHEPREFIX`` (:external:py:data:`sys.pycache_prefix`) setting.
102103

103104
There may be times when the caching behavior is undesirable for whatever reason.
104105
Often in development, it is not desirable to allow namespace caching since such files may get out of sync of other uncached modules you are frequently updating, causing hard-to-diagnose bugs.
105-
In such cases, you can tell the Basilisp import mechanism to always ignore the cached copy of a namespace using the ``BASILISP_DO_NOT_CACHE_NAMESPACES`` environment variable.
106-
107-
.. code-block:: bash
108-
109-
export BASILISP_DO_NOT_CACHE_NAMESPACES=true
106+
In such cases, you can tell the Basilisp import mechanism to always ignore the cached copy of a namespace using the ``BASILISP_DO_NOT_CACHE_NAMESPACES`` environment variable or ``--disable-ns-cache`` CLI flag.
107+
Additionally, it is possible to disable Basilisp's generation of bytecode files using the standard Python ``PYTHONDONTWRITEBYTECODE`` environment variable or :external:py:data:`sys.dont_write_bytecode` value.
110108

111109
.. _direct_linking:
112110

src/basilisp/importer.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,10 @@ def _exec_module(
359359
collect_bytecode=all_bytecode.append,
360360
)
361361

362+
if sys.dont_write_bytecode:
363+
logger.debug(f"Skipping bytecode generation for '{fullname}'")
364+
return
365+
362366
# Cache the bytecode that was collected through the compilation run.
363367
cache_file_bytes = _basilisp_bytecode(
364368
path_stats["mtime"], path_stats["size"], all_bytecode

tests/basilisp/importer_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,21 @@ def test_import_module_with_cache(
226226
using_cache = load_namespace(cached_module_ns)
227227
assert cached_module_ns == using_cache.find(sym.symbol("val")).value
228228

229+
def test_import_module_without_writing_cache(
230+
self,
231+
monkeypatch,
232+
module_dir,
233+
make_new_module,
234+
load_namespace,
235+
):
236+
monkeypatch.setattr(sys, "dont_write_bytecode", True)
237+
module_path = ["importer", "namespace", "no_bytecode.lpy"]
238+
make_new_module(*module_path, ns_name="importer.namespace.no-bytecode")
239+
load_namespace("importer.namespace.no-bytecode")
240+
assert not os.path.exists(
241+
importer._cache_from_source(os.path.join(module_dir, *module_path))
242+
)
243+
229244
def test_import_module_with_invalid_cache_magic_number(
230245
self, module_dir, cached_module_ns, cached_module_file, load_namespace
231246
):

0 commit comments

Comments
 (0)