2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-21 17:12:22 +00:00

Add specialization to prevent use of Callback<char*>.

[SVN r8242]
This commit is contained in:
Dave Abrahams
2000-11-17 19:58:31 +00:00
parent 2a266510fe
commit 559f1c04a6
2 changed files with 42 additions and 0 deletions

View File

@@ -333,6 +333,27 @@ struct Callback<void>
}
};
// Make it a compile-time error to try to return a const char* from a virtual
// function. The standard conversion
//
// from_python(PyObject* string, py::Type<const char*>)
//
// returns a pointer to the character array which is internal to string. The
// problem with trying to do this in a standard callback function is that the
// Python string would likely be destroyed upon return from the calling function
// (py::Callback<const char*>::call[_method]) when its reference count is
// decremented. If you absolutely need to do this and you're sure it's safe (it
// usually isn't), you can use
//
// py::String result(py::Callback<py::String>::call[_method](...args...));
// ...result.c_str()... // access the char* array
template <>
struct Callback<const char*>
{
// Try hard to generate a readable error message
typedef struct unsafe_since_python_string_may_be_destroyed {} call, call_method;
};
} // namespace py
#endif // CALLBACK_DWA_052100_H_

View File

@@ -87,6 +87,27 @@ struct Callback<void>
+
"""};
// Make it a compile-time error to try to return a const char* from a virtual
// function. The standard conversion
//
// from_python(PyObject* string, py::Type<const char*>)
//
// returns a pointer to the character array which is internal to string. The
// problem with trying to do this in a standard callback function is that the
// Python string would likely be destroyed upon return from the calling function
// (py::Callback<const char*>::call[_method]) when its reference count is
// decremented. If you absolutely need to do this and you're sure it's safe (it
// usually isn't), you can use
//
// py::String result(py::Callback<py::String>::call[_method](...args...));
// ...result.c_str()... // access the char* array
template <>
struct Callback<const char*>
{
// Try hard to generate a readable error message
typedef struct unsafe_since_python_string_may_be_destroyed {} call, call_method;
};
} // namespace py
#endif // CALLBACK_DWA_052100_H_