Repository: spyder-ide/spyder

Issue:
Debug: "Run Current Line" past the end of a script doesn't exit the debugger
## Description of your problem
Using "Run Current Line (ctrl-f10)" past the end of a program doesn't exit, but starts debugging in sitecustomize.py

**What steps will reproduce the problem?**

1. start debugging with this script:
```
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
x = 1
```
2. press `ctrl-f10` 2 or 3 times (just past the end of the script) 
3. sitecostomize.py will popup

**What is the expected output? What do you see instead?**
It should end debugging

## Versions and main components

* Spyder Version: 3.x branch
* Python Version: 3.6
* Qt Version: 5.6
* PyQt Version: 5.6
* Operating system:


## Dependencies

cython >=0.21    :  0.25.2 (OK)
jedi >=0.9.0     :  0.10.2 (OK)
nbconvert >=4.0  :  4.2.0 (OK)
numpy >=1.7      :  1.11.3 (OK)
pandas >=0.13.1  :  0.19.2 (OK)
psutil >=0.3     :  5.0.1 (OK)
pycodestyle >=2.3:  2.3.1 (OK)
pyflakes >=0.6.0 :  1.5.0 (OK)
pygments >=2.0   :  2.1.3 (OK)
pylint >=0.25    :  1.6.4 (OK)
qtconsole >=4.2.0:  4.3.0 (OK)
rope >=0.9.4     :  0.9.4-1 (OK)
sphinx >=0.6.6   :  1.5.1 (OK)
sympy >=0.7.3    :  1.0 (OK)


Discussion:
It seems a small error, so I'm leaving this one for 3.2.1
@dalthviz, please work on this one.

We need to detect if we are at the end of the file and stop debugging it that's the case.

Resolving patch:
diff --git a/spyder/utils/site/sitecustomize.py b/spyder/utils/site/sitecustomize.py
--- a/spyder/utils/site/sitecustomize.py
+++ b/spyder/utils/site/sitecustomize.py
@@ -423,12 +423,15 @@ def user_return(self, frame, return_value):
 
 @monkeypatch_method(pdb.Pdb, 'Pdb')
 def interaction(self, frame, traceback):
-    self.setup(frame, traceback)
-    if self.send_initial_notification:
-        self.notify_spyder(frame)
-    self.print_stack_entry(self.stack[self.curindex])
-    self._cmdloop()
-    self.forget()
+    if "sitecustomize.py" not in frame.f_code.co_filename:
+        self.setup(frame, traceback)
+        if self.send_initial_notification:
+            self.notify_spyder(frame)
+        self.print_stack_entry(self.stack[self.curindex])
+        self._cmdloop()
+        self.forget()
+    else:
+        self.run('exit')
 
 
 @monkeypatch_method(pdb.Pdb, 'Pdb')


Test patch:
diff --git a/spyder/app/tests/test_mainwindow.py b/spyder/app/tests/test_mainwindow.py
--- a/spyder/app/tests/test_mainwindow.py
+++ b/spyder/app/tests/test_mainwindow.py
@@ -830,6 +830,26 @@ def test_c_and_n_pdb_commands(main_window, qtbot):
     qtbot.wait(500)
     assert nsb.editor.model.rowCount() == 2
 
+    # Verify that doesn't go to sitecustomize.py with next and stops
+    # the debugging session.
+    qtbot.keyClicks(control, 'n')
+    qtbot.keyClick(control, Qt.Key_Enter)
+    qtbot.wait(500)
+
+    qtbot.keyClicks(control, 'n')
+    qtbot.keyClick(control, Qt.Key_Enter)
+    qtbot.wait(500)
+
+    assert nsb.editor.model.rowCount() == 3
+
+    qtbot.keyClicks(control, 'n')
+    qtbot.keyClick(control, Qt.Key_Enter)
+    qtbot.wait(500)
+
+    # Assert that the prompt appear
+    shell.clear_console()
+    assert 'In [2]:' in control.toPlainText()
+
     # Remove breakpoint and close test file
     main_window.editor.clear_all_breakpoints()
     main_window.editor.close_file()