Boost.python automatically converts the parameter

I use boost.python to port the C ++ 'A' class, which takes a string as a constructor. Then I have a function "fun (A & arg)" that references the parameter "A". I would like to have a python wrapper for "fun", which is such that if I pass in a variable that is a link to a python string, that link will first be automatically converted to a "A" link.

An example may help. On the python side, I would like to be able to do this:

a = 'some string'
fun(a)

and then have "a" actually (link to) "A" and not (link to) the source string. I want to do this because I would like it not to be written as

a = A('some string')
fun(a)

(You may have good reason to doubt that this is a relevant savings system, but let me assume that this is important to me).

Is this possible? If you do not use boost.python, perhaps directly using the Python-C API?

Note. I know that if I wrote

fun('some string')

There is no way to refer to the string to be converted to refer to any other type.

+3
source share
2 answers

It is possible, but the solution may depend on the implementation of Python.

For example, in Python 2.7 the module can inspectalso sys.settrace()be used to change locals()for a specific frame. This can also be done in the Python / C API, but Python frame management in Python is often much more manageable.

example.py update_locals() locals() :

import inspect
import sys


def _force_locals(old_frame, new_locals):
    ''' Force a new set of locals on a given frame. 

    :param old_frame: The frame to which locals will be applied.
    :type old_frame: frame.
    :param new_locals: What locals() should return for old_frame.
    :type new_locals: dict.

    .. note:: This function will force a custom trace function onto
              the old_frame.  Within the context of a trace function
              (often used for debugging), a frame f_locals is
              modifiable.  In most execution paths, f_locals is 
              writable but not modifiable.

    '''
    # Force tracing by setting the global tracing function to
    # any non-None function.  
    if not sys.gettrace():
        sys.settrace(lambda *args, **keys: None)

    # Custom trace function that will force locals.
    def trace(frame, event, arg):
        # Update the frame locals.
        frame.f_locals.update(new_locals)
        # Set frame to use default trace, preventing this trace
        # function from resetting the locals on each trace call.
        del frame.f_trace

    # Set the old frame trace function to the custom trace.
    old_frame.f_trace = trace


def update_locals(frame, *refs):
    ''' Modifies a frame locals based on the provided references.

    :param frame: The frame from which a locals will be updated.
    :type frame: frame.
    :param refs: Iterable pair of (old_ref, new_ref) tuples.
    :type refs: Iterable type of pairs.

    '''
    new_locals = frame.f_locals.copy()
    has_changes = False

    # If a frame local has an identity patch with a provided
    # reference, then update new_locals with the new reference.
    for key, ref in new_locals.iteritems():
        for old_ref, new_ref in refs:
            if ref is old_ref:
                new_locals[key] = new_ref
                has_changes = True

    # If new_locals has changes, then force them onto the frame.
    if has_changes:
        _force_locals(frame, new_locals)

:

>>> import example
>>> import inspect
>>> x = 42
>>> x
42
>>> example.update_locals(inspect.currentframe(), (x, '3.14'))
>>> x
'3.14'

x int(42), example.update_locals() x str('3.14') .


++ fun() Python, A, str, ++ fun(A&) .

++ Spam fun(Spam&) _example Python.

#include <iostream>
#include <string>
#include <boost/python.hpp>

/// @brief Mockup type.
class Spam
{
public:
  explicit Spam(std::string str)
    : str_(str)
  {}

  void action()
  {
    std::cout << "Spam::action(): " << str_ << std::endl;
  }

private:
  std::string str_;
};

/// @brief Mockup function.
void fun(Spam& spam)
{
  std::cout << "fun() -> ";
  spam.action();
}

BOOST_PYTHON_MODULE(_example)
{
  namespace python = boost::python;

  python::class_<Spam>("Spam", python::init<std::string>());
  python::def("fun", &fun);
}

example _example.fun(), Spam, , fun(), str ,

from _example import *

import inspect
import sys


def _force_locals(old_frame, new_locals):
    ''' Force a new set of locals on a given frame. 

    :param old_frame: The frame to which locals will be applied.
    :type old_frame: frame.
    :param new_locals: What locals() should return for old_frame.
    :type new_locals: dict.

    .. note:: This function will force a custom trace function onto
              the old_frame.  Within the context of a trace function
              (often used for debugging), a frame f_locals is
              modifiable.  In most execution paths, f_locals is 
              writable but not modifiable.

    '''
    # Force tracing by setting the global tracing function to
    # any non-None function.  
    if not sys.gettrace():
        sys.settrace(lambda *args, **keys: None)

    # Custom trace function that will force locals.
    def trace(frame, event, arg):
        # Update the frame locals.
        frame.f_locals.update(new_locals)
        # Set frame to use default trace, preventing this trace
        # function from resetting the locals on each trace call.
        del frame.f_trace

    # Set the old frame trace function to the custom trace.
    old_frame.f_trace = trace


def _update_locals(frame, *refs):
    ''' Modifies a frame locals based on the provided references.

    :param frame: The frame from which a locals will be updated.
    :type frame: frame.
    :param refs: Iterable pair of (old_ref, new_ref) tuples.
    :type refs: Iterable type of pairs.

    '''
    new_locals = frame.f_locals.copy()
    has_changes = False

    # If a frame local has an identity patch with a provided
    # reference, then update new_locals with the new reference.
    for key, ref in new_locals.iteritems():
        for old_ref, new_ref in refs:
            if ref is old_ref:
                new_locals[key] = new_ref
                has_changes = True

    # If new_locals has changes, then force them onto the frame.
    if has_changes:
        _force_locals(frame, new_locals)


def _patch_fun():
    old_fun = fun
    # Create a function that will perform custom operations then
    # delegate to the original function.
    def patch(spam, *args, **kwargs):
        if isinstance(spam, str):
            old_spam, spam = spam, Spam(spam)

            # In the caller frame, force the variables that reference
            # old_spam to now reference spam.
            _update_locals(
                inspect.currentframe(1), # Caller frame.
                (old_spam, spam))

        return old_fun(spam, *args, **kwargs)
    return patch

fun = _patch_fun()

:

>>> import example
>>> s1 = example.Spam('abc')
>>> type(s1)
<class '_example.Spam'>
>>> example.fun(s1)
fun() -> Spam::action(): abc
>>> type(s1) # s1 type has not changed.
<class '_example.Spam'>
>>> s2 = 'def'
>>> type(s2)
<type 'str'>
>>> example.fun(s2)
fun() -> Spam::action(): def
>>> type(s2) # s2 type has changed.
<class '_example.Spam'>
>>> example.fun('ghi')
fun() -> Spam::action(): ghi

, fun() , .

+3

fun(a) a. , fun(a), - a, a, .

, Python , a = fun(a), a ( , ).

0

All Articles