Skip to content

Commit 5b88673

Browse files
authored
Allow users to disable emitting Python AST strings (#356)
Emitting the generated Python AST to strings and then to stdout or `*generated-python*` Var adds a bit of time to startup, which is a fine price to pay during development, but would be more onerous to normal users. This PR creates an environment var `BASILISP_EMIT_GENERATED_PYTHON` which can be set to disable the emission of generated Python. Eventually, the default value will switch to `false` (when development settles down), but for now having `true` set as the default value is tremendously useful.
1 parent c2db50d commit 5b88673

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

src/basilisp/importer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ def exec_module(self, module):
263263

264264
# Check if a valid, cached version of this Basilisp namespace exists and, if so,
265265
# load it and bypass the expensive compilation process below.
266-
if os.getenv(_NO_CACHE_ENVVAR, None) == "True":
266+
if os.getenv(_NO_CACHE_ENVVAR, None) == "true":
267267
self._exec_module(fullname, spec.loader_state, path_stats, module)
268268
else:
269269
try:

src/basilisp/lang/compiler/__init__.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
import ast
22
import itertools
3+
import os
34
import types
4-
from typing import Optional, Callable, Any, Iterable, List, Dict
5+
from typing import Any, Callable, Dict, Iterable, List, Optional
56

67
from astor import code_gen as codegen
78

89
import basilisp.lang.runtime as runtime
910
from basilisp.lang.compiler.exception import CompilerException, CompilerPhase # noqa
1011
from basilisp.lang.compiler.generator import ( # noqa
11-
GeneratorContext,
1212
GeneratedPyAST,
13+
GeneratorContext,
14+
USE_VAR_INDIRECTION,
15+
WARN_ON_VAR_INDIRECTION,
1316
expressionize as _expressionize,
1417
gen_py_ast,
1518
py_module_preamble,
1619
statementize as _statementize,
17-
USE_VAR_INDIRECTION,
18-
WARN_ON_VAR_INDIRECTION,
1920
)
2021
from basilisp.lang.compiler.optimizer import PythonASTOptimizer
2122
from basilisp.lang.compiler.parser import ( # noqa
2223
ParserContext,
23-
parse_ast,
2424
WARN_ON_SHADOWED_NAME,
2525
WARN_ON_SHADOWED_VAR,
2626
WARN_ON_UNUSED_NAMES,
27+
parse_ast,
2728
)
2829
from basilisp.lang.typing import ReaderForm
2930
from basilisp.lang.util import genname
@@ -66,6 +67,23 @@ def py_ast_optimizer(self) -> PythonASTOptimizer:
6667
return self._optimizer
6768

6869

70+
def _emit_ast_string(module: ast.AST) -> None: # pragma: no cover
71+
"""Emit the generated Python AST string either to standard out or to the
72+
*generated-python* dynamic Var for the current namespace. If the
73+
BASILISP_EMIT_GENERATED_PYTHON env var is not set True, this method is a
74+
no-op."""
75+
# TODO: eventually, this default should become "false" but during this
76+
# period of heavy development, having it set to "true" by default
77+
# is tremendously useful
78+
if os.getenv("BASILISP_EMIT_GENERATED_PYTHON", "true") != "true":
79+
return
80+
81+
if runtime.print_generated_python():
82+
print(to_py_str(module))
83+
else:
84+
runtime.add_generated_python(to_py_str(module))
85+
86+
6987
def compile_and_exec_form( # pylint: disable= too-many-arguments
7088
form: ReaderForm,
7189
ctx: CompilerContext,
@@ -102,10 +120,7 @@ def compile_and_exec_form( # pylint: disable= too-many-arguments
102120
ast_module = ctx.py_ast_optimizer.visit(ast_module)
103121
ast.fix_missing_locations(ast_module)
104122

105-
if runtime.print_generated_python():
106-
print(to_py_str(ast_module)) # pragma: no cover
107-
else:
108-
runtime.add_generated_python(to_py_str(ast_module))
123+
_emit_ast_string(ast_module)
109124

110125
bytecode = compile(ast_module, ctx.filename, "exec")
111126
if collect_bytecode:
@@ -135,10 +150,7 @@ def _incremental_compile_module(
135150
module = optimizer.visit(module)
136151
ast.fix_missing_locations(module)
137152

138-
if runtime.print_generated_python():
139-
print(to_py_str(module)) # pragma: no cover
140-
else:
141-
runtime.add_generated_python(to_py_str(module))
153+
_emit_ast_string(module)
142154

143155
bytecode = compile(module, source_filename, "exec")
144156
if collect_bytecode:

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ envlist = py36,py37,mypy,format,lint
55
parallel_show_output = {env:TOX_SHOW_OUTPUT:false}
66
passenv = TRAVIS TRAVIS_*
77
setenv =
8-
BASILISP_DO_NOT_CACHE_NAMESPACES = True
8+
BASILISP_DO_NOT_CACHE_NAMESPACES = true
99
whitelist_externals = rm
1010
deps =
1111
coverage

0 commit comments

Comments
 (0)