Merge remote-tracking branch 'fix-211'

This commit is contained in:
Daniele Varrazzo 2018-05-20 12:50:22 +01:00
commit 8c969304fd
4 changed files with 25 additions and 12 deletions

2
NEWS
View File

@ -17,6 +17,8 @@ Other changes:
What's new in psycopg 2.7.5 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 - Fixed building on Solaris 11 and derivatives such as SmartOS and illumos
(:ticket:`#677`). (:ticket:`#677`).
- Maybe fixed building on MSYS2 (as reported in :ticket:`#658`). - Maybe fixed building on MSYS2 (as reported in :ticket:`#658`).

View File

@ -363,12 +363,15 @@ class NamedTupleCursor(_cursor):
return return
def _make_nt(self): def _make_nt(self):
# ascii except alnum and underscore
nochars = ' !"#$%&\'()*+,-./:;<=>?@[\\]^`{|}~'
re_clean = _re.compile('[' + _re.escape(nochars) + ']')
def f(s): def f(s):
# NOTE: Python 3 actually allows unicode chars in fields s = re_clean.sub('_', s)
s = _re.sub('[^a-zA-Z0-9_]', '_', s)
# Python identifier cannot start with numbers, namedtuple fields # Python identifier cannot start with numbers, namedtuple fields
# cannot start with underscore. So... # cannot start with underscore. So...
if _re.match('^[0-9_]', s): if s[0] == '_' or '0' <= s[0] <= '9':
s = 'f' + s s = 'f' + s
return s return s

View File

@ -56,15 +56,15 @@ fi
# Unsupported postgres versions that we still support # Unsupported postgres versions that we still support
# Images built by https://github.com/psycopg/psycopg2-wheels/tree/build-dinosaurs # Images built by https://github.com/psycopg/psycopg2-wheels/tree/build-dinosaurs
if [[ -n "$TEST_PAST" ]]; then if [[ -n "$TEST_PAST" ]]; then
run_test 7.4
run_test 8.0
run_test 8.1
run_test 8.2
run_test 8.3
run_test 8.4
run_test 9.0
run_test 9.1
run_test 9.2 run_test 9.2
run_test 9.1
run_test 9.0
run_test 8.4
run_test 8.3
run_test 8.2
run_test 8.1
run_test 8.0
run_test 7.4
fi fi
# Postgres built from master # Postgres built from master

View File

@ -19,7 +19,7 @@ from datetime import timedelta
import psycopg2 import psycopg2
import psycopg2.extras import psycopg2.extras
import unittest import unittest
from .testutils import ConnectingTestCase, skip_before_postgres from .testutils import ConnectingTestCase, skip_before_postgres, skip_before_python
class ExtrasDictCursorTests(ConnectingTestCase): class ExtrasDictCursorTests(ConnectingTestCase):
@ -357,6 +357,14 @@ class NamedTupleCursorTest(ConnectingTestCase):
self.assertEqual(rv.f_column_, 2) self.assertEqual(rv.f_column_, 2)
self.assertEqual(rv.f3, 3) self.assertEqual(rv.f3, 3)
@skip_before_python(3)
@skip_before_postgres(8)
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)
def test_minimal_generation(self): def test_minimal_generation(self):
# Instrument the class to verify it gets called the minimum number of times. # Instrument the class to verify it gets called the minimum number of times.
from psycopg2.extras import NamedTupleCursor from psycopg2.extras import NamedTupleCursor