Menu

#3 C to C call doesn't work

open
nobody
None
5
2004-07-30
2004-07-30
Anonymous
No

A call from a C function to another raises an error.

import PyInline
pyin = PyInline.build(code = """
#include <stdio.h>
double f(double x){return x;}
double g(double x){return f(x);}
""", language = "C")
print pyin.g(0.1)

gives

Traceback (most recent call last):
File "testpyin.py", line 18, in ?
""", language = "C")
File "PyInline\__init__.py",
line 38, in build
return b.build()
File "PyInline\C.py", line 40
, in build
self._parse()
File "PyInline\C.py", line 92
, in _parse
d['params'] = self._parseParams(d['rawparams'])
File "PyInline\C.py", line 11
0, in _parseParams
return [self._parseParam(p) for p in rawparams]
File "PyInline\C.py", line 11
8, in _parseParam
raise BuildError("Error parsing parameter %s" % p)
PyInline.BuildError: Error parsing parameter x

Discussion

  • Nobody/Anonymous

    Logged In: NO

    A call to C standard functions causes the same error:

    import PyInline
    pyin = PyInline.build(code = """
    #include <stdlib.h>
    double f(double x)
    {
    double y;
    y = x * atof("1.1"); /* error */
    /* y = x * atof("1.1") + 0.0; */ /* works */
    /* y = atof("1.1") * x; */ /* works */
    /* y = atof("1.1"); */ /* works */
    /* y = 0.0 + atof("1.1"); */ /* works */

    /\* return atof\("1.1"\); \*/ /\* error \*/
    return y;
    

    }
    """, language = "C")
    print pyin.f(0.1)

    It seems that c_function_def in c_util.py has a bug of
    detecting function defs incorrectly, so the work-around is
    to avoid something like "* f(...);" and "return f(...);".

    Hope this helps...

     
  • Xavier Combelle

    Xavier Combelle - 2004-08-31

    Logged In: YES
    user_id=1105764

    The point is that the return line should be like
    return z

    the folowwing code work perfectly:

    import PyInline
    pyin = PyInline.build(code = """
    #include <stdio.h>
    double f(double x){return 2*x;}
    double g(double x){double z = f(x); return z;}
    """, language = "C")
    print pyin.g(0.1)

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.