[SciPy-user] [weave] newbie : compilation issue
Eric Jones
eric at enthought.com
Sun Sep 21 02:45:34 EDT 2003
Hey JBR,
The docs are out of date compared to the CVS. I don't have all your source to check against, but I would expect the following is fairly close to what you want. Weave sould do all the work translating Python variables to C/C++ for you, at least in the case of "standard" argument types which you are using, so the PyObject* stuff you have in your code isn't necessary.
If the code below doesn't work, attach the C files your using and I see what I can do to help out.
eric
from weave import ext_tools
def build_date():
"""contruit le module d'extension
"""
module = ext_tools.ext_module('date_ext')
#fonction de positionnement dans un fichier
"""
positionne_flot a 4 arguments, un p_pointeur de fichier en lecture,
un p_pointeur de fichier en écriture (log), un caractère et une
date (double)
"""
fichier_date_c = open('date.c','r')
code_date_c = fichier_date_c.read()
# arguments
fichier_src = fichier_date_c
fichier_log = open('log.log','w')
pdt = 'a'
date = 1.23
ext_code = "return_val = positionne_flot(&p_file,pdt[0],date,&p_log);"
fonction = ext_tools.ext_function('c_positionne_flot',ext_code,['fichier_src','pdt','date','fichier_log'])
fonction.customize.add_support_code(code_date_c)
module.add_function(fonction)
module.compile()
try:
import date_ext
except ImportError:
build_date()
import date_ext
Jean-Baptiste Richet - AREAS <jb.richet.areas at normandnet.fr> wrote ..
> Hi,
>
> I'm trying to have weave take my C functions into Python. I try to build
> a module with ext_tools, but I don't know much about writing Python
> extensions (actually, nothing). I just try to guess how to do from the
> tutorial and its examples. But of course it doesn't work.
> First, let me give you the error message:
> =============================
> jb at areas:~/routines/commun$ python ext_date.py
> /home/jb/routines/commun/date_ext.cpp: In function `struct PyObject *
> c_positionne_flot(PyObject *, PyObject *, PyObject *)':
> /home/jb/routines/commun/date_ext.cpp:925: `raw_locals' undeclared
> (first use this function)
> /home/jb/routines/commun/date_ext.cpp:925: (Each undeclared identifier
> is reported only once
> /home/jb/routines/commun/date_ext.cpp:925: for each function it appears
> in.)
> /home/jb/routines/commun/date_ext.cpp:925: `raw_globals' undeclared
> (first use this function)
> /home/jb/routines/commun/date_ext.cpp:925: implicit declaration of
> function `int get_variable(...)'
> Traceback (most recent call last):
> File "ext_date.py", line 50, in ?
> build_date()
> File "ext_date.py", line 45, in build_date
> module.compile()
> File "/usr/local/lib/python2.3/site-packages/weave/ext_tools.py", line
> 349, in compile
> verbose = verbose, **kw)
> File "/usr/local/lib/python2.3/site-packages/weave/build_tools.py",
> line 194,
> in build_extension
> setup(name = module_name, ext_modules = [ext],verbose=verb)
> File "/usr/local/lib/python2.3/distutils/core.py", line 166, in setup
> raise SystemExit, "error: " + str(msg)
> weave.build_tools.CompileError: error: command 'gcc' failed with exit
> status 1
> jb at areas:~/routines/commun$
> ====================================
>
> Maybe I lack some #include or something like that ?
> The line :
> PyObject* py_file = get_variable("fichier_src",raw_locals,raw_globals);
> from which the error is raised, was found in the weave tutorial about file
> conversions. I can hardly understand what it does.
> Here is the code at the bottom of the message.
> [I hesitate to give the date.c code, or the date_ext.cpp code, it's a bit
> long, and there's no compile error from it. If you need it to help me,
> please tell me.]
>
> thanks for your help,
> JBR
>
> ================ ext_date.py =========================
> # -*- coding: UTF-8 -*-
>
> #test d'utilisation de weave pour créer une bibliothèque
>
> from weave import ext_tools
>
> fichier_date_c = open('date.c','r')
> code_date_c = fichier_date_c.read()
>
> fichier_src = fichier_date_c
> fichier_log = open('log.log','w')
> pdt = 'a'
> date = 1.23
> def build_date():
> """contruit le module d'extension
> """
> module = ext_tools.ext_module('date_ext')
>
> #fonction de positionnement dans un fichier
> """
> positionne_flot a 4 arguments, un p_pointeur de fichier en lecture,
> un p_pointeur de fichier en écriture (log), un caractère et une
> date (double)
> """
> ext_code = """
> PyObject* py_file = get_variable("fichier_src",raw_locals,raw_globals);
> FILE* file = py_to_file(py_file,"fichier_src");
> PyObject* py_log = get_variable("fichier_log",raw_locals,raw_globals);
> FILE* log = py_to_file(py_log,"fichier_log");
>
> FILE** p_file = &file;
> FILE** p_log = &log;
>
> char c_pdt = std::string(pdt).c_str()[0];
>
> int resultat;
>
> resultat = positionne_flot(p_file,c_pdt,date,p_log);
>
> return_val = Py::new_reference_to(Py::Int(resultat));
> """
> fonction = ext_tools.ext_function('c_positionne_flot',ext_code,['fichier_src','pdt','date','fichier_log'])
> fonction.customize.add_support_code(code_date_c)
> module.add_function(fonction)
> module.compile()
>
> try:
> import date_ext
> except ImportError:
> build_date()
> import date_ext
> =============================================
>
> --
> Jean-Baptiste Richet
> AREAS
> _________________________________
> Association régionale pour l'étude
> et l'amélioration des sols
> 2, avenue Foch
> 76460 Saint Valéry en Caux
> tél : +33 2 35 97 25 12
> fax : +33 2 35 97 25 73
> _________________________________
>
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.net
> http://www.scipy.net/mailman/listinfo/scipy-user
More information about the SciPy-User
mailing list