diff --git a/pytools/py_codegen.py b/pytools/py_codegen.py
index 54fae76c618ad4dd25b640a4f632580e4f2eb998..25c60b48b1ba71670681d53a6bbfeee4072787aa 100644
--- a/pytools/py_codegen.py
+++ b/pytools/py_codegen.py
@@ -82,6 +82,9 @@ class PythonFunctionGenerator(PythonCodeGenerator):
 
     def get_function(self):
         result_dict = {}
-        exec(compile(self.get(), "<generated function %s>" % self.name, "exec"),
+        source_text = self.get()
+        exec(compile(source_text, "<generated function %s>" % self.name, "exec"),
                 result_dict)
-        return result_dict[self.name]
+        func = result_dict[self.name]
+        result_dict["_MODULE_SOURCE_CODE"] = source_text
+        return func