@@ -508,7 +508,7 @@ def extract_python(
508
508
:rtype: ``iterator``
509
509
"""
510
510
funcname = lineno = message_lineno = None
511
- call_stack = - 1
511
+ call_stack = [] # line numbers of calls
512
512
buf = []
513
513
messages = []
514
514
translator_comments = []
@@ -526,7 +526,7 @@ def extract_python(
526
526
current_fstring_start = None
527
527
528
528
for tok , value , (lineno , _ ), _ , _ in tokens :
529
- if call_stack == - 1 and tok == NAME and value in ('def' , 'class' ):
529
+ if not call_stack and tok == NAME and value in ('def' , 'class' ):
530
530
in_def = True
531
531
elif tok == OP and value == '(' :
532
532
if in_def :
@@ -535,12 +535,12 @@ def extract_python(
535
535
in_def = False
536
536
continue
537
537
if funcname :
538
- call_stack += 1
538
+ call_stack . append ( lineno )
539
539
elif in_def and tok == OP and value == ':' :
540
540
# End of a class definition without parens
541
541
in_def = False
542
542
continue
543
- elif call_stack == - 1 and tok == COMMENT :
543
+ elif not call_stack and tok == COMMENT :
544
544
# Strip the comment token from the line
545
545
value = value [1 :].strip ()
546
546
if in_translator_comments and \
@@ -555,7 +555,7 @@ def extract_python(
555
555
in_translator_comments = True
556
556
translator_comments .append ((lineno , value ))
557
557
break
558
- elif funcname and call_stack == 0 :
558
+ elif funcname and len ( call_stack ) == 1 :
559
559
nested = (tok == NAME and value in keywords )
560
560
if (tok == OP and value == ')' ) or nested :
561
561
if buf :
@@ -565,17 +565,20 @@ def extract_python(
565
565
messages .append (None )
566
566
567
567
messages = tuple (messages ) if len (messages ) > 1 else messages [0 ]
568
- # Comments don't apply unless they immediately
569
- # precede the message
570
- if translator_comments and \
571
- translator_comments [- 1 ][0 ] < message_lineno - 1 :
572
- translator_comments = []
568
+
569
+ if translator_comments :
570
+ last_comment_lineno = translator_comments [- 1 ][0 ]
571
+ if last_comment_lineno < min (message_lineno , call_stack [- 1 ]) - 1 :
572
+ # Comments don't apply unless they immediately
573
+ # precede the message, or the line where the parenthesis token
574
+ # to start this message's translation call is.
575
+ translator_comments .clear ()
573
576
574
577
yield (message_lineno , funcname , messages ,
575
578
[comment [1 ] for comment in translator_comments ])
576
579
577
580
funcname = lineno = message_lineno = None
578
- call_stack = - 1
581
+ call_stack . clear ()
579
582
messages = []
580
583
translator_comments = []
581
584
in_translator_comments = False
@@ -619,9 +622,9 @@ def extract_python(
619
622
620
623
elif tok != NL and not message_lineno :
621
624
message_lineno = lineno
622
- elif call_stack > 0 and tok == OP and value == ')' :
623
- call_stack -= 1
624
- elif funcname and call_stack == - 1 :
625
+ elif len ( call_stack ) > 1 and tok == OP and value == ')' :
626
+ call_stack . pop ()
627
+ elif funcname and not call_stack :
625
628
funcname = None
626
629
elif tok == NAME and value in keywords :
627
630
funcname = value
0 commit comments