Thanks Robert, I hope colloquium tea at UW still brings good food.
When you say I was "declaring a new variable", I think you're referring to
cdef float use_fun_ptr(float, float, float (float, float))
Right? I was declaring function in the same way that variables are
declared at the top of c-code. This "function prototype" isn't
necessary always, but it doesn't hurt, does it? (I'm a novice)
As for the compile error, I have python 2.5 and cython 0.9.6.14-1 on
my Ubuntu Intrepid machine. The following block of code gives me the
following error (code, error, and setup.py all pasted below).
--------------------------------- example_cython.pyx
--------------------------
# This block will not compile
cdef float use_fun_ptr(float a, float b, float (*pt2Func) (float, float)):
cdef float result
result = pt2Func(a,b)
return result
---------------------------------- compile error
------------------------------------
$ python setup.py build_ext --inplace
running build_ext
cythoning example_cython.pyx to example_cython.c
Traceback (most recent call last):
File "setup.py", line 12, in <module>
ext_modules = [Extension("example_cython", ["example_cython.pyx"])]
File "/usr/lib/python2.5/distutils/core.py", line 151, in setup
dist.run_commands()
File "/usr/lib/python2.5/distutils/dist.py", line 974, in run_commands
self.run_command(cmd)
File "/usr/lib/python2.5/distutils/dist.py", line 994, in run_command
cmd_obj.run()
File "/usr/lib/python2.5/distutils/command/build_ext.py", line 290, in run
self.build_extensions()
File
"/var/lib/python-support/python2.5/Cython/Distutils/build_ext.py", line
81, in build_extensions
ext.sources = self.cython_sources(ext.sources, ext)
File
"/var/lib/python-support/python2.5/Cython/Distutils/build_ext.py", line
196, in cython_sources
full_module_name=module_name)
File "/var/lib/python-support/python2.5/Cython/Compiler/Main.py", line
316, in compile
return context.compile(source, options, full_module_name)
File "/var/lib/python-support/python2.5/Cython/Compiler/Main.py", line
213, in compile
tree.process_implementation(scope, options, result)
File
"/var/lib/python-support/python2.5/Cython/Compiler/ModuleNode.py", line
48, in process_implementation
self.analyse_declarations(env)
File
"/var/lib/python-support/python2.5/Cython/Compiler/ModuleNode.py", line
45, in analyse_declarations
self.body.analyse_declarations(env)
File "/var/lib/python-support/python2.5/Cython/Compiler/Nodes.py",
line 1046, in analyse_declarations
name_declarator, type = self.declarator.analyse(base_type, env,
self.body is not None)
File "/var/lib/python-support/python2.5/Cython/Compiler/Nodes.py",
line 480, in analyse
name_declarator, type = arg_node.analyse(env, nonempty = nonempty)
File "/var/lib/python-support/python2.5/Cython/Compiler/Nodes.py",
line 580, in analyse
return self.declarator.analyse(base_type, env, nonempty = nonempty)
File "/var/lib/python-support/python2.5/Cython/Compiler/Nodes.py",
line 480, in analyse
name_declarator, type = arg_node.analyse(env, nonempty = nonempty)
File "/var/lib/python-support/python2.5/Cython/Compiler/Nodes.py",
line 580, in analyse
return self.declarator.analyse(base_type, env, nonempty = nonempty)
File "/var/lib/python-support/python2.5/Cython/Compiler/Nodes.py",
line 390, in analyse
self.name = base_type.name
AttributeError: CFloatType instance has no attribute 'name'
---------------------------------- setup.py
---------------------------------------------
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
setup(
cmdclass = {"build_ext": build_ext},
ext_modules = [Extension("example_cython", ["example_cython.pyx"])]
)
-------------------------------------------------------------------------------------------------
Thanks,
Ian
Robert Bradshaw wrote:
On Thu, 27 Aug 2009, Ian Langmore wrote:
I'd like to pass function pointers as in e.g. c. In python of course
function pointers are automatically passed. I can't find documentation
on this. I tried both c and python-like syntax and neither worked.
Below is my "c-syntax" (failed) attempt.
------------------------------------------------------------------------------------------------------------
cdef float Plus (float a, float b):
return a+b
############
# This block will not compile
cdef use_fun_ptr(float a, float b, float (*pt2Func) (float, float)):
cdef float result
result = pt2Func(a,b)
return result
############
Works for me, what version of Cython are you using?
def main():
cdef float use_fun_ptr(float, float, float (float, float))
Here you're declaring a new variable, are you sure that's what you want to
do?
# This line works
print "Using Plus directly, " + str(Plus(2,5))
#
print "Using use_fun_ptr, " + str( use_fun_ptr(2, 5, &Plus) )
------------------------------------------------------------------------------------------------------------
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev
--
******************************************************************
Ian Langmore
Postdoctoral Researcher, Columbia Univ. Applied Mathematics
415-272-6321
www.columbia.edu/~il2176
******************************************************************
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev