本文整理汇总了C++中PythonObject::get方法的典型用法代码示例。如果您正苦于以下问题:C++ PythonObject::get方法的具体用法?C++ PythonObject::get怎么用?C++ PythonObject::get使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PythonObject
的用法示例。
在下文中一共展示了PythonObject::get方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: SetItemAtIndex
void PythonTuple::SetItemAtIndex(uint32_t index, const PythonObject &object) {
if (IsAllocated() && object.IsValid()) {
// PyTuple_SetItem is documented to "steal" a reference, so we need to
// convert it to an owned reference by incrementing it.
Py_INCREF(object.get());
PyTuple_SetItem(m_py_obj, index, object.get());
}
}
示例2: PythonObject
PythonObject
PythonDictionary::GetItemForKey(const PythonObject &key) const
{
if (IsAllocated() && key.IsValid())
return PythonObject(PyRefType::Borrowed, PyDict_GetItem(m_py_obj, key.get()));
return PythonObject();
}
示例3: AppendItem
void PythonList::AppendItem(const PythonObject &object) {
if (IsAllocated() && object.IsValid()) {
// `PyList_Append` does *not* steal a reference, so do not call `Py_INCREF`
// here like we do with `PyList_SetItem`.
PyList_Append(m_py_obj, object.get());
}
}
示例4: callFunctionWithArgs
PythonObject callFunctionWithArgs(
PythonObject function,
const std::vector<PythonObject>& args,
const std::vector<std::pair<std::string, PythonObject>>& kwargs)
{
if (!PyCallable_Check(function.get())) {
throw WrappyError("Wrappy: Supplied object isn't callable.");
}
// Build tuple
size_t sz = args.size();
PythonObject tuple(PythonObject::owning {}, PyTuple_New(sz));
if (!tuple) {
PyErr_Print();
throw WrappyError("Wrappy: Couldn't create python typle.");
}
for (size_t i = 0; i < sz; ++i) {
PyObject* arg = args.at(i).get();
Py_XINCREF(arg); // PyTuple_SetItem steals a reference
PyTuple_SetItem(tuple.get(), i, arg);
}
// Build kwargs dict
PythonObject dict(PythonObject::owning {}, PyDict_New());
if (!dict) {
PyErr_Print();
throw WrappyError("Wrappy: Couldn't create python dictionary.");
}
for (const auto& kv : kwargs) {
PyDict_SetItemString(dict.get(), kv.first.c_str(), kv.second.get());
}
PythonObject res(PythonObject::owning{},
PyObject_Call(function.get(), tuple.get(), dict.get()));
if (!res) {
PyErr_Print();
throw WrappyError("Wrappy: Error calling function");
}
return res;
}
示例5: one
TEST_F(PythonDataObjectsTest, TestPythonCallableInvoke) {
auto list = m_builtins_module.ResolveName("list").AsType<PythonCallable>();
PythonInteger one(1);
PythonString two("two");
PythonTuple three = {one, two};
PythonTuple tuple_to_convert = {one, two, three};
PythonObject result = list({tuple_to_convert});
EXPECT_TRUE(PythonList::Check(result.get()));
auto list_result = result.AsType<PythonList>();
EXPECT_EQ(3U, list_result.GetSize());
EXPECT_EQ(one.get(), list_result.GetItemAtIndex(0).get());
EXPECT_EQ(two.get(), list_result.GetItemAtIndex(1).get());
EXPECT_EQ(three.get(), list_result.GetItemAtIndex(2).get());
}
示例6: SetItemForKey
void PythonDictionary::SetItemForKey(const PythonObject &key,
const PythonObject &value) {
if (IsAllocated() && key.IsValid() && value.IsValid())
PyDict_SetItem(m_py_obj, key.get(), value.get());
}
示例7:
void
PythonDictionary::SetItemForKey (const PythonString &key, const PythonObject &value)
{
if (m_py_obj && key && value)
PyDict_SetItem(m_py_obj, key.get(), value.get());
}
示例8: PythonObject
PythonString::PythonString (const PythonObject &object) :
PythonObject()
{
Reset(object.get()); // Use "Reset()" to ensure that py_obj is a string
}