You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support mutating row in --convert without returning it #371
def_compile_code(code, imports, variable="value"):
locals= {}
globals= {"r": recipes, "recipes": recipes}
# If user defined a convert() function, return thattry:
exec(code, globals, locals)
returnlocals["convert"]
except (AttributeError, SyntaxError, NameError, KeyError, TypeError):
pass# Try compiling their code as a function insteadbody_variants= [code]
# If single line and no 'return', try adding the returnif"\n"notincodeandnotcode.strip().startswith("return "):
body_variants.insert(0, "return {}".format(code))
forvariantinbody_variants:
new_code= ["def fn({}):".format(variable)]
forlineinvariant.split("\n"):
new_code.append(" {}".format(line))
try:
code_o=compile("\n".join(new_code), "<string>", "exec")
breakexceptSyntaxError:
# Try another variant, e.g. for 'return row["column"] = 1'continueforimport_inimports:
globals[import_.split(".")[0]] =__import__(import_)
exec(code_o, globals, locals)
returnlocals["fn"]
The previous code for highlighting errors in syntax (which was already a bit confused thanks to the added return, see #355 (comment) - isn't compatible with this approach at all. I'm going to ditch it and just show a generic Error: Could not compile code message.
Activity
simonw commentedon Jan 9, 2022
Might be a case of modifying this line:
sqlite-utils/sqlite_utils/cli.py
Line 828 in e0c476b
To:
simonw commentedon Jan 9, 2022
Also need to update relevant docs for that example.
simonw commentedon Jan 9, 2022
Tried this test:
And got this error:
The code snippet compilation isn't currently compatible with this.
simonw commentedon Jan 9, 2022
Here's the code in question:
sqlite-utils/sqlite_utils/utils.py
Lines 288 to 299 in b8c1340
simonw commentedon Jan 9, 2022
This seems to work:
simonw commentedon Jan 9, 2022
The previous code for highlighting errors in syntax (which was already a bit confused thanks to the added
return, see #355 (comment) - isn't compatible with this approach at all. I'm going to ditch it and just show a genericError: Could not compile codemessage.Fix for cog error
Release 3.21
Release 3.21
--convert function can now modify row in place, closes simonw#371