1

我正在尝试从使用 boost.python 导出的 C++ 类的构造函数访问 python 本地变量,但 PyEval_GetLocals() 似乎返回全局而不是本地字典。一个例子:在 C++ 中我做

class X {
   public:
      X() {
         boost::python::object locals(boost::python::borrowed(PyEval_GetLocals()));
         locals["xyz"]=42
      }
};

BOOST_PYTHON_MODULE(test) {
   class_<X>("X", init<>());
}

如果我现在用 Python

x = X()
print(xyz)

我得到“42”作为输出(如预期的那样)。但是,同样的情况发生在

def fun():
    x = X()

print(xyz)

尽管 'fun()' 已经创建了一个新范围,但它也会打印 '42'。我本来希望在 fun() 退出后“xyz”名称再次超出范围,因此在我到达 print 语句时会留下未定义的“xyz”。

我究竟做错了什么?有没有办法从 C++ 对象或函数中访问本地名称?

4

1 回答 1

1

我认为测试用例可能会导致误报。您是否有可能在调用之前忘记del了变量?xyzfun()

定义一个函数会在当前范围内创建一个局部变量,该变量引用该函数对象。例如:

def fun():
    x = X()

创建由当前范围内function的变量引用的对象。fun如果调用该函数,则(默认情况下)创建一个新的本地范围,其中返回的对象将在函数的本地范围内X()引用,而不是在调用者的框架内。xlocals()


这是一个基于原始代码的示例:

#include <boost/python.hpp>

/// @brief Mockup types.
struct X
{
  X()
  {
    // Borrow a reference from the locals dictionary to create a handle.
    // If PyEval_GetLocals() returns NULL, then Boost.Python will throw.
    namespace python = boost::python;
    python::object locals(python::borrowed(PyEval_GetLocals()));

    // Inject a reference to the int(42) object as 'xyz' into the 
    // frame's local variables.
    locals["xyz"] = 42;
  }
};

BOOST_PYTHON_MODULE(example)
{
  namespace python = boost::python;
  python::class_<X>("X", python::init<>());
}

断言可见性的交互式使用:

>>> import example
>>> def fun():
...     assert('xyz' not in locals())
...     x = example.X()
...     assert('xyz' in locals())
...     assert('xyz' not in globals())
... 
>>> assert('xyz' not in globals())
>>> fun()
>>> assert('xyz' not in globals())
>>> x = example.X()
>>> assert('xyz' in globals())
>>> del xyz
>>> fun()
>>> assert('xyz' not in globals())

为了完整起见,可以使用未设置标志的aFuncionType来构造 a ,从而导致用于函数调用的帧的返回值与. 这是一个交互式使用示例,演示了这一点:CodeTypeco_flagsnewlocalslocals()globals()

>>> def fun():
...     x = 42
...     print "local id in fun:", id(locals())
... 
>>> import types
>>> def no_locals(fn):
...     func_code = fn.func_code
...     return types.FunctionType(
...         types.CodeType(
...             func_code.co_argcount,
...             func_code.co_nlocals,
...             func_code.co_stacksize,
...             func_code.co_flags & ~2, # disable newlocals
...             func_code.co_code,
...             func_code.co_consts,
...             func_code.co_names,
...             func_code.co_varnames,
...             func_code.co_filename,
...             func_code.co_name,
...             func_code.co_firstlineno,
...             func_code.co_lnotab),
...         globals())
... 
>>> id(globals())
3075430164L
>>> assert('x' not in locals())
>>> fun()
local id in fun: 3074819588
>>> assert('x' not in locals())
>>> fun = no_locals(fun) # disable newlocals flag for fun
>>> assert('x' not in locals())
>>> fun()
local id in fun: 3075430164
>>> assert('x' in locals())
>>> x
42

即使在禁用newlocals标志之后,我也必须调用 insidelocals()fun()观察x被插入到全局符号表中。

于 2014-03-09T22:08:02.053 回答