Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/pr/231' into pr231
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzeek committed Mar 30, 2016
2 parents 4a3205d + f84ef1f commit 4df1e07
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/sqlalchemy/cextension/resultproxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ BaseRowProxy_subscript(BaseRowProxy *self, PyObject *key)
#if PY_MAJOR_VERSION < 3
if (PyInt_CheckExact(key)) {
index = PyInt_AS_LONG(key);
if (index < 0)
index += BaseRowProxy_length(self);
} else
#endif

Expand All @@ -271,6 +273,8 @@ BaseRowProxy_subscript(BaseRowProxy *self, PyObject *key)
if ((index == -1) && PyErr_Occurred())
/* -1 can be either the actual value, or an error flag. */
return NULL;
if (index < 0)
index += BaseRowProxy_length(self);
} else if (PySlice_Check(key)) {
values = PyObject_GetItem(self->row, key);
if (values == NULL)
Expand Down
9 changes: 8 additions & 1 deletion lib/sqlalchemy/engine/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,17 @@ def __init__(self, parent, cursor_description):

self._keymap = {}
if not _baserowproxy_usecext:
# keymap indexes by integer index...
# keymap indexes by integer index: this is only used
# in the pure Python BaseRowProxy.__getitem__
# implementation to avoid an expensive
# isinstance(key, util.int_types) in the most common
# case path
self._keymap.update([
(elem[0], (elem[3], elem[4], elem[0]))
for elem in raw
] + [
(elem[0] - num_ctx_cols, (elem[3], elem[4], elem[0]))
for elem in raw
])

# processors in key order for certain per-row
Expand Down
19 changes: 19 additions & 0 deletions test/engine/test_execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,25 @@ def test_rowproxy_is_sequence(self):
{'key': (None, None, 0), 0: (None, None, 0)})
assert isinstance(row, collections.Sequence)

def test_rowproxy_getitem(self):
metadata = MetaData()
metadata.bind = 'sqlite://'
values = Table('users', metadata,
Column('key', String(10), primary_key=True),
Column('value', String(10)))
values.create()

values.insert().execute(key='One', value='Uno')
row = values.select().execute().fetchone()

assert row['key'] == 'One'
assert row['value'] == 'Uno'
assert row[0] == 'One'
assert row[1] == 'Uno'
assert row[-2] == 'One'
assert row[-1] == 'Uno'
assert row[1:0:-1] == ('Uno',)

@testing.requires.cextensions
def test_row_c_sequence_check(self):
import csv
Expand Down

0 comments on commit 4df1e07

Please sign in to comment.