@@ -87,7 +87,7 @@ def assert_POT_equal(self, expected, actual):
8787 self .maxDiff = None
8888 self .assertEqual (normalize_POT_file (expected ), normalize_POT_file (actual ))
8989
90- def extract_from_str (self , module_content , * , args = (), strict = True ):
90+ def extract_from_str (self , module_content , * , args = (), strict = True , with_stderr = False ):
9191 """Return all msgids extracted from module_content."""
9292 filename = 'test.py'
9393 with temp_cwd (None ):
@@ -98,12 +98,18 @@ def extract_from_str(self, module_content, *, args=(), strict=True):
9898 self .assertEqual (res .err , b'' )
9999 with open ('messages.pot' , encoding = 'utf-8' ) as fp :
100100 data = fp .read ()
101- return self .get_msgids (data )
101+ msgids = self .get_msgids (data )
102+ if not with_stderr :
103+ return msgids
104+ return msgids , res .err
102105
103106 def extract_docstrings_from_str (self , module_content ):
104107 """Return all docstrings extracted from module_content."""
105108 return self .extract_from_str (module_content , args = ('--docstrings' ,), strict = False )
106109
110+ def get_stderr (self , module_content ):
111+ return self .extract_from_str (module_content , strict = False , with_stderr = True )[1 ]
112+
107113 def test_header (self ):
108114 """Make sure the required fields are in the header, according to:
109115 http://www.gnu.org/software/gettext/manual/gettext.html#Header-Entry
@@ -407,6 +413,24 @@ def test_files_list(self):
407413 self .assertIn (f'msgid "{ text2 } "' , data )
408414 self .assertNotIn (text3 , data )
409415
416+ def test_error_messages (self ):
417+ """Test that pygettext outputs error messages to stderr."""
418+ stderr = self .get_stderr (dedent ('''\
419+ _(1+2)
420+ ngettext('foo')
421+ dgettext(*args, 'foo')
422+ ''' ))
423+
424+ # Normalize line endings on Windows
425+ stderr = stderr .decode ('utf-8' ).replace ('\r ' , '' )
426+
427+ self .assertEqual (
428+ stderr ,
429+ "*** test.py:1: Expected a string constant for argument 1, got 1 + 2\n "
430+ "*** test.py:2: Expected at least 2 positional argument(s) in gettext call, got 1\n "
431+ "*** test.py:3: Variable positional arguments are not allowed in gettext calls\n "
432+ )
433+
410434
411435def update_POT_snapshots ():
412436 for input_file in DATA_DIR .glob ('*.py' ):
0 commit comments