Boost.Python的自动转换参数 [英] Boost.python automatically convert parameter
问题描述
我使用Boost.Python的包裹C ++类'A',它接受一个字符串作为构造函数。然后,我有一个函数'乐趣(A和ARG)这需要一个'A'作为参数的引用。我想有乐趣,这是这样的,如果我传递一个变量这是一个Python字符串的引用,此引用第一自动转换为引用的A巨蟒包装。
I am using boost.python to wrap a C++ class 'A' which takes a string as constructor. I then have a function 'fun(A& arg)' which takes a reference to an 'A' as parameter. I would like to have a python wrapper for 'fun' which is such that if I pass a variable which is a reference to a python string, this reference is first automatically converted to a reference to an 'A'.
一个例子可能会有帮助。在蟒蛇的一面,我希望能够做到这一点:
An example might help. On the python side, I would like to be able to do this:
a = 'some string'
fun(a)
再有'一'实际上是(参考)一个'A',不是这样的(参考)原始字符串。我想这样做,因为我希望能够避免写这为
and then have 'a' actually be (a reference to) an 'A', not (a reference to) the original string. I want to do this because I would like to be able to avoid writing this as
a = A('some string')
fun(a)
(你可以有足够的理由怀疑,这是一个有关保护程序,但让我们姑且认为它的问题对我来说)。
(you may have good reasons to doubt that this is a relevant saver, but let's just assume that it matters for me).
是这样子可能什么?如果不使用Boost.Python的,也许是直接使用Python-C API?
Is anything like this possible? If not using boost.python, perhaps directly using the Python-C API?
的注意的:我知道的事实,如果我会写
Note: I am aware of the fact that if I would write
fun('some string')
没有办法为参考的字符串转换为是某些其它类型的引用。
there is no way for the reference to the string to be converted to be a reference to some other type.
推荐答案
这是可能的,但解决的办法可能是依赖于Python实现。
This is possible, but the solution may be dependent on the Python implementation.
例如,在Python 2.7时, 检查
模块和 SYS .settrace()
可以用来修改的 当地人()
一个特定的框架。这也可以了Python / C API中完成,但它往往是更便于管理Python中操纵的Python框架。
For example, in Python 2.7, the inspect
module and sys.settrace()
can be used to modify locals()
on a specific frame. This can also be accomplished in the Python/C API, but it is often far more manageable to manipulate Python frames within Python.
在下面的 example.py
的 update_locals()
功能将更新当地人()
在给定的框架:
In the below example.py
, the update_locals()
function will update the locals()
in a given frame:
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's 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's 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's trace function to the custom trace.
old_frame.f_trace = trace
def update_locals(frame, *refs):
''' Modifies a frame's 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's 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)
对象,但的例子。 update_locals()
函数更改 X
引用 STR('3.14')
对象。
The x
variable referenced the int(42)
object, but the example.update_locals()
function changed x
to reference str('3.14')
object.
使用能够修改调用者的框架,下一步是猴子修补C ++ 乐趣()
在Python构建的一个实例A
如果参数的一个实例 STR
,然后委托给C ++ 乐趣(A和)
功能和更新呼叫者的框架。
With being able to modify the caller's frame, the next step is to monkey patch the C++ fun()
in Python to construct an instance of A
if the argument is an instance of str
, then delegate to the C++ fun(A&)
function and update the caller's frame.
在下面的例子中,一个C ++ 垃圾邮件
键入和乐趣(垃圾邮件和放大器;)
功能暴露于 _example
Python模块。
In example below, a C++ Spam
type and fun(Spam&)
function are exposed to the _example
Python module.
#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.fun()
来构建一个垃圾邮件
对象,如果>提供的是
STR
和操纵呼叫者的帧以类似的方式如以上证实:
A higher level example
module will monkey patch _example.fun()
to construct a Spam
object if the argument provided to fun()
is an instance of str
and manipulate the caller's frame in a similar manner as demonstrated above:
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's 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's 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's trace function to the custom trace.
old_frame.f_trace = trace
def _update_locals(frame, *refs):
''' Modifies a frame's 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's 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's frame, force the variables that reference
# old_spam to now reference spam.
_update_locals(
inspect.currentframe(1), # Caller's 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's type has not changed.
<class '_example.Spam'>
>>> s2 = 'def'
>>> type(s2)
<type 'str'>
>>> example.fun(s2)
fun() -> Spam::action(): def
>>> type(s2) # s2's type has changed.
<class '_example.Spam'>
>>> example.fun('ghi')
fun() -> Spam::action(): ghi
请注意,在上面的例子中,帧操作仅修改的乐趣()的调用者的框架,而不是整个堆栈。
Note that the frame manipulation in the above example only modifies's fun()
's caller's frame and not the entire stack.
这篇关于Boost.Python的自动转换参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!