Re: print representation for Cython methods in IPython 0.13

12 views
Skip to first unread message

Bradley Froehle

unread,
Oct 26, 2012, 12:59:35 PM10/26/12
to cython...@googlegroups.com
Hi John:

There's nothing to be done in Cython here.  As you point out, a.seed is a builtin (i.e. compiled) method and type(a.seed) returns `builtin_function_or_method` correctly.

This behavior is consistent with builtin Python objects.

In [24]: f = open('links.txt', 'r')

In [25]: f.read
Out[25]: <function read>

In [26]: type(f.read)
Out[26]: builtin_function_or_method

In [27]: print f.read
<built-in method read of file object at 0x25830c0>

Can you file a bug report with IPython?

-Brad

On Friday, October 26, 2012 8:50:47 AM UTC-7, John Palmieri wrote:
This is essentially quoted from a post to the IPython mailing list: http://mail.scipy.org/pipermail/ipython-dev/2012-October/010503.html.

We're running into a printing difference with Cython methods in the new IPython.  I think the problem boils down to: methods of Cython objects
are classed as "built in functions", even though their repr reveals more information.  So for Cython methods,

  https://github.com/ipython/ipython/blob/master/IPython/lib/pretty.py#L669

gets applied, rather than this line:

  https://github.com/ipython/ipython/blob/master/IPython/lib/pretty.py#L671.

Here is an example from numpy illustrating this, and there are also examples in Sage (like 4.gcd -- see http://trac.sagemath.org/sage_trac/ticket/12719):

In [1]: import numpy as np

In [2]: a=np.random.mtrand.RandomState()

In [3]: a.seed
Out[3]: <function seed>

In [4]: repr(a.seed)
Out[4]: '<built-in method seed of mtrand.RandomState object at 0x1004d93c0>'

In [5]: print a.seed
<built-in method seed of mtrand.RandomState object at 0x1004d93c0>

In [6]: type(a.seed)
Out[6]: builtin_function_or_method

In [7]: a.seed.__class__.__module__
Out[7]: '__builtin__'


Notice that str() and repr() of the Cython method gave much more useful information than the default built-in function printer.

How do we fix this? We could patch IPython, but maybe a better solution would be to change Cython so that its methods are not built in functions. Suggestions?

Thanks,

John

Reply all
Reply to author
Forward
0 new messages