1515 :copyright: (c) 2013-2022 by the Babel Team.
1616 :license: BSD, see LICENSE for more details.
1717"""
18-
18+ import ast
1919import os
2020from os .path import relpath
2121import sys
@@ -487,14 +487,9 @@ def extract_python(fileobj, keywords, comment_tags, options):
487487 if nested :
488488 funcname = value
489489 elif tok == STRING :
490- # Unwrap quotes in a safe manner, maintaining the string's
491- # encoding
492- # https://sourceforge.net/tracker/?func=detail&atid=355470&
493- # aid=617979&group_id=5470
494- code = compile ('# coding=%s\n %s' % (str (encoding ), value ),
495- '<string>' , 'eval' , future_flags )
496- value = eval (code , {'__builtins__' : {}}, {})
497- buf .append (value )
490+ val = _parse_python_string (value , encoding , future_flags )
491+ if val is not None :
492+ buf .append (val )
498493 elif tok == OP and value == ',' :
499494 if buf :
500495 messages .append ('' .join (buf ))
@@ -516,6 +511,29 @@ def extract_python(fileobj, keywords, comment_tags, options):
516511 funcname = value
517512
518513
514+ def _parse_python_string (value , encoding , future_flags ):
515+ # Unwrap quotes in a safe manner, maintaining the string's encoding
516+ # https://sourceforge.net/tracker/?func=detail&atid=355470&aid=617979&group_id=5470
517+ code = compile (
518+ f'# coding={ str (encoding )} \n { value } ' ,
519+ '<string>' ,
520+ 'eval' ,
521+ ast .PyCF_ONLY_AST | future_flags ,
522+ )
523+ if not isinstance (code , ast .Expression ):
524+ return None
525+ body = code .body
526+ if isinstance (body , ast .Str ):
527+ return body .s
528+ if isinstance (body , ast .JoinedStr ): # f-string
529+ if all (isinstance (node , ast .Str ) for node in body .values ):
530+ return '' .join (node .s for node in body .values )
531+ if all (isinstance (node , ast .Constant ) for node in body .values ):
532+ return '' .join (str (node .value ) for node in body .values )
533+ # TODO: could raise an error or warning when not all nodes are constants
534+ return None
535+
536+
519537def extract_javascript (fileobj , keywords , comment_tags , options ):
520538 """Extract messages from JavaScript source code.
521539
0 commit comments