473,386 Members | 1,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

PyObject_New not running tp_new for iterators?

I'm trying to extend Python with an iterator class that should be
returned from a factory function.

For some reason the iterator object is not being properly initialised if
the iterator is created in a C function. It works just fine if the
object is created using the class contructor

Trying to minimise the cut-n-paste, but:
typedef struct {
PyObject_HEAD

int i;

} MyIter;
static PyObject *
MyIter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
MyIter *self;

self = (MyIter *)type->tp_alloc(type, 0);
if (self) {
self->i = 0;
}

return (PyObject *)self;
}

static PyObject *
MyIter_Iter(PyObject *self)
{
Py_INCREF(self);
return self;
}

static PyObject *
MyIter_Next(PyObject *self)
{
MyIter *ep = (MyIter *) self;
return Py_BuildValue("i", ep->i++);
}

plus the usual type stucture. This simple iterator returns the integers
from 0 to (programmer's) infinity.

This works fine if I create the object directly:
Python 2.3.1 (#3, Oct 1 2003, 16:18:11)
[GCC 3.3] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
import myiter
e = myiter.MyIter()
str(e) '<myiter.MyIter object at 0x184050>' e.next() 0 e.next() 1 e.next() 2 e.next() 3
However, if I create the MyIter object from a C factory function:
static PyObject *
FromFile(PyObject *self, PyObject *args)
{
MyIter *ro;

if (!PyArg_ParseTuple(args, ""))
return NULL;

if (!(ro = PyObject_New(MyIter, &MyIterType)))
return NULL;

return (PyObject *)ro;
}

static PyMethodDef My_methods[] = {
{"FromFile", FromFile, METH_VARARGS,
"Return an iterator."},
{NULL} /* Sentinel */
};


then the MyIter object is not properly initialised!
f = myiter.FromFile()
f <myiter.MyIter object at 0x184070> f.next() -2080374694 f.next() -2080374693 f.next() -2080374692


I tried a similar problem with a plane-old-object using tp_members
(rather than an iterator object) and that worked fine, even from a
factory function.

I've looked long and hard at the API doc, but I'm stumped..... I've
checked a couple of examples and they just do what I'm doing!

What am I missing?
Jul 19 '05 #1
0 983

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Steven Bethard | last post by:
So, as I understand it, in Python 3000, zip will basically be replaced with izip, meaning that instead of returning a list, it will return an iterator. This is great for situations like: zip(*)...
18
by: deancoo | last post by:
I have gotten into the habit of often using copy along with an insert iterator. There are scenarios where I process quite a lot of data this way. Can someone give me a general feel as to how much...
1
by: Marcin Kaliciñski | last post by:
template<class RanAccIt> void some_algorithm(RanAccIt begin, RanAccIt end) { // this algorithm involves calling std::lexicographical_compare // on range [begin, end), and on reverse of this range...
3
by: codefixer | last post by:
Hello, I am trying to understand if ITERATORS are tied to CONTAINERS. I know the difference between 5 different or 6(Trivial, on SGI). But what I fail to understand is how can I declare all 5...
8
by: babak | last post by:
Hi everyone I have a problem with Iterators and containers in STL that hopefully someone can help me with. This is what I try to do: I have an associative (map) container and I have a...
24
by: Lasse Vågsæther Karlsen | last post by:
I need to merge several sources of values into one stream of values. All of the sources are sorted already and I need to retrieve the values from them all in sorted order. In other words: s1 = ...
2
by: ma740988 | last post by:
typedef std::vector < std::complex < double > > complex_vec_type; // option1 int main() { complex_vec_type cc ( 24000 ); complex_vec_type dd ( &cc, &cc ); } versus
90
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
18
by: desktop | last post by:
1) I have this code: std::list<intmylist; mylist.push_back(1); mylist.push_back(2); mylist.push_back(3); mylist.push_back(4);
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.