title: pybind11: how to implement a with context manager
body: <p>I'm trying to implement a Python with context manager with pybind11.</p>
  
  <p>Following <a href="https://docs.python.org/3/reference/datamodel.html#context-managers" rel="nofollow noreferrer">Python's documentation</a>, my first version is:</p>
  
  <pre><code>    py::class_&lt;MyResource&gt; (module, "CustomResource", "A custom ressource")
      .def("__enter__", [&amp;] (MyResource&amp; r) { r.lock(); }
          , "Enter the runtime context related to this object")
      .def("__exit__", [&amp;] (MyResource&amp; r, void* exc_type, void* exc_value, void* traceback) { r.unlock(); }
          , "Exit the runtime context related to this object")
      ;
  </code></pre>
  
  <p>I don't know what the types of <code>exc_type</code>, <code>exc_value</code> and <code>traceback</code>. I guess they can be simple <code>pybind11::object</code>? </p>
  
  <p>Are they more specific bindings, I can use?</p>