@@ -178,10 +178,11 @@ impl Compiler {
178178 posonlyarg_count : usize ,
179179 arg_count : usize ,
180180 kwonlyarg_count : usize ,
181- source_path : String ,
182- first_line_number : usize ,
183181 obj_name : String ,
184182 ) {
183+ let source_path = self . source_path . clone ( ) ;
184+ let first_line_number = self . get_source_line_number ( ) ;
185+
185186 let table = self
186187 . symbol_table_stack
187188 . last_mut ( )
@@ -824,14 +825,11 @@ impl Compiler {
824825 funcflags |= bytecode:: MakeFunctionFlags :: KW_ONLY_DEFAULTS ;
825826 }
826827
827- let line_number = self . get_source_line_number ( ) ;
828828 self . push_output (
829829 bytecode:: CodeFlags :: NEW_LOCALS | bytecode:: CodeFlags :: IS_OPTIMIZED ,
830830 args. posonlyargs_count ,
831831 args. args . len ( ) ,
832832 args. kwonlyargs . len ( ) ,
833- self . source_path . clone ( ) ,
834- line_number,
835833 name. to_owned ( ) ,
836834 ) ;
837835
@@ -900,8 +898,9 @@ impl Compiler {
900898 // except handlers:
901899 self . switch_to_block ( handler_block) ;
902900 // Exception is on top of stack now
903- let mut next_handler = self . new_block ( ) ;
904901 for handler in handlers {
902+ let next_handler = self . new_block ( ) ;
903+
905904 // If we gave a typ,
906905 // check if this handler can handle the exception:
907906 if let Some ( exc_type) = & handler. typ {
@@ -937,7 +936,7 @@ impl Compiler {
937936 self . emit ( Instruction :: PopException ) ;
938937
939938 if finalbody. is_some ( ) {
940- self . emit ( Instruction :: PopBlock ) ; // pop finally block
939+ self . emit ( Instruction :: PopBlock ) ; // pop excepthandler block
941940 // We enter the finally block, without exception.
942941 self . emit ( Instruction :: EnterFinally ) ;
943942 }
@@ -948,13 +947,8 @@ impl Compiler {
948947
949948 // Emit a new label for the next handler
950949 self . switch_to_block ( next_handler) ;
951- next_handler = self . new_block ( ) ;
952950 }
953951
954- self . emit ( Instruction :: Jump {
955- target : next_handler,
956- } ) ;
957- self . switch_to_block ( next_handler) ;
958952 // If code flows here, we have an unhandled exception,
959953 // raise the exception again!
960954 self . emit ( Instruction :: Raise {
@@ -971,7 +965,7 @@ impl Compiler {
971965 if finalbody. is_some ( ) {
972966 self . emit ( Instruction :: PopBlock ) ; // pop finally block
973967
974- // We enter the finally block, without return / exception.
968+ // We enter the finallyhandler block, without return / exception.
975969 self . emit ( Instruction :: EnterFinally ) ;
976970 }
977971
@@ -998,6 +992,9 @@ impl Compiler {
998992
999993 self . prepare_decorators ( decorator_list) ?;
1000994 let mut funcflags = self . enter_function ( name, args) ?;
995+ self . current_codeinfo ( )
996+ . flags
997+ . set ( bytecode:: CodeFlags :: IS_COROUTINE , is_async) ;
1001998
1002999 // remember to restore self.ctx.in_loop to the original after the function is compiled
10031000 let prev_ctx = self . ctx ;
@@ -1031,7 +1028,7 @@ impl Compiler {
10311028 }
10321029 }
10331030
1034- let mut code = self . pop_code_object ( ) ;
1031+ let code = self . pop_code_object ( ) ;
10351032 self . current_qualified_path = old_qualified_path;
10361033 self . ctx = prev_ctx;
10371034
@@ -1081,10 +1078,6 @@ impl Compiler {
10811078 } ) ;
10821079 }
10831080
1084- if is_async {
1085- code. flags |= bytecode:: CodeFlags :: IS_COROUTINE ;
1086- }
1087-
10881081 if self . build_closure ( & code) {
10891082 funcflags |= bytecode:: MakeFunctionFlags :: CLOSURE ;
10901083 }
@@ -1221,16 +1214,7 @@ impl Compiler {
12211214 self . current_qualified_path = Some ( qualified_name. clone ( ) ) ;
12221215
12231216 self . emit ( Instruction :: LoadBuildClass ) ;
1224- let line_number = self . get_source_line_number ( ) ;
1225- self . push_output (
1226- bytecode:: CodeFlags :: empty ( ) ,
1227- 0 ,
1228- 0 ,
1229- 0 ,
1230- self . source_path . clone ( ) ,
1231- line_number,
1232- name. to_owned ( ) ,
1233- ) ;
1217+ self . push_output ( bytecode:: CodeFlags :: empty ( ) , 0 , 0 , 0 , name. to_owned ( ) ) ;
12341218
12351219 let ( new_body, doc_str) = get_doc ( body) ;
12361220
@@ -2175,22 +2159,24 @@ impl Compiler {
21752159 }
21762160 . to_owned ( ) ;
21772161
2178- let line_number = self . get_source_line_number ( ) ;
21792162 // Create magnificent function <listcomp>:
21802163 self . push_output (
21812164 bytecode:: CodeFlags :: NEW_LOCALS | bytecode:: CodeFlags :: IS_OPTIMIZED ,
21822165 1 ,
21832166 1 ,
21842167 0 ,
2185- self . source_path . clone ( ) ,
2186- line_number,
21872168 name. clone ( ) ,
21882169 ) ;
21892170 let arg0 = self . varname ( ".0" ) ;
21902171
2172+ let is_genexpr = matches ! ( kind, ast:: ComprehensionKind :: GeneratorExpression { .. } ) ;
2173+
21912174 // Create empty object of proper type:
21922175 match kind {
2193- ast:: ComprehensionKind :: GeneratorExpression { .. } => { }
2176+ ast:: ComprehensionKind :: GeneratorExpression { .. } => {
2177+ // pop the None that was passed to kickstart the generator
2178+ self . emit ( Instruction :: Pop ) ;
2179+ }
21942180 ast:: ComprehensionKind :: List { .. } => {
21952181 self . emit ( Instruction :: BuildList {
21962182 size : 0 ,
@@ -2300,6 +2286,10 @@ impl Compiler {
23002286 self . emit ( Instruction :: PopBlock ) ;
23012287 }
23022288
2289+ if is_genexpr {
2290+ self . emit_constant ( ConstantData :: None )
2291+ }
2292+
23032293 // Return freshly filled list:
23042294 self . emit ( Instruction :: ReturnValue ) ;
23052295
0 commit comments