You can access data you need without ctypes
:
>>> obj = xrange(1,11,2)
>>> obj.__reduce__()[1]
(1, 11, 2)
>>> len(obj)
5
Note, that __reduce__()
method is exactly for serialization. Read this chapter in documentation for more information.
Update: But sure you can access internal data with ctypes
too:
from ctypes import *
PyObject_HEAD = [
('ob_refcnt', c_size_t),
('ob_type', c_void_p),
]
class XRangeType(Structure):
_fields_ = PyObject_HEAD + [
('start', c_long),
('step', c_long),
('len', c_long),
]
range_obj = xrange(1, 11, 2)
c_range_obj = cast(c_void_p(id(range_obj)), POINTER(XRangeType)).contents
print c_range_obj.start, c_range_obj.step, c_range_obj.len