Changeset 3161

Show
Ignore:
Timestamp:
12/15/07 16:50:52 (5 years ago)
Author:
tasuku
Message:

added iterkeys/itervalues/iteritems on pytc.BDB

Location:
lang/python/pytc/trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • lang/python/pytc/trunk/pytc.c

    r3160 r3161  
    934934  0,                                           /* tp_richcompare */ 
    935935  0,                                           /* tp_weaklistoffset */ 
    936   PyTCHDB_GetIter_keys,                        /* tp_iter */ 
     936  (getiterfunc)PyTCHDB_GetIter_keys,           /* tp_iter */ 
    937937  (iternextfunc)PyTCHDB_iternext,              /* tp_iternext */ 
    938938  PyTCHDB_methods,                             /* tp_methods */ 
     
    10411041static PyObject * 
    10421042PyBDBCUR_iternext(PyBDBCUR *self) { 
    1043   char *key; 
    1044   int key_len; 
    1045   PyObject *ret; 
    1046  
     1043  /* TODO: performance up */ 
     1044  GET_TCXSTR_KEY_VALUE(tcbdbcurrec,self->cur) 
     1045  if (result) { 
     1046    switch (self->itype) { 
     1047      case iter_key: 
     1048        ret = PyString_FromStringAndSize(tcxstrptr(key), tcxstrsize(key)); 
     1049        break; 
     1050      case iter_value: 
     1051        ret = PyString_FromStringAndSize(tcxstrptr(value), tcxstrsize(value)); 
     1052        break; 
     1053      case iter_item: 
     1054        ret = Py_BuildValue("(s#s#)", tcxstrptr(key), tcxstrsize(key), 
     1055                                      tcxstrptr(value), tcxstrsize(value)); 
     1056        break; 
     1057    } 
     1058  } 
    10471059  Py_BEGIN_ALLOW_THREADS 
    1048   key = tcbdbcurkey(self->cur, &key_len); 
     1060  result = tcbdbcurnext(self->cur); 
    10491061  Py_END_ALLOW_THREADS 
    1050  
    1051   if (key) { 
    1052     ret = PyString_FromStringAndSize(key, key_len); 
    1053     free(key); 
    1054     Py_BEGIN_ALLOW_THREADS 
    1055     tcbdbcurnext(self->cur); 
    1056     Py_END_ALLOW_THREADS 
    1057     return ret; 
    1058   } 
    1059   return NULL; 
     1062  CLEAR_TCXSTR_KEY_VALUE() 
    10601063} 
    10611064 
     
    17411744   METH_NOARGS, 
    17421745   ""}, 
     1746  {"iteritems", (PyCFunction)PyTCBDB_GetIter_items, 
     1747   METH_NOARGS, 
     1748   ""}, 
     1749  {"iterkeys", (PyCFunction)PyTCBDB_GetIter_keys, 
     1750   METH_NOARGS, 
     1751   ""}, 
     1752  {"itervalues", (PyCFunction)PyTCBDB_GetIter_values, 
     1753   METH_NOARGS, 
     1754   ""}, 
    17431755  /* 
    17441756  {"", (PyCFunction)PyTCBDB_, 
     
    17981810  0,                                           /* tp_richcompare */ 
    17991811  0,                                           /* tp_weaklistoffset */ 
    1800   PyTCBDB_GetIter_keys,                        /* tp_iter */ 
     1812  (getiterfunc)PyTCBDB_GetIter_keys,           /* tp_iter */ 
    18011813  0,                                           /* tp_iternext */ 
    18021814  PyTCBDB_methods,                             /* tp_methods */ 
  • lang/python/pytc/trunk/tests/testBDB.py

    r3102 r3161  
    100100      ('moru', 'pui'), ('moruta', 'puipui')]) 
    101101 
     102    result = [] 
     103    for key in db.iterkeys(): 
     104      result.append(key) 
     105    self.assertEqual(sorted(result), ['hamu', 'kiki', 'kiki', 'moru', 'moruta']) 
     106 
     107    result = [] 
     108    for value in db.itervalues(): 
     109      result.append(value) 
     110    self.assertEqual(sorted(result), 
     111      ['ju', 'nya-nya-nya-', 'pui', 'puipui', 'unya-n']) 
     112 
     113    result = [] 
     114    for (key, value) in db.iteritems(): 
     115      result.append((key, value)) 
     116    self.assertEqual(sorted(result),[ 
     117      ('hamu', 'ju'), ('kiki', 'nya-nya-nya-'), ('kiki', 'unya-n'), 
     118      ('moru', 'pui'), ('moruta', 'puipui')]) 
     119 
    102120    # this bug is reported by id:a2c 
    103121    self.assertRaises(TypeError, eval, 'db[:]', globals(), locals())