diff --git a/Cython/Compiler/Code.py b/Cython/Compiler/Code.py
index 48fbbd01156736d85d0f8e4b62596525e0205102..2a426fe35a0969b1646c1e853522f5b2f71acc3f 100644
--- a/Cython/Compiler/Code.py
+++ b/Cython/Compiler/Code.py
@@ -236,7 +236,8 @@ class GlobalState(object):
         'before_global_var',
         'global_var',
         'all_the_rest',
-        'utility_code_def'
+        'utility_code_def',
+        'end'
     ]
     
 
diff --git a/Cython/Compiler/ModuleNode.py b/Cython/Compiler/ModuleNode.py
index 32be4c715e26ea1f2c0345ffdc7960d0f949cf10..63b72a3a2b88c2a76d0b51a2fdc0550d38fecca9 100644
--- a/Cython/Compiler/ModuleNode.py
+++ b/Cython/Compiler/ModuleNode.py
@@ -425,7 +425,8 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
             code.putln('#include "%s"' % filename)
         code.putln("#ifndef Py_PYTHON_H")
         code.putln("    #error Python headers needed to compile C extensions, please install development version of Python.")
-        code.putln("#endif")
+        code.putln("#else")
+        code.globalstate["end"].putln("#endif /* Py_PYTHON_H */")
         code.putln("#ifndef PY_LONG_LONG")
         code.putln("  #define PY_LONG_LONG LONG_LONG")
         code.putln("#endif")