]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
xgettext: Python: Improve heuristic for format strings.
authorBruno Haible <bruno@clisp.org>
Sun, 1 Jun 2025 23:26:23 +0000 (01:26 +0200)
committerBruno Haible <bruno@clisp.org>
Mon, 2 Jun 2025 00:22:53 +0000 (02:22 +0200)
* gettext-tools/src/format-python.c (struct spec): Add field
'likely_intentional_directives'.
(format_parse): Set it to the number of directives that don't contain a space.
(format_is_unlikely_intentional): New function.
(formatstring_python): Use it.
* gettext-tools/tests/format-python-3: New file.
* gettext-tools/tests/Makefile.am (TESTS): Add it.

gettext-tools/src/format-python.c
gettext-tools/tests/Makefile.am
gettext-tools/tests/format-python-3 [new file with mode: 0755]

index 9359abc3c384d5bbd06e194990871212c51998c3..ba2f6a0195953e3fff818a4256862c8bcdea4a47 100644 (file)
@@ -90,6 +90,11 @@ struct unnamed_arg
 struct spec
 {
   unsigned int directives;
+  /* We consider a directive as "likely intentional" if it does not contain a
+     space.  This prevents xgettext from flagging strings like "100% complete"
+     as 'python-format' if they don't occur in a context that requires a format
+     string.  */
+  unsigned int likely_intentional_directives;
   unsigned int named_arg_count;
   unsigned int unnamed_arg_count;
   struct named_arg *named;
@@ -117,6 +122,7 @@ format_parse (const char *format, bool translated, char *fdi,
   struct spec *result;
 
   spec.directives = 0;
+  spec.likely_intentional_directives = 0;
   spec.named_arg_count = 0;
   spec.unnamed_arg_count = 0;
   spec.named = NULL;
@@ -130,6 +136,7 @@ format_parse (const char *format, bool translated, char *fdi,
         char *name = NULL;
         bool zero_precision = false;
         enum format_arg_type type;
+        bool likely_intentional = true;
 
         FDI_SET (format - 1, FMTDIR_START);
         spec.directives++;
@@ -171,7 +178,11 @@ format_parse (const char *format, bool translated, char *fdi,
 
         while (*format == '-' || *format == '+' || *format == ' '
                || *format == '#' || *format == '0')
-          format++;
+          {
+            if (*format == ' ')
+              likely_intentional = false;
+            format++;
+          }
 
         if (*format == '*')
           {
@@ -312,6 +323,8 @@ format_parse (const char *format, bool translated, char *fdi,
             spec.unnamed_arg_count++;
           }
 
+        if (likely_intentional)
+          spec.likely_intentional_directives++;
         FDI_SET (format, FMTDIR_END);
 
         format++;
@@ -409,6 +422,14 @@ format_get_number_of_directives (void *descr)
   return spec->directives;
 }
 
+static bool
+format_is_unlikely_intentional (void *descr)
+{
+  struct spec *spec = (struct spec *) descr;
+
+  return spec->likely_intentional_directives == 0;
+}
+
 static bool
 format_check (void *msgid_descr, void *msgstr_descr, bool equality,
               formatstring_error_logger_t error_logger, void *error_logger_data,
@@ -541,7 +562,7 @@ struct formatstring_parser formatstring_python =
   format_parse,
   format_free,
   format_get_number_of_directives,
-  NULL,
+  format_is_unlikely_intentional,
   format_check
 };
 
index edf08b48957da13c2a1f8bb2125b7b8fda1e3dfb..43c0521c9881e48dea30884e3e5749a1b53589a2 100644 (file)
@@ -215,7 +215,7 @@ TESTS = gettext-1 gettext-2 \
        format-lua-1 format-lua-2 \
        format-modula2-1 format-modula2-2 \
        format-php-1 format-php-2 format-php-3 \
-       format-python-1 format-python-2 \
+       format-python-1 format-python-2 format-python-3 \
        format-python-brace-1 format-python-brace-2 \
        format-pascal-1 format-pascal-2 \
        format-perl-1 format-perl-2 format-perl-3 \
diff --git a/gettext-tools/tests/format-python-3 b/gettext-tools/tests/format-python-3
new file mode 100755 (executable)
index 0000000..4daaa87
--- /dev/null
@@ -0,0 +1,48 @@
+#! /bin/sh
+. "${srcdir=.}/init.sh"; path_prepend_ . ../src
+
+# Test heuristic recognition of Python format strings like "100% complete".
+
+cat <<\EOF > f-p-3.py
+gettext("Test 1a is 100% complete");
+apply(gettext("Test 1b is 100% complete"), 120);
+gettext("Test 2a from 0% complete to 100% complete");
+apply(gettext("Test 2b from 0% complete to 100% complete"), 120, 121);
+gettext("Test 3a of %s is 100% complete");
+apply(gettext("Test 3b of %s is 100% complete"), "foo", 120);
+EOF
+
+: ${XGETTEXT=xgettext}
+${XGETTEXT} --flag=apply:1:python-format \
+            --omit-header --no-location -d f-p-3.tmp f-p-3.py || Exit 1
+LC_ALL=C tr -d '\r' < f-p-3.tmp.po > f-p-3.po || Exit 1
+
+cat <<\EOF > f-p-3.ok
+msgid "Test 1a is 100% complete"
+msgstr ""
+
+#, python-format
+msgid "Test 1b is 100% complete"
+msgstr ""
+
+msgid "Test 2a from 0% complete to 100% complete"
+msgstr ""
+
+#, python-format
+msgid "Test 2b from 0% complete to 100% complete"
+msgstr ""
+
+#, python-format
+msgid "Test 3a of %s is 100% complete"
+msgstr ""
+
+#, python-format
+msgid "Test 3b of %s is 100% complete"
+msgstr ""
+EOF
+
+: ${DIFF=diff}
+${DIFF} f-p-3.ok f-p-3.po
+result=$?
+
+exit $result