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

The __unicode__ method in the C API, and other __-methods

I made a bridge between my language Kogut
<http://qrnik.knm.org.pl/~qrczak/kogut/kogut.html> and Python,
which gives my language instant access to Python libraries.

There is a minor annoyance in the C API: the __unicode__ method does not
have its own slot in type objects, it's not treated specially like __str__.

/* XXX As soon as we have a tp_unicode slot, we should
check this before trying the __unicode__
method. */
if (unicodestr == NULL) {
unicodestr= PyString_InternFromString(
"__unicode__");
if (unicodestr == NULL)
return NULL;
}
func = PyObject_GetAttr(v, unicodestr);

Strings in Kogut are Unicode only. Thus on one hand I must use the
equivalent of the above code when I want to convert an arbitrary Python
object to a Python Unicode object, and on the other hand the implementation
of tp_getattro of Kogut objects wrapped in Python has a special case,
only one special case, which checks for "__unicode__" and returns the
appropriate convert-to-Unicode bound method instead of forwarding the
attribute access to the original object.

Could __unicode__ have its slot in the C API in future Python versions?
This is the only missing method of this kind I suppose.

* * *

Unless I count iteritems.

The problem is that in Kogut iteration over a dictionary gives key-value
pairs, where in Python it defaults to iterating over keys. The standard
dict type provides iteritems, which does not have a C API equivalent.

Currently iterating over a Python dictionary from Kogut produces key-value
pairs, because this is what Kogut expects - I call iteritems through
PyObject_GetAttr.

Iterating over a Kogut dictionary from Python is somewhat wrong, because
it blindly returns what the Kogut iterator produces, that is: pairs
(and they even can't be automatically converted to Python pairs, unless
I treat this case specially). I think I will fix it and return keys only
when the Kogut object is a dictionary.

* * *

Maybe the wrapped objects should support all these __-methods. I mean, for
example len(obj) works (it calls the C slot which calls the Size function
in Kogut), but obj.__len__ doesn't work (it tries to blindly access the
attribute named __len__ in Kogut, which doesn't exist).

Is that wrong? Does Python code assume that len(obj) is equivalent to
obj.__len__()?

Improving this would be tedious, because I would have to make bound
methods myself. I can't use PyObject_GenericGetAttr because I must
forward regular attribute lookup to Kogut, so I would probably have
to code everything by hand.

Also I don't known which methods are available without trying to call them,
so all objects would have the __len__ slot, with for non-collection
objects throws an exception when applied.

--
__("< Marcin Kowalczyk
\__/ qr****@knm.org.pl
^^ http://qrnik.knm.org.pl/~qrczak/

Jul 18 '05 #1
0 1184

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

Similar topics

2
by: Florian Preknya | last post by:
Is there a posibility to overwrite a private method (one that starts with __ ) ? I read that they are just formely private, they are prefixed with the class name to have an obtured visibility, so...
2
by: | last post by:
I need to find out dynamically if an object is instance of a Class but I don't know how to use this isinstance buit-in function. I have the object and the name of the class, what should I do? ...
6
by: j_mckitrick | last post by:
I've done this before: data = for k in self.cong.tm.li] #li is list, tm is dict instead of: for k in self.cong.tm.li: data.append(self.cong.tm)
18
by: Karl Pech | last post by:
Hi, I got a task there I have to compute pi using the Method above. So I wrote the following program: --- import random import math
1
by: martijn | last post by:
H! I'm busy with testing python and now i'm trying to check if a url makes a forward to a other location with the same content. So it will be possible to scan unique website's. I already made...
2
by: Felix Finch | last post by:
I am a self taught SQL novice, possibly beneath contempt :-) Two questions here. One is how to define two tables whose columns reference each other, the other is whether trying to do this means...
19
by: Roman Mashak | last post by:
Hello, All! I often meet that '_' or '__' is used as prefix to functions/macros/variables names. I wonder does it have some strict meaning? I didn't find any distinct explanation in C standard...
36
by: danielx | last post by:
At first I was going to post the following: <!-- beginning of my original post --> I just discovered the inspect module, which contains the isfunction and ismethod functions. For some reason,...
18
by: g.ankush1 | last post by:
I have seen many variables or structures declared as _ or __ prefixed . Can anyone explain the significance of _ or __ particularly . I mean , I wanted to know the convention for using _ and __ .
3
by: Manlio Perillo | last post by:
Hi all. I have just noticed that exception objects does not handle well Unicode arguments. Traceback (most recent call last): File "<stdin>", line 1, in ? UnicodeEncodeError: 'ascii' codec...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...

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.