Make PYBIND11_MODULE name usable with define (#1082)

This commit is contained in:
tzh1043 2017-09-13 19:02:53 +02:00 committed by Dean Moldovan
parent 9f82370e48
commit d81d11a61c
2 changed files with 12 additions and 10 deletions

View File

@ -205,6 +205,7 @@ extern "C" {
#define PYBIND11_TRY_NEXT_OVERLOAD ((PyObject *) 1) // special failure return code #define PYBIND11_TRY_NEXT_OVERLOAD ((PyObject *) 1) // special failure return code
#define PYBIND11_STRINGIFY(x) #x #define PYBIND11_STRINGIFY(x) #x
#define PYBIND11_TOSTRING(x) PYBIND11_STRINGIFY(x) #define PYBIND11_TOSTRING(x) PYBIND11_STRINGIFY(x)
#define PYBIND11_CONCAT(first, second) first##second
/** \rst /** \rst
***Deprecated in favor of PYBIND11_MODULE*** ***Deprecated in favor of PYBIND11_MODULE***
@ -267,7 +268,7 @@ extern "C" {
} }
\endrst */ \endrst */
#define PYBIND11_MODULE(name, variable) \ #define PYBIND11_MODULE(name, variable) \
static void pybind11_init_##name(pybind11::module &); \ static void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &); \
PYBIND11_PLUGIN_IMPL(name) { \ PYBIND11_PLUGIN_IMPL(name) { \
int major, minor; \ int major, minor; \
if (sscanf(Py_GetVersion(), "%i.%i", &major, &minor) != 2) { \ if (sscanf(Py_GetVersion(), "%i.%i", &major, &minor) != 2) { \
@ -281,9 +282,9 @@ extern "C" {
major, minor); \ major, minor); \
return nullptr; \ return nullptr; \
} \ } \
auto m = pybind11::module(#name); \ auto m = pybind11::module(PYBIND11_TOSTRING(name)); \
try { \ try { \
pybind11_init_##name(m); \ PYBIND11_CONCAT(pybind11_init_, name)(m); \
return m.ptr(); \ return m.ptr(); \
} catch (pybind11::error_already_set &e) { \ } catch (pybind11::error_already_set &e) { \
PyErr_SetString(PyExc_ImportError, e.what()); \ PyErr_SetString(PyExc_ImportError, e.what()); \
@ -293,7 +294,7 @@ extern "C" {
return nullptr; \ return nullptr; \
} \ } \
} \ } \
void pybind11_init_##name(pybind11::module &variable) void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &variable)
NAMESPACE_BEGIN(PYBIND11_NAMESPACE) NAMESPACE_BEGIN(PYBIND11_NAMESPACE)

View File

@ -44,11 +44,11 @@
} }
\endrst */ \endrst */
#define PYBIND11_EMBEDDED_MODULE(name, variable) \ #define PYBIND11_EMBEDDED_MODULE(name, variable) \
static void pybind11_init_##name(pybind11::module &); \ static void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &); \
static PyObject *pybind11_init_wrapper_##name() { \ static PyObject PYBIND11_CONCAT(*pybind11_init_wrapper_, name)() { \
auto m = pybind11::module(#name); \ auto m = pybind11::module(PYBIND11_TOSTRING(name)); \
try { \ try { \
pybind11_init_##name(m); \ PYBIND11_CONCAT(pybind11_init_, name)(m); \
return m.ptr(); \ return m.ptr(); \
} catch (pybind11::error_already_set &e) { \ } catch (pybind11::error_already_set &e) { \
PyErr_SetString(PyExc_ImportError, e.what()); \ PyErr_SetString(PyExc_ImportError, e.what()); \
@ -59,8 +59,9 @@
} \ } \
} \ } \
PYBIND11_EMBEDDED_MODULE_IMPL(name) \ PYBIND11_EMBEDDED_MODULE_IMPL(name) \
pybind11::detail::embedded_module name(#name, pybind11_init_impl_##name); \ pybind11::detail::embedded_module name(PYBIND11_TOSTRING(name), \
void pybind11_init_##name(pybind11::module &variable) PYBIND11_CONCAT(pybind11_init_impl_, name)); \
void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &variable)
NAMESPACE_BEGIN(PYBIND11_NAMESPACE) NAMESPACE_BEGIN(PYBIND11_NAMESPACE)