Steve O'Neill
Steve O'Neill

Reputation: 95

Functions defined in Cython .pyx become built-in?

I was playing with the cython tutorial (http://docs.cython.org/en/latest/src/tutorial/cython_tutorial.html)

And I have a directory like so:

somedir/ExampleCython.py
somedir/fib.pyx
somedir/fib.so
somedir/function_file.py
somedir/setup.py

Where the fib.pyx defines the function fib from the cython tutorial linked above, function_file.py contains the following definition:

def moopy(param):
    param += 1
    return param

and ExampleCython.py contains the following:

import fib as moo

print moo.fib

from function_file import moopy

print moopy

I build the fib.so using python setup.py build_ext --inplace, then I run python ExampleCython.py and I get the following output:

<built-in function fib>
<function moopy at 0x1002b4398>

Now, I know there is no python built-in called fib, and just to test I tried changing the name 'fib' to something silly like 'poopypants' and it still says it's built-in. What is happening to the namespace of the fib function? I have this issue in a much larger block of code elsewhere which is preventing me from pickling a class I created - pickle complains that it can't find my function in the module it belongs to - but I figured I'd simplify it for the question here.

For reference - python 2.7.11/cython 0.26 on OSX 10.12.6

Upvotes: 2

Views: 466

Answers (1)

DavidW
DavidW

Reputation: 30915

This was solved in a comment but posted as an answer for completeness:

Python is describing the functions as "built-in function" purely to mean that it's compiled in C, rather than meaning that it's a standard library function, and so there was issue with this - the correct function was being found in the correct module.

On my PC it appears as <cyfunction module.function_name at 0xsome_address> which is a more helpful message. I think this might be a Python 3 improvement (but I'm not 100% sure).

Upvotes: 2

Related Questions