Richard Kreckel
25 years ago
commit
d57000b193
11 changed files with 196 additions and 0 deletions
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Set-Cookie: i_like_gitea=ed0585c0d56bc638; Path=/; HttpOnly; SameSite=Lax
Set-Cookie: _csrf=hMPwzetG6HEn24212YOTkHcBAjY6MTczNDk4MTA5MTE0MTUxMDQ3MQ; Path=/; Expires=Tue, 24 Dec 2024 19:11:31 GMT; HttpOnly; SameSite=Lax
Set-Cookie: macaron_flash=; Path=/; Max-Age=0; HttpOnly; SameSite=Lax
X-Frame-Options: SAMEORIGIN
Date: Mon, 23 Dec 2024 19:11:31 GMT
Transfer-Encoding: chunked
db16
sp/cln_mirror - include/cln/output.h at 6d4cabf35e0bc58bac8d4a348c9d2349df183fdf - cln_mirror - Gitea: Git with a cup of tea
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
1.9 KiB
// Output functions.
#ifndef _CL_OUTPUT_H
#define _CL_OUTPUT_H
#include "cln/types.h"
#include "cln/floatformat.h"
#include "cln/io.h"
#include "cln/string.h"
namespace cln {
struct cl_print_rational_flags {
// Base in which rational numbers are to be printed.
unsigned int rational_base;
// Flag whether to print radix specifiers in Common Lisp syntax for
// rational numbers (#nR or #b or #o or #x prefixes, trailing dot).
cl_boolean rational_readably;
// Constructor.
cl_print_rational_flags () :
rational_base (10),
rational_readably (cl_false) {}
};
struct cl_print_float_flags {
// Flag whether to prefer type specific exponent markers over 'E'.
cl_boolean float_readably;
// If !float_readably, the format which earns the 'E' exponent marker.
float_format_t default_float_format;
// Constructor.
cl_print_float_flags () :
float_readably (cl_false),
default_float_format (float_format_ffloat) {}
};
struct cl_print_real_flags : cl_print_rational_flags, cl_print_float_flags {};
struct cl_print_complex_flags {
// Flag whether to use the Common Lisp #C(realpart imagpart) syntax,
cl_boolean complex_readably;
// Constructor.
cl_print_complex_flags () :
complex_readably (cl_false) {}
};
struct cl_print_number_flags : cl_print_real_flags, cl_print_complex_flags {};
enum cl_print_vector_syntax_t {
vsyntax_algebraic, // [a, b, c]
vsyntax_pretty, // [a b c]
vsyntax_commonlisp // #(a b c)
};
struct cl_print_vector_flags {
cl_print_vector_syntax_t vector_syntax;
// Constructor.
cl_print_vector_flags () :
vector_syntax (vsyntax_pretty) {}
};
struct cl_print_univpoly_flags {
cl_string univpoly_varname;
// Constructor.
cl_print_univpoly_flags () :
univpoly_varname ("x") {}
};
struct cl_print_flags : cl_print_number_flags, cl_print_vector_flags, cl_print_univpoly_flags {};
extern cl_print_flags default_print_flags;
} // namespace cln
#endif /* _CL_OUTPUT_H */
0
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Set-Cookie: i_like_gitea=ef3244eb29401f65; Path=/; HttpOnly; SameSite=Lax
Set-Cookie: _csrf=zP6c9sZb1aalG48bcYPI-kQFLKE6MTczNDk4MTA5MTA2OTA2MTE4Nw; Path=/; Expires=Tue, 24 Dec 2024 19:11:31 GMT; HttpOnly; SameSite=Lax
Set-Cookie: macaron_flash=; Path=/; Max-Age=0; HttpOnly; SameSite=Lax
X-Frame-Options: SAMEORIGIN
Date: Mon, 23 Dec 2024 19:11:31 GMT
Transfer-Encoding: chunked
20663
sp/tempestpy - resources/pybind11/tests/test_builtin_casters.py at da97ba15909014b1f3a15428ccdc45888712050d - tempestpy - Gitea: Git with a cup of tea
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
221 lines
7.8 KiB
# Python < 3 needs this: coding=utf-8
import pytest
from pybind11_tests import builtin_casters as m
from pybind11_tests import UserType, IncType
def test_simple_string():
assert m.string_roundtrip("const char *") == "const char *"
def test_unicode_conversion():
"""Tests unicode conversion and error reporting."""
assert m.good_utf8_string() == u"Say utf8โฝ ๐ ๐"
assert m.good_utf16_string() == u"bโฝ๐๐z"
assert m.good_utf32_string() == u"a๐๐โฝz"
assert m.good_wchar_string() == u"aโธ๐z"
with pytest.raises(UnicodeDecodeError):
m.bad_utf8_string()
with pytest.raises(UnicodeDecodeError):
m.bad_utf16_string()
# These are provided only if they actually fail (they don't when 32-bit and under Python 2.7)
if hasattr(m, "bad_utf32_string"):
with pytest.raises(UnicodeDecodeError):
m.bad_utf32_string()
if hasattr(m, "bad_wchar_string"):
with pytest.raises(UnicodeDecodeError):
m.bad_wchar_string()
assert m.u8_Z() == 'Z'
assert m.u8_eacute() == u'รฉ'
assert m.u16_ibang() == u'โฝ'
assert m.u32_mathbfA() == u'๐'
assert m.wchar_heart() == u'โฅ'
def test_single_char_arguments():
"""Tests failures for passing invalid inputs to char-accepting functions"""
def toobig_message(r):
return "Character code point not in range({0:#x})".format(r)
toolong_message = "Expected a character, but multi-character string found"
assert m.ord_char(u'a') == 0x61 # simple ASCII
assert m.ord_char(u'รฉ') == 0xE9 # requires 2 bytes in utf-8, but can be stuffed in a char
with pytest.raises(ValueError) as excinfo:
assert m.ord_char(u'ฤ') == 0x100 # requires 2 bytes, doesn't fit in a char
assert str(excinfo.value) == toobig_message(0x100)
with pytest.raises(ValueError) as excinfo:
assert m.ord_char(u'ab')
assert str(excinfo.value) == toolong_message
assert m.ord_char16(u'a') == 0x61
assert m.ord_char16(u'รฉ') == 0xE9
assert m.ord_char16(u'ฤ') == 0x100
assert m.ord_char16(u'โฝ') == 0x203d
assert m.ord_char16(u'โฅ') == 0x2665
with pytest.raises(ValueError) as excinfo:
assert m.ord_char16(u'๐') == 0x1F382 # requires surrogate pair
assert str(excinfo.value) == toobig_message(0x10000)
with pytest.raises(ValueError) as excinfo:
assert m.ord_char16(u'aa')
assert str(excinfo.value) == toolong_message
assert m.ord_char32(u'a') == 0x61
assert m.ord_char32(u'รฉ') == 0xE9
assert m.ord_char32(u'ฤ') == 0x100
assert m.ord_char32(u'โฝ') == 0x203d
assert m.ord_char32(u'โฅ') == 0x2665
assert m.ord_char32(u'๐') == 0x1F382
with pytest.raises(ValueError) as excinfo:
assert m.ord_char32(u'aa')
assert str(excinfo.value) == toolong_message
assert m.ord_wchar(u'a') == 0x61
assert m.ord_wchar(u'รฉ') == 0xE9
assert m.ord_wchar(u'ฤ') == 0x100
assert m.ord_wchar(u'โฝ') == 0x203d
assert m.ord_wchar(u'โฅ') == 0x2665
if m.wchar_size == 2:
with pytest.raises(ValueError) as excinfo:
assert m.ord_wchar(u'๐') == 0x1F382 # requires surrogate pair
assert str(excinfo.value) == toobig_message(0x10000)
else:
assert m.ord_wchar(u'๐') == 0x1F382
with pytest.raises(ValueError) as excinfo:
assert m.ord_wchar(u'aa')
assert str(excinfo.value) == toolong_message
def test_bytes_to_string():
"""Tests the ability to pass bytes to C++ string-accepting functions. Note that this is
one-way: the only way to return bytes to Python is via the pybind11::bytes class."""
# Issue #816
import sys
byte = bytes if sys.version_info[0] < 3 else str
assert m.strlen(byte("hi")) == 2
assert m.string_length(byte("world")) == 5
assert m.string_length(byte("a\x00b")) == 3
assert m.strlen(byte("a\x00b")) == 1 # C-string limitation
# passing in a utf8 encoded string should work
assert m.string_length(u'๐ฉ'.encode("utf8")) == 4
@pytest.mark.skipif(not hasattr(m, "has_string_view"), reason="no <string_view>")
def test_string_view(capture):
"""Tests support for C++17 string_view arguments and return values"""
assert m.string_view_chars("Hi") == [72, 105]
assert m.string_view_chars("Hi ๐") == [72, 105, 32, 0xf0, 0x9f, 0x8e, 0x82]
assert m.string_view16_chars("Hi ๐") == [72, 105, 32, 0xd83c, 0xdf82]
assert m.string_view32_chars("Hi ๐") == [72, 105, 32, 127874]
assert m.string_view_return() == "utf8 secret ๐"
assert m.string_view16_return() == "utf16 secret ๐"
assert m.string_view32_return() == "utf32 secret ๐"
with capture:
m.string_view_print("Hi")
m.string_view_print("utf8 ๐")
m.string_view16_print("utf16 ๐")
m.string_view32_print("utf32 ๐")
assert capture == """
Hi 2
utf8 ๐ 9
utf16 ๐ 8
utf32 ๐ 7
"""
with capture:
m.string_view_print("Hi, ascii")
m.string_view_print("Hi, utf8 ๐")
m.string_view16_print("Hi, utf16 ๐")
m.string_view32_print("Hi, utf32 ๐")
assert capture == """
Hi, ascii 9
Hi, utf8 ๐ 13
Hi, utf16 ๐ 12
Hi, utf32 ๐ 11
"""
def test_tuple(doc):
"""std::pair <-> tuple & std::tuple <-> tuple"""
assert m.pair_passthrough((True, "test")) == ("test", True)
assert m.tuple_passthrough((True, "test", 5)) == (5, "test", True)
# Any sequence can be cast to a std::pair or std::tuple
assert m.pair_passthrough([True, "test"]) == ("test", True)
assert m.tuple_passthrough([True, "test", 5]) == (5, "test", True)
assert doc(m.pair_passthrough) == """
pair_passthrough(arg0: Tuple[bool, str]) -> Tuple[str, bool]
Return a pair in reversed order
"""
assert doc(m.tuple_passthrough) == """
tuple_passthrough(arg0: Tuple[bool, str, int]) -> Tuple[int, str, bool]
Return a triple in reversed order
"""
def test_builtins_cast_return_none():
"""Casters produced with PYBIND11_TYPE_CASTER() should convert nullptr to None"""
assert m.return_none_string() is None
assert m.return_none_char() is None
assert m.return_none_bool() is None
assert m.return_none_int() is None
assert m.return_none_float() is None
def test_none_deferred():
"""None passed as various argument types should defer to other overloads"""
assert not m.defer_none_cstring("abc")
assert m.defer_none_cstring(None)
assert not m.defer_none_custom(UserType())
assert m.defer_none_custom(None)
assert m.nodefer_none_void(None)
def test_void_caster():
assert m.load_nullptr_t(None) is None
assert m.cast_nullptr_t() is None
def test_reference_wrapper():
"""std::reference_wrapper for builtin and user types"""
assert m.refwrap_builtin(42) == 420
assert m.refwrap_usertype(UserType(42)) == 42
with pytest.raises(TypeError) as excinfo:
m.refwrap_builtin(None)
assert "incompatible function arguments" in str(excinfo.value)
with pytest.raises(TypeError) as excinfo:
m.refwrap_usertype(None)
assert "incompatible function arguments" in str(excinfo.value)
a1 = m.refwrap_list(copy=True)
a2 = m.refwrap_list(copy=True)
assert [x.value for x in a1] == [2, 3]
assert [x.value for x in a2] == [2, 3]
assert not a1[0] is a2[0] and not a1[1] is a2[1]
b1 = m.refwrap_list(copy=False)
b2 = m.refwrap_list(copy=False)
assert [x.value for x in b1] == [1, 2]
assert [x.value for x in b2] == [1, 2]
assert b1[0] is b2[0] and b1[1] is b2[1]
assert m.refwrap_iiw(IncType(5)) == 5
assert m.refwrap_call_iiw(
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Set-Cookie: i_like_gitea=ed0585c0d56bc638; Path=/; HttpOnly; SameSite=Lax
Set-Cookie: _csrf=hMPwzetG6HEn24212YOTkHcBAjY6MTczNDk4MTA5MTE0MTUxMDQ3MQ; Path=/; Expires=Tue, 24 Dec 2024 19:11:31 GMT; HttpOnly; SameSite=Lax
Set-Cookie: macaron_flash=; Path=/; Max-Age=0; HttpOnly; SameSite=Lax
X-Frame-Options: SAMEORIGIN
Date: Mon, 23 Dec 2024 19:11:31 GMT
Transfer-Encoding: chunked
db16
sp/cln_mirror - include/cln/output.h at 6d4cabf35e0bc58bac8d4a348c9d2349df183fdf - cln_mirror - Gitea: Git with a cup of tea
0
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Set-Cookie: i_like_gitea=ef3244eb29401f65; Path=/; HttpOnly; SameSite=Lax
Set-Cookie: _csrf=zP6c9sZb1aalG48bcYPI-kQFLKE6MTczNDk4MTA5MTA2OTA2MTE4Nw; Path=/; Expires=Tue, 24 Dec 2024 19:11:31 GMT; HttpOnly; SameSite=Lax
Set-Cookie: macaron_flash=; Path=/; Max-Age=0; HttpOnly; SameSite=Lax
X-Frame-Options: SAMEORIGIN
Date: Mon, 23 Dec 2024 19:11:31 GMT
Transfer-Encoding: chunked
20663
sp/tempestpy - resources/pybind11/tests/test_builtin_casters.py at da97ba15909014b1f3a15428ccdc45888712050d - tempestpy - Gitea: Git with a cup of tea
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
1.9 KiB
74 lines
1.9 KiB
// Output functions.
|
|
|
|
#ifndef _CL_OUTPUT_H
|
|
#define _CL_OUTPUT_H
|
|
|
|
#include "cln/types.h"
|
|
#include "cln/floatformat.h"
|
|
#include "cln/io.h"
|
|
#include "cln/string.h"
|
|
|
|
namespace cln {
|
|
|
|
struct cl_print_rational_flags {
|
|
// Base in which rational numbers are to be printed.
|
|
unsigned int rational_base;
|
|
// Flag whether to print radix specifiers in Common Lisp syntax for
|
|
// rational numbers (#nR or #b or #o or #x prefixes, trailing dot).
|
|
cl_boolean rational_readably;
|
|
// Constructor.
|
|
cl_print_rational_flags () :
|
|
rational_base (10),
|
|
rational_readably (cl_false) {}
|
|
};
|
|
|
|
struct cl_print_float_flags {
|
|
// Flag whether to prefer type specific exponent markers over 'E'.
|
|
cl_boolean float_readably;
|
|
// If !float_readably, the format which earns the 'E' exponent marker.
|
|
float_format_t default_float_format;
|
|
// Constructor.
|
|
cl_print_float_flags () :
|
|
float_readably (cl_false),
|
|
default_float_format (float_format_ffloat) {}
|
|
};
|
|
|
|
struct cl_print_real_flags : cl_print_rational_flags, cl_print_float_flags {};
|
|
|
|
struct cl_print_complex_flags {
|
|
// Flag whether to use the Common Lisp #C(realpart imagpart) syntax,
|
|
cl_boolean complex_readably;
|
|
// Constructor.
|
|
cl_print_complex_flags () :
|
|
complex_readably (cl_false) {}
|
|
};
|
|
|
|
struct cl_print_number_flags : cl_print_real_flags, cl_print_complex_flags {};
|
|
|
|
enum cl_print_vector_syntax_t {
|
|
vsyntax_algebraic, // [a, b, c]
|
|
vsyntax_pretty, // [a b c]
|
|
vsyntax_commonlisp // #(a b c)
|
|
};
|
|
|
|
struct cl_print_vector_flags {
|
|
cl_print_vector_syntax_t vector_syntax;
|
|
// Constructor.
|
|
cl_print_vector_flags () :
|
|
vector_syntax (vsyntax_pretty) {}
|
|
};
|
|
|
|
struct cl_print_univpoly_flags {
|
|
cl_string univpoly_varname;
|
|
// Constructor.
|
|
cl_print_univpoly_flags () :
|
|
univpoly_varname ("x") {}
|
|
};
|
|
|
|
struct cl_print_flags : cl_print_number_flags, cl_print_vector_flags, cl_print_univpoly_flags {};
|
|
|
|
extern cl_print_flags default_print_flags;
|
|
|
|
} // namespace cln
|
|
|
|
#endif /* _CL_OUTPUT_H */
|
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
221 lines
7.8 KiB
221 lines
7.8 KiB
# Python < 3 needs this: coding=utf-8
|
|
import pytest
|
|
|
|
from pybind11_tests import builtin_casters as m
|
|
from pybind11_tests import UserType, IncType
|
|
|
|
|
|
def test_simple_string():
|
|
assert m.string_roundtrip("const char *") == "const char *"
|
|
|
|
|
|
def test_unicode_conversion():
|
|
"""Tests unicode conversion and error reporting."""
|
|
assert m.good_utf8_string() == u"Say utf8โฝ ๐ ๐"
|
|
assert m.good_utf16_string() == u"bโฝ๐๐z"
|
|
assert m.good_utf32_string() == u"a๐๐โฝz"
|
|
assert m.good_wchar_string() == u"aโธ๐z"
|
|
|
|
with pytest.raises(UnicodeDecodeError):
|
|
m.bad_utf8_string()
|
|
|
|
with pytest.raises(UnicodeDecodeError):
|
|
m.bad_utf16_string()
|
|
|
|
# These are provided only if they actually fail (they don't when 32-bit and under Python 2.7)
|
|
if hasattr(m, "bad_utf32_string"):
|
|
with pytest.raises(UnicodeDecodeError):
|
|
m.bad_utf32_string()
|
|
if hasattr(m, "bad_wchar_string"):
|
|
with pytest.raises(UnicodeDecodeError):
|
|
m.bad_wchar_string()
|
|
|
|
assert m.u8_Z() == 'Z'
|
|
assert m.u8_eacute() == u'รฉ'
|
|
assert m.u16_ibang() == u'โฝ'
|
|
assert m.u32_mathbfA() == u'๐'
|
|
assert m.wchar_heart() == u'โฅ'
|
|
|
|
|
|
def test_single_char_arguments():
|
|
"""Tests failures for passing invalid inputs to char-accepting functions"""
|
|
def toobig_message(r):
|
|
return "Character code point not in range({0:#x})".format(r)
|
|
toolong_message = "Expected a character, but multi-character string found"
|
|
|
|
assert m.ord_char(u'a') == 0x61 # simple ASCII
|
|
assert m.ord_char(u'รฉ') == 0xE9 # requires 2 bytes in utf-8, but can be stuffed in a char
|
|
with pytest.raises(ValueError) as excinfo:
|
|
assert m.ord_char(u'ฤ') == 0x100 # requires 2 bytes, doesn't fit in a char
|
|
assert str(excinfo.value) == toobig_message(0x100)
|
|
with pytest.raises(ValueError) as excinfo:
|
|
assert m.ord_char(u'ab')
|
|
assert str(excinfo.value) == toolong_message
|
|
|
|
assert m.ord_char16(u'a') == 0x61
|
|
assert m.ord_char16(u'รฉ') == 0xE9
|
|
assert m.ord_char16(u'ฤ') == 0x100
|
|
assert m.ord_char16(u'โฝ') == 0x203d
|
|
assert m.ord_char16(u'โฅ') == 0x2665
|
|
with pytest.raises(ValueError) as excinfo:
|
|
assert m.ord_char16(u'๐') == 0x1F382 # requires surrogate pair
|
|
assert str(excinfo.value) == toobig_message(0x10000)
|
|
with pytest.raises(ValueError) as excinfo:
|
|
assert m.ord_char16(u'aa')
|
|
assert str(excinfo.value) == toolong_message
|
|
|
|
assert m.ord_char32(u'a') == 0x61
|
|
assert m.ord_char32(u'รฉ') == 0xE9
|
|
assert m.ord_char32(u'ฤ') == 0x100
|
|
assert m.ord_char32(u'โฝ') == 0x203d
|
|
assert m.ord_char32(u'โฅ') == 0x2665
|
|
assert m.ord_char32(u'๐') == 0x1F382
|
|
with pytest.raises(ValueError) as excinfo:
|
|
assert m.ord_char32(u'aa')
|
|
assert str(excinfo.value) == toolong_message
|
|
|
|
assert m.ord_wchar(u'a') == 0x61
|
|
assert m.ord_wchar(u'รฉ') == 0xE9
|
|
assert m.ord_wchar(u'ฤ') == 0x100
|
|
assert m.ord_wchar(u'โฝ') == 0x203d
|
|
assert m.ord_wchar(u'โฅ') == 0x2665
|
|
if m.wchar_size == 2:
|
|
with pytest.raises(ValueError) as excinfo:
|
|
assert m.ord_wchar(u'๐') == 0x1F382 # requires surrogate pair
|
|
assert str(excinfo.value) == toobig_message(0x10000)
|
|
else:
|
|
assert m.ord_wchar(u'๐') == 0x1F382
|
|
with pytest.raises(ValueError) as excinfo:
|
|
assert m.ord_wchar(u'aa')
|
|
assert str(excinfo.value) == toolong_message
|
|
|
|
|
|
def test_bytes_to_string():
|
|
"""Tests the ability to pass bytes to C++ string-accepting functions. Note that this is
|
|
one-way: the only way to return bytes to Python is via the pybind11::bytes class."""
|
|
# Issue #816
|
|
import sys
|
|
byte = bytes if sys.version_info[0] < 3 else str
|
|
|
|
assert m.strlen(byte("hi")) == 2
|
|
assert m.string_length(byte("world")) == 5
|
|
assert m.string_length(byte("a\x00b")) == 3
|
|
assert m.strlen(byte("a\x00b")) == 1 # C-string limitation
|
|
|
|
# passing in a utf8 encoded string should work
|
|
assert m.string_length(u'๐ฉ'.encode("utf8")) == 4
|
|
|
|
|
|
@pytest.mark.skipif(not hasattr(m, "has_string_view"), reason="no <string_view>")
|
|
def test_string_view(capture):
|
|
"""Tests support for C++17 string_view arguments and return values"""
|
|
assert m.string_view_chars("Hi") == [72, 105]
|
|
assert m.string_view_chars("Hi ๐") == [72, 105, 32, 0xf0, 0x9f, 0x8e, 0x82]
|
|
assert m.string_view16_chars("Hi ๐") == [72, 105, 32, 0xd83c, 0xdf82]
|
|
assert m.string_view32_chars("Hi ๐") == [72, 105, 32, 127874]
|
|
|
|
assert m.string_view_return() == "utf8 secret ๐"
|
|
assert m.string_view16_return() == "utf16 secret ๐"
|
|
assert m.string_view32_return() == "utf32 secret ๐"
|
|
|
|
with capture:
|
|
m.string_view_print("Hi")
|
|
m.string_view_print("utf8 ๐")
|
|
m.string_view16_print("utf16 ๐")
|
|
m.string_view32_print("utf32 ๐")
|
|
assert capture == """
|
|
Hi 2
|
|
utf8 ๐ 9
|
|
utf16 ๐ 8
|
|
utf32 ๐ 7
|
|
"""
|
|
|
|
with capture:
|
|
m.string_view_print("Hi, ascii")
|
|
m.string_view_print("Hi, utf8 ๐")
|
|
m.string_view16_print("Hi, utf16 ๐")
|
|
m.string_view32_print("Hi, utf32 ๐")
|
|
assert capture == """
|
|
Hi, ascii 9
|
|
Hi, utf8 ๐ 13
|
|
Hi, utf16 ๐ 12
|
|
Hi, utf32 ๐ 11
|
|
"""
|
|
|
|
|
|
def test_tuple(doc):
|
|
"""std::pair <-> tuple & std::tuple <-> tuple"""
|
|
assert m.pair_passthrough((True, "test")) == ("test", True)
|
|
assert m.tuple_passthrough((True, "test", 5)) == (5, "test", True)
|
|
# Any sequence can be cast to a std::pair or std::tuple
|
|
assert m.pair_passthrough([True, "test"]) == ("test", True)
|
|
assert m.tuple_passthrough([True, "test", 5]) == (5, "test", True)
|
|
|
|
assert doc(m.pair_passthrough) == """
|
|
pair_passthrough(arg0: Tuple[bool, str]) -> Tuple[str, bool]
|
|
|
|
Return a pair in reversed order
|
|
"""
|
|
assert doc(m.tuple_passthrough) == """
|
|
tuple_passthrough(arg0: Tuple[bool, str, int]) -> Tuple[int, str, bool]
|
|
|
|
Return a triple in reversed order
|
|
"""
|
|
|
|
|
|
def test_builtins_cast_return_none():
|
|
"""Casters produced with PYBIND11_TYPE_CASTER() should convert nullptr to None"""
|
|
assert m.return_none_string() is None
|
|
assert m.return_none_char() is None
|
|
assert m.return_none_bool() is None
|
|
assert m.return_none_int() is None
|
|
assert m.return_none_float() is None
|
|
|
|
|
|
def test_none_deferred():
|
|
"""None passed as various argument types should defer to other overloads"""
|
|
assert not m.defer_none_cstring("abc")
|
|
assert m.defer_none_cstring(None)
|
|
assert not m.defer_none_custom(UserType())
|
|
assert m.defer_none_custom(None)
|
|
assert m.nodefer_none_void(None)
|
|
|
|
|
|
def test_void_caster():
|
|
assert m.load_nullptr_t(None) is None
|
|
assert m.cast_nullptr_t() is None
|
|
|
|
|
|
def test_reference_wrapper():
|
|
"""std::reference_wrapper for builtin and user types"""
|
|
assert m.refwrap_builtin(42) == 420
|
|
assert m.refwrap_usertype(UserType(42)) == 42
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
m.refwrap_builtin(None)
|
|
assert "incompatible function arguments" in str(excinfo.value)
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
m.refwrap_usertype(None)
|
|
assert "incompatible function arguments" in str(excinfo.value)
|
|
|
|
a1 = m.refwrap_list(copy=True)
|
|
a2 = m.refwrap_list(copy=True)
|
|
assert [x.value for x in a1] == [2, 3]
|
|
assert [x.value for x in a2] == [2, 3]
|
|
assert not a1[0] is a2[0] and not a1[1] is a2[1]
|
|
|
|
b1 = m.refwrap_list(copy=False)
|
|
b2 = m.refwrap_list(copy=False)
|
|
assert [x.value for x in b1] == [1, 2]
|
|
assert [x.value for x in b2] == [1, 2]
|
|
assert b1[0] is b2[0] and b1[1] is b2[1]
|
|
|
|
assert m.refwrap_iiw(IncType(5)) == 5
|
|
assert m.refwrap_call_iiw( |