aboutsummaryrefslogtreecommitdiffstats
path: root/netlib/test/data/verificationcerts/generate.py
diff options
context:
space:
mode:
Diffstat (limited to 'netlib/test/data/verificationcerts/generate.py')
0 files changed, 0 insertions, 0 deletions
d='n84' href='#n84'>84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
/*
    tests/pybind11_cross_module_tests.cpp -- contains tests that require multiple modules

    Copyright (c) 2017 Jason Rhinelander <jason@imaginary.ca>

    All rights reserved. Use of this source code is governed by a
    BSD-style license that can be found in the LICENSE file.
*/

#include <pybind11/stl_bind.h>

#include "local_bindings.h"
#include "pybind11_tests.h"
#include "test_exceptions.h"

#include <numeric>
#include <utility>

PYBIND11_MODULE(pybind11_cross_module_tests, m) {
    m.doc() = "pybind11 cross-module test module";

    // test_local_bindings.py tests:
    //
    // Definitions here are tested by importing both this module and the
    // relevant pybind11_tests submodule from a test_whatever.py

    // test_load_external
    bind_local<ExternalType1>(m, "ExternalType1", py::module_local());
    bind_local<ExternalType2>(m, "ExternalType2", py::module_local());

    // test_exceptions.py
    py::register_local_exception<LocalSimpleException>(m, "LocalSimpleException");
    m.def("raise_runtime_error", []() {
        PyErr_SetString(PyExc_RuntimeError, "My runtime error");
        throw py::error_already_set();
    });
    m.def("raise_value_error", []() {
        PyErr_SetString(PyExc_ValueError, "My value error");
        throw py::error_already_set();
    });
    m.def("throw_pybind_value_error", []() { throw py::value_error("pybind11 value error"); });
    m.def("throw_pybind_type_error", []() { throw py::type_error("pybind11 type error"); });
    m.def("throw_stop_iteration", []() { throw py::stop_iteration(); });
    m.def("throw_local_error", []() { throw LocalException("just local"); });
    m.def("throw_local_simple_error", []() { throw LocalSimpleException("external mod"); });
    py::register_exception_translator([](std::exception_ptr p) {
        try {
            if (p) {
                std::rethrow_exception(p);
            }
        } catch (const shared_exception &e) {
            PyErr_SetString(PyExc_KeyError, e.what());
        }
    });

    // translate the local exception into a key error but only in this module
    py::register_local_exception_translator([](std::exception_ptr p) {
        try {
            if (p) {
                std::rethrow_exception(p);
            }
        } catch (const LocalException &e) {
            PyErr_SetString(PyExc_KeyError, e.what());
        }
    });

    // test_local_bindings.py
    // Local to both:
    bind_local<LocalType, 1>(m, "LocalType", py::module_local()).def("get2", [](LocalType &t) {
        return t.i + 2;
    });

    // Can only be called with our python type:
    m.def("local_value", [](LocalType &l) { return l.i; });

    // test_nonlocal_failure
    // This registration will fail (global registration when LocalFail is already registered
    // globally in the main test module):
    m.def("register_nonlocal", [m]() { bind_local<NonLocalType, 0>(m, "NonLocalType"); });