]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
fix: restore pre-3.1 behaviour converting fields to named tuples attributes
authorJolbas <39026960+Jolbas@users.noreply.github.com>
Sun, 25 Sep 2022 14:11:36 +0000 (16:11 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 28 Sep 2022 00:45:59 +0000 (01:45 +0100)
Let more valid identifiers through _as_python_identifier(). Also 2.5x faster if no change is needed

Close #386

psycopg/psycopg/_encodings.py

index d293949e34d13835608b8214d02f7d1e6f4dfe5d..b1b21267c5c25a4c4bd6706bbfd60f1036c2c0db 100644 (file)
@@ -146,10 +146,13 @@ def _as_python_identifier(s: str, prefix: str = "f") -> str:
     Replace all non-valid chars with '_' and prefix the value with *prefix* if
     the first letter is an '_'.
     """
-    s = _re_clean.sub("_", s)
-    # Python identifier cannot start with numbers, namedtuple fields
-    # cannot start with underscore. So...
-    if s[0] == "_" or "0" <= s[0] <= "9":
+    if not s.isidentifier():
+        if s[0] in "1234567890":
+            s = prefix + s
+        if not s.isidentifier():
+            s = _re_clean.sub("_", s)
+    # namedtuple fields cannot start with underscore. So...
+    if s[0] == "_":
         s = prefix + s
     return s