CodeSafe Functions
Last updated
Was this helpful?
Last updated
Was this helpful?
Was this helpful?
Available functions:
safe_eval
(expr, allowed_builtins, allowed_vars, timeout=5, restricted_imports, allowed_function_calls, immediate_termination=False, file_access=False, network_access=False)
: Evaluate an expression safely.
encrypt_to_file
(code, output_file=None, mapping=default)
: Encrypt code to a file.
run
(encrypted_file, mapping=default)
: Run an encrypted code file (only available for code/formats Python understands)
decrypt_to_file
(encrypted_file, output_file=None, mapping=default)
: Decrypt an encrypted file to a new file.
encrypt
(code, mapping=default)
: Encrypt code, and return it.
decrypt
(encrypted_code, mapping=default)
: Decrypt the code, and return it.
Evaluate an expression safely.
Parameters:
expr (str): The expression to evaluate.
allowed_builtins (dict, optional): A dictionary of allowed built-in functions. Defaults to {}.
allowed_vars (dict, optional): A dictionary of allowed variables and functions. Defaults to {}.
expr
: An expression to evaluate.
allowed_builtins
: A dictionary object of allowed built-in functions. builtins = {"abs": abs, "max": max}
allowed_vars
: A dictionary object of allowed variables and functions. {"x": 10, "y": 20, "custom_sum": custom_sum, "long_running_function": long_running_function, "sleep": sleep}
. For custom functions, other functions like time.sleep, and other variables in your code.
timeout
: The evaluation limit for the call, defaults to 5 seconds
.
restricted_imports
: A list of restricted imports or modules. ['os', 'sys']
.
allowed_function_calls
: A list of allowed function calls. Even when allowed_vars contains the function, it is not callable unless passed to this parameter as well. ['custom_sum', 'max', 'abs', 'long_running_function', 'sleep']
immediate_termination
: A bool that controls whether to forcibly terminate an evaluation, if it exceeds the timeout, otherwise, the evaluation will continue until it is finished.
file_access
: A bool that controls file access, and allows or disallows access to the filesystem. It restricts additional functions and modules from being called. Defaults to False
.
internet_access
: A bool that controls internet access, and allows or disallows access to network, sockets, requests, and other libraries. Defaults to False
.
Encrypt code to a file.
Parameters:
code (str): The Python code to encrypt.
output_file (str): The path to the output Python file to embed encrypted code.
mapping (dict): The mapping dictionary for character replacements.
Returns:
None
Run an encrypted code file (only available for code/formats Python understands).
Parameters:
encrypted_file (str): Path to the Python file with embedded encrypted code.
mapping (dict): The mapping dictionary for character replacements.
Returns:
None
Decrypt an encrypted file to a new file.
Parameters:
encrypted_file (str): Path to the Python file with embedded encrypted code.
output_file (str): Path to the output Python file for decrypted code.
mapping (dict): The mapping dictionary for character replacements.
Returns:
None
Encrypt code, and return it.
Parameters:
code (str): The Python code to encrypt.
mapping (dict): The mapping dictionary for character replacements.
Returns:
str: The encrypted code.
Decrypt code, and return it.
Parameters:
code (str): The Python code to decrypt.
mapping (dict): The mapping dictionary for character replacements.
Returns:
str: The decrypted code.