Repository: spyder-ide/spyder
Pull request: PR: Fix control-click URL in tutorial ValueError
<!--- Make sure to read the Contributing Guidelines:                   --->
<!--- https://github.com/spyder-ide/spyder/blob/master/CONTRIBUTING.md --->
<!--- and follow PEP 8, PEP 257 and Spyder's code style:               --->
<!--- https://github.com/spyder-ide/spyder/wiki/Dev:-Coding-Style      --->

## Description of Changes


* [ ] Wrote at least one-line docstrings (for any new functions)
* [ ] Added unit test(s) covering the changes (if testable)
<!--- Remember that an image/animation is worth a thousand words! --->
* [ ] Included a screenshot or animation (if affecting the UI, see [Licecap](https://www.cockos.com/licecap/))


<!--- Explain what you've done and why --->
Add a try catch to prevent the ValueError in tutorial



### Issue(s) Resolved

<!--- List the issue(s) below, in the form "Fixes #1234"; one per line --->

Fixes #9849


### Affirmation

By submitting this Pull Request or typing my (user)name below,
I affirm the [Developer Certificate of Origin](https://developercertificate.org)
with respect to all commits and content included in this PR,
and understand I am releasing the same under Spyder's MIT (Expat) license.

<!--- TYPE YOUR USER/NAME AFTER THE FOLLOWING: --->
I certify the above statement is true and correct: Steff456

<!--- Thanks for your help making Spyder better for everyone! --->


Problem statement:
Control-click URL in tutorial causes ValueError
## Description

### What steps will reproduce the problem?

<!--- You can use Markdown here --->

In the Spyder tutorial, the 'Recommended first steps for Python beginners' section, control-left click on the 'IPython interpreter' link.

I did this because I thought it might open the URL in a new window.  It opens the URL successfully (in my web browser, same as a normal left-click), but it issues the "Spyder has encountered an internal problem!" bug report page.

I'm using windows 10, 64-bit.  Default browser is Firefox.

### Traceback
```python-traceback
  File "C:\Users\mckay\Anaconda3\lib\site-packages\spyder\widgets\browser.py", line 168, in createWindow
    webbrowser.open(to_text_string(self.url().toString()))
  File "C:\Users\mckay\Anaconda3\lib\webbrowser.py", line 78, in open
    if browser.open(url, new, autoraise):
  File "C:\Users\mckay\Anaconda3\lib\webbrowser.py", line 582, in open
    os.startfile(url)
ValueError: startfile: filepath too long for Windows
```

## Versions

* Spyder version: 3.3.1 
* Python version: 3.7.0
* Qt version: 5.8.0
* PyQt5 version: 5.8.2
* Operating System: Windows 10

### Dependencies

```
pyflakes >=0.6.0  :  2.0.0 (OK)
pycodestyle >=2.3 :  2.4.0 (OK)
pygments >=2.0    :  2.2.0 (OK)
sphinx >=0.6.6    :  1.7.9 (OK)
rope >=0.9.4      :  0.11.0 (OK)
jedi >=0.9.0      :  0.12.1 (OK)
nbconvert >=4.0   :  5.4.0 (OK)
pandas >=0.13.1   :  0.24.2 (OK)
numpy >=1.7       :  1.15.1 (OK)
sympy >=0.7.3     :  1.1.1 (OK)
cython >=0.21     :  0.28.5 (OK)
qtconsole >=4.2.0 :  4.4.1 (OK)
IPython >=4.0     :  6.5.0 (OK)
matplotlib >=2.0.0:  3.0.3 (OK)
pylint >=0.25     :  2.1.1 (OK)
```

Hints from issue discussion:
Thanks for reporting. We'll fix this in Spyder 4.

@steff456, simply catch this error in a try/except and `pass` if it appears.

Commit message: add parent to widget

Patch to review:
diff --git a/spyder/widgets/browser.py b/spyder/widgets/browser.py
index 5a177460182..023f4af225e 100644
--- a/spyder/widgets/browser.py
+++ b/spyder/widgets/browser.py
@@ -168,7 +168,10 @@ def zoom_in(self):
     #------ QWebEngineView API -------------------------------------------------------
     def createWindow(self, webwindowtype):
         import webbrowser
-        webbrowser.open(to_text_string(self.url().toString()))
+        try:
+            webbrowser.open(to_text_string(self.url().toString()))
+        except ValueError:
+            pass
 
     def contextMenuEvent(self, event):
         menu = QMenu(self)
diff --git a/spyder/widgets/tests/test_browser.py b/spyder/widgets/tests/test_browser.py
index 5d605826c46..d73de042aa0 100644
--- a/spyder/widgets/tests/test_browser.py
+++ b/spyder/widgets/tests/test_browser.py
@@ -10,9 +10,9 @@
 
 # Test library imports
 import pytest
-
+from qtpy.QtWebEngineWidgets import QWebEnginePage
 # Local imports
-from spyder.widgets.browser import WebBrowser
+from spyder.widgets.browser import WebBrowser, WebView
 
 
 @pytest.fixture
@@ -31,5 +31,13 @@ def test_browser(browser):
     assert browser
 
 
+def test_webview_open_regression(qtbot):
+    """Check creating of new window."""
+    wb = WebView(None)
+    qtbot.addWidget(wb)
+    wb.setUrl('problem_url')
+    wb.createWindow(QWebEnginePage.WebBrowserWindow)
+
+
 if __name__ == "__main__":
     pytest.main()


Code review comments:
diff_hunk: @@ -168,7 +168,10 @@ def zoom_in(self):
     #------ QWebEngineView API -------------------------------------------------------
     def createWindow(self, webwindowtype):
         import webbrowser
-        webbrowser.open(to_text_string(self.url().toString()))
+        try:
path: spyder/widgets/browser.py
text: @user1:
```suggestion
        # See: spyder-ide/spyder#9849
        try:
```