Repository: spyder-ide/spyder

Issue:
Error when trying to load Python files in the Variable Explorer
## Description

### What steps will reproduce the problem?

<!--- You can use Markdown here --->

trying to open a .py file is not supported by Spyder

### Traceback
```python-traceback
  File "C:\Program Files\Spyder\pkgs\spyder\plugins\variableexplorer\widgets\main_widget.py", line 178, in <lambda>
    triggered=lambda x: self.import_data(),
  File "C:\Program Files\Spyder\pkgs\spyder\plugins\variableexplorer\widgets\main_widget.py", line 488, in import_data
    nsb.import_data(filenames=filenames)
  File "C:\Program Files\Spyder\pkgs\spyder\plugins\variableexplorer\widgets\namespacebrowser.py", line 223, in import_data
    error_message = self.shellwidget.load_data(self.filename, ext)
  File "C:\Program Files\Spyder\pkgs\spyder\plugins\ipythonconsole\widgets\namespacebrowser.py", line 155, in load_data
    return self.call_kernel(
  File "C:\Program Files\Spyder\pkgs\spyder_kernels\comms\commbase.py", line 557, in __call__
    return self._comms_wrapper._get_call_return_value(
  File "C:\Program Files\Spyder\pkgs\spyder\plugins\ipythonconsole\comms\kernelcomm.py", line 225, in _get_call_return_value
    return super(KernelComm, self)._get_call_return_value(
  File "C:\Program Files\Spyder\pkgs\spyder_kernels\comms\commbase.py", line 440, in _get_call_return_value
    return self._sync_error(reply['value'])
  File "C:\Program Files\Spyder\pkgs\spyder_kernels\comms\commbase.py", line 496, in _sync_error
    error_wrapper.raise_error()
  File "C:\Program Files\Spyder\pkgs\spyder_kernels\comms\commbase.py", line 90, in raise_error
    raise self.etype(self)
Exception in comms call load_data:

  File "C:\Program Files\Spyder\pkgs\spyder_kernels\comms\commbase.py", line 343, in _handle_remote_call
    return_value = self._remote_callback(

  File "C:\Program Files\Spyder\pkgs\spyder_kernels\comms\frontendcomm.py", line 270, in _remote_callback
    return super(FrontendComm, self)._remote_callback(

  File "C:\Program Files\Spyder\pkgs\spyder_kernels\comms\commbase.py", line 356, in _remote_callback
    return self._remote_call_handlers[call_name](

  File "C:\Program Files\Spyder\pkgs\spyder_kernels\console\kernel.py", line 267, in load_data
    data, error_message = load_func(filename)

  File "C:\Program Files\Spyder\pkgs\spyder_kernels\utils\iofuncs.py", line 382, in load_dictionary
    with tarfile.open(filename, "r") as tar:

  File "tarfile.py", line 1608, in open

tarfile.ReadError: file could not be opened successfully

```

## Versions

* Spyder version: 5.3.0 
* Python version: 3.8.10
* Qt version: 5.15.2
* PyQt5 version: 5.15.6
* Operating System: Windows 10

### Dependencies

```
# Mandatory:
atomicwrites >=1.2.0                 :  1.4.0 (OK)
chardet >=2.0.0                      :  4.0.0 (OK)
cloudpickle >=0.5.0                  :  2.0.0 (OK)
cookiecutter >=1.6.0                 :  1.7.3 (OK)
diff_match_patch >=20181111          :  20200713 (OK)
intervaltree                         :  None (OK)
IPython >=7.31.1;<8.0.0              :  7.32.0 (OK)
jedi >=0.17.2;<0.19.0                :  0.18.1 (OK)
jellyfish >=0.7                      :  0.9.0 (OK)
jsonschema >=3.2.0                   :  4.4.0 (OK)
keyring >=17.0.0                     :  23.5.0 (OK)
nbconvert >=4.0                      :  6.4.5 (OK)
numpydoc >=0.6.0                     :  1.2.1 (OK)
paramiko >=2.4.0                     :  2.10.3 (OK)
parso >=0.7.0;<0.9.0                 :  0.8.3 (OK)
pexpect >=4.4.0                      :  4.8.0 (OK)
pickleshare >=0.4                    :  0.7.5 (OK)
psutil >=5.3                         :  5.9.0 (OK)
pygments >=2.0                       :  2.11.2 (OK)
pylint >=2.5.0                       :  2.13.3 (OK)
pyls_spyder >=0.4.0                  :  0.4.0 (OK)
pylsp >=1.4.1;<1.5.0                 :  1.4.1 (OK)
pylsp_black >=1.2.0                  :  1.2.0 (OK)
qdarkstyle >=3.0.2;<3.1.0            :  3.0.3 (OK)
qstylizer >=0.1.10                   :  0.2.1 (OK)
qtawesome >=1.0.2                    :  1.1.1 (OK)
qtconsole >=5.3.0;<5.4.0             :  5.3.0 (OK)
qtpy >=2.0.1                         :  2.0.1 (OK)
rtree >=0.9.7                        :  0.9.7 (OK)
setuptools >=49.6.0                  :  61.2.0 (OK)
sphinx >=0.6.6                       :  4.5.0 (OK)
spyder_kernels >=2.3.0;<2.4.0        :  2.3.0 (OK)
textdistance >=4.2.0                 :  4.2.2 (OK)
three_merge >=0.1.1                  :  0.1.1 (OK)
watchdog                             :  2.1.7 (OK)
zmq >=17                             :  22.3.0 (OK)

# Optional:
cython >=0.21                        :  0.29.28 (OK)
matplotlib >=3.0.0                   :  3.5.1 (OK)
numpy >=1.7                          :  1.22.3 (OK)
pandas >=1.1.1                       :  1.4.1 (OK)
scipy >=0.17.0                       :  1.8.0 (OK)
sympy >=0.7.3                        :  1.10.1 (OK)

# Spyder plugins:
spyder_terminal.terminalplugin 1.2.2 :  1.2.2 (OK)
```



Discussion:
Hey @gl4500, thanks for reporting. It seems you're trying to load a Python file, or a file with `.py` extension in the Variable Explorer. However, that's not supported by Spyder, which is the reason why received this error.

I understand it's confusing that we're showing it, so we'll catch it and display an informative message instead in a future version. Sorry for the inconvenience.

Resolving patch:
diff --git a/spyder/plugins/ipythonconsole/widgets/namespacebrowser.py b/spyder/plugins/ipythonconsole/widgets/namespacebrowser.py
--- a/spyder/plugins/ipythonconsole/widgets/namespacebrowser.py
+++ b/spyder/plugins/ipythonconsole/widgets/namespacebrowser.py
@@ -9,24 +9,23 @@
 the Variable Explorer
 """
 
+# Standard library imports
 import logging
-import time
-try:
-    time.monotonic  # time.monotonic new in 3.3
-except AttributeError:
-    time.monotonic = time.time
-
 from pickle import PicklingError, UnpicklingError
+import tarfile
+import time
 
-from qtpy.QtWidgets import QMessageBox
-
+# Third-party imports
 from qtconsole.rich_jupyter_widget import RichJupyterWidget
+from qtpy.QtWidgets import QMessageBox
+from spyder_kernels.comms.commbase import CommError
 
+# Local imports
 from spyder.config.base import _
-from spyder.py3compat import PY2, to_text_string, TimeoutError
-from spyder_kernels.comms.commbase import CommError
+from spyder.config.utils import IMPORT_EXT
 
 
+# For logging
 logger = logging.getLogger(__name__)
 
 # Max time before giving up when making a blocking call to the kernel
@@ -169,6 +168,13 @@ def load_data(self, filename, ext):
         except TimeoutError:
             msg = _("Data is too big to be loaded")
             return msg
+        except tarfile.ReadError:
+            # Fixes spyder-ide/spyder#19126
+            msg = _(f"The file could not be opened successfully. Recall that "
+                    f"the Variable Explorer supports the following file "
+                    f"extensions to import data:"
+                    f"<br><br><tt>{', '.join(IMPORT_EXT)}</tt>")
+            return msg
         except (UnpicklingError, RuntimeError, CommError):
             return None
 


Test patch: