summaryrefslogtreecommitdiff
path: root/lib/extras.py
diff options
context:
space:
mode:
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>2018-05-13 23:51:21 +0100
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>2018-05-18 12:15:50 +0100
commitbc84b6233eaa1e7a6302b51f8ab8950534ff1813 (patch)
tree076031f0e5fec49835b25375963c6e042a8b1315 /lib/extras.py
parent548e28135023252ea79830e52521e2a8c1c0bd37 (diff)
downloadpsycopg2-bc84b6233eaa1e7a6302b51f8ab8950534ff1813.tar.gz
Allow non-ascii chars in namedtuple fields
They can be valid chars in Python 3. Or maybe not? In which case Python will throw an exception, but that's fine. Fix regression introduced fixing #211
Diffstat (limited to 'lib/extras.py')
-rw-r--r--lib/extras.py9
1 files changed, 6 insertions, 3 deletions
diff --git a/lib/extras.py b/lib/extras.py
index 1f85d53..9c26ccb 100644
--- a/lib/extras.py
+++ b/lib/extras.py
@@ -363,12 +363,15 @@ class NamedTupleCursor(_cursor):
return
def _make_nt(self):
+ # ascii except alnum and underscore
+ nochars = ' !"#$%&\'()*+,-./:;<=>?@[\\]^`{|}~'
+ re_clean = _re.compile('[' + _re.escape(nochars) + ']')
+
def f(s):
- # NOTE: Python 3 actually allows unicode chars in fields
- s = _re.sub('[^a-zA-Z0-9_]', '_', s)
+ s = re_clean.sub('_', s)
# Python identifier cannot start with numbers, namedtuple fields
# cannot start with underscore. So...
- if _re.match('^[0-9_]', s):
+ if s[0] == '_' or '0' <= s[0] <= '9':
s = 'f' + s
return s