Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   python 3 problem: how to convert an extension method into a classMethod (http://www.velocityreviews.com/forums/t958100-python-3-problem-how-to-convert-an-extension-method-into-a-classmethod.html)

Robin Becker 02-26-2013 05:21 PM

python 3 problem: how to convert an extension method into a classMethod
 
In python 2 I was able to improve speed of reportlab using a C extension to
optimize some heavily used methods.

so I was able to do this


class A:
.....
def method(self,...):
....


try:
from extension import c_method
import new
A.method = new.instancemethod(c_method,None,A)
except:
pass

and if the try succeeds our method is bound as a class method ie is unbound and
works fine when I call it.

In python 3 this doesn't seem to work at all. In fact the new module is gone.
The types.MethodType stuff doesn't seem to work.

Is there a way in Python 3.3 to make this happen? This particular method is
short, but is called many times so adding python wrapping layers is not a good
way forward.

If the above cannot be made to work (another great victory for Python 3) then is
there a way to bind an external method to the instance without incurring too
much overhead.

Alternatively could it make sense to implement an accelerated basetype that just
contains the accelerated methods of class A. I could then imagine doing
something like

try:
from extension import class c_baseA as baseA
except:
class baseA:
def method(....)

class A(baseA):
.....

presumably I then get some kind of penalty for the base class lookup, but how
bad is that?
--
Robin Becker


Steven D'Aprano 02-27-2013 04:46 AM

Re: python 3 problem: how to convert an extension method into aclassMethod
 
On Tue, 26 Feb 2013 17:21:16 +0000, Robin Becker wrote:

> In python 2 I was able to improve speed of reportlab using a C extension
> to optimize some heavily used methods.
>
> so I was able to do this
>
>
> class A:
> .....
> def method(self,...):
> ....
>
>
> try:
> from extension import c_method
> import new
> A.method = new.instancemethod(c_method,None,A)
> except:
> pass


Why are you suppressing and ignoring arbitrary errors here? That doesn't
sound good. Surely a better way would be:

import new
try:
from extension import c_method
except ImportError:
pass
else:
A.method = new.instancemethod(c_method, None, A)



> and if the try succeeds our method is bound as a class method ie is
> unbound and works fine when I call it.
>
> In python 3 this doesn't seem to work at all. In fact the new module is
> gone. The types.MethodType stuff doesn't seem to work.


I've never tried this with a function written in C, but for one written
in Python all you need is this:

A.method = c_method


Try that and see if it works with your function written in C. I expect
that it will, provided that the function is written as a method
descriptor. I don't know enough about C extensions to tell you how to do
that, sorry.



--
Steven


All times are GMT. The time now is 10:23 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57