test_builtin_casters.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. # -*- coding: utf-8 -*-
  2. import pytest
  3. import env # noqa: F401
  4. from pybind11_tests import builtin_casters as m
  5. from pybind11_tests import UserType, IncType
  6. def test_simple_string():
  7. assert m.string_roundtrip("const char *") == "const char *"
  8. def test_unicode_conversion():
  9. """Tests unicode conversion and error reporting."""
  10. assert m.good_utf8_string() == u"Say utf8‽ 🎂 𝐀"
  11. assert m.good_utf16_string() == u"b‽🎂𝐀z"
  12. assert m.good_utf32_string() == u"a𝐀🎂‽z"
  13. assert m.good_wchar_string() == u"a⸘𝐀z"
  14. if hasattr(m, "has_u8string"):
  15. assert m.good_utf8_u8string() == u"Say utf8‽ 🎂 𝐀"
  16. with pytest.raises(UnicodeDecodeError):
  17. m.bad_utf8_string()
  18. with pytest.raises(UnicodeDecodeError):
  19. m.bad_utf16_string()
  20. # These are provided only if they actually fail (they don't when 32-bit and under Python 2.7)
  21. if hasattr(m, "bad_utf32_string"):
  22. with pytest.raises(UnicodeDecodeError):
  23. m.bad_utf32_string()
  24. if hasattr(m, "bad_wchar_string"):
  25. with pytest.raises(UnicodeDecodeError):
  26. m.bad_wchar_string()
  27. if hasattr(m, "has_u8string"):
  28. with pytest.raises(UnicodeDecodeError):
  29. m.bad_utf8_u8string()
  30. assert m.u8_Z() == "Z"
  31. assert m.u8_eacute() == u"é"
  32. assert m.u16_ibang() == u"‽"
  33. assert m.u32_mathbfA() == u"𝐀"
  34. assert m.wchar_heart() == u"♥"
  35. if hasattr(m, "has_u8string"):
  36. assert m.u8_char8_Z() == "Z"
  37. def test_single_char_arguments():
  38. """Tests failures for passing invalid inputs to char-accepting functions"""
  39. def toobig_message(r):
  40. return "Character code point not in range({0:#x})".format(r)
  41. toolong_message = "Expected a character, but multi-character string found"
  42. assert m.ord_char(u"a") == 0x61 # simple ASCII
  43. assert m.ord_char_lv(u"b") == 0x62
  44. assert (
  45. m.ord_char(u"é") == 0xE9
  46. ) # requires 2 bytes in utf-8, but can be stuffed in a char
  47. with pytest.raises(ValueError) as excinfo:
  48. assert m.ord_char(u"Ā") == 0x100 # requires 2 bytes, doesn't fit in a char
  49. assert str(excinfo.value) == toobig_message(0x100)
  50. with pytest.raises(ValueError) as excinfo:
  51. assert m.ord_char(u"ab")
  52. assert str(excinfo.value) == toolong_message
  53. assert m.ord_char16(u"a") == 0x61
  54. assert m.ord_char16(u"é") == 0xE9
  55. assert m.ord_char16_lv(u"ê") == 0xEA
  56. assert m.ord_char16(u"Ā") == 0x100
  57. assert m.ord_char16(u"‽") == 0x203D
  58. assert m.ord_char16(u"♥") == 0x2665
  59. assert m.ord_char16_lv(u"♡") == 0x2661
  60. with pytest.raises(ValueError) as excinfo:
  61. assert m.ord_char16(u"🎂") == 0x1F382 # requires surrogate pair
  62. assert str(excinfo.value) == toobig_message(0x10000)
  63. with pytest.raises(ValueError) as excinfo:
  64. assert m.ord_char16(u"aa")
  65. assert str(excinfo.value) == toolong_message
  66. assert m.ord_char32(u"a") == 0x61
  67. assert m.ord_char32(u"é") == 0xE9
  68. assert m.ord_char32(u"Ā") == 0x100
  69. assert m.ord_char32(u"‽") == 0x203D
  70. assert m.ord_char32(u"♥") == 0x2665
  71. assert m.ord_char32(u"🎂") == 0x1F382
  72. with pytest.raises(ValueError) as excinfo:
  73. assert m.ord_char32(u"aa")
  74. assert str(excinfo.value) == toolong_message
  75. assert m.ord_wchar(u"a") == 0x61
  76. assert m.ord_wchar(u"é") == 0xE9
  77. assert m.ord_wchar(u"Ā") == 0x100
  78. assert m.ord_wchar(u"‽") == 0x203D
  79. assert m.ord_wchar(u"♥") == 0x2665
  80. if m.wchar_size == 2:
  81. with pytest.raises(ValueError) as excinfo:
  82. assert m.ord_wchar(u"🎂") == 0x1F382 # requires surrogate pair
  83. assert str(excinfo.value) == toobig_message(0x10000)
  84. else:
  85. assert m.ord_wchar(u"🎂") == 0x1F382
  86. with pytest.raises(ValueError) as excinfo:
  87. assert m.ord_wchar(u"aa")
  88. assert str(excinfo.value) == toolong_message
  89. if hasattr(m, "has_u8string"):
  90. assert m.ord_char8(u"a") == 0x61 # simple ASCII
  91. assert m.ord_char8_lv(u"b") == 0x62
  92. assert (
  93. m.ord_char8(u"é") == 0xE9
  94. ) # requires 2 bytes in utf-8, but can be stuffed in a char
  95. with pytest.raises(ValueError) as excinfo:
  96. assert m.ord_char8(u"Ā") == 0x100 # requires 2 bytes, doesn't fit in a char
  97. assert str(excinfo.value) == toobig_message(0x100)
  98. with pytest.raises(ValueError) as excinfo:
  99. assert m.ord_char8(u"ab")
  100. assert str(excinfo.value) == toolong_message
  101. def test_bytes_to_string():
  102. """Tests the ability to pass bytes to C++ string-accepting functions. Note that this is
  103. one-way: the only way to return bytes to Python is via the pybind11::bytes class."""
  104. # Issue #816
  105. def to_bytes(s):
  106. b = s if env.PY2 else s.encode("utf8")
  107. assert isinstance(b, bytes)
  108. return b
  109. assert m.strlen(to_bytes("hi")) == 2
  110. assert m.string_length(to_bytes("world")) == 5
  111. assert m.string_length(to_bytes("a\x00b")) == 3
  112. assert m.strlen(to_bytes("a\x00b")) == 1 # C-string limitation
  113. # passing in a utf8 encoded string should work
  114. assert m.string_length(u"💩".encode("utf8")) == 4
  115. @pytest.mark.skipif(not hasattr(m, "has_string_view"), reason="no <string_view>")
  116. def test_string_view(capture):
  117. """Tests support for C++17 string_view arguments and return values"""
  118. assert m.string_view_chars("Hi") == [72, 105]
  119. assert m.string_view_chars("Hi 🎂") == [72, 105, 32, 0xF0, 0x9F, 0x8E, 0x82]
  120. assert m.string_view16_chars(u"Hi 🎂") == [72, 105, 32, 0xD83C, 0xDF82]
  121. assert m.string_view32_chars(u"Hi 🎂") == [72, 105, 32, 127874]
  122. if hasattr(m, "has_u8string"):
  123. assert m.string_view8_chars("Hi") == [72, 105]
  124. assert m.string_view8_chars(u"Hi 🎂") == [72, 105, 32, 0xF0, 0x9F, 0x8E, 0x82]
  125. assert m.string_view_return() == u"utf8 secret 🎂"
  126. assert m.string_view16_return() == u"utf16 secret 🎂"
  127. assert m.string_view32_return() == u"utf32 secret 🎂"
  128. if hasattr(m, "has_u8string"):
  129. assert m.string_view8_return() == u"utf8 secret 🎂"
  130. with capture:
  131. m.string_view_print("Hi")
  132. m.string_view_print("utf8 🎂")
  133. m.string_view16_print(u"utf16 🎂")
  134. m.string_view32_print(u"utf32 🎂")
  135. assert (
  136. capture
  137. == u"""
  138. Hi 2
  139. utf8 🎂 9
  140. utf16 🎂 8
  141. utf32 🎂 7
  142. """
  143. )
  144. if hasattr(m, "has_u8string"):
  145. with capture:
  146. m.string_view8_print("Hi")
  147. m.string_view8_print(u"utf8 🎂")
  148. assert (
  149. capture
  150. == u"""
  151. Hi 2
  152. utf8 🎂 9
  153. """
  154. )
  155. with capture:
  156. m.string_view_print("Hi, ascii")
  157. m.string_view_print("Hi, utf8 🎂")
  158. m.string_view16_print(u"Hi, utf16 🎂")
  159. m.string_view32_print(u"Hi, utf32 🎂")
  160. assert (
  161. capture
  162. == u"""
  163. Hi, ascii 9
  164. Hi, utf8 🎂 13
  165. Hi, utf16 🎂 12
  166. Hi, utf32 🎂 11
  167. """
  168. )
  169. if hasattr(m, "has_u8string"):
  170. with capture:
  171. m.string_view8_print("Hi, ascii")
  172. m.string_view8_print(u"Hi, utf8 🎂")
  173. assert (
  174. capture
  175. == u"""
  176. Hi, ascii 9
  177. Hi, utf8 🎂 13
  178. """
  179. )
  180. def test_integer_casting():
  181. """Issue #929 - out-of-range integer values shouldn't be accepted"""
  182. assert m.i32_str(-1) == "-1"
  183. assert m.i64_str(-1) == "-1"
  184. assert m.i32_str(2000000000) == "2000000000"
  185. assert m.u32_str(2000000000) == "2000000000"
  186. if env.PY2:
  187. assert m.i32_str(long(-1)) == "-1" # noqa: F821 undefined name 'long'
  188. assert m.i64_str(long(-1)) == "-1" # noqa: F821 undefined name 'long'
  189. assert (
  190. m.i64_str(long(-999999999999)) # noqa: F821 undefined name 'long'
  191. == "-999999999999"
  192. )
  193. assert (
  194. m.u64_str(long(999999999999)) # noqa: F821 undefined name 'long'
  195. == "999999999999"
  196. )
  197. else:
  198. assert m.i64_str(-999999999999) == "-999999999999"
  199. assert m.u64_str(999999999999) == "999999999999"
  200. with pytest.raises(TypeError) as excinfo:
  201. m.u32_str(-1)
  202. assert "incompatible function arguments" in str(excinfo.value)
  203. with pytest.raises(TypeError) as excinfo:
  204. m.u64_str(-1)
  205. assert "incompatible function arguments" in str(excinfo.value)
  206. with pytest.raises(TypeError) as excinfo:
  207. m.i32_str(-3000000000)
  208. assert "incompatible function arguments" in str(excinfo.value)
  209. with pytest.raises(TypeError) as excinfo:
  210. m.i32_str(3000000000)
  211. assert "incompatible function arguments" in str(excinfo.value)
  212. if env.PY2:
  213. with pytest.raises(TypeError) as excinfo:
  214. m.u32_str(long(-1)) # noqa: F821 undefined name 'long'
  215. assert "incompatible function arguments" in str(excinfo.value)
  216. with pytest.raises(TypeError) as excinfo:
  217. m.u64_str(long(-1)) # noqa: F821 undefined name 'long'
  218. assert "incompatible function arguments" in str(excinfo.value)
  219. def test_int_convert():
  220. class Int(object):
  221. def __int__(self):
  222. return 42
  223. class NotInt(object):
  224. pass
  225. class Float(object):
  226. def __float__(self):
  227. return 41.99999
  228. class Index(object):
  229. def __index__(self):
  230. return 42
  231. class IntAndIndex(object):
  232. def __int__(self):
  233. return 42
  234. def __index__(self):
  235. return 0
  236. class RaisingTypeErrorOnIndex(object):
  237. def __index__(self):
  238. raise TypeError
  239. def __int__(self):
  240. return 42
  241. class RaisingValueErrorOnIndex(object):
  242. def __index__(self):
  243. raise ValueError
  244. def __int__(self):
  245. return 42
  246. convert, noconvert = m.int_passthrough, m.int_passthrough_noconvert
  247. def requires_conversion(v):
  248. pytest.raises(TypeError, noconvert, v)
  249. def cant_convert(v):
  250. pytest.raises(TypeError, convert, v)
  251. assert convert(7) == 7
  252. assert noconvert(7) == 7
  253. cant_convert(3.14159)
  254. # TODO: Avoid DeprecationWarning in `PyLong_AsLong` (and similar)
  255. if (3, 8) <= env.PY < (3, 10):
  256. with pytest.deprecated_call():
  257. assert convert(Int()) == 42
  258. else:
  259. assert convert(Int()) == 42
  260. requires_conversion(Int())
  261. cant_convert(NotInt())
  262. cant_convert(Float())
  263. # Before Python 3.8, `PyLong_AsLong` does not pick up on `obj.__index__`,
  264. # but pybind11 "backports" this behavior.
  265. assert convert(Index()) == 42
  266. assert noconvert(Index()) == 42
  267. assert convert(IntAndIndex()) == 0 # Fishy; `int(DoubleThought)` == 42
  268. assert noconvert(IntAndIndex()) == 0
  269. assert convert(RaisingTypeErrorOnIndex()) == 42
  270. requires_conversion(RaisingTypeErrorOnIndex())
  271. assert convert(RaisingValueErrorOnIndex()) == 42
  272. requires_conversion(RaisingValueErrorOnIndex())
  273. def test_numpy_int_convert():
  274. np = pytest.importorskip("numpy")
  275. convert, noconvert = m.int_passthrough, m.int_passthrough_noconvert
  276. def require_implicit(v):
  277. pytest.raises(TypeError, noconvert, v)
  278. # `np.intc` is an alias that corresponds to a C++ `int`
  279. assert convert(np.intc(42)) == 42
  280. assert noconvert(np.intc(42)) == 42
  281. # The implicit conversion from np.float32 is undesirable but currently accepted.
  282. # TODO: Avoid DeprecationWarning in `PyLong_AsLong` (and similar)
  283. if (3, 8) <= env.PY < (3, 10):
  284. with pytest.deprecated_call():
  285. assert convert(np.float32(3.14159)) == 3
  286. else:
  287. assert convert(np.float32(3.14159)) == 3
  288. require_implicit(np.float32(3.14159))
  289. def test_tuple(doc):
  290. """std::pair <-> tuple & std::tuple <-> tuple"""
  291. assert m.pair_passthrough((True, "test")) == ("test", True)
  292. assert m.tuple_passthrough((True, "test", 5)) == (5, "test", True)
  293. # Any sequence can be cast to a std::pair or std::tuple
  294. assert m.pair_passthrough([True, "test"]) == ("test", True)
  295. assert m.tuple_passthrough([True, "test", 5]) == (5, "test", True)
  296. assert m.empty_tuple() == ()
  297. assert (
  298. doc(m.pair_passthrough)
  299. == """
  300. pair_passthrough(arg0: Tuple[bool, str]) -> Tuple[str, bool]
  301. Return a pair in reversed order
  302. """
  303. )
  304. assert (
  305. doc(m.tuple_passthrough)
  306. == """
  307. tuple_passthrough(arg0: Tuple[bool, str, int]) -> Tuple[int, str, bool]
  308. Return a triple in reversed order
  309. """
  310. )
  311. assert m.rvalue_pair() == ("rvalue", "rvalue")
  312. assert m.lvalue_pair() == ("lvalue", "lvalue")
  313. assert m.rvalue_tuple() == ("rvalue", "rvalue", "rvalue")
  314. assert m.lvalue_tuple() == ("lvalue", "lvalue", "lvalue")
  315. assert m.rvalue_nested() == ("rvalue", ("rvalue", ("rvalue", "rvalue")))
  316. assert m.lvalue_nested() == ("lvalue", ("lvalue", ("lvalue", "lvalue")))
  317. assert m.int_string_pair() == (2, "items")
  318. def test_builtins_cast_return_none():
  319. """Casters produced with PYBIND11_TYPE_CASTER() should convert nullptr to None"""
  320. assert m.return_none_string() is None
  321. assert m.return_none_char() is None
  322. assert m.return_none_bool() is None
  323. assert m.return_none_int() is None
  324. assert m.return_none_float() is None
  325. assert m.return_none_pair() is None
  326. def test_none_deferred():
  327. """None passed as various argument types should defer to other overloads"""
  328. assert not m.defer_none_cstring("abc")
  329. assert m.defer_none_cstring(None)
  330. assert not m.defer_none_custom(UserType())
  331. assert m.defer_none_custom(None)
  332. assert m.nodefer_none_void(None)
  333. def test_void_caster():
  334. assert m.load_nullptr_t(None) is None
  335. assert m.cast_nullptr_t() is None
  336. def test_reference_wrapper():
  337. """std::reference_wrapper for builtin and user types"""
  338. assert m.refwrap_builtin(42) == 420
  339. assert m.refwrap_usertype(UserType(42)) == 42
  340. assert m.refwrap_usertype_const(UserType(42)) == 42
  341. with pytest.raises(TypeError) as excinfo:
  342. m.refwrap_builtin(None)
  343. assert "incompatible function arguments" in str(excinfo.value)
  344. with pytest.raises(TypeError) as excinfo:
  345. m.refwrap_usertype(None)
  346. assert "incompatible function arguments" in str(excinfo.value)
  347. assert m.refwrap_lvalue().value == 1
  348. assert m.refwrap_lvalue_const().value == 1
  349. a1 = m.refwrap_list(copy=True)
  350. a2 = m.refwrap_list(copy=True)
  351. assert [x.value for x in a1] == [2, 3]
  352. assert [x.value for x in a2] == [2, 3]
  353. assert not a1[0] is a2[0] and not a1[1] is a2[1]
  354. b1 = m.refwrap_list(copy=False)
  355. b2 = m.refwrap_list(copy=False)
  356. assert [x.value for x in b1] == [1, 2]
  357. assert [x.value for x in b2] == [1, 2]
  358. assert b1[0] is b2[0] and b1[1] is b2[1]
  359. assert m.refwrap_iiw(IncType(5)) == 5
  360. assert m.refwrap_call_iiw(IncType(10), m.refwrap_iiw) == [10, 10, 10, 10]
  361. def test_complex_cast():
  362. """std::complex casts"""
  363. assert m.complex_cast(1) == "1.0"
  364. assert m.complex_cast(2j) == "(0.0, 2.0)"
  365. def test_bool_caster():
  366. """Test bool caster implicit conversions."""
  367. convert, noconvert = m.bool_passthrough, m.bool_passthrough_noconvert
  368. def require_implicit(v):
  369. pytest.raises(TypeError, noconvert, v)
  370. def cant_convert(v):
  371. pytest.raises(TypeError, convert, v)
  372. # straight up bool
  373. assert convert(True) is True
  374. assert convert(False) is False
  375. assert noconvert(True) is True
  376. assert noconvert(False) is False
  377. # None requires implicit conversion
  378. require_implicit(None)
  379. assert convert(None) is False
  380. class A(object):
  381. def __init__(self, x):
  382. self.x = x
  383. def __nonzero__(self):
  384. return self.x
  385. def __bool__(self):
  386. return self.x
  387. class B(object):
  388. pass
  389. # Arbitrary objects are not accepted
  390. cant_convert(object())
  391. cant_convert(B())
  392. # Objects with __nonzero__ / __bool__ defined can be converted
  393. require_implicit(A(True))
  394. assert convert(A(True)) is True
  395. assert convert(A(False)) is False
  396. def test_numpy_bool():
  397. np = pytest.importorskip("numpy")
  398. convert, noconvert = m.bool_passthrough, m.bool_passthrough_noconvert
  399. def cant_convert(v):
  400. pytest.raises(TypeError, convert, v)
  401. # np.bool_ is not considered implicit
  402. assert convert(np.bool_(True)) is True
  403. assert convert(np.bool_(False)) is False
  404. assert noconvert(np.bool_(True)) is True
  405. assert noconvert(np.bool_(False)) is False
  406. cant_convert(np.zeros(2, dtype="int"))
  407. def test_int_long():
  408. """In Python 2, a C++ int should return a Python int rather than long
  409. if possible: longs are not always accepted where ints are used (such
  410. as the argument to sys.exit()). A C++ long long is always a Python
  411. long."""
  412. import sys
  413. must_be_long = type(getattr(sys, "maxint", 1) + 1)
  414. assert isinstance(m.int_cast(), int)
  415. assert isinstance(m.long_cast(), int)
  416. assert isinstance(m.longlong_cast(), must_be_long)
  417. def test_void_caster_2():
  418. assert m.test_void_caster()
  419. def test_const_ref_caster():
  420. """Verifies that const-ref is propagated through type_caster cast_op.
  421. The returned ConstRefCasted type is a mimimal type that is constructed to
  422. reference the casting mode used.
  423. """
  424. x = False
  425. assert m.takes(x) == 1
  426. assert m.takes_move(x) == 1
  427. assert m.takes_ptr(x) == 3
  428. assert m.takes_ref(x) == 2
  429. assert m.takes_ref_wrap(x) == 2
  430. assert m.takes_const_ptr(x) == 5
  431. assert m.takes_const_ref(x) == 4
  432. assert m.takes_const_ref_wrap(x) == 4