Skip to content

Commit

Permalink
Fixed the bug that caused segfault upon trying to iterate over a table
Browse files Browse the repository at this point in the history
using "for in".

PR:
Obtained from:
Submitted by:
Reviewed by:
  • Loading branch information
grisha committed Jul 14, 2003
1 parent 3e3418e commit bbb6824
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
18 changes: 5 additions & 13 deletions src/tableobject.c
Expand Up @@ -57,7 +57,7 @@
*
* tableobject.c
*
* $Id: tableobject.c,v 1.27 2003/06/09 23:09:31 grisha Exp $
* $Id: tableobject.c,v 1.28 2003/07/14 20:51:32 grisha Exp $
*
*/

Expand Down Expand Up @@ -1107,7 +1107,7 @@ typedef struct {
tableobject *table;
int ti_nelts;
int ti_pos;
binaryfunc ti_select;
tableselectfunc ti_select;
} tableiterobject;

static PyObject *tableiter_new(tableobject *table, tableselectfunc select)
Expand All @@ -1120,7 +1120,7 @@ static PyObject *tableiter_new(tableobject *table, tableselectfunc select)
ti->table = table;
ti->ti_nelts = table->table->a.nelts;
ti->ti_pos = 0;
ti->ti_select = (binaryfunc)select;
ti->ti_select = select;
return (PyObject *)ti;
}

Expand All @@ -1132,7 +1132,6 @@ static void tableiter_dealloc(tableiterobject *ti)

static PyObject *tableiter_next(tableiterobject *ti, PyObject *args)
{
PyObject *key, *val;
apr_table_entry_t *elts = (apr_table_entry_t *) ti->table->table->a.elts;

/* make sure the table hasn't change while being iterated */
Expand All @@ -1146,10 +1145,7 @@ static PyObject *tableiter_next(tableiterobject *ti, PyObject *args)
/* return the next key/val */

if (ti->ti_pos < ti->table->table->a.nelts) {
key = PyString_FromString(elts[ti->ti_pos].key);
val = PyString_FromString(elts[ti->ti_pos].val);
ti->ti_pos++;
return (*ti->ti_select)(key, val);
return (*ti->ti_select)(&elts[ti->ti_pos++]);
}

/* the end has been reached */
Expand All @@ -1172,7 +1168,6 @@ static PyMethodDef tableiter_methods[] = {

static PyObject *tableiter_iternext(tableiterobject *ti)
{
PyObject *key, *val;
apr_table_entry_t *elts = (apr_table_entry_t *) ti->table->table->a.elts;

/* make sure the table hasn't change while being iterated */
Expand All @@ -1186,10 +1181,7 @@ static PyObject *tableiter_iternext(tableiterobject *ti)
/* return the next key/val */

if (ti->ti_pos < ti->table->table->a.nelts) {
key = PyString_FromString(elts[ti->ti_pos].key);
val = PyString_FromString(elts[ti->ti_pos].val);
ti->ti_pos++;
return (*ti->ti_select)(key, val);
return (*ti->ti_select)(&elts[ti->ti_pos++]);
}

/* the end has been reached */
Expand Down
19 changes: 13 additions & 6 deletions test/htdocs/tests.py
Expand Up @@ -52,7 +52,7 @@
# information on the Apache Software Foundation, please see
# <http://www.apache.org/>.
#
# $Id: tests.py,v 1.29 2003/06/24 04:16:00 grisha Exp $
# $Id: tests.py,v 1.30 2003/07/14 20:51:32 grisha Exp $
#

# mod_python tests
Expand Down Expand Up @@ -268,7 +268,7 @@ def test_req_members(self):
log(" req.headers_in: %s" % `req.headers_in`)
if req.headers_in["Host"][:13].lower() != "test_internal":
self.fail("The 'Host' header should begin with 'test_internal'")

log(" req.headers_out: %s" % `req.headers_out`)
if ((not req.headers_out.has_key("content-length")) or
req.headers_out["content-length"] != "15"):
Expand Down Expand Up @@ -434,12 +434,12 @@ def test_server_members(self):
self.fail("server.is_virtual should be 1")

log(" server.timeout: %s" % `server.timeout`)
if not server.timeout in (5000000, 300000000):
self.fail("server.timeout should be 5000000 or 300000000")
if not server.timeout in (5.0, 300.0):
self.fail("server.timeout should be 5.0 or 300.0")

log(" server.keep_alive_timeout: %s" % `server.keep_alive_timeout`)
if server.keep_alive_timeout != 15000000:
self.fail("server.keep_alive_timeout should be 15000000")
if server.keep_alive_timeout != 15.0:
self.fail("server.keep_alive_timeout should be 15.0")

log(" server.keep_alive_max: %s" % `server.keep_alive_max`)
if server.keep_alive_max != 100:
Expand Down Expand Up @@ -931,5 +931,12 @@ def __getitem__(self, key):
str(ta), str(tb))
if a: raise TestFailed, 'a not empty after popitems: %s' % str(a)
if b: raise TestFailed, 'b not empty after popitems: %s' % str(b)

# iteration (just make sure we can iterate without a segfault)
d = apache.table({'a' : '1', 'b' : '2', 'c' : '3'})
log(" for k in table")
for k in d:
pass

log(" _test_table test finished")

0 comments on commit bbb6824

Please sign in to comment.