473,385 Members | 1,824 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,385 software developers and data experts.

Python lists ans sequence protocol from C API

Dear all,

I have run into a problem using python lists and sequence protocol.
The first code snippet uses explicit list operations and works fine.
PyObject *argseq, *ov;
int i, v, len;

len = 2;
argseq = PyList_New(len);
for (i=0; i<len; i++) {
ov = PyInt_FromLong(i);
printf("Index %d. Success %d.\n", i, PyList_SetItem(argseq, i, ov));
}
for (i=0; i<len; i++) {
ov = PyList_GetItem(argseq,i);
v = PyInt_AsLong(ov);
printf("Index %d. Read %d.\n", i, v);
}

Output:

Index 0. Success 0.
Index 1. Success 0.
Index 0. Read 0.
Index 1. Read 1.

Argseq results in a python list [0, 1], as it should.
However, if I change the above code to use sequence protocol
for GetItem and SetItem (see below), the program fails when calling
PySequence_SetItem with the following error message:
"Unhandled exception at 0x1e04ed1a in python.exe:
0xC0000005: Access violation reading location 0x00000000."
Could it be that basic lists do not support sequence protocol?
Or am I missing something? I'm using Python 2.3.4 on Windows XP.

PyObject *argseq, *ov;
int i, v, len;

len = 2;
argseq = PyList_New(len);
for (i=0; i<len; i++) {
ov = PyInt_FromLong(i);
printf("Index %d. Success %d.\n", i, PySequence_SetItem(argseq, i, ov));
}
for (i=0; i<len; i++) {
ov = PySequence_GetItem(argseq,i);
v = PyInt_AsLong(ov);
printf("Index %d. Read %d.\n", i, v);
}

Why would I wish to use sequence with basic lists protocol? Because my code should
also deal with other sequence types, possibly subclassed from python lists.

Thanks,

Matjaz.
Jul 18 '05 #1
3 1982
Matjaz <su******@email.si> wrote:
...
PySequence_SetItem with the following error message:
"Unhandled exception at 0x1e04ed1a in python.exe:
0xC0000005: Access violation reading location 0x00000000."
Could it be that basic lists do not support sequence protocol?
Once they're valid Python list objects, they do...
Or am I missing something? I'm using Python 2.3.4 on Windows XP.
You're missing the fact that you're never building a valid Python list
object in your code. The slots, as PyObject*, are 'random', probably
null pointers. That's why you're supposed to use PyList_SET_ITEM
specifically to initialize these 'slots' WITHOUT trying to decref the
previously held item... there IS no 'previously held item'...!
PyObject *argseq, *ov;
int i, v, len;

len = 2;
argseq = PyList_New(len);
This does NOT initialize the slots of list argseq, as above explained.
for (i=0; i<len; i++) {
ov = PyInt_FromLong(i);
printf("Index %d. Success %d.\n", i, PySequence_SetItem(argseq, i, ov));
}
But this does try to decref that null pointer (or whatever), so, BOOM.
Why would I wish to use sequence with basic lists protocol? Because my
code should also deal with other sequence types, possibly subclassed from
python lists.


Nevertheless they'll need to be properly built, inizialized, first.

Add initialization, such as an immediate:
for (i=0; i<len; i++) {
PyList_SET_ITEM(argseq, i, Py_BuildValue(""));
}
just after your
argseq = PyList_New(len);
and you should be fine with PySequence_whateveryouwish now.
Alex
Jul 18 '05 #2
Alex, thanks. In the meantime I also found out what I
was doing wrong. One question, though. Would you
prefer using Py_BuildValue("") over Py_None and
increfing it?

Matjaz.

Alex Martelli wrote:
Matjaz <su******@email.si> wrote:
...
PySequence_SetItem with the following error message:
"Unhandled exception at 0x1e04ed1a in python.exe:
0xC0000005: Access violation reading location 0x00000000."
Could it be that basic lists do not support sequence protocol?

Once they're valid Python list objects, they do...

Or am I missing something? I'm using Python 2.3.4 on Windows XP.

You're missing the fact that you're never building a valid Python list
object in your code. The slots, as PyObject*, are 'random', probably
null pointers. That's why you're supposed to use PyList_SET_ITEM
specifically to initialize these 'slots' WITHOUT trying to decref the
previously held item... there IS no 'previously held item'...!

PyObject *argseq, *ov;
int i, v, len;

len = 2;
argseq = PyList_New(len);

This does NOT initialize the slots of list argseq, as above explained.

for (i=0; i<len; i++) {
ov = PyInt_FromLong(i);
printf("Index %d. Success %d.\n", i, PySequence_SetItem(argseq, i, ov));
}

But this does try to decref that null pointer (or whatever), so, BOOM.

Why would I wish to use sequence with basic lists protocol? Because my
code should also deal with other sequence types, possibly subclassed from
python lists.

Nevertheless they'll need to be properly built, inizialized, first.

Add initialization, such as an immediate:
for (i=0; i<len; i++) {
PyList_SET_ITEM(argseq, i, Py_BuildValue(""));
}
just after your
argseq = PyList_New(len);
and you should be fine with PySequence_whateveryouwish now.
Alex

Jul 18 '05 #3
Matjaz <su******@email.si> wrote:
Alex, thanks. In the meantime I also found out what I
was doing wrong. One question, though. Would you
prefer using Py_BuildValue("") over Py_None and
increfing it?


Py_None with suitable incref is faster, Py_BuildValue is more compact,
not much in it either way.

Alex
Jul 18 '05 #4

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

Similar topics

8
by: Bo Peng | last post by:
Dear list, I am writing a Python extension module that needs a way to expose pieces of a big C array to python. Currently, I am using NumPy like the following: PyObject* res =...
63
by: Davor | last post by:
Is it possible to write purely procedural code in Python, or the OO constructs in both language and supporting libraries have got so embedded that it's impossible to avoid them? Also, is anyone...
10
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. ...
105
by: Christoph Zwerschke | last post by:
Sometimes I find myself stumbling over Python issues which have to do with what I perceive as a lack of orthogonality. For instance, I just wanted to use the index() method on a tuple which does...
40
by: brad | last post by:
Will len(a_string) become a_string.len()? I was just reading http://docs.python.org/dev/3.0/whatsnew/3.0.html One of the criticisms of Python compared to other OO languages is that it isn't OO...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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...

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.