On Nov 25, 2009, at 1:55 AM, Joachim Saul wrote:
> Robert Bradshaw:
>> On Nov 24, 2009, at 4:25 PM, Lisandro Dalcin wrote:
>>
>>> On Tue, Nov 24, 2009 at 7:47 PM, Joachim Saul <[email protected]>
>>> wrote:
>>>> Indeed, Pyrex compatibility is a requirement for me. I want to use/
>>>> test
>>>> the Cython-generated code but need to be able to revert to Pyrex in
>>>> case
>>>> of problems.
>>> What kind of problems are you worried about? Could you elaborate on
>>> this?
>>
>> I'm curious too.
>
> "in case of problems" - I haven't encountered any so far but last time
> when I tried a couple months ago I did run into issues that forced
> me to
> postpone the migration from Pyrex to Cython. Now with 0.12 things have
> improved quite considerably. Yet it makes me sleep better if during
> the
> transition I can translate the codes with both Pyrex and Cython, Pyrex
> being the fallback.
Sure, that makes sense. We certainly try to target multiple C compilers.
>>>> Anyway, what I have done is
>>>>
>>>> def foo(bytes bar=bytes("")):
>>>> c_foo(bar)
>>>>
>>> Can you try this: ?
>>>
>>> def foo(bytes bar=b""):
>>> c_foo(bar)
>>
>> I could be wrong, but I don't think Pyrex supports the b prefix on
>> strings.
>
> That's the point.
>
>> BTW, it be easier to just do
>>
>> def foo(bar=""):
>> c_foo(bar)
>
> True, but the resulting code then lacks the static typing I want here
> for consistency with the rest of the code. Plus, as pointed out by
> Stefan Behnel, it is not portable to Python 3.
Nor is
def foo(bytes bar=b""):
...
unless you actually expect users to be passing bytes objects (It will
reject unicode objects, which string literals will be in Py3).
Suggesting that people use the bytes type instead of the string type
is probably not a good idea, as the bytes type is so different in Py3
and not usually what people want.
> I would assume that in addition to None, any value that is
> acceptable as
> argument to bytes() should also be acceptable as a default value for a
> bytes-type parameter, following the Rule of Least Surprizeā¢.
That's not the case, with Pyrex or with Cython. Actually, bytes("") is
illegal in Python 3 (because you have to explicitly give an encoding).
I think this is similar to how one can't do
range("5")
even though range expects and int and int has a constructor from a
string.
> This would consequently also allow code such as
>
> def foo(bytes bar=5):
>
> unless forbidden in Python 3.
>
> I am not yet very much into Cython and much less the internals, so
> this
> is just from a rather naive user's perspective, and I recognize that
> there may very well be points speaking against it.
There are two options for what you want to do here:
(1) Target Python 2 only (Pyrex forces this) and assume all your
strings are ASCII only or already in the correct encoding for the C
library calls. Use str everywhere, just as you have been doing (though
without pre-declaring it as an extern definition, which I don't think
is necessary in Pyrex either). You can still use str in Python 3, it's
just that str -> char* will not happen automatically.
(2) Explicitly and manually handle all encoding/decoding every time
you want to pass from a Python string to a C string, as in
http://wiki.cython.org/FAQ#HowdoIpassaPythonstringparameterontoaClibrary.3F
In either case, we should be handling __builtin__ redeclarations more
gracefully.
- Robert
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev