예제 #1
0
파일: dict.go 프로젝트: gbbr/textmate
// Map returns a Go map that contains the values from the Python dictionary,
// indexed by the keys.  The keys and values are the same as in the Python
// dictionary, but changes to the Go map are not propogated back to the Python
// dictionary.
//
// Note: the map holds borrowed references
func (d *Dict) Map() map[Object]Object {
	m := make(map[Object]Object, d.Size())
	var p C.Py_ssize_t
	var k *C.PyObject
	var v *C.PyObject
	for int(C.PyDict_Next(c(d), &p, &k, &v)) != 0 {
		key := newObject(k)
		value := newObject(v)
		m[key] = value
	}
	return m
}
예제 #2
0
파일: dict.go 프로젝트: gbbr/textmate
// MapString is similar to Map, except that the keys are first converted to
// strings.  If the keys are not all Python strings, then an error is returned.
//
// Note: the map holds borrowed references
func (d *Dict) MapString() (map[string]Object, error) {
	m := make(map[string]Object, d.Size())
	var p C.Py_ssize_t
	var k *C.PyObject
	var v *C.PyObject
	for int(C.PyDict_Next(c(d), &p, &k, &v)) != 0 {
		key := newObject(k)
		value := newObject(v)
		s, ok := key.(*Unicode)
		if !ok {
			return nil, TypeError.Err("%v is not a string", key)
		}
		m[s.String()] = value
	}
	return m, nil
}
예제 #3
0
파일: dict.go 프로젝트: remh/go-python
// int PyDict_Next(PyObject *p, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
// Iterate over all key-value pairs in the dictionary p. The Py_ssize_t referred to by ppos must be initialized to 0 prior to the first call to this function to start the iteration; the function returns true for each pair in the dictionary, and false once all pairs have been reported. The parameters pkey and pvalue should either point to PyObject* variables that will be filled in with each key and value, respectively, or may be NULL. Any references returned through them are borrowed. ppos should not be altered during iteration. Its value represents offsets within the internal dictionary structure, and since the structure is sparse, the offsets are not consecutive.
//
// For example:
//
// PyObject *key, *value;
// Py_ssize_t pos = 0;
//
// while (PyDict_Next(self->dict, &pos, &key, &value)) {
//     // do something interesting with the values...
//     ...
// }
// The dictionary p should not be mutated during iteration. It is safe (since Python 2.1) to modify the values of the keys as you iterate over the dictionary, but only so long as the set of keys does not change. For example:
//
// PyObject *key, *value;
// Py_ssize_t pos = 0;
//
// while (PyDict_Next(self->dict, &pos, &key, &value)) {
//     int i = PyInt_AS_LONG(value) + 1;
//     PyObject *o = PyInt_FromLong(i);
//     if (o == NULL)
//         return -1;
//     if (PyDict_SetItem(self->dict, key, o) < 0) {
//         Py_DECREF(o);
//         return -1;
//     }
//     Py_DECREF(o);
// }
// Changed in version 2.5: This function used an int * type for ppos. This might require changes in your code for properly supporting 64-bit systems.
func PyDict_Next(self *PyObject, pos *int, key, value **PyObject) error {
	if pos == nil {
		return errors.New("invalid position")
	}

	c_pos := C.Py_ssize_t(*pos)
	c_key := topy(*key)
	c_val := topy(*value)

	err := C.PyDict_Next(topy(self), &c_pos, &c_key, &c_val)

	*pos = int(c_pos)
	*key = togo(c_key)
	*value = togo(c_val)

	return int2err(err)
}
예제 #4
0
파일: dict.go 프로젝트: MogeiWang/py
// Iterate over all key-value pairs in the dictionary d.
// The Py_ssize_t referred to by ppos must be initialized to 0 prior to the first call to this function
// to start the iteration; the function returns true for each pair in the dictionary, and false once all
// pairs have been reported. The parameters pkey and pvalue should either point to PyObject* variables
// that will be filled in with each key and value, respectively, or may be NULL. Any references returned
// through them are borrowed. ppos should not be altered during iteration. Its value represents offsets
// within the internal dictionary structure, and since the structure is sparse, the offsets are not consecutive.
func (d *Dict) Next(pos *DictIter, k, v **Base) bool {
	k1 := (**C.PyObject)(unsafe.Pointer(k))
	v1 := (**C.PyObject)(unsafe.Pointer(v))
	return C.PyDict_Next(d.c(), (*C.Py_ssize_t)(pos), k1, v1) != 0
}