From 559f1c04a6796c19fbc70722cc38abb7e1a3f4a7 Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Fri, 17 Nov 2000 19:58:31 +0000 Subject: [PATCH] Add specialization to prevent use of Callback. [SVN r8242] --- callback.h | 21 +++++++++++++++++++++ gen_callback.py | 21 +++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/callback.h b/callback.h index 88ffe918..cb0ba6c1 100644 --- a/callback.h +++ b/callback.h @@ -333,6 +333,27 @@ struct Callback } }; +// 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) +// +// 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::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::call[_method](...args...)); +// ...result.c_str()... // access the char* array +template <> +struct Callback +{ + // 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_ diff --git a/gen_callback.py b/gen_callback.py index c9d7a408..1f34f9d7 100644 --- a/gen_callback.py +++ b/gen_callback.py @@ -87,6 +87,27 @@ struct Callback + """}; +// 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) +// +// 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::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::call[_method](...args...)); +// ...result.c_str()... // access the char* array +template <> +struct Callback +{ + // 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_