Although this works, it might be too naive approach to follow.
I put it here just for suggestions.
I see 2 options here.
1 - Call pyexec from pycall to do the conditional checking for either __builtin__ or builtins depending on python version.
That still leaves us to check if the attribute is present in __main__ or in builtins namespace.
2 - Use boost_python 's c++ api somehow to do the above check
For a future change---not needed for htis PR---maybe a run-time check rather than compile time macro check is more flexible. Some day it would be good to support dynamically selecting python at runtime.
I've merged this. The logic here starts to look a little cluttered, so I pushed a follow up commit to try to flatten it a bit by separating the builtins logic from the rest of it, see 9077907.
Also your check for whether module has the function was wrong so I took that out. That would have meant that something like pycall("math.str") would fail to find the function str in math and fall back to looking for it in the local or global namespaces.
And you had mixed tabs and spaces, that is now fixed. Please try to use spaces only going forward.
I am not actually sure that the Python 2 vs 3 check could be made a runtime check at this level. The libpython API is different, so compiling it against one or the other results in something that can't work with both versions. I think if we want to support both Python 2 and 3, maybe we will need separate .oct files under a facade interface, something I've thought about, but way in the future.