Skip to content

Commit aec070b

Browse files
authored
Fix builtins so they don't interfere with our execution (#10511)
* Fix builtins * Format with black * Move variable value and variable list to just be for tests
1 parent 66db7ed commit aec070b

File tree

6 files changed

+18
-13
lines changed

6 files changed

+18
-13
lines changed

news/2 Fixes/10280.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix problem with Data Viewer not working when builtin functions are overridden (like max).

pythonFiles/datascience/getJupyterVariableDataFrameInfo.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import json as _VSCODE_json
33
import pandas as _VSCODE_pd
44
import pandas.io.json as _VSCODE_pd_json
5+
import builtins as _VSCODE_builtins
56

67
# _VSCode_sub_supportsDataExplorer will contain our list of data explorer supported types
78
_VSCode_supportsDataExplorer = "['list', 'Series', 'dict', 'ndarray', 'DataFrame']"
@@ -21,7 +22,7 @@ def _VSCODE_getRowCount(var):
2122
return 0
2223
elif hasattr(var, "__len__"):
2324
try:
24-
return len(var)
25+
return _VSCODE_builtins.len(var)
2526
except TypeError:
2627
return 0
2728

@@ -34,7 +35,7 @@ def _VSCODE_getRowCount(var):
3435
del _VSCODE_targetVariable
3536
else:
3637
del _VSCode_supportsDataExplorer
37-
_VSCODE_evalResult = eval(_VSCODE_targetVariable["name"])
38+
_VSCODE_evalResult = _VSCODE_builtins.eval(_VSCODE_targetVariable["name"])
3839

3940
# Figure out shape if not already there. Use the shape to compute the row count
4041
_VSCODE_targetVariable["rowCount"] = _VSCODE_getRowCount(_VSCODE_evalResult)
@@ -76,7 +77,7 @@ def _VSCODE_getRowCount(var):
7677

7778
# Compute the index column. It may have been renamed
7879
_VSCODE_indexColumn = _VSCODE_df.index.name if _VSCODE_df.index.name else "index"
79-
_VSCODE_columnTypes = list(_VSCODE_df.dtypes)
80+
_VSCODE_columnTypes = _VSCODE_builtins.list(_VSCODE_df.dtypes)
8081
del _VSCODE_df
8182

8283
# Make sure the index column exists
@@ -86,7 +87,9 @@ def _VSCODE_getRowCount(var):
8687

8788
# Then loop and generate our output json
8889
_VSCODE_columns = []
89-
for _VSCODE_n in range(0, len(_VSCODE_columnNames)):
90+
for _VSCODE_n in _VSCODE_builtins.range(
91+
0, _VSCODE_builtins.len(_VSCODE_columnNames)
92+
):
9093
_VSCODE_column_type = _VSCODE_columnTypes[_VSCODE_n]
9194
_VSCODE_column_name = str(_VSCODE_columnNames[_VSCODE_n])
9295
_VSCODE_colobj = {}
@@ -114,3 +117,4 @@ def _VSCODE_getRowCount(var):
114117
del _VSCODE_json
115118
del _VSCODE_pd
116119
del _VSCODE_pd_json
120+
del _VSCODE_builtins

pythonFiles/datascience/getJupyterVariableDataFrameRows.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22
import json as _VSCODE_json
33
import pandas as _VSCODE_pd
44
import pandas.io.json as _VSCODE_pd_json
5+
import builtins as _VSCODE_builtins
56

67
# In IJupyterVariables.getValue this '_VSCode_JupyterTestValue' will be replaced with the json stringified value of the target variable
78
# Indexes off of _VSCODE_targetVariable need to index types that are part of IJupyterVariable
89
_VSCODE_targetVariable = _VSCODE_json.loads("""_VSCode_JupyterTestValue""")
9-
_VSCODE_evalResult = eval(_VSCODE_targetVariable["name"])
10+
_VSCODE_evalResult = _VSCODE_builtins.eval(_VSCODE_targetVariable["name"])
1011

1112
# _VSCode_JupyterStartRow and _VSCode_JupyterEndRow should be replaced dynamically with the literals
1213
# for our start and end rows
13-
_VSCODE_startRow = max(_VSCode_JupyterStartRow, 0)
14-
_VSCODE_endRow = min(_VSCode_JupyterEndRow, _VSCODE_targetVariable["rowCount"])
14+
_VSCODE_startRow = _VSCODE_builtins.max(_VSCode_JupyterStartRow, 0)
15+
_VSCODE_endRow = _VSCODE_builtins.min(
16+
_VSCode_JupyterEndRow, _VSCODE_targetVariable["rowCount"]
17+
)
1518

1619
# Assume we have a dataframe. If not, turn our eval result into a dataframe
1720
_VSCODE_df = _VSCODE_evalResult
@@ -46,3 +49,4 @@
4649
del _VSCODE_json
4750
del _VSCODE_pd
4851
del _VSCODE_pd_json
52+
del _VSCODE_builtins

pythonFiles/tests/ipython/scripts.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ def execute_script(file, replace_dict=dict([])):
4545

4646
def get_variables(capsys):
4747
path = os.path.dirname(os.path.abspath(__file__))
48-
file = os.path.abspath(
49-
os.path.join(path, "../../datascience/getJupyterVariableList.py")
50-
)
48+
file = os.path.abspath(os.path.join(path, "./getJupyterVariableList.py"))
5149
if execute_script(file):
5250
read_out = capsys.readouterr()
5351
return json.loads(read_out.out)
@@ -64,9 +62,7 @@ def find_variable_json(varList, varName):
6462
def get_variable_value(variables, name, capsys):
6563
varJson = find_variable_json(variables, name)
6664
path = os.path.dirname(os.path.abspath(__file__))
67-
file = os.path.abspath(
68-
os.path.join(path, "../../datascience/getJupyterVariableValue.py")
69-
)
65+
file = os.path.abspath(os.path.join(path, "./getJupyterVariableValue.py"))
7066
keys = dict([("_VSCode_JupyterTestValue", json.dumps(varJson))])
7167
if execute_script(file, keys):
7268
read_out = capsys.readouterr()

0 commit comments

Comments
 (0)