{"action":"created","issue":{"url":"https://api.github.com/repos/pybind/pybind11/issues/686","repository_url":"https://api.github.com/repos/pybind/pybind11","labels_url":"https://api.github.com/repos/pybind/pybind11/issues/686/labels{/name}","comments_url":"https://api.github.com/repos/pybind/pybind11/issues/686/comments","events_url":"https://api.github.com/repos/pybind/pybind11/issues/686/events","html_url":"https://github.com/pybind/pybind11/issues/686","id":209246221,"number":686,"title":"Abort when `error_already_set` is thrown inside `PYBIND11_PLUGIN`","user":{"login":"TallJimbo","id":1330720,"avatar_url":"https://avatars.githubusercontent.com/u/1330720?v=3","gravatar_id":"","url":"https://api.github.com/users/TallJimbo","html_url":"https://github.com/TallJimbo","followers_url":"https://api.github.com/users/TallJimbo/followers","following_url":"https://api.github.com/users/TallJimbo/following{/other_user}","gists_url":"https://api.github.com/users/TallJimbo/gists{/gist_id}","starred_url":"https://api.github.com/users/TallJimbo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TallJimbo/subscriptions","organizations_url":"https://api.github.com/users/TallJimbo/orgs","repos_url":"https://api.github.com/users/TallJimbo/repos","events_url":"https://api.github.com/users/TallJimbo/events{/privacy}","received_events_url":"https://api.github.com/users/TallJimbo/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2017-02-21T19:21:15Z","updated_at":"2017-02-21T19:28:40Z","closed_at":"2017-02-21T19:28:40Z","body":"When `error_already_set` is thrown by Python C++ API code inside the `PYBIND11_PLUGIN` macro block, the `std::exception` catch block defined by the macro somehow doesn't work, and the result is a `py::error_already_set` that propagates up and terminates the program, but with a *different* Python exception type and message than the original; regardless of what the original is, I always see:\r\n```\r\nterminate called after throwing an instance of 'pybind11::error_already_set'\r\n  what():  SystemError: <built-in method __contains__ of dict object at 0x7ffff6bab480> returned a result with an error set\r\n```\r\n\r\nI haven't been able to figure out how this is happening, but it's easily fixed by adding an extra\r\n```\r\ntry {\r\n    ...\r\n} catch (pybind11::error_already_set & err) {\r\n    err.restore();\r\n    return nullptr;\r\n}\r\n```\r\naround the offending code.  My best guess is that the `error_already_set` destructor is causing another instance of `py::error_already_set` to be raised somehow, but that too seems impossible.\r\n\r\nIn any case, my recommendation is that we add an explicit catch block for `error_already_set` to the `PYBIND11_PLUGIN` macro that simply uses that exception rather than raising `ImportError` (or at least uses the original error message).  That will make it much easier to diagnose failing code.\r\n\r\nHere's a complete example to reproduce the problem:\r\n```\r\n#include \"pybind11/pybind11.h\"\r\n\r\nPYBIND11_PLUGIN(import_abort) {\r\n    pybind11::module mod(\"import_abort\");\r\n    pybind11::module::import(\"some-other-module-that-does-not-exist\");\r\n    return mod.ptr();\r\n}\r\n```\r\nSimply import this module to trigger the failure.\r\n"},"comment":{"url":"https://api.github.com/repos/pybind/pybind11/issues/comments/281453276","html_url":"https://github.com/pybind/pybind11/issues/686#issuecomment-281453276","issue_url":"https://api.github.com/repos/pybind/pybind11/issues/686","id":281453276,"user":{"login":"TallJimbo","id":1330720,"avatar_url":"https://avatars.githubusercontent.com/u/1330720?v=3","gravatar_id":"","url":"https://api.github.com/users/TallJimbo","html_url":"https://github.com/TallJimbo","followers_url":"https://api.github.com/users/TallJimbo/followers","following_url":"https://api.github.com/users/TallJimbo/following{/other_user}","gists_url":"https://api.github.com/users/TallJimbo/gists{/gist_id}","starred_url":"https://api.github.com/users/TallJimbo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TallJimbo/subscriptions","organizations_url":"https://api.github.com/users/TallJimbo/orgs","repos_url":"https://api.github.com/users/TallJimbo/repos","events_url":"https://api.github.com/users/TallJimbo/events{/privacy}","received_events_url":"https://api.github.com/users/TallJimbo/received_events","type":"User","site_admin":false},"created_at":"2017-02-21T19:28:40Z","updated_at":"2017-02-21T19:28:40Z","body":"That explains it; I have not.  Sorry for the noise."}}
{"actor":{"display_login":"TallJimbo"}}