mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-16 14:23:28 +07:00
PY-48306 [debugger] [IJ-MR-167160] added type check for objects, when accessing dtype. restricted to certain container types
(cherry picked from commit 60808c3e70d39aad2efee45fa120eb08a8828079) IJ-MR-172080 GitOrigin-RevId: edada7f5ecf70fe905ff7fe1b56531fe311d668c
This commit is contained in:
committed by
intellij-monorepo-bot
parent
6e01bf9926
commit
db9dd95e6b
@@ -15,7 +15,8 @@ from _pydevd_bundle.pydevd_constants import dict_iter_items, dict_keys, IS_PY3K,
|
|||||||
GET_FRAME_RETURN_GROUP
|
GET_FRAME_RETURN_GROUP
|
||||||
from _pydevd_bundle.pydevd_extension_api import TypeResolveProvider, StrPresentationProvider
|
from _pydevd_bundle.pydevd_extension_api import TypeResolveProvider, StrPresentationProvider
|
||||||
from _pydevd_bundle.pydevd_user_type_renderers_utils import try_get_type_renderer_for_var
|
from _pydevd_bundle.pydevd_user_type_renderers_utils import try_get_type_renderer_for_var
|
||||||
from _pydevd_bundle.pydevd_utils import is_string, should_evaluate_full_value, should_evaluate_shape
|
from _pydevd_bundle.pydevd_utils import is_string, should_evaluate_full_value, \
|
||||||
|
should_evaluate_shape, is_container_with_shape_dtype
|
||||||
from _pydevd_bundle.pydevd_vars import get_label, array_default_format, is_able_to_format_number, MAXIMUM_ARRAY_SIZE, \
|
from _pydevd_bundle.pydevd_vars import get_label, array_default_format, is_able_to_format_number, MAXIMUM_ARRAY_SIZE, \
|
||||||
get_column_formatter_by_type, get_formatted_row_elements, IAtPolarsAccessor, DEFAULT_DF_FORMAT, DATAFRAME_HEADER_LOAD_MAX_SIZE
|
get_column_formatter_by_type, get_formatted_row_elements, IAtPolarsAccessor, DEFAULT_DF_FORMAT, DATAFRAME_HEADER_LOAD_MAX_SIZE
|
||||||
from pydev_console.pydev_protocol import DebugValue, GetArrayResponse, ArrayData, ArrayHeaders, ColHeader, RowHeader, \
|
from pydev_console.pydev_protocol import DebugValue, GetArrayResponse, ArrayData, ArrayHeaders, ColHeader, RowHeader, \
|
||||||
@@ -333,7 +334,7 @@ def var_to_struct(val, name, format='%s', do_trim=True, evaluate_full_value=True
|
|||||||
|
|
||||||
# shape to struct
|
# shape to struct
|
||||||
try:
|
try:
|
||||||
if should_evaluate_shape():
|
if should_evaluate_shape() and is_container_with_shape_dtype(type_qualifier, typeName, v):
|
||||||
if hasattr(v, 'shape') and not callable(v.shape):
|
if hasattr(v, 'shape') and not callable(v.shape):
|
||||||
debug_value.shape = str(tuple(v.shape))
|
debug_value.shape = str(tuple(v.shape))
|
||||||
elif hasattr(v, '__len__') and not is_string(v):
|
elif hasattr(v, '__len__') and not is_string(v):
|
||||||
@@ -344,8 +345,10 @@ def var_to_struct(val, name, format='%s', do_trim=True, evaluate_full_value=True
|
|||||||
# data type info to xml (for arrays and tensors)
|
# data type info to xml (for arrays and tensors)
|
||||||
debug_value.arrayElementType = ''
|
debug_value.arrayElementType = ''
|
||||||
try:
|
try:
|
||||||
if hasattr(v, 'dtype') and hasattr(v.dtype, 'name'):
|
if (is_container_with_shape_dtype(type_qualifier, typeName, v)
|
||||||
debug_value.arrayElementType = v.dtype.name
|
and hasattr(v, 'dtype')
|
||||||
|
and hasattr(v.dtype, 'name')):
|
||||||
|
debug_value.arrayElementType = str(v.dtype)
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,16 @@ import os
|
|||||||
import signal
|
import signal
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
|
try:
|
||||||
|
import torch
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
import tensorflow as tf
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
|
|
||||||
import pydevd_file_utils
|
import pydevd_file_utils
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -606,6 +616,24 @@ def is_pandas_container(type_qualifier, var_type, var):
|
|||||||
def is_numpy_container(type_qualifier, var_type, var):
|
def is_numpy_container(type_qualifier, var_type, var):
|
||||||
return var_type == "ndarray" and type_qualifier == "numpy" and hasattr(var, "shape")
|
return var_type == "ndarray" and type_qualifier == "numpy" and hasattr(var, "shape")
|
||||||
|
|
||||||
|
def is_pytorch_tensor(type_qualifier, var):
|
||||||
|
try:
|
||||||
|
import torch
|
||||||
|
return type_qualifier == "torch" and torch.is_tensor(var)
|
||||||
|
except ImportError:
|
||||||
|
return False # Can't be torch if it is not installed
|
||||||
|
|
||||||
|
def is_tf_tensor(type_qualifier, var):
|
||||||
|
try:
|
||||||
|
import tensorflow as tf
|
||||||
|
return type_qualifier.startswith("tensorflow") and tf.is_tensor(var)
|
||||||
|
except ImportError:
|
||||||
|
return False # Can't be tensorflow if it is not installed
|
||||||
|
|
||||||
|
def is_container_with_shape_dtype(type_qualifier, var_type, var):
|
||||||
|
return (is_numpy_container(type_qualifier, var_type, var)
|
||||||
|
or is_pytorch_tensor(type_qualifier, var)
|
||||||
|
or is_tf_tensor(type_qualifier, var))
|
||||||
|
|
||||||
def is_builtin(x):
|
def is_builtin(x):
|
||||||
return getattr(x, '__module__', None) == BUILTINS_MODULE_NAME
|
return getattr(x, '__module__', None) == BUILTINS_MODULE_NAME
|
||||||
@@ -640,11 +668,21 @@ def is_safe_to_access(obj, attr_name):
|
|||||||
of attribute access in the most risk-free manner. As an example, it leverages the
|
of attribute access in the most risk-free manner. As an example, it leverages the
|
||||||
`inspect` module, facilitating attribute retrieval without triggering any
|
`inspect` module, facilitating attribute retrieval without triggering any
|
||||||
descriptor functionality.
|
descriptor functionality.
|
||||||
|
|
||||||
|
Note
|
||||||
|
----
|
||||||
|
This function performs a strict check for potential side-effects, the access can be safe even if `False` is returned.
|
||||||
|
This might need to be checked more precisely for some special types.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
attr = inspect.getattr_static(obj, attr_name, None)
|
attr = inspect.getattr_static(obj, attr_name, None)
|
||||||
|
|
||||||
|
# Filter out objects that don't contain the given attribute
|
||||||
|
if attr is None:
|
||||||
|
return False
|
||||||
|
|
||||||
# Should we check for other descriptor types here?
|
# Should we check for other descriptor types here?
|
||||||
if inspect.isgetsetdescriptor(attr):
|
if inspect.isgetsetdescriptor(attr) or isinstance(attr, property):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ from _pydevd_bundle.pydevd_repr_utils import get_value_repr
|
|||||||
from _pydevd_bundle.pydevd_user_type_renderers_utils import \
|
from _pydevd_bundle.pydevd_user_type_renderers_utils import \
|
||||||
try_get_type_renderer_for_var
|
try_get_type_renderer_for_var
|
||||||
from _pydevd_bundle.pydevd_utils import is_string, should_evaluate_full_value, \
|
from _pydevd_bundle.pydevd_utils import is_string, should_evaluate_full_value, \
|
||||||
should_evaluate_shape
|
should_evaluate_shape, is_safe_to_access, is_container_with_shape_dtype
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import types
|
import types
|
||||||
@@ -344,8 +344,7 @@ def var_to_xml(val, name, do_trim=True, additional_in_xml='', evaluate_full_valu
|
|||||||
# shape to xml
|
# shape to xml
|
||||||
xml_shape = ''
|
xml_shape = ''
|
||||||
try:
|
try:
|
||||||
# if should_evaluate_shape() and is_safe_to_access(v, 'shape'):
|
if should_evaluate_shape() and is_container_with_shape_dtype(type_qualifier, typeName, v):
|
||||||
if should_evaluate_shape():
|
|
||||||
if hasattr(v, 'shape') and not callable(v.shape):
|
if hasattr(v, 'shape') and not callable(v.shape):
|
||||||
xml_shape = ' shape="%s"' % make_valid_xml_value(str(tuple(v.shape)))
|
xml_shape = ' shape="%s"' % make_valid_xml_value(str(tuple(v.shape)))
|
||||||
elif hasattr(v, '__len__') and not is_string(v):
|
elif hasattr(v, '__len__') and not is_string(v):
|
||||||
@@ -357,7 +356,8 @@ def var_to_xml(val, name, do_trim=True, additional_in_xml='', evaluate_full_valu
|
|||||||
# we use it for view as image
|
# we use it for view as image
|
||||||
xml_data_type = ''
|
xml_data_type = ''
|
||||||
try:
|
try:
|
||||||
if hasattr(v, 'dtype') and hasattr(v.dtype, 'name'):
|
if (is_container_with_shape_dtype(type_qualifier, typeName, v)
|
||||||
|
and hasattr(v.dtype, 'name')):
|
||||||
xml_data_type = ' arrayElementType="%s"' % make_valid_xml_value(v.dtype.name)
|
xml_data_type = ' arrayElementType="%s"' % make_valid_xml_value(v.dtype.name)
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|||||||
Reference in New Issue
Block a user