Skip to content

Commit

Permalink
fix: use safe_exec's safe_globals instead of making new
Browse files Browse the repository at this point in the history
  • Loading branch information
nextchamp-saqib committed Sep 7, 2023
1 parent 76d013d commit 83a52d6
Showing 1 changed file with 4 additions and 28 deletions.
32 changes: 4 additions & 28 deletions insights/insights/doctype/insights_query/insights_script_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,7 @@

import frappe
import pandas as pd
import RestrictedPython.Guards
from frappe.utils.safe_exec import (
NamespaceDict,
_getattr,
_getitem,
_write,
add_data_utils,
compile_restricted,
get_python_builtins,
)
from frappe.utils.safe_exec import compile_restricted, get_safe_globals

from .utils import get_columns_with_inferred_types

Expand Down Expand Up @@ -92,8 +83,7 @@ def get_selected_tables(self):


def get_safe_exec_globals():
datautils = frappe._dict()
add_data_utils(datautils)
safe_globals = get_safe_globals()

pandas = frappe._dict()
pandas.DataFrame = pd.DataFrame
Expand All @@ -102,20 +92,6 @@ def get_safe_exec_globals():
# mock out to_csv and to_json to prevent users from writing to disk
pandas.DataFrame.to_csv = lambda *args, **kwargs: None
pandas.DataFrame.to_json = lambda *args, **kwargs: None
safe_globals.pandas = pandas

out = NamespaceDict(
utils=datautils,
as_json=frappe.as_json,
parse_json=frappe.parse_json,
make_get_request=frappe.integrations.utils.make_get_request,
pandas=pandas,
)

out._write_ = _write
out._getitem_ = _getitem
out._getattr_ = _getattr
out._getiter_ = iter
out._iter_unpack_sequence_ = RestrictedPython.Guards.guarded_iter_unpack_sequence
out.update(get_python_builtins())

return out
return safe_globals

0 comments on commit 83a52d6

Please sign in to comment.