Repository: spyder-ide/spyder

Issue:
Error when creating Pdb history file
## Description

### What steps will reproduce the problem?

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

To predict Daily stock value prediction with previuos 60 days data

### Traceback
```python-traceback
Exception ignored in: <function ShellWidget.__del__ at 0x000001A27B4BBA60>
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\plugins\ipythonconsole\widgets\shell.py", line 139, in __del__
    if (self.shutdown_thread is not None
AttributeError: 'ShellWidget' object has no attribute 'shutdown_thread'
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\history.py", line 77, in catch_corrupt_db
    return f(self, *a, **kw)
  File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\history.py", line 244, in init_db
    self.db = sqlite3.connect(str(self.hist_file), **kwargs)
sqlite3.OperationalError: unable to open database file

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\plugins\projects\plugin.py", line 379, in create_new_project
    self._create_project(root_path, project_type_id=project_type)
  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\plugins\projects\plugin.py", line 402, in _create_project
    self.open_project(path=root_path, project=project)
  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\plugins\projects\plugin.py", line 476, in open_project
    self.restart_consoles()
  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\plugins\projects\plugin.py", line 670, in restart_consoles
    self.ipyconsole.restart()
  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\plugins\ipythonconsole\plugin.py", line 1442, in restart
    self.create_new_client(give_focus=False)
  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\plugins\ipythonconsole\plugin.py", line 1028, in create_new_client
    client = ClientWidget(self, id_=client_id,
  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\plugins\ipythonconsole\widgets\client.py", line 139, in __init__
    self.shellwidget = ShellWidget(config=config_options,
  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\plugins\ipythonconsole\widgets\shell.py", line 89, in __init__
    super(ShellWidget, self).__init__(*args, **kw)
  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\plugins\ipythonconsole\widgets\debugging.py", line 197, in __init__
    super(DebuggingWidget, self).__init__(*args, **kwargs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\plugins\ipythonconsole\widgets\debugging.py", line 67, in __init__
    self._pdb_history_file = PdbHistory()
  File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\history.py", line 539, in __init__
    super(HistoryManager, self).__init__(shell=shell, config=config,
  File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\history.py", line 219, in __init__
    self.init_db()
  File "C:\ProgramData\Anaconda3\lib\site-packages\decorator.py", line 232, in fun
    return caller(func, *(extras + args), **kw)
  File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\history.py", line 85, in catch_corrupt_db
    elif self.hist_file.is_file():
AttributeError: 'str' object has no attribute 'is_file'
```

## Versions

* Spyder version: 5.1.5 
* Python version: 3.9.12
* Qt version: 5.9.7
* PyQt5 version: 5.9.2
* 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 >=3.0.2          :  3.1.0 (OK)
IPython >=7.6.0               :  8.2.0 (OK)
jedi >=0.17.2;<0.19.0         :  0.18.1 (OK)
jsonschema >=3.2.0            :  4.4.0 (OK)
keyring >=17.0.0              :  23.4.0 (OK)
nbconvert >=4.0               :  6.4.4 (OK)
numpydoc >=0.6.0              :  1.2 (OK)
paramiko >=2.4.0              :  2.8.1 (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.8.0 (OK)
pygments >=2.0                :  2.11.2 (OK)
pylint >=2.5.0;<2.10.0        :  2.9.6 (OK)
pyls_spyder >=0.4.0           :  0.4.0 (OK)
pylsp >=1.2.2;<1.3.0          :  1.2.4 (OK)
pylsp_black >=1.0.0           :  None (OK)
qdarkstyle =3.0.2             :  3.0.2 (OK)
qstylizer >=0.1.10            :  0.1.10 (OK)
qtawesome >=1.0.2             :  1.0.3 (OK)
qtconsole >=5.1.0             :  5.3.0 (OK)
qtpy >=1.5.0                  :  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.4.0 (OK)
spyder_kernels >=2.1.1;<2.2.0 :  2.1.3 (OK)
textdistance >=4.2.0          :  4.2.1 (OK)
three_merge >=0.1.1           :  0.1.1 (OK)
watchdog >=0.10.3             :  2.1.6 (OK)
zmq >=17                      :  22.3.0 (OK)

# Optional:
cython >=0.21                 :  0.29.28 (OK)
matplotlib >=2.0.0            :  3.5.1 (OK)
numpy >=1.7                   :  1.21.5 (OK)
pandas >=1.1.1                :  1.4.2 (OK)
scipy >=0.17.0                :  1.7.3 (OK)
sympy >=0.7.3                 :  1.10.1 (OK)
```



Discussion:
Hey @renjithkr1, thanks for reporting. We'll fix this problem in our next version (5.3.2), to be released next week.

Resolving patch:
diff --git a/spyder/plugins/ipythonconsole/widgets/debugging.py b/spyder/plugins/ipythonconsole/widgets/debugging.py
--- a/spyder/plugins/ipythonconsole/widgets/debugging.py
+++ b/spyder/plugins/ipythonconsole/widgets/debugging.py
@@ -65,12 +65,23 @@ def __init__(self, *args, **kwargs):
         # History
         self._pdb_history_input_number = 0  # Input number for current session
         self._saved_pdb_history_input_number = []  # for recursive debugging
-        self._pdb_history_file = PdbHistory()
-        self._pdb_history = [
-            line[-1] for line in self._pdb_history_file.get_tail(
-                self.PDB_HIST_MAX, include_latest=True)]
+
+        # Catch any exception that prevents to create or access the history
+        # file to avoid errors.
+        # Fixes spyder-ide/spyder#18531
+        try:
+            self._pdb_history_file = PdbHistory()
+            self._pdb_history = [
+                line[-1] for line in self._pdb_history_file.get_tail(
+                    self.PDB_HIST_MAX, include_latest=True)
+            ]
+        except Exception:
+            self._pdb_history_file = None
+            self._pdb_history = []
+
         self._pdb_history_edits = {}  # Temporary history edits
         self._pdb_history_index = len(self._pdb_history)
+
         # super init
         super(DebuggingHistoryWidget, self).__init__(*args, **kwargs)
 
@@ -78,12 +89,14 @@ def __init__(self, *args, **kwargs):
     def new_history_session(self):
         """Start a new history session."""
         self._pdb_history_input_number = 0
-        self._pdb_history_file.new_session()
+        if self._pdb_history_file is not None:
+            self._pdb_history_file.new_session()
 
     def end_history_session(self):
         """End an history session."""
         self._pdb_history_input_number = 0
-        self._pdb_history_file.end_session()
+        if self._pdb_history_file is not None:
+            self._pdb_history_file.end_session()
 
     def add_to_pdb_history(self, line):
         """Add command to history"""
@@ -103,7 +116,8 @@ def add_to_pdb_history(self, line):
         cmd = line.split(" ")[0]
         args = line.split(" ")[1:]
         is_pdb_cmd = (
-                cmd.strip() and cmd[0] != '!' and "do_" + cmd in dir(pdb.Pdb))
+            cmd.strip() and cmd[0] != '!' and "do_" + cmd in dir(pdb.Pdb)
+        )
         if self.is_pdb_using_exclamantion_mark():
             is_pdb_cmd = is_pdb_cmd or (
                 cmd.strip() and cmd[0] == '!'
@@ -112,7 +126,8 @@ def add_to_pdb_history(self, line):
         if cmd and (not is_pdb_cmd or len(args) > 0):
             self._pdb_history.append(line)
             self._pdb_history_index = len(self._pdb_history)
-            self._pdb_history_file.store_inputs(line_num, line)
+            if self._pdb_history_file is not None:
+                self._pdb_history_file.store_inputs(line_num, line)
 
     # --- Private API (overrode by us) --------------------------------
     @property
@@ -202,14 +217,16 @@ def shutdown(self):
         """
         Close the save thread and database file.
         """
-        try:
-            self._pdb_history_file.save_thread.stop()
-        except AttributeError:
-            pass
-        try:
-            self._pdb_history_file.db.close()
-        except AttributeError:
-            pass
+        if self._pdb_history_file is not None:
+            try:
+                self._pdb_history_file.save_thread.stop()
+            except AttributeError:
+                pass
+
+            try:
+                self._pdb_history_file.db.close()
+            except AttributeError:
+                pass
 
     # --- Comm API --------------------------------------------------
 


Test patch: