C++ Boost

Boost.Python

Frequently Asked Questions (FAQs)

㣼뎊̢㨆AQ㩼/h2>


How can I wrap a function which takes a function pointer as an argument?
I'm getting the "attempt to return dangling reference" error. What am I doing wrong?
Is return_internal_reference efficient?
How can I wrap functions which take C++ containers as arguments?
fatal error C1204:Compiler limit:internal structure overflow
How do I debug my Python extensions?
Why doesn't my *= operator work?
Does Boost.Python work with Mac OS X?
How can I find the existing PyObject that holds a C++ object?
How can I wrap a function which needs to take ownership of a raw pointer?
Compilation takes too much time and eats too much memory! What can I do to make it faster?
How do I create sub-packages using Boost.Python?
error C2064: term does not evaluate to a function taking 2 arguments
How can I automatically convert my custom string type to and from a Python string?
Why is my automatic to-python conversion not being found?
Is Boost.Python thread-aware/compatible with multiple interpreters?
ȧꎷ◰⎊펪ꯊ햸ի儺㿼/a>
ΒŶ彁ˡʔͼ絻ؒҽӃ㨡ttempt to return dangling reference㩡ᴭγᣠĄ/⻶ԄأOa>
return_internal_referenceŠ蟂ࣿ
ȧꎷ◰⎊펪C++ȝƷ儺㿼/a>
ւu䭎204㺱ҫƷϞֆ㺄ڲ。鶊糶㨍 fatal error C1204:Compiler limit:internal structure overflow㩼/a>
ȧꎵ犔Β儐ython)չ㿼/a>
ΪʲoΒ儼code>*=ԋˣ緄됐Ę㿼/a>
Boost.PythonĜ籓Oڍac OS X㿼/a>
Βȧꎲńܕҵ퓵ӐC++攏㵄ϖӐPyObject㿠XXX
攓ڐ蒪퓊Ւ븶ԭʼָի˹ӐȨ儺㬍 ȧꎷ◰㿼/a>
᠒뻨瑁ˌ붠儊ἤ㬶臒㔵䁋̫栵Ąڴ棡 Եѹ⅄܈E츼ﬣOa>
ԵѹӃBoost.Python䴽蘚༣賵b-package㩣Oa>
䭎064㺸I롼Ƌ㎪2趲Ίꯊ퍊 㨥rror C2064: term does not evaluate to a function taking 2 arguments㩼/a>
ΒȧꎲńܔڎҵėԶ蒥ז維Ѝꍐythonז維줗Զ뻣Oa>
ΪʲoΒython儗Զ뻃듐Ւ彣Oa>
Boost.PythonԚ栽⊍Ʒʱ㬍 ʇ旗̸߳꯼賊廣質read-aware/compatible㩣Oa>

How can I wrap a function which takes a function pointer as an argument?

ȧꎷ◰⎊펪ꯊ햸ի儺㿼/a>

If what you're trying to do is something like this:

ȧ黃名ͼբѹ׶㺍

typedef boost::function<void (string s) > funcptr;

void foo(funcptr fp)
{
    fp("hello,world!");
}

BOOST_PYTHON_MODULE(test)
{
    def("foo",foo) ;
}

And then:

Ȼ곣ꍊ

>>> def hello(s):
...    print s
...
>>> foo(hello)
hello, world!

The short answer is: "you can't". This is not a Boost.Python limitation so much as a limitation of C++. The problem is that a Python function is actually data, and the only way of associating data with a C++ function pointer is to store it in a static variable of the function. The problem with that is that you can only associate one piece of data with every C++ function, and we have no way of compiling a new C++ function on-the-fly for every Python function you decide to pass to foo. In other words, this could work if the C++ function is always going to invoke the same Python function, but you probably don't want that.

첶̵Ļشʇ㺡಻ᱡ㍊ բ⻊ǂoost.Python粲ޖƣ춸ʇC++粲ޖơ㍊ Ί̢ԚӚ㬐ythonꯊ톤ʵNJ㬍 渽늽ꏵ歸븶C++ꯊ햸ի載蒻罷裬 ʇ챣䦎꺯ʽ齉⌬ᤁ 渃︶C++ꯊ햻Ĝᣴ撻睊㬍 ⢇Ҏ҃ǎ޷莪y趴뵝踼code>foo儐ythonꯊ 촊ᵘ᠒뒻趐μă++ꯊ 뻾仰˵㬈繻C++ꯊ헜ʇ巔uem>ͬһ趼/em>Pythonꯊ ˼ܹ䗷㬵넇ܲ늇ģϫҪ儡㍊

If you have the luxury of changing the C++ code you're wrapping, pass it an object instead and call that; the overloaded function call operator will invoke the Python function you pass it behind the object.

ȧ黃ㄜ輸Ą㒪碗ൄC++亂룬 Ď괫坒븶object⢵瓃˼㻍 ֘Ԙ儺巔Oˋ㷻롵瓃ģ˹䫵ݵļcode>objectᳺ㵄Pythonꯊ

For more perspective on the issue, see this posting.

攓ڸO錢儸춠雵㣬ǫﴼa href="http://aspn.activestate.com/ASPN/Mail/Message/1554837">բ趌뗓.


I'm getting the "attempt to return dangling reference" error. What am I doing wrong?

ΒŶ彁ˡʔͼ絻ؒҽӃ 㨡ttempt to return dangling reference㩡ᴭγᣠ Ą/⻶ԄأOa>

That exception is protecting you from causing a nasty crash. It usually happens in response to some code like this:

胒쳣ʇ᣻䄺aӚԬ㉎㏕儱