Repository: spyder-ide/spyder

Issue:
Spyder crashes upon typing `net(` in editor
## Description of your problem
I execute the code below. Then try to type `net(` and Spyder crashes. 

**What steps will reproduce the problem?**
Execute this code:
```    
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
import numpy as np



class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        # 1 input image channel, 6 output channels, 5x5 square convolution
        # kernel
        self.conv1 = nn.Conv2d(1, 6, 5) # Takes 4D input: nSamples + nChannels x Height x Width
        self.conv2 = nn.Conv2d(6, 16, 5)
        # an affine operation: y = Wx + b
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        # Max pooling over a (2, 2) window
        x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
        # If the size is a square you can only specify a single number
        x = F.max_pool2d(F.relu(self.conv2(x)), 2)
        x = x.view(-1, self.num_flat_features(x))
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

    def num_flat_features(self, x):
        size = x.size()[1:]  # all dimensions except the batch dimension
        num_features = 1
        for s in size:
            num_features *= s
        return num_features

net = Net()
print(net)
```
*Code comes from PyTorch Tutorial webpage (http://pytorch.org/tutorials/beginner/blitz/neural_networks_tutorial.html)

I saved this file as YSTest.py

**What is the expected output? What do you see instead?**
Expected output is word `net()` appearing in editor. The `net` object is instance of Net class, so there should be no reason why I am unable to get this to work properly. 

**Please provide any additional information below**
Often, but not always (and this is hard to replicate, but _**very annoying**_) Spyder will crash if I type `net(` into the YSTest.py file BEFORE executing a single line of code. Curiously, this problem manifests only if `net(` is typed in the YSTest.py file that has the code above. Typing `net(` in a new Python file adjacent to the YSTest.py file results in no error (with or without packages loaded). In fact, if I execute the code above and type `net(` in a new python file in Spyder, there is no error. Similarly, there is no error with typing `net(` in the IPython kernel (it even brings up the expected in-line documentation support instantly!)

This led me to believe the YSTest.py file is corrupted, so I made new such file in a clean directory by manually typing all commands above in. Problem persists. Running `spyder  --reset` and restarting my computer did not resolve the issue either...

## Versions and main components

* Spyder Version: 3.2.4
* Python Version: 2.7
* Qt Version: 5.6.2
* PyQt Version: 5.6
* Operating system: Ubuntu 14.04

## Dependencies

Please go to the menu entry `Help > Optional Dependencies` (or
`Help > Dependencies`), press the button `Copy to clipboard`
and paste the contents below:

IPython >=4.0;<6.0:  5.4.1 (OK)
cython >=0.21     :  0.26.1 (OK)
jedi >=0.9.0      :  0.10.2 (OK)
nbconvert >=4.0   :  5.3.1 (OK)
numpy >=1.7       :  1.13.3 (OK)
pandas >=0.13.1   :  0.20.3 (OK)
psutil >=0.3      :  5.4.0 (OK)
pycodestyle >=2.3 :  2.3.1 (OK)
pyflakes >=0.5.0  :  1.6.0 (OK)
pygments >=2.0    :  2.2.0 (OK)
pylint >=0.25     :  1.7.4 (OK)
qtconsole >=4.2.0 :  4.3.1 (OK)
rope >=0.9.4      :  0.10.5 (OK)
sphinx >=0.6.6    :  1.6.3 (OK)
sympy >=0.7.3     :  1.1.1 (OK)


Additional info: 
PyTorch version: 0.3.0.post4 
*Installed PyTorch via Anaconda for Python 2.7 as per official install instructions (http://pytorch.org/): 
`conda install pytorch torchvision -c pytorch` for PyTorch _without_ CUDA





Discussion:
Please update to our 3.2.5 version and try again.
Just updated Spyder to 3.2.5 following your directions:
`conda update qt pyqt`
`conda update spyder`

Unfortunately, problem persists exactly as before.
I also tried making a fresh virtual environment with `conda create -n Clean anaconda python=2.7`. Using Anaconda-navigator, I change to `Clean` virtual environment and clicked to install Spyder 3.2.5. I then opened the YSTest.py file, typed `net(` and Spyder promptly crashed...

Is this problem with Spyder, the way Spyder is being installed by Anaconda-navigator 1.6.9, or is something very fundamentally wrong with my installation of Conda command line tools, Anaconda, and/or Anaconda-Navigator? If it's the last point, I would think the solution is to completely uninstall and re-install Anaconda. If Anaconda-navigator is the problem, I can try updating it; would you happen to know the best way to do that (I ask because I tried to update with Anaconda-Navigator GUI and found many packages were badly broken). 
> Is this problem with Spyder, the way Spyder is being installed by Anaconda-navigator 1.6.9, or is something very fundamentally wrong with my installation of Conda command line tools, Anaconda, and/or Anaconda-Navigator?

Generally speaking, if this new problem only occurs when spyder is installed in a specific manner, rather than intrinsic to Spyder's code, it is very likely an issue with that install or install method. As to the specifics of your Anaconda installation, we can only guess at what is wrong, as we don't have direct access to it.

> Anaconda-navigator 1.6.9

FYI, 1.6.11 is available. You should be able to update in the GUI, or with `conda update anaconda-navigator`. Also, `conda update conda` usually never hurts. Worth trying, at least...

> If it's the last point, I would think the solution is to completely uninstall and re-install Anaconda.

Probably a good idea in any case with the general instability you describe. At the very least, if the problem did persist, this would rule out most user-side issues, and indicate that it is either something specific to `torch`, an issue with Spyder's third party completion libraries (`jedi` and `rope`), or genuinely an issue in Spyder's codebase.

Thanks!
I have Ubuntu 16.04 machine at home. It is "naive" it in that is has never had Anaconda installed on it. I installed Anaconda and Anaconda-Navigator on this computer, following directions from Anaconda website (download Linux installer; install with bash Anaconda2-xxxxxxx.sh, following directions there after and accepting all defaults). 

Then installed PyTorch, again using conda for installation as advised above. 

Executed code above. Crashes immediately upon typing `net(` in the editor. Have you been able to recreate the problem? I wonder if it is specific to Anaconda, Spyder, PyTorch, python2, etc. installation on Ubuntu. Though, that seems a bit unlikely.

For what it's worth, the terminal window that was used to launch Anaconda-navigator throws this message when Spyder crashes upon typing `net(`:

``` /home/yohan/.anaconda/navigator/scripts/spyder.sh: line 3: 24235 Segmentation fault      (core dumped) /home/yohan/anaconda2/bin/spyder > /home/yohan/.anaconda/navigator/scripts/spyder-out-2.txt 2> /home/yohan/.anaconda/navigator/scripts/spyder-err-2.txt```

I opened both the text files indicated; unfortunately they are totally blank...(`

As side note, it crashes if I change `net(` to `neta(`, `myNet(`, or `convNet2`; basically anything that has `net` in it causes this crashing error.... Curiously, this does not crash 
```
bob = Net()
print bob
```
`bob(` 

So one solution is for me to completely avoid using the word `net` in my code (and be super-careful when typing in PyTorch functions with the word "net" in them. But I'm quite curious to know why this error occurs and I how I can properly get rid of it. It's a bit unnatural and unwieldy  to build a series of Net classes for a neural network and be completely barred from calling something it a "net" or "network," lest the IDE crash spontaneously. Moreover, several tools in PyTorch are functions with the word "net" in them...

Am I correct in surmising that this error has to do with Spyder and PyTorch not playing nice? That'd be quite a pity. I need PyTorch for some things at work, and Spyder is my preferred IDE.
> Have you been able to recreate the problem?

Unfortunately not yet, as `conda install pytorch torchvision -c pytorch` did not find any results. Do you have more specific instructions for installing as you did or should I dig some more?

> Am I correct in believing this error has to do with Spyder and PyTorch not playing nice? 

Could be; if so would probably be due to one of Spyder's third party introspection libraries `rope` and or `jedi`. In terms of the hard crash, if you go into Spyder Preferences and disable everything under Editor > Code Introspection/Analysis, do you still get that issue (after restarting)? If not, and you reenable things one by one, which causes the error? 
> conda install pytorch torchvision -c pytorch did not find any results. 

Very interesting; this is the command suggested by PyTorch themselves: http://pytorch.org/ `conda install pytorch torchvision -c pytorch` should work for Linux and OSX, regardless of whether you have  CUDA installed or not. Are you using Windows? If so, the PyTorch developers seem to be having some trouble supporting it unfortunately. 

> disable everything under Editor > Code Introspection/Analysis, do you still get that issue (after restarting)

No!! Thanks for the tip!
Disabled every option in editor and re-enabled one-by-one. "Balloon tips" exclusively is the problem; works perfectly with all other options enabled.
> Are you using Windows?

That would explain it; both my main personal and work machines are Windoze atm. I've got an old box with Debian/Ubuntu but I've decommissioned it and am going to build a new top of the line one Soon™, and one of the machines I support is a Mac but don't have direct access currently, so I can't easily test on either atm.

> Disabled every option in editor and re-enabled one-by-one. "Balloon tips" exclusively is the problem; works perfectly with all other options enabled.

Very interesting! Since those need to introspect the functions in the package to read their signatures, probably has to do with something between doing that (in Spyder's third party introspection libraries) and displaying the calltip, probably closer to the former. @ccordoba12 Thoughts? Also, might it have anything to do with the troubles with `test_calltip` by any chance? 
>so I can't easily test on either atm

Sorry to hear that. Y'all have been very helpful; if there are easy things that you would like me to test to further explore the problem, I'm happy to do that. 

I think I understand your last point. Good luck to you, and I look forward to seeing a patch (if possible) soon. 
> if there are easy things that you would like me to test to further explore the problem, I'm happy to do that

If you re-enable the popup hints in the preferences, and then from `bash` run 
```
export SPYDER_DEBUG="3" 
spyder
```
to enable spyder debug mode and then start it from that command line, you can then watch exactly what's going on under the hood when the behavior happens, which would probably be of some help in fixing it. Also, if you observe the specific call Spyder's making to `jedi` (I presume its `jedi` and not `rope` here...), you can try introspecting it yourself with `jedi` independently and seeing if you get the same results. If so, then its `jedi` business and not really directly controllable by spyder...though Spyder shouldn't crash, of course.

> Good luck to you, and I look forward to seeing a patch (if possible) soon.

Unfortunately, the bad news is that Spyder is currently unfunded at the moment so there is a good chance it'll be a while until it gets fixed, if that, unless you're able to isolate the problem (or someone else has time and steps up). I'm new to the team (and python in general) who's main job is helping manage the issue tracker; and as I currently only have easy access to Windows boxes, there isn't much I can do about it anyway even if I was a pro. However, if someone else isn't able to right away and it isn't a problem with `jedi` or a peculiarity of `pytorch`, you are welcome to submit a PR if you figure out what's going on.
> enable spyder debug mode and then start it from that command line, you can then watch exactly what's going on under the hood when the behavior happens, which would probably be of some help in fixing it.

Tried this today. I execute the code above, no errors; terminal shows bunch of outputs that show everything is okay (not posting here, bc. very long; both `jedi` and `rope` initialize as expected without errors). Program ends, and I click to new empty line at end of program; Jedi is initialized without problems (first line in terminal output below). I then type `net(` and program crashes. Below, find rest of terminal output. Seems like problem is with `rope` or `torch.nn.modules.module.Module modul`

```
Initialized jedi
info request
info request from rope finished: "{'calltip': u'__call__(*input, **kwargs)', 'note': u'Present in torch.nn.modules.module.Module modul" in 0.2 sec
Segmentation fault (core dumped)
```

Let me know if there are others things you'd like me to explore.


Thanks for the debug info. We'll take a look at this in our next release, sorry for the inconvenience in the meantime.
Really appreciate your assistance in helping solve this, @ysumath1. You've been very helpful in getting it fixed for everyone!
@ysumath1   I have Ubuntu 16.04 installed and I tried everything I could to recreate the error you're seeing, but I couldn't recreate it.  One difference between your install and mine is that I have Anaconda 3 and not Anaconda 2.  That's the only thing I didn't try to change.

But, if it will help you, here's what I did.
- Update Anaconda 3 to the latest version.
- Create a 2.7 virtual environment.  `conda create -n my-venv python=2.7`
- Go into the venv.  `source activate my-venv`
- Install Spyder and PyTorch (and any other dependencies) with `conda install`.
- Start Spyder with `spyder`.
- Create a new file with the PyTorch tutorial code.  Run it and then type `net(`.

When I do that, it doesn't crash or throw a seg fault.
Sorry about that, no idea how it happened. I blame my `cat`.
@csabella 

Thanks for the suggestion. I will try this on my personal computer this weekend. Other forums (Reddit, GitHub, Google Groups) seem to indicate problems arise with concurrent installations of Anaconda2 and Anaconda3, because they are based on incompatible Python versions; recommended approach is replacement of one with the other. 

I am working on a fairly complex system of Anaconda and independently-developed packages at work that took a while to get set up and play nice with Anaconda2 python 2.7. Probably it is not a good idea for me to replace Anaconda2 with Anaconda3 at work for now. However, if I create python 3.x virtual environment in Anaconda2 and attempt to install Spyder there, can I install the Anaconda3 Spyder version (presumably Spyder version that is built in python 3) that does not throw problems? If so, could I then tell that Spyder version to use the Anaconda2 python 2.7 interpreter and module path that is presently set as my system python path and expect identical behavior to current set-up, with respect to execution, package importation, etc. (basically everything that is not this issue)? Or, would I absolutely need to replace Anaconda2 with Anaconda3? 
@ysumath1   Let's see if it works on your personal computer first before addressing your questions.  I don't know if Anaconda 3 will fix it, but it was the only difference I could find between our setups, so I'm hoping it will.  Recreating an issue is sometimes the hardest part about debugging.   :-)
@csabella Tried your suggestion on my personal computer. Completely removed Anaconda2 (deleted ~/anaconda2 folder + other anaconda looking files + edited $PATH variable) and installed anaconda3 with python 3.6. Into this python 3.6 environment, I installed PyTorch + dependencies, ran my test code in Spyder, no errors :)

Then, I created a new python 2.7 virtual environment with `conda create -n py2Env python=2.7`. Here, I installed Spyder and PyTorch. Curiously the previous settings (color scheme + disabled balloon tips) from my OLD anaconda2 installation of Spyder were ported to this instance of Spyder. I was _very_ surprised, as I was under the impression I had completely removed all of anaconda2... It is my belief that this would have removed Spyder as well.. Are Spyder settings somewhere else on my computer where Spyder magically knows to look, and could that have muddled with the Spyder installation in the python2.7 virtual environment? Or, are Spyder settings stored on AnacondaCloud somehow? I don't think I have an account with them and moreover, I'm not even signed into AnacondaCloud in this new instance of Anaconda-Navigator...

![image](https://user-images.githubusercontent.com/24642735/34456152-cbda5a0c-ed5d-11e7-863e-1e6243f7e071.png)

In any event, I execute the YSTest.py code above, and that runs as expected. I type `net(` (after re-enabling the balloon tips option in editor) and that works (I get net() and some balloon tips). To make sure it's not a fluke, I create a new line and type 'net(` and Spyder crashes..... 
Not sure if this helps any, but in both work machine and personal computer, I see this error message in terminal immediately upon launching Spyder (I use `anaconda-navigator &` then launch Spyder from the Anaconda Navigator GUI window).

`~$ /home/yohan/.anaconda/navigator/scripts/py2Env/spyder.sh: line 2: /home/yohan/anaconda3/envs/py2Env/bin/activate: No such file or directory`
> Are Spyder settings somewhere else on my computer where Spyder magically knows to look

Well it isn't quite magic, but they are just in your user directory `~`, under a hidden directory (`.spyder-py3` on my Windows machine).

> I see this error message in terminal immediately upon launching Spyder 

That error is with your Anaconda navigator install; it can find the executable/script it needs to active the correct environment. Nothing to do with Spyder, so nothing we can do about that part.
>  I type net( (after re-enabling the balloon tips option in editor) and that works (I get net() and some 
>  balloon tips). To make sure it's not a fluke, I create a new line and type 'net(` and Spyder crashes..... 

@ysumath1  I can now recreate it by typing `net(` twice!  Thank you for your diligence in helping us work on this.  Next step for me is to now find the root cause.  :-)

Resolving patch:
diff --git a/spyder/utils/introspection/manager.py b/spyder/utils/introspection/manager.py
--- a/spyder/utils/introspection/manager.py
+++ b/spyder/utils/introspection/manager.py
@@ -321,7 +321,7 @@ def _handle_info_result(self, resp, info, prev_info):
         if resp['name']:
             self.send_to_help.emit(
                 resp['name'], resp['argspec'],
-                resp['note'], resp['docstring'],
+                resp['note'], resp['docstring'] or resp['name'],
                 not prev_info.auto)
 
     def _handle_definition_result(self, resp, info, prev_info):


Test patch: