ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> cpp-sig
cpp-sig
[C++-sig] Re: boost.python answer - thanks!
by Mark Higgins other posts by this author
Feb 25 2003 5:50AM messages near this date
[C++-sig] Re: boost.python answer - thanks! | [C++-sig] Re: C++ object from PyObject pointer
Yup, defining

double Passer::val()
{
   object ret = m_f( m_x );
   return extract<double> ( ret );
}

works like a charm. Thanks! I didn't realise the boost API was
as clean and complete as it apparently is.


--- David Abrahams <dave@[...].com>  wrote:
>  "mghiggins <mghiggins@[...].com>" <mghiggins@[...].com>
>  writes:
>  
>  > Thanks, that helped a lot!
>  
>  As I asked you before, please post your Boost.Python questions
>  to the
>  C++-sig.
>  
>  > Another perhaps more basic question: how do I pass a python
>  function
>  > object to a C++ method and use it internally? e.g., if I
>  have a class
>  >
>  > class Passer
>  > {
>  >  public:
>  >    Passer() {};
>  >    void setx( dict x ) { this->m_x = x; };
>  >    void setf( object f ) { this->m_f = f; };
>  >    double val();
>  >    double xval() { return extract<double>( m_x['v'] ); };
>  >
>  >  private:
>  >    dict   m_x;
>  >    object m_f;
>  > };
>  >
>  > double Passer::val()
>  > {
>  >    PyObject * f   = m_f.ptr();
>  >    PyObject * ret = PyEval_CallObject( f, m_x.ptr() );
>  
>  
>  why not just
>  
>       object ret = m_f(m_x);
>  
>  ??
>  
>  >    double RetVal;
>  >    if( ret )
>  >       RetVal = 1;
>  >    else
>  >       RetVal = -1;
>  >
>  >    PyArg_Parse( ret, "(f)", &RetVal );
>  >    return RetVal;
>  > }
>  
>  I don't know what your PyArg_Parse thing is supposed to be
>  doing. I'm
>  not really sure what all of that 1/-1 stuff is about either. 
>  Is
>  RetVal a tuple of one element?  Maybe you want:
>  
>   double Passer::val()
>   {
>       return m_f(m_x)[0];
>   }
>  
>  > The setx method seems to work fine with a dictionary like
>  {'v':4} and
>  > the xval method returns its 'v' element properly.
>  >
>  > But the setf method doesn't seem to work correctly... when I
>  call
>  > val() from python after setting on a function, PyObject *
>  ret comes
>  > back NULL (which I learn from getting RetVal == -1 as a
>  return value).
>  
>  That might just mean that f raised a Python exception.  You're
>  masking the problem by returning -1 instead of using the
>  Boost.Python
>  facilities which propagate the Python exception through your
>  C++ code
>  and back to Python.
>  
>  > I'm sure I'm doing something dumb by making the type of
>  setf's arg be
>  > "object" rather than something specifying a function, but I
>  don't know
>  > what. :) Anything obvious I'm missing here?
>  
>  The only dumb-ish thing is using the raw Python 'C' API
>  instead of
>  what Boost.Python gives you.  Try the simple approach I
>  outlined
>  above; it might be revealing.
>  
>  -- 
>  Dave Abrahams
>  Boost Consulting
>  www.boost-consulting.com
>  


=====
-------------------------------------------------------
Mark Higgins, mghiggins@[...].com
http://www.molala.net

Opinions expressed herein do not reflect my own views;
I haven't had free will since last year when aliens ate
my brain.

__________________________________________________
Do you Yahoo!?
Yahoo! Tax Center - forms, calculators, tips, more
http://taxes.yahoo.com/

_______________________________________________
C++-sig mailing list
C++-sig@[...].org
http://mail.python.org/mailman/listinfo/c++-sig
Thread:
David Abrahams
Mark Higgins

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved