github.com/kaydxh/golang@v0.0.131/pkg/gocv/cgo/third_path/pybind11/docs/faq.rst (about)

     1  Frequently asked questions
     2  ##########################
     3  
     4  "ImportError: dynamic module does not define init function"
     5  ===========================================================
     6  
     7  1. Make sure that the name specified in PYBIND11_MODULE is identical to the
     8  filename of the extension library (without suffixes such as ``.so``).
     9  
    10  2. If the above did not fix the issue, you are likely using an incompatible
    11  version of Python that does not match what you compiled with.
    12  
    13  "Symbol not found: ``__Py_ZeroStruct`` / ``_PyInstanceMethod_Type``"
    14  ========================================================================
    15  
    16  See the first answer.
    17  
    18  "SystemError: dynamic module not initialized properly"
    19  ======================================================
    20  
    21  See the first answer.
    22  
    23  The Python interpreter immediately crashes when importing my module
    24  ===================================================================
    25  
    26  See the first answer.
    27  
    28  .. _faq_reference_arguments:
    29  
    30  Limitations involving reference arguments
    31  =========================================
    32  
    33  In C++, it's fairly common to pass arguments using mutable references or
    34  mutable pointers, which allows both read and write access to the value
    35  supplied by the caller. This is sometimes done for efficiency reasons, or to
    36  realize functions that have multiple return values. Here are two very basic
    37  examples:
    38  
    39  .. code-block:: cpp
    40  
    41      void increment(int &i) { i++; }
    42      void increment_ptr(int *i) { (*i)++; }
    43  
    44  In Python, all arguments are passed by reference, so there is no general
    45  issue in binding such code from Python.
    46  
    47  However, certain basic Python types (like ``str``, ``int``, ``bool``,
    48  ``float``, etc.) are **immutable**. This means that the following attempt
    49  to port the function to Python doesn't have the same effect on the value
    50  provided by the caller -- in fact, it does nothing at all.
    51  
    52  .. code-block:: python
    53  
    54      def increment(i):
    55          i += 1  # nope..
    56  
    57  pybind11 is also affected by such language-level conventions, which means that
    58  binding ``increment`` or ``increment_ptr`` will also create Python functions
    59  that don't modify their arguments.
    60  
    61  Although inconvenient, one workaround is to encapsulate the immutable types in
    62  a custom type that does allow modifications.
    63  
    64  An other alternative involves binding a small wrapper lambda function that
    65  returns a tuple with all output arguments (see the remainder of the
    66  documentation for examples on binding lambda functions). An example:
    67  
    68  .. code-block:: cpp
    69  
    70      int foo(int &i) { i++; return 123; }
    71  
    72  and the binding code
    73  
    74  .. code-block:: cpp
    75  
    76     m.def("foo", [](int i) { int rv = foo(i); return std::make_tuple(rv, i); });
    77  
    78  
    79  How can I reduce the build time?
    80  ================================
    81  
    82  It's good practice to split binding code over multiple files, as in the
    83  following example:
    84  
    85  :file:`example.cpp`:
    86  
    87  .. code-block:: cpp
    88  
    89      void init_ex1(py::module_ &);
    90      void init_ex2(py::module_ &);
    91      /* ... */
    92  
    93      PYBIND11_MODULE(example, m) {
    94          init_ex1(m);
    95          init_ex2(m);
    96          /* ... */
    97      }
    98  
    99  :file:`ex1.cpp`:
   100  
   101  .. code-block:: cpp
   102  
   103      void init_ex1(py::module_ &m) {
   104          m.def("add", [](int a, int b) { return a + b; });
   105      }
   106  
   107  :file:`ex2.cpp`:
   108  
   109  .. code-block:: cpp
   110  
   111      void init_ex2(py::module_ &m) {
   112          m.def("sub", [](int a, int b) { return a - b; });
   113      }
   114  
   115  :command:`python`:
   116  
   117  .. code-block:: pycon
   118  
   119      >>> import example
   120      >>> example.add(1, 2)
   121      3
   122      >>> example.sub(1, 1)
   123      0
   124  
   125  As shown above, the various ``init_ex`` functions should be contained in
   126  separate files that can be compiled independently from one another, and then
   127  linked together into the same final shared object.  Following this approach
   128  will:
   129  
   130  1. reduce memory requirements per compilation unit.
   131  
   132  2. enable parallel builds (if desired).
   133  
   134  3. allow for faster incremental builds. For instance, when a single class
   135     definition is changed, only a subset of the binding code will generally need
   136     to be recompiled.
   137  
   138  "recursive template instantiation exceeded maximum depth of 256"
   139  ================================================================
   140  
   141  If you receive an error about excessive recursive template evaluation, try
   142  specifying a larger value, e.g. ``-ftemplate-depth=1024`` on GCC/Clang. The
   143  culprit is generally the generation of function signatures at compile time
   144  using C++14 template metaprogramming.
   145  
   146  .. _`faq:hidden_visibility`:
   147  
   148  "'SomeClass' declared with greater visibility than the type of its field 'SomeClass::member' [-Wattributes]"
   149  ============================================================================================================
   150  
   151  This error typically indicates that you are compiling without the required
   152  ``-fvisibility`` flag.  pybind11 code internally forces hidden visibility on
   153  all internal code, but if non-hidden (and thus *exported*) code attempts to
   154  include a pybind type (for example, ``py::object`` or ``py::list``) you can run
   155  into this warning.
   156  
   157  To avoid it, make sure you are specifying ``-fvisibility=hidden`` when
   158  compiling pybind code.
   159  
   160  As to why ``-fvisibility=hidden`` is necessary, because pybind modules could
   161  have been compiled under different versions of pybind itself, it is also
   162  important that the symbols defined in one module do not clash with the
   163  potentially-incompatible symbols defined in another.  While Python extension
   164  modules are usually loaded with localized symbols (under POSIX systems
   165  typically using ``dlopen`` with the ``RTLD_LOCAL`` flag), this Python default
   166  can be changed, but even if it isn't it is not always enough to guarantee
   167  complete independence of the symbols involved when not using
   168  ``-fvisibility=hidden``.
   169  
   170  Additionally, ``-fvisibility=hidden`` can deliver considerably binary size
   171  savings. (See the following section for more details.)
   172  
   173  
   174  .. _`faq:symhidden`:
   175  
   176  How can I create smaller binaries?
   177  ==================================
   178  
   179  To do its job, pybind11 extensively relies on a programming technique known as
   180  *template metaprogramming*, which is a way of performing computation at compile
   181  time using type information. Template metaprogramming usually instantiates code
   182  involving significant numbers of deeply nested types that are either completely
   183  removed or reduced to just a few instructions during the compiler's optimization
   184  phase. However, due to the nested nature of these types, the resulting symbol
   185  names in the compiled extension library can be extremely long. For instance,
   186  the included test suite contains the following symbol:
   187  
   188  .. only:: html
   189  
   190      .. code-block:: none
   191  
   192          _​_​Z​N​8​p​y​b​i​n​d​1​1​1​2​c​p​p​_​f​u​n​c​t​i​o​n​C​1​I​v​8​E​x​a​m​p​l​e​2​J​R​N​S​t​3​_​_​1​6​v​e​c​t​o​r​I​N​S​3​_​1​2​b​a​s​i​c​_​s​t​r​i​n​g​I​w​N​S​3​_​1​1​c​h​a​r​_​t​r​a​i​t​s​I​w​E​E​N​S​3​_​9​a​l​l​o​c​a​t​o​r​I​w​E​E​E​E​N​S​8​_​I​S​A​_​E​E​E​E​E​J​N​S​_​4​n​a​m​e​E​N​S​_​7​s​i​b​l​i​n​g​E​N​S​_​9​i​s​_​m​e​t​h​o​d​E​A​2​8​_​c​E​E​E​M​T​0​_​F​T​_​D​p​T​1​_​E​D​p​R​K​T​2​_
   193  
   194  .. only:: not html
   195  
   196      .. code-block:: cpp
   197  
   198          __ZN8pybind1112cpp_functionC1Iv8Example2JRNSt3__16vectorINS3_12basic_stringIwNS3_11char_traitsIwEENS3_9allocatorIwEEEENS8_ISA_EEEEEJNS_4nameENS_7siblingENS_9is_methodEA28_cEEEMT0_FT_DpT1_EDpRKT2_
   199  
   200  which is the mangled form of the following function type:
   201  
   202  .. code-block:: cpp
   203  
   204      pybind11::cpp_function::cpp_function<void, Example2, std::__1::vector<std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >, std::__1::allocator<std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > > >&, pybind11::name, pybind11::sibling, pybind11::is_method, char [28]>(void (Example2::*)(std::__1::vector<std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >, std::__1::allocator<std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > > >&), pybind11::name const&, pybind11::sibling const&, pybind11::is_method const&, char const (&) [28])
   205  
   206  The memory needed to store just the mangled name of this function (196 bytes)
   207  is larger than the actual piece of code (111 bytes) it represents! On the other
   208  hand, it's silly to even give this function a name -- after all, it's just a
   209  tiny cog in a bigger piece of machinery that is not exposed to the outside
   210  world. So we'll generally only want to export symbols for those functions which
   211  are actually called from the outside.
   212  
   213  This can be achieved by specifying the parameter ``-fvisibility=hidden`` to GCC
   214  and Clang, which sets the default symbol visibility to *hidden*, which has a
   215  tremendous impact on the final binary size of the resulting extension library.
   216  (On Visual Studio, symbols are already hidden by default, so nothing needs to
   217  be done there.)
   218  
   219  In addition to decreasing binary size, ``-fvisibility=hidden`` also avoids
   220  potential serious issues when loading multiple modules and is required for
   221  proper pybind operation.  See the previous FAQ entry for more details.
   222  
   223  How can I properly handle Ctrl-C in long-running functions?
   224  ===========================================================
   225  
   226  Ctrl-C is received by the Python interpreter, and holds it until the GIL
   227  is released, so a long-running function won't be interrupted.
   228  
   229  To interrupt from inside your function, you can use the ``PyErr_CheckSignals()``
   230  function, that will tell if a signal has been raised on the Python side.  This
   231  function merely checks a flag, so its impact is negligible. When a signal has
   232  been received, you must either explicitly interrupt execution by throwing
   233  ``py::error_already_set`` (which will propagate the existing
   234  ``KeyboardInterrupt``), or clear the error (which you usually will not want):
   235  
   236  .. code-block:: cpp
   237  
   238      PYBIND11_MODULE(example, m)
   239      {
   240          m.def("long running_func", []()
   241          {
   242              for (;;) {
   243                  if (PyErr_CheckSignals() != 0)
   244                      throw py::error_already_set();
   245                  // Long running iteration
   246              }
   247          });
   248      }
   249  
   250  CMake doesn't detect the right Python version
   251  =============================================
   252  
   253  The CMake-based build system will try to automatically detect the installed
   254  version of Python and link against that. When this fails, or when there are
   255  multiple versions of Python and it finds the wrong one, delete
   256  ``CMakeCache.txt`` and then add ``-DPYTHON_EXECUTABLE=$(which python)`` to your
   257  CMake configure line. (Replace ``$(which python)`` with a path to python if
   258  your prefer.)
   259  
   260  You can alternatively try ``-DPYBIND11_FINDPYTHON=ON``, which will activate the
   261  new CMake FindPython support instead of pybind11's custom search. Requires
   262  CMake 3.12+, and 3.15+ or 3.18.2+ are even better. You can set this in your
   263  ``CMakeLists.txt`` before adding or finding pybind11, as well.
   264  
   265  Inconsistent detection of Python version in CMake and pybind11
   266  ==============================================================
   267  
   268  The functions ``find_package(PythonInterp)`` and ``find_package(PythonLibs)``
   269  provided by CMake for Python version detection are modified by pybind11 due to
   270  unreliability and limitations that make them unsuitable for pybind11's needs.
   271  Instead pybind11 provides its own, more reliable Python detection CMake code.
   272  Conflicts can arise, however, when using pybind11 in a project that *also* uses
   273  the CMake Python detection in a system with several Python versions installed.
   274  
   275  This difference may cause inconsistencies and errors if *both* mechanisms are
   276  used in the same project.
   277  
   278  There are three possible solutions:
   279  
   280  1. Avoid using ``find_package(PythonInterp)`` and ``find_package(PythonLibs)``
   281     from CMake and rely on pybind11 in detecting Python version. If this is not
   282     possible, the CMake machinery should be called *before* including pybind11.
   283  2. Set ``PYBIND11_FINDPYTHON`` to ``True`` or use ``find_package(Python
   284     COMPONENTS Interpreter Development)`` on modern CMake (3.12+, 3.15+ better,
   285     3.18.2+ best). Pybind11 in these cases uses the new CMake FindPython instead
   286     of the old, deprecated search tools, and these modules are much better at
   287     finding the correct Python.
   288  3. Set ``PYBIND11_NOPYTHON`` to ``TRUE``. Pybind11 will not search for Python.
   289     However, you will have to use the target-based system, and do more setup
   290     yourself, because it does not know about or include things that depend on
   291     Python, like ``pybind11_add_module``. This might be ideal for integrating
   292     into an existing system, like scikit-build's Python helpers.
   293  
   294  How to cite this project?
   295  =========================
   296  
   297  We suggest the following BibTeX template to cite pybind11 in scientific
   298  discourse:
   299  
   300  .. code-block:: bash
   301  
   302      @misc{pybind11,
   303         author = {Wenzel Jakob and Jason Rhinelander and Dean Moldovan},
   304         year = {2017},
   305         note = {https://github.com/pybind/pybind11},
   306         title = {pybind11 -- Seamless operability between C++11 and Python}
   307      }