4

I'm trying to use ctypes to extract data from internal python structures. Namely, I'm trying to read the 4 fields in an xrange:

typedef struct {
    PyObject_HEAD
    long    start;
    long    step;
    long    len;
} rangeobject;

Is there any standard way of getting at such fields within python itself?

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
UsAaR33
  • 3,536
  • 2
  • 34
  • 55

2 Answers2

5

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
Denis Otkidach
  • 32,032
  • 8
  • 79
  • 100
  • Sorry, I forgot to mention in the question that I need to be compatible with Python 2.5. Serialization was only added in python 2.6. – UsAaR33 Nov 25 '09 at 09:16
  • Worked perfectly; thanks so much. (And now it is clear how to do this for general types) – UsAaR33 Nov 27 '09 at 09:23
0

The ctypes module isn't meant for accessing Python internals. ctypes lets you deal with C libraries in C terms, but coding in Python.

You probably want a C extension, which in many ways is the opposite of ctypes. With a C extension, you deal with Python code in Python terms, but code in C.

UPDATED: Since you want pure Python, why do you need to access the internals of a built-in xrange object? xrange is very simple: create your own in Python, and do what you want with it.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • In the ideal world, I would use one. However, what I am distributing must be written in pure python; I cannot have the users to compile python extensions. – UsAaR33 Nov 25 '09 at 03:18
  • I'm implementing a library (open-source part of PiCloud(.com) ) which needs to serialize such objects in python 2.5. Note: I can always do a str(xrange_object) and then parse, but besides being very kludgey won't work for other C objects I'm trying to get at. – UsAaR33 Nov 25 '09 at 04:28