Test PRI* macros even when we can't test NLS translation.
authorTom Lane <[email protected]>
Tue, 16 Dec 2025 17:01:46 +0000 (12:01 -0500)
committerTom Lane <[email protected]>
Tue, 16 Dec 2025 17:01:46 +0000 (12:01 -0500)
Further research shows that the reason commit 7db6809ce failed
is that recent glibc versions short-circuit translation attempts
when LC_MESSAGES is 'C.<encoding>', not only when it's 'C'.
There seems no way around that, so we'll have to live with only
testing NLS when a suitable real locale is installed.

However, something can still be salvaged: it still seems like a
good idea to verify that the PRI* macros work as-expected even when
we can't check their translations (see f8715ec86 for motivation).
Hence, adjust the test to always run the ereport calls, and tweak
the parameter values in hopes of detecting any cases where there's
confusion about the actual widths of the parameters.

Discussion: https://postgr.es/m/1991599.1765818338@sss.pgh.pa.us

src/test/regress/expected/nls.out
src/test/regress/expected/nls_1.out
src/test/regress/expected/nls_2.out
src/test/regress/regress.c
src/test/regress/sql/nls.sql

index 2bc795fc822049b9ec1ace15f53cf805479bbffb..465d8c7d0bed362c327b41975c44803ccdea1995 100644 (file)
@@ -28,27 +28,22 @@ begin
   raise log 'NLS regression test: lc_messages = %',
     current_setting('lc_messages');
 end $$;
-SELECT current_setting('lc_messages') = 'C' AS failed \gset
-\if :failed
-\echo Could not find an acceptable spelling of es_ES locale
-\quit
-\endif
 SELECT test_translation();
 NOTICE:  traducido PRId64 = 424242424242
 NOTICE:  traducido PRId32 = -1234
-NOTICE:  traducido PRIdMAX = -5678
-NOTICE:  traducido PRIdPTR = 9999
+NOTICE:  traducido PRIdMAX = -123456789012
+NOTICE:  traducido PRIdPTR = -9999
 NOTICE:  traducido PRIu64 = 424242424242
-NOTICE:  traducido PRIu32 = 1234
-NOTICE:  traducido PRIuMAX = 5678
+NOTICE:  traducido PRIu32 = 4294966062
+NOTICE:  traducido PRIuMAX = 123456789012
 NOTICE:  traducido PRIuPTR = 9999
 NOTICE:  traducido PRIx64 = 62c6d1a9b2
-NOTICE:  traducido PRIx32 = 4d2
-NOTICE:  traducido PRIxMAX = 162e
+NOTICE:  traducido PRIx32 = fffffb2e
+NOTICE:  traducido PRIxMAX = 1cbe991a14
 NOTICE:  traducido PRIxPTR = 270f
 NOTICE:  traducido PRIX64 = 62C6D1A9B2
-NOTICE:  traducido PRIX32 = 4D2
-NOTICE:  traducido PRIXMAX = 162E
+NOTICE:  traducido PRIX32 = FFFFFB2E
+NOTICE:  traducido PRIXMAX = 1CBE991A14
 NOTICE:  traducido PRIXPTR = 270F
  test_translation 
 ------------------
index 3117fa21ae02d77632242e2d8af1262ce11b8037..1498aa62111a8532723feb17a2507741aae5c3b8 100644 (file)
@@ -28,13 +28,24 @@ begin
   raise log 'NLS regression test: lc_messages = %',
     current_setting('lc_messages');
 end $$;
-SELECT current_setting('lc_messages') = 'C' AS failed \gset
-\if :failed
-\echo Could not find an acceptable spelling of es_ES locale
-\quit
-\endif
 SELECT test_translation();
 NOTICE:  NLS is not enabled
+NOTICE:  translated PRId64 = 424242424242
+NOTICE:  translated PRId32 = -1234
+NOTICE:  translated PRIdMAX = -123456789012
+NOTICE:  translated PRIdPTR = -9999
+NOTICE:  translated PRIu64 = 424242424242
+NOTICE:  translated PRIu32 = 4294966062
+NOTICE:  translated PRIuMAX = 123456789012
+NOTICE:  translated PRIuPTR = 9999
+NOTICE:  translated PRIx64 = 62c6d1a9b2
+NOTICE:  translated PRIx32 = fffffb2e
+NOTICE:  translated PRIxMAX = 1cbe991a14
+NOTICE:  translated PRIxPTR = 270f
+NOTICE:  translated PRIX64 = 62C6D1A9B2
+NOTICE:  translated PRIX32 = FFFFFB2E
+NOTICE:  translated PRIXMAX = 1CBE991A14
+NOTICE:  translated PRIXPTR = 270F
  test_translation 
 ------------------
  
index cb8e4b59d1673680bf32cc4cb768bb78ef7a3697..54852a39925ce55c62edeedcdead313359488191 100644 (file)
@@ -28,8 +28,27 @@ begin
   raise log 'NLS regression test: lc_messages = %',
     current_setting('lc_messages');
 end $$;
-SELECT current_setting('lc_messages') = 'C' AS failed \gset
-\if :failed
-\echo Could not find an acceptable spelling of es_ES locale
-Could not find an acceptable spelling of es_ES locale
-\quit
+SELECT test_translation();
+NOTICE:  lc_messages is 'C'
+NOTICE:  translated PRId64 = 424242424242
+NOTICE:  translated PRId32 = -1234
+NOTICE:  translated PRIdMAX = -123456789012
+NOTICE:  translated PRIdPTR = -9999
+NOTICE:  translated PRIu64 = 424242424242
+NOTICE:  translated PRIu32 = 4294966062
+NOTICE:  translated PRIuMAX = 123456789012
+NOTICE:  translated PRIuPTR = 9999
+NOTICE:  translated PRIx64 = 62c6d1a9b2
+NOTICE:  translated PRIx32 = fffffb2e
+NOTICE:  translated PRIxMAX = 1cbe991a14
+NOTICE:  translated PRIxPTR = 270f
+NOTICE:  translated PRIX64 = 62C6D1A9B2
+NOTICE:  translated PRIX32 = FFFFFB2E
+NOTICE:  translated PRIXMAX = 1CBE991A14
+NOTICE:  translated PRIXPTR = 270F
+ test_translation 
+------------------
+(1 row)
+
+RESET lc_messages;
index 26ae0a6c78785d48aa5ca7e6dbbd47e48e71ef88..acac34d40b9e07b5cc3473887aae0b00f8afd446 100644 (file)
@@ -1156,6 +1156,10 @@ test_relpath(PG_FUNCTION_ARGS)
 
 /*
  * Simple test to verify NLS support, particularly that the PRI* macros work.
+ *
+ * A secondary objective is to verify that <inttypes.h>'s values for the
+ * PRI* macros match what our snprintf.c code will do.  Therefore, we run
+ * the ereport() calls even when we know that translation will not happen.
  */
 PG_FUNCTION_INFO_V1(test_translation);
 Datum
@@ -1185,44 +1189,52 @@ test_translation(PG_FUNCTION_ARGS)
        inited = true;
    }
 
+   /*
+    * If nls.sql failed to select a non-C locale, no translation will happen.
+    * Report that so that we can distinguish this outcome from brokenness.
+    * (We do this here, not in nls.sql, so as to need only 3 expected files.)
+    */
+   if (strcmp(GetConfigOption("lc_messages", false, false), "C") == 0)
+       elog(NOTICE, "lc_messages is 'C'");
+#else
+   elog(NOTICE, "NLS is not enabled");
+#endif
+
    ereport(NOTICE,
            errmsg("translated PRId64 = %" PRId64, (int64) 424242424242));
    ereport(NOTICE,
            errmsg("translated PRId32 = %" PRId32, (int32) -1234));
    ereport(NOTICE,
-           errmsg("translated PRIdMAX = %" PRIdMAX, (intmax_t) -5678));
+           errmsg("translated PRIdMAX = %" PRIdMAX, (intmax_t) -123456789012));
    ereport(NOTICE,
-           errmsg("translated PRIdPTR = %" PRIdPTR, (intptr_t) 9999));
+           errmsg("translated PRIdPTR = %" PRIdPTR, (intptr_t) -9999));
 
    ereport(NOTICE,
            errmsg("translated PRIu64 = %" PRIu64, (uint64) 424242424242));
    ereport(NOTICE,
-           errmsg("translated PRIu32 = %" PRIu32, (uint32) 1234));
+           errmsg("translated PRIu32 = %" PRIu32, (uint32) -1234));
    ereport(NOTICE,
-           errmsg("translated PRIuMAX = %" PRIuMAX, (uintmax_t) 5678));
+           errmsg("translated PRIuMAX = %" PRIuMAX, (uintmax_t) 123456789012));
    ereport(NOTICE,
            errmsg("translated PRIuPTR = %" PRIuPTR, (uintptr_t) 9999));
 
    ereport(NOTICE,
            errmsg("translated PRIx64 = %" PRIx64, (uint64) 424242424242));
    ereport(NOTICE,
-           errmsg("translated PRIx32 = %" PRIx32, (uint32) 1234));
+           errmsg("translated PRIx32 = %" PRIx32, (uint32) -1234));
    ereport(NOTICE,
-           errmsg("translated PRIxMAX = %" PRIxMAX, (uintmax_t) 5678));
+           errmsg("translated PRIxMAX = %" PRIxMAX, (uintmax_t) 123456789012));
    ereport(NOTICE,
            errmsg("translated PRIxPTR = %" PRIxPTR, (uintptr_t) 9999));
 
    ereport(NOTICE,
            errmsg("translated PRIX64 = %" PRIX64, (uint64) 424242424242));
    ereport(NOTICE,
-           errmsg("translated PRIX32 = %" PRIX32, (uint32) 1234));
+           errmsg("translated PRIX32 = %" PRIX32, (uint32) -1234));
    ereport(NOTICE,
-           errmsg("translated PRIXMAX = %" PRIXMAX, (uintmax_t) 5678));
+           errmsg("translated PRIXMAX = %" PRIXMAX, (uintmax_t) 123456789012));
    ereport(NOTICE,
            errmsg("translated PRIXPTR = %" PRIXPTR, (uintptr_t) 9999));
-#else
-   elog(NOTICE, "NLS is not enabled");
-#endif
 
    PG_RETURN_VOID();
 }
index 9c605af2f0ba58039765d0da2cc958bf2be0215d..4433a1fd036a0a025d38489d4df264bd0e70508f 100644 (file)
@@ -33,12 +33,6 @@ begin
     current_setting('lc_messages');
 end $$;
 
-SELECT current_setting('lc_messages') = 'C' AS failed \gset
-\if :failed
-\echo Could not find an acceptable spelling of es_ES locale
-\quit
-\endif
-
 SELECT test_translation();
 
 RESET lc_messages;