Repository: spyder-ide/spyder

Issue:
AttributeError in profiler
## Description

### What steps will reproduce the problem?

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

Press F10
a window popped up
pressed ESC to close it

### Traceback
```python-traceback
  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_profiler\widgets\profilergui.py", line 264, in <lambda>
    self.finished(ec, es))
  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_profiler\widgets\profilergui.py", line 335, in finished
    self.show_data(justanalyzed=True)
  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_profiler\widgets\profilergui.py", line 362, in show_data
    self.datatree.show_tree()
  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_profiler\widgets\profilergui.py", line 526, in show_tree
    rootkey = self.find_root()  # This root contains profiler overhead
  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_profiler\widgets\profilergui.py", line 507, in find_root
    self.profdata.sort_stats("cumulative")
AttributeError: 'NoneType' object has no attribute 'sort_stats'
```

## Versions

* Spyder version: 3.3.1 
* Python version: 3.7.0
* Qt version: 5.9.6
* PyQt5 version: 5.9.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.23.4 (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:  2.2.3 (OK)
pylint >=0.25     :  2.1.1 (OK)
```



Discussion:
Please give us a step-by-step description to reproduce this error. Else we don't know how to solve it.
 I have seen the issue I reported one more timeBut now, it doesn't happen, and hitting run > profile doesn't cause it either. FYI, I am running Windows 10 
thanks 
    On Friday, November 30, 2018, 7:37:49 PM CST, Joel Godin <notifications@github.com> wrote:  
 
 
For info: Doesn't happen on OSX.
If you go to the menu at the top, hit 'Run' > 'Profile' do you get the same error?

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.
   

Resolving patch:
diff --git a/spyder_profiler/widgets/profilergui.py b/spyder_profiler/widgets/profilergui.py
--- a/spyder_profiler/widgets/profilergui.py
+++ b/spyder_profiler/widgets/profilergui.py
@@ -30,7 +30,7 @@
                             QTreeWidget, QTreeWidgetItem, QVBoxLayout, QWidget)
 
 # Local imports
-from spyder.config.base import get_conf_path, get_translation, debug_print
+from spyder.config.base import get_conf_path, get_translation
 from spyder.py3compat import to_text_string
 from spyder.utils import icon_manager as ima
 from spyder.utils.qthelpers import (create_toolbutton, get_item_user_text,
@@ -466,20 +466,24 @@ def initialize_view(self):
     def load_data(self, profdatafile):
         """Load profiler data saved by profile/cProfile module"""
         import pstats
+        # Fixes spyder-ide/spyder#6220
         try:
             stats_indi = [pstats.Stats(profdatafile), ]
         except (OSError, IOError):
+            self.profdata = None
             return
         self.profdata = stats_indi[0]
         
         if self.compare_file is not None:
+            # Fixes spyder-ide/spyder#5587
             try:
                 stats_indi.append(pstats.Stats(self.compare_file))
             except (OSError, IOError) as e:
                 QMessageBox.critical(
                     self, _("Error"),
-                    _("Error when trying to load profiler results"))
-                debug_print("Error when calling pstats, {}".format(e))
+                    _("Error when trying to load profiler results. "
+                      "The error was<br><br>"
+                      "<tt>{0}</tt>").format(e))
                 self.compare_file = None
         map(lambda x: x.calc_callees(), stats_indi)
         self.profdata.calc_callees()
@@ -495,12 +499,16 @@ def hide_diff_cols(self, hide):
             self.setColumnHidden(i, hide)
     
     def save_data(self, filename):
-        """"""
+        """Save profiler data."""
         self.stats1[0].dump_stats(filename)
 
     def find_root(self):
         """Find a function without a caller"""
-        self.profdata.sort_stats("cumulative")
+        # Fixes spyder-ide/spyder#8336
+        if self.profdata is not None:
+            self.profdata.sort_stats("cumulative")
+        else:
+            return
         for func in self.profdata.fcn_list:
             if ('~', 0) != func[0:2] and not func[2].startswith(
                     '<built-in method exec>'):
@@ -520,7 +528,7 @@ def show_tree(self):
         self.setItemsExpandable(True)
         self.setSortingEnabled(False)
         rootkey = self.find_root()  # This root contains profiler overhead
-        if rootkey:
+        if rootkey is not None:
             self.populate_tree(self, self.find_callees(rootkey))
             self.resizeColumnToContents(0)
             self.setSortingEnabled(True)


Test patch: