Repository: ipython/ipython

Issue:
Incorrect global declaration breaks iPython error display
```
Python 2.7.6 |Anaconda 1.9.0 (32-bit)| (default, Nov 11 2013, 10:50:31) [MSC v.1500 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.

IPython 1.1.0 -- 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]: def a():
   ...:     x = 11
   ...:     global x
<input>:3: SyntaxWarning: name 'x' is assigned to before global declaration
<input>:3: SyntaxWarning: name 'x' is assigned to before global declaration
<input>:3: SyntaxWarning: name 'x' is assigned to before global declaration
   ...: **I hit enter again here**
<input>:3: SyntaxWarning: name 'x' is assigned to before global declaration
<input>:3: SyntaxWarning: name 'x' is assigned to before global declaration
<input>:3: SyntaxWarning: name 'x' is assigned to before global declaration
<input>:3: SyntaxWarning: name 'x' is assigned to before global declaration
<input>:3: SyntaxWarning: name 'x' is assigned to before global declaration
<input>:3: SyntaxWarning: name 'x' is assigned to before global declaration
<input>:3: SyntaxWarning: name 'x' is assigned to before global declaration
<input>:3: SyntaxWarning: name 'x' is assigned to before global declaration
<input>:3: SyntaxWarning: name 'x' is assigned to before global declaration
<ipython-input-1-25742883ea0e>:3: SyntaxWarning: name 'x' is assigned to before
global declaration
  global x

In [2]:
```



Discussion:
Wow, I've never seen a SyntaxWarning before. I can reproduce this - it's presumably related to our parsing the code to work out when it's finished.


Resolving patch:
diff --git a/IPython/core/inputsplitter.py b/IPython/core/inputsplitter.py
--- a/IPython/core/inputsplitter.py
+++ b/IPython/core/inputsplitter.py
@@ -20,6 +20,7 @@
 import codeop
 import re
 import sys
+import warnings
 
 from IPython.utils.py3compat import cast_unicode
 from IPython.core.inputtransformer import (leading_indent,
@@ -308,14 +309,16 @@ def push(self, lines):
 
         self._update_indent(lines)
         try:
-            self.code = self._compile(source, symbol="exec")
+            with warnings.catch_warnings():
+                warnings.simplefilter('error', SyntaxWarning)
+                self.code = self._compile(source, symbol="exec")
         # Invalid syntax can produce any of a number of different errors from
         # inside the compiler, so we have to catch them all.  Syntax errors
         # immediately produce a 'ready' block, so the invalid Python can be
         # sent to the kernel for evaluation with possible ipython
         # special-syntax conversion.
         except (SyntaxError, OverflowError, ValueError, TypeError,
-                MemoryError):
+                MemoryError, SyntaxWarning):
             self._is_complete = True
             self._is_invalid = True
         else:


Test patch:
diff --git a/IPython/core/tests/test_inputsplitter.py b/IPython/core/tests/test_inputsplitter.py
--- a/IPython/core/tests/test_inputsplitter.py
+++ b/IPython/core/tests/test_inputsplitter.py
@@ -348,6 +348,7 @@ def test_check_complete(self):
         self.assertEqual(isp.check_complete("for a in range(5):"), ('incomplete', 4))
         self.assertEqual(isp.check_complete("raise = 2"), ('invalid', None))
         self.assertEqual(isp.check_complete("a = [1,\n2,"), ('incomplete', 0))
+        self.assertEqual(isp.check_complete("def a():\n x=1\n global x"), ('invalid', None))
 
 class InteractiveLoopTestCase(unittest.TestCase):
     """Tests for an interactive loop like a python shell.