summaryrefslogtreecommitdiff
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-13 23:51:21 +0100
commit8ad39c27124ffe1350446597cbfa5dca2107c844 (patch)
tree0c3fed4e383a1613560e8ed30118c08e836146d6
parent667079b2095ef4eabcaa9a2d6e3a8b0164f1c729 (diff)
downloadpsycopg2-8ad39c27124ffe1350446597cbfa5dca2107c844.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
-rw-r--r--NEWS2
-rw-r--r--lib/extras.py9
-rwxr-xr-xtests/test_extras_dictcursor.py10
3 files changed, 17 insertions, 4 deletions
diff --git a/NEWS b/NEWS
index 2a58b9d..5bde2cc 100644
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,8 @@ Current release
What's new in psycopg 2.7.5
^^^^^^^^^^^^^^^^^^^^^^^^^^^
+- Allow non-ascii chars in namedtuple fields (regression introduced fixing
+ :ticket':`#211`).
- Fixed building on Solaris 11 and derivatives such as SmartOS and illumos
(:ticket:`#677`).
- Maybe fixed building on MSYS2 (as reported in :ticket:`#658`).
diff --git a/lib/extras.py b/lib/extras.py
index f8a21e5..996b93d 100644
--- a/lib/extras.py
+++ b/lib/extras.py
@@ -368,12 +368,15 @@ class NamedTupleCursor(_cursor):
raise self._exc
else:
def _make_nt(self, namedtuple=namedtuple):
+ # 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
diff --git a/tests/test_extras_dictcursor.py b/tests/test_extras_dictcursor.py
index 5899aea..b371994 100755
--- a/tests/test_extras_dictcursor.py
+++ b/tests/test_extras_dictcursor.py
@@ -19,7 +19,7 @@ from datetime import timedelta
import psycopg2
import psycopg2.extras
from testutils import unittest, ConnectingTestCase, skip_before_postgres
-from testutils import skip_if_no_namedtuple
+from testutils import skip_before_python, skip_if_no_namedtuple
class ExtrasDictCursorTests(ConnectingTestCase):
@@ -391,6 +391,14 @@ class NamedTupleCursorTest(ConnectingTestCase):
self.assertEqual(rv.f_column_, 2)
self.assertEqual(rv.f3, 3)
+ @skip_before_python(3)
+ @skip_if_no_namedtuple
+ def test_nonascii_name(self):
+ curs = self.conn.cursor()
+ curs.execute('select 1 as \xe5h\xe9')
+ rv = curs.fetchone()
+ self.assertEqual(getattr(rv, '\xe5h\xe9'), 1)
+
@skip_if_no_namedtuple
def test_minimal_generation(self):
# Instrument the class to verify it gets called the minimum number of times.