Repository: ipython/ipython

Issue:
quit() doesn't work in terminal
```
[bfroehle@highorder]$ ipython
Python 2.7.2+ (default, Oct  4 2011, 20:06:09) 
Type "copyright", "credits" or "license" for more information.

IPython 0.12.beta -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: quit()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/home/bfroehle/<ipython-input-1-369dbed58733> in <module>()
----> 1 quit(())

TypeError: __call__() takes exactly 1 argument (2 given)
```



Discussion:
`git bisect` places the regression in commit 17afbbef.

This looks like an issue with how we handle doing autocalls. I'll look into it.

Another oddity... why does `/quit` turn into `quit(())` but `/f` only turns into `f()`?

```
In [1]: /quit
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/home/bfroehle/<ipython-input-1-369dbed58733> in <module>()
----> 1 quit(())

TypeError: __call__() takes exactly 1 argument (2 given)

```

Again, I guess it's to do with the fact that `quit` is an IPyAutocall object, which forces it to become a function call separately from the normal autocall mechanisms.


Resolving patch:
diff --git a/IPython/core/prefilter.py b/IPython/core/prefilter.py
--- a/IPython/core/prefilter.py
+++ b/IPython/core/prefilter.py
@@ -826,29 +826,33 @@ def handle(self, line_info):
         elif esc == ESC_PAREN:
             newcmd = '%s(%s)' % (ifun,",".join(the_rest.split()))
         else:
-            # Auto-paren.
-            # We only apply it to argument-less calls if the autocall
-            # parameter is set to 2.  We only need to check that autocall is <
-            # 2, since this function isn't called unless it's at least 1.
-            if not the_rest and (self.shell.autocall < 2) and not force_auto:
-                newcmd = '%s %s' % (ifun,the_rest)
-                auto_rewrite = False
+            # Auto-paren.       
+            if force_auto:
+                # Don't rewrite if it is already a call.
+                do_rewrite = not the_rest.startswith('(')
             else:
-                if not force_auto and the_rest.startswith('['):
-                    if hasattr(obj,'__getitem__'):
-                        # Don't autocall in this case: item access for an object
-                        # which is BOTH callable and implements __getitem__.
-                        newcmd = '%s %s' % (ifun,the_rest)
-                        auto_rewrite = False
-                    else:
-                        # if the object doesn't support [] access, go ahead and
-                        # autocall
-                        newcmd = '%s(%s)' % (ifun.rstrip(),the_rest)
-                elif the_rest.endswith(';'):
-                    newcmd = '%s(%s);' % (ifun.rstrip(),the_rest[:-1])
+                if not the_rest:
+                    # We only apply it to argument-less calls if the autocall
+                    # parameter is set to 2.
+                    do_rewrite = (self.shell.autocall >= 2)
+                elif the_rest.startswith('[') and hasattr(obj, '__getitem__'):
+                    # Don't autocall in this case: item access for an object
+                    # which is BOTH callable and implements __getitem__.
+                    do_rewrite = False
                 else:
-                    newcmd = '%s(%s)' % (ifun.rstrip(), the_rest)
+                    do_rewrite = True
 
+            # Figure out the rewritten command
+            if do_rewrite:
+                if the_rest.endswith(';'):
+                    newcmd = '%s(%s);' % (ifun.rstrip(),the_rest[:-1])
+                else:
+                    newcmd = '%s(%s)' % (ifun.rstrip(), the_rest)                
+            else:
+                normal_handler = self.prefilter_manager.get_handler_by_name('normal')
+                return normal_handler.handle(line_info)
+        
+        # Display the rewritten call
         if auto_rewrite:
             self.shell.auto_rewrite_input(newcmd)
 


Test patch:
diff --git a/IPython/core/tests/test_handlers.py b/IPython/core/tests/test_handlers.py
--- a/IPython/core/tests/test_handlers.py
+++ b/IPython/core/tests/test_handlers.py
@@ -132,6 +132,8 @@ def test_handlers():
     run([
         ('len "abc"',       'len "abc"'),
         ('autocallable',    'autocallable()'),
+        # Don't add extra brackets (gh-1117)
+        ('autocallable()',    'autocallable()'),
         (",list 1 2 3",     'list("1", "2", "3")'),
         (";list 1 2 3",     'list("1 2 3")'),
         ("/len range(1,4)", 'len(range(1,4))'),
@@ -148,7 +150,7 @@ def test_handlers():
         ('len [1,2]', 'len([1,2])'),      # len doesn't support __getitem__...
         ('call_idx [1]', 'call_idx [1]'), # call_idx *does*..
         ('call_idx 1', 'call_idx(1)'),
-        ('len', 'len '), # only at 2 does it auto-call on single args
+        ('len', 'len'), # only at 2 does it auto-call on single args
         ])
     ip.magic('autocall 2')
     run([