@@ -360,17 +360,42 @@ public sealed class PythonGenerator : IEnumerator, IEnumerator<object>, ICodeFor
             }
         }
 
+        private FunctionStack? fnStack = null;
+
+        private void SaveFunctionStack(bool done) {
+            if (!Context.LanguageContext.PythonOptions.Frames) return;
+            if (!done) {
+                var stack = PythonOps.GetFunctionStack();
+                fnStack = stack[stack.Count - 1];
+                stack.RemoveAt(stack.Count - 1);
+            }
+            else {
+                fnStack = null;
+            }
+        }
+
+        private void RestoreFunctionStack() {
+            if (!Context.LanguageContext.PythonOptions.Frames) return;
+            if (fnStack != null) {
+                List<FunctionStack> stack = PythonOps.GetFunctionStack();
+                stack.Add(fnStack.Value);
+            }
+        }
+
         /// <summary>
         /// Core implementation of IEnumerator.MoveNext()
         /// </summary>
         private bool MoveNextWorker() {
             bool ret = false;
             try {
+                RestoreFunctionStack();
                 try {
-                    _next(_data);
-                    ret = State != GeneratorRewriter.Finished;
+                    ret = GetNext();
                 } finally {
                     Active = false;
+
+                    SaveFunctionStack(!ret);
+
                     if (!ret) {
                         Close();
                     }
@@ -392,6 +417,8 @@ public sealed class PythonGenerator : IEnumerator, IEnumerator<object>, ICodeFor
             // includes exception handling blocks.
             Exception save = SaveCurrentException();
 
+            RestoreFunctionStack();
+
             bool ret = false;
             try {
                 // This calls into the delegate that has the real body of the generator.
@@ -410,6 +437,8 @@ public sealed class PythonGenerator : IEnumerator, IEnumerator<object>, ICodeFor
                 RestoreCurrentException(save);
                 Active = false;
 
+                SaveFunctionStack(!ret);
+
                 // If _next() returned false, or did not return (thus leavintg ret assigned to its initial value of false), then 
                 // the body of the generator has exited and the generator is now closed.
                 if (!ret) {