@@ -102,8 +102,13 @@ def extract_from_str(self, module_content, *, args=(), strict=True,
102102 res = assert_python_ok ('-Xutf8' , self .script , * args , filename )
103103 if strict :
104104 self .assertEqual (res .err , b'' )
105- with open ('messages.pot' , encoding = 'utf-8' ) as fp :
106- data = fp .read ()
105+ if Path ('messages.pot' ).exists ():
106+ with open ('messages.pot' , encoding = 'utf-8' ) as fp :
107+ data = fp .read ()
108+ else :
109+ # No output file is generated when no messages are found
110+ # (gh-133049); there are no msgids to return.
111+ data = ''
107112 if not raw :
108113 data = self .get_msgids (data )
109114 if not with_stderr :
@@ -122,7 +127,8 @@ def test_header(self):
122127 http://www.gnu.org/software/gettext/manual/gettext.html#Header-Entry
123128 """
124129 with temp_cwd (None ) as cwd :
125- assert_python_ok ('-Xutf8' , self .script )
130+ Path ('test.py' ).write_text ("_('hello')\n " , encoding = 'utf-8' )
131+ assert_python_ok ('-Xutf8' , self .script , 'test.py' )
126132 with open ('messages.pot' , encoding = 'utf-8' ) as fp :
127133 data = fp .read ()
128134 header = self .get_header (data )
@@ -149,7 +155,8 @@ def test_POT_Creation_Date(self):
149155 """ Match the date format from xgettext for POT-Creation-Date """
150156 from datetime import datetime
151157 with temp_cwd (None ) as cwd :
152- assert_python_ok ('-Xutf8' , self .script )
158+ Path ('test.py' ).write_text ("_('hello')\n " , encoding = 'utf-8' )
159+ assert_python_ok ('-Xutf8' , self .script , 'test.py' )
153160 with open ('messages.pot' , encoding = 'utf-8' ) as fp :
154161 data = fp .read ()
155162 header = self .get_header (data )
@@ -165,9 +172,10 @@ def test_POT_Creation_Date(self):
165172 def test_output_option (self ):
166173 for opt in ('-o' , '--output=' ):
167174 with temp_cwd ():
168- assert_python_ok (self .script , f'{ opt } test' )
175+ Path ('test.py' ).write_text ("_('hello')\n " , encoding = 'utf-8' )
176+ assert_python_ok (self .script , f'{ opt } test' , 'test.py' )
169177 self .assertTrue (os .path .exists ('test' ))
170- res = assert_python_ok (self .script , f'{ opt } -' )
178+ res = assert_python_ok (self .script , f'{ opt } -' , 'test.py' )
171179 self .assertIn (b'Project-Id-Version: PACKAGE VERSION' , res .out )
172180
173181 def test_funcdocstring (self ):
@@ -386,7 +394,7 @@ async def _(x):
386394 class _(object):
387395 pass
388396 ''' ))
389- self .assertEqual (msgids , ['' ])
397+ self .assertEqual (msgids , [])
390398
391399 def test_pygettext_output (self ):
392400 """Test that the pygettext output exactly matches snapshots."""
@@ -450,6 +458,34 @@ def test_error_messages(self):
450458 "*** test.py:3: Variable positional arguments are not allowed in gettext calls\n "
451459 )
452460
461+ def test_no_messages_no_output (self ):
462+ """No output file is generated when no messages are found (gh-133049)."""
463+ with temp_cwd (None ):
464+ with open ('test.py' , 'w' , encoding = 'utf-8' ) as fp :
465+ fp .write ('x = 1\n ' )
466+ res = assert_python_ok ('-Xutf8' , self .script , 'test.py' )
467+ self .assertEqual (res .out , b'' )
468+ self .assertEqual (res .err , b'' )
469+ self .assertFalse (Path ('messages.pot' ).exists ())
470+
471+ def test_no_messages_no_stdout_output (self ):
472+ """No output is written to stdout when no messages are found (gh-133049)."""
473+ with temp_cwd (None ):
474+ with open ('test.py' , 'w' , encoding = 'utf-8' ) as fp :
475+ fp .write ('x = 1\n ' )
476+ res = assert_python_ok ('-Xutf8' , self .script , '-o' , '-' , 'test.py' )
477+ self .assertEqual (res .out , b'' )
478+ self .assertEqual (res .err , b'' )
479+
480+ def test_no_messages_verbose (self ):
481+ """Verbose mode reports that no output file was generated (gh-133049)."""
482+ with temp_cwd (None ):
483+ with open ('test.py' , 'w' , encoding = 'utf-8' ) as fp :
484+ fp .write ('x = 1\n ' )
485+ res = assert_python_ok ('-Xutf8' , self .script , '-v' , 'test.py' )
486+ self .assertIn (b'no output file generated' , res .err )
487+ self .assertFalse (Path ('messages.pot' ).exists ())
488+
453489 def test_extract_all_comments (self ):
454490 """
455491 Test that the --add-comments option without an
@@ -584,7 +620,7 @@ def test_multiple_keywords_same_funcname_errors(self):
584620 _(42, y)
585621 ''' ), args = ('--keyword=_:1' , '--keyword=_:2' ), with_stderr = True ,
586622 strict = False )
587- self .assertEqual (msgids , ['' ])
623+ self .assertEqual (msgids , [])
588624 # Normalize line endings on Windows
589625 stderr = stderr .decode ('utf-8' ).replace ('\r ' , '' )
590626 self .assertEqual (
0 commit comments