Skip to content

Disable expressify code caching on Pyodide #1046

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions shiny/express/expressify_decorator/_expressify.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
runtime_checkable,
)

from ..._shinyenv import is_pyodide
from ._func_displayhook import _expressify_decorator_function_def
from ._helpers import find_code_for_func
from ._node_transformers import (
Expand Down Expand Up @@ -70,12 +71,18 @@ def decorator(fn: TFunc) -> TFunc:
if hasattr(unwrapped_fn, expressify_attr):
return fn

if unwrapped_fn.__code__ in code_cache:
fcode = code_cache[fn.__code__]
else:
# Save for next time
if is_pyodide:
# Disable code caching on Pyodide due to bug in hashing bytecode in 0.22.1.
# When Pyodide is updated to a newer version, this will be not be needed.
# https://github.com/posit-dev/py-shiny/issues/1042#issuecomment-1901945787
fcode = _transform_body(cast(types.FunctionType, unwrapped_fn))
code_cache[unwrapped_fn.__code__] = fcode
else:
if unwrapped_fn.__code__ in code_cache:
fcode = code_cache[unwrapped_fn.__code__]
else:
# Save for next time
fcode = _transform_body(cast(types.FunctionType, unwrapped_fn))
code_cache[unwrapped_fn.__code__] = fcode

unwrapped_fn.__code__ = fcode
setattr(unwrapped_fn, expressify_attr, True)
Expand All @@ -96,12 +103,18 @@ def expressify() -> Callable[[TFunc], TFunc]:

def expressify(fn: TFunc | None = None) -> TFunc | Callable[[TFunc], TFunc]:
def decorator(fn: TFunc) -> TFunc:
if fn.__code__ in code_cache:
fcode = code_cache[fn.__code__]
else:
# Save for next time
if is_pyodide:
# Disable code caching on Pyodide due to bug in hashing bytecode in 0.22.1.
# When Pyodide is updated to a newer version, this will be not be needed.
# https://github.com/posit-dev/py-shiny/issues/1042#issuecomment-1901945787
fcode = _transform_body(cast(types.FunctionType, fn))
code_cache[fn.__code__] = fcode
else:
if fn.__code__ in code_cache:
fcode = code_cache[fn.__code__]
else:
# Save for next time
fcode = _transform_body(cast(types.FunctionType, fn))
code_cache[fn.__code__] = fcode

# Create a new function from the code object
new_func = types.FunctionType(
Expand Down