Issue #18038: SyntaxError raised during compilation sources with illegal
encoding now always contains an encoding name.
diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c
index 93a4a5c..7fe384b 100644
--- a/Parser/tokenizer.c
+++ b/Parser/tokenizer.c
@@ -291,20 +291,20 @@
                     tok->encoding = cs;
                     tok->decoding_state = STATE_NORMAL;
                 }
-                else
+                else {
+                    PyErr_Format(PyExc_SyntaxError,
+                                 "encoding problem: %s", cs);
                     PyMem_FREE(cs);
+                }
             }
         } else {                /* then, compare cs with BOM */
             r = (strcmp(tok->encoding, cs) == 0);
+            if (!r)
+                PyErr_Format(PyExc_SyntaxError,
+                             "encoding problem: %s with BOM", cs);
             PyMem_FREE(cs);
         }
     }
-    if (!r) {
-        cs = tok->encoding;
-        if (!cs)
-            cs = "with BOM";
-        PyErr_Format(PyExc_SyntaxError, "encoding problem: %s", cs);
-    }
     return r;
 }