I have done some work towards this in the hg tip, my solution was to use exec to define a raise_ global function that is compatible on both versions.
Try out my branch, I haven't done much work lately but I got quite a way with support. I had a compat module that had a big bunch of global fixes.
Have a look at https://bitbucket.org/damoxc/gevent-py3/overview, I need to sync it up to the latest code in Denis' tree but I got quite a way with making it compatible. I'm thinking that maybe a fresh solution using 2to3 might result in cleaner code though. The major issue I had is byte strings, a lot of the network related code only accepts byte strings. All the import issues can be resolved by try/except ImportError...
If you're willing to help then give it a look. Some of my work has been merged upstream. I'll resync my branch when I get home later today.
I agree with the 2to3 solution, I was mostly just playing around to
see what was involved to get a better grips and what gotchas there
were for the future.
>
> **I'm thinking maybe all strings should be bytes inside gevent?**
> **Could alias bytes to str in Python 2 and change all instances?**
>
That's already the case in Python 2.6+. The problem is the bytes
method in Python3 expects an encoding parameter which the str() method
in Python2 doesn't which is nuisance. Would we be able to add a custom
fixer in 2to3 that converts all normal strings to byte strings, and
any unicode strings to strings?
Yes it's fine, in Python 3 the except: does the same thing as sys.exc_clear().
>> > **I'm thinking maybe all strings should be bytes inside gevent?**
>> > **Could alias bytes to str in Python 2 and change all instances?**
>>
>> That's already the case in Python 2.6+. The problem is the bytes
>> method in Python3 expects an encoding parameter which the str() method
>> in Python2 doesn't which is nuisance. Would we be able to add a custom
>> fixer in 2to3 that converts all normal strings to byte strings, and
>> any unicode strings to strings?
>
> I think I need to look at the full range of usage to understand this
> better.
> I thought that if we treated incoming as *already* bytes no encoding
> was needed.
> I'm referring to the bytes type, not a function.
> When I say alias bytes to str what I mean is the reverse of the
> default.
> In normal 2to3 usage all Python 2 (bytes) str are treated as unicode
> str in Py3k.
> I want to treat Python 2 str as Py3k bytes type.
> I think an encoding is only needed when swapping between bytes/str.
> I'm pretty unsure of all this right now so I'll start looking.
>
Yes but when strings are defined in Python3 they are str by default,
not bytes. So we'll need all strings in the gevent source converted to
b'foo' instead of 'foo', or 'foo' instead of u'foo'
>
> I've currently seem to have a working Py3k core, hub and with the code
> from
> your repo, a socket.py as well. I think I need to have a working
> queue.py next.
>
queue.py might be a tricky one if I remember correctly, certainly one
module was as you can't do comparisons between objects of different
types in Python 3 e.g. 3 < '3' doesn't work.
> I'm still floundering with the complexity of the gevent build.
> I'd appreciate it if you can nominate a 'bottom up' order for building
> the source files.
> I'm testing manually so I need to convert python source files one at a
> time,
> and I can't add multiple dependencies at one time, if you see what I
> mean.
> I think I can add queue to core hub and socket without unsatisfied
> dependencies,
> can you give me a the next few source files to address?
> Like I said, I don't understand the build. I'm not up to speed on this
> advanced package management.
I think pywsgi can be done without too much difficultly.
event.py looks like a candidate as well.
after that baseserver.py, followed by server.py
Excellent, I'll try and get around to testing this out on Arch using
3.2.x later on, cython seems to be okay for 2.x on Arch too, cython
--version says 0.15.
> In theory these commits should be good.
> A clone of anil_g/gevent should be able to prove it builds in Python 2
> anywhere the current denis/gevent tip builds.
>
> Commits are:
> 1. Add 2to3 call in setup.py to enable Py3k builds.
> 2. Provide custom fixer in util. Needs to be manually installed to
> work.
I'll have a look at adding a fix_strings custom fixer, we'll have to
disable the unicode fixer and then the replacement fixer will do both
jobs
> 3. Provided your socket.py changes to simultaneously support Py3k.
> 4. Updated util/cython_ifdef.py usage to simultaneously support Py3k /
> Python 2.
>
Could also be achieved by using a setuptools build task and running it
through 2to3 manually, might be a bit cleaner?
It is, it's not called greenlet though if memory serves,
py.magic.greenlet or something along those lines. The gevent greenlet
module just contains wrapper classes around the greenlet class it
imports from the external module. And you are correct, the last
release of the external module doesn't work with Python 3.2, however
hg tip does :-)
>> I'll have a look at adding a fix_strings custom fixer, we'll have to
>> disable the unicode fixer and then the replacement fixer will do both
>
> Sounds like that could do an awesome amount of work if it works.
> I've not really looked at the string usage in gevent yet.
> I thought gevent is like a 'carrier' and never needs to actually
> refer to the string content.
> E.g. It should never need to know the encoding.
>
It doesn't, and it shouldn't, which is why mostly all strings want to
be converted to byte strings, b'foo' instead of 'foo'.
>> > 3. Provided your socket.py changes to simultaneously support Py3k.
>> > 4. Updated util/cython_ifdef.py usage to simultaneously support Py3k /
>> > Python 2.
>> Could also be achieved by using a setuptools build task and running it
>> through 2to3 manually, might be a bit cleaner?
>
> cython_ifdef.py needs to be dual version because it's part of the
> build.
> Unless do extra work in setup to run it through 2to3 before calling,
> but I think it's dual compatible now anyway.
> I just found it easier that way.
> Since it's part of the build process it'll never be in production
> code.
>
> socket.py is put through 2to3 during build, as will every .py.
> Some things are just not handled by 2to3 fixers yet though.
> I haven't got a handle on custom fixers for advanced usage (yet?).
> You already had changes to socket.py and I wanted to test quickly.
> Maybe in some cases it may just be easier, quicker and maintainable
> to do *some* dual version coding.
>
Oh totally, there's a lot of things that should support dual version,
in my eyes fixers really should only be used when there is a backwards
incompatible change that needs to be made (such as b'foo').
I could be selective, I think pywsgi needed a lot of byte strings if I
recall correctly.
> 2. We may never need a b'' string fixer, maybe it's not that bad.
I've written one already in case we do ;-)
> 3. String handling in each python file is going to be different.
Agreed.
> 4. I feel I need to look at ares.pyx next for string / bytes handling.
> What do you think?
I've also figured out how integrate our own fixers into the build
process [1]. Getting the tests running properly will be another
important step I think.
I've just committed and pushed a bunch of changes to my fork [1] of
your repo if you'd like to take a look. Just a few compatibility fixes
and integrating the custom fixer into the setup process. echoserver.py
works when run through 2to3 now with those fixes.
A small step in the right direction ;-) I was contemplating starting
to write some tests that could be run via nose, instead of the custom
way gevent does it at the moment. But py3 support takes priority I
think.