error converting list to tuple

S

Stefan Seefeld

hi there,

I'm trying to convert a tuple to a list,
and get a 'TypeError: list objects are unhashable'.

Can anybody enlighten me as to the possible causes
for this ? Where does hashing come into play during
this conversion ?

Could it be that my runtime is corrupted ?
The code is executed from within a C++ extension
module I'm developing...

Thanks for any help,
Stefan

PS: I'm using python 2.3.3 in case that matters
 
J

john fabiani

Stefan said:
hi there,

I'm trying to convert a tuple to a list,
and get a 'TypeError: list objects are unhashable'.

Can anybody enlighten me as to the possible causes
for this ? Where does hashing come into play during
this conversion ?

Could it be that my runtime is corrupted ?
The code is executed from within a C++ extension
module I'm developing...

Thanks for any help,
Stefan

PS: I'm using python 2.3.3 in case that matters
God I can't believe I think I can answer this question:
mylist=[]
t=('123','hello','bye')
for count in range(0,len(t)):
mylist.append(t[count])

It works for me.
John
 
J

john fabiani

john said:
Stefan said:
hi there,

I'm trying to convert a tuple to a list,
and get a 'TypeError: list objects are unhashable'.

Can anybody enlighten me as to the possible causes
for this ? Where does hashing come into play during
this conversion ?

Could it be that my runtime is corrupted ?
The code is executed from within a C++ extension
module I'm developing...

Thanks for any help,
Stefan

PS: I'm using python 2.3.3 in case that matters
God I can't believe I think I can answer this question:
mylist=[]
t=('123','hello','bye')
for count in range(0,len(t)):
mylist.append(t[count])

It works for me.
John
even better than above:

mylist=list(t)


See everyone I'm learning but it is not easy for an old man to learn new
tricks.

John
 
S

Stefan Seefeld

john said:
God I can't believe I think I can answer this question:

well, unfortunately that wasn't my question <wink/>

I do:

t = ('hello',)
l = list(t)

which is the standard tuple->list conversion. And yes,
it does work when I run that code in isolation.

The point is that the above, as simple as it looks,
fails with said exception in a very specific context
(I call this code from within a C++ extension module),
so I'm wondering whether I could have messed up the
python runtime somehow or whether there are other
possible explanations for the exception.

Regards,
Stefan
 
T

Terry Reedy

Stefan Seefeld said:
(I call this code from within a C++ extension module),

You should have said that in the beginning since that is a whole different
ball game: extension modules in C are not Python.
so I'm wondering whether I could have messed up the
python runtime somehow or whether there are other
possible explanations for the exception.

It is easy to mess up in C ;-). You are most likely to get an answer (from
another C extension writer) if you post a short as possible code snippet
that exhibits the error, and the actual and complete error message.

tjr
 
S

Stefan Seefeld

Terry said:
You should have said that in the beginning since that is a whole different
ball game: extension modules in C are not Python.

ok, let me be more specific: From my C++ code I call a method
of a python object which happens to do the tuple->list conversion.

And as Python is implemented in C, I thought it didn't matter,
it's the same underlying API / object model, just different syntax ;-)
It is easy to mess up in C ;-). You are most likely to get an answer (from
another C extension writer) if you post a short as possible code snippet
that exhibits the error, and the actual and complete error message.

That's (part of) the problem: I can't reproduce this in isolation,
so I wanted to know how I could debug this.

What I do is calling 'PyObject_Str' on the python object. That object
has its '__str__' method implemented in a way that involves said
tuple->list conversion, so the call raises an exception and
the call returns 0.

Thanks,
Stefan
 
S

Stefan Seefeld

Terry said:
It is easy to mess up in C ;-). You are most likely to get an answer (from
another C extension writer) if you post a short as possible code snippet
that exhibits the error, and the actual and complete error message.

I found the problem, and I'd like to share my new insight, as I don't
think it is obvious:

I hold an object reference that is a dictionary, and I modify it with
the PyDict_* family of functions.

However, the real object is not a builtin 'dict', but instead it is
derived from it:

class Dictionary(dict):
...

I just realized that PyDict_SetItem (et al.) don't lookup the
methods in the object's __dict__, so the method that gets really
executed is not the overloaded I wrote, but the method from the
base class.

Replacing 'PyDict_SetItem' by 'PyObject_SetItem' did the trick.
I guess 'PyDict_SetItem' is a shortcut to circumvent the lengthy
method lookup. Fair enough, but the python C API documentation could
be a bit more clear about that...

Kind regards,
Stefan

PS: for the curious: the missing link between the dict and
the list / tuple issue is that I derived my own dictionary
precisely to be able to use a list as key.
 
T

Thomas Heller

Stefan Seefeld said:
I found the problem, and I'd like to share my new insight, as I don't
think it is obvious:

I hold an object reference that is a dictionary, and I modify it with
the PyDict_* family of functions.

However, the real object is not a builtin 'dict', but instead it is
derived from it:

class Dictionary(dict):
...

I just realized that PyDict_SetItem (et al.) don't lookup the
methods in the object's __dict__, so the method that gets really
executed is not the overloaded I wrote, but the method from the
base class.

Replacing 'PyDict_SetItem' by 'PyObject_SetItem' did the trick.
I guess 'PyDict_SetItem' is a shortcut to circumvent the lengthy
method lookup. Fair enough, but the python C API documentation could
be a bit more clear about that...

I would guess the *real* problem is that you are ignoring the return
value of PyDict_SetItem (or whatever api you use). In this case you get
weird errors afterwards.

Thomas
 
S

Stefan Seefeld

Thomas said:
I would guess the *real* problem is that you are ignoring the return
value of PyDict_SetItem (or whatever api you use). In this case you get
weird errors afterwards.

good shot ! Well, what the 'real' problem is is probably a matter
of perspective, but you are quite right in that I failed to check
a return value (and associated exception) so python's own code
eventually bumped into it in an unrelated context.

But back to the other issue: isn't there any text that explains
when I should be using PyDict_GetItem and when PyObject_GetItem
and what they are doing byhind the scene ?

Regards,
Stefan
 
T

Thomas Heller

Stefan Seefeld said:
good shot ! Well, what the 'real' problem is is probably a matter
of perspective, but you are quite right in that I failed to check
a return value (and associated exception) so python's own code
eventually bumped into it in an unrelated context.

And that will always catch you!
But back to the other issue: isn't there any text that explains
when I should be using PyDict_GetItem and when PyObject_GetItem
and what they are doing byhind the scene ?

The PyObject_GetItem() function belongs to the Abstract Objects Layer.
<http://docs.python.org/api/abstract.html>
The docs say:

6. Abstract Objects Layer

The functions in this chapter interact with Python objects regardless
of their type, or with wide classes of object types (e.g. all
numerical types, or all sequence types). When used on object types for
which they do not apply, they will raise a Python exception.

while PyDict_GetItem() belongs to the Concrete Objects Layer for
PyDictObject <types http://docs.python.org/api/concrete.html>:

7. Concrete Objects Layer

The functions in this chapter are specific to certain Python object
types. Passing them an object of the wrong type is not a good idea; if
you receive an object from a Python program and you are not sure that
it has the right type, you must perform a type check first; for
example, to check that an object is a dictionary, use
PyDict_Check(). The chapter is structured like the ``family tree'' of
Python object types.

Reading the include files may also help - once you get the idea it's simple.

Thomas
 
S

Stefan Seefeld

7. Concrete Objects Layer

The functions in this chapter are specific to certain Python object
types. Passing them an object of the wrong type is not a good idea; if
you receive an object from a Python program and you are not sure that
it has the right type, you must perform a type check first; for
example, to check that an object is a dictionary, use
PyDict_Check(). The chapter is structured like the ``family tree'' of
Python object types.

yeah, I did read that. The inconsistency (or even error, I would argue) is
in the interpretation of 'is a'. PyDict_Check reports 'true' when called
on my 'Dictionary' object, yet the PyDict_* API only applies to the
slice representing the base class. I don't think that is correct, it is
at least very surprising.

The API only supports two extremes:

* the object's type is an *exact* match for a built-in type, thus a highly
optimized API can be used to access its methods.

* the object's type is unknown, it may or may not support a certain 'protocol',
i.e. for all method invocations a full method lookup has to be done.

That sounds good for the pre 2.2 world, where built-in types and user-defined
types were distinct sets.

Regards,
Stefan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,197
Messages
2,571,041
Members
47,643
Latest member
ashutoshjha_1101

Latest Threads

Top