# -*- coding: utf-8 -*- import pytest from pybind11_tests import stl as m from pybind11_tests import UserType from pybind11_tests import ConstructorStats def test_vector(doc): """std::vector <-> list""" lst = m.cast_vector() assert lst == [1] lst.append(2) assert m.load_vector(lst) assert m.load_vector(tuple(lst)) assert m.cast_bool_vector() == [True, False] assert m.load_bool_vector([True, False]) assert doc(m.cast_vector) == "cast_vector() -> List[int]" assert doc(m.load_vector) == "load_vector(arg0: List[int]) -> bool" # Test regression caused by 936: pointers to stl containers weren't castable assert m.cast_ptr_vector() == ["lvalue", "lvalue"] def test_deque(doc): """std::deque <-> list""" lst = m.cast_deque() assert lst == [1] lst.append(2) assert m.load_deque(lst) assert m.load_deque(tuple(lst)) def test_array(doc): """std::array <-> list""" lst = m.cast_array() assert lst == [1, 2] assert m.load_array(lst) assert doc(m.cast_array) == "cast_array() -> List[int[2]]" assert doc(m.load_array) == "load_array(arg0: List[int[2]]) -> bool" def test_valarray(doc): """std::valarray <-> list""" lst = m.cast_valarray() assert lst == [1, 4, 9] assert m.load_valarray(lst) assert doc(m.cast_valarray) == "cast_valarray() -> List[int]" assert doc(m.load_valarray) == "load_valarray(arg0: List[int]) -> bool" def test_map(doc): """std::map <-> dict""" d = m.cast_map() assert d == {"key": "value"} assert "key" in d d["key2"] = "value2" assert "key2" in d assert m.load_map(d) assert doc(m.cast_map) == "cast_map() -> Dict[str, str]" assert doc(m.load_map) == "load_map(arg0: Dict[str, str]) -> bool" def test_set(doc): """std::set <-> set""" s = m.cast_set() assert s == {"key1", "key2"} s.add("key3") assert m.load_set(s) assert doc(m.cast_set) == "cast_set() -> Set[str]" assert doc(m.load_set) == "load_set(arg0: Set[str]) -> bool" def test_recursive_casting(): """Tests that stl casters preserve lvalue/rvalue context for container values""" assert m.cast_rv_vector() == ["rvalue", "rvalue"] assert m.cast_lv_vector() == ["lvalue", "lvalue"] assert m.cast_rv_array() == ["rvalue", "rvalue", "rvalue"] assert m.cast_lv_array() == ["lvalue", "lvalue"] assert m.cast_rv_map() == {"a": "rvalue"} assert m.cast_lv_map() == {"a": "lvalue", "b": "lvalue"} assert m.cast_rv_nested() == [[[{"b": "rvalue", "c": "rvalue"}], [{"a": "rvalue"}]]] assert m.cast_lv_nested() == { "a": [[["lvalue", "lvalue"]], [["lvalue", "lvalue"]]], "b": [[["lvalue", "lvalue"], ["lvalue", "lvalue"]]], } # Issue #853 test case: z = m.cast_unique_ptr_vector() assert z[0].value == 7 and z[1].value == 42 def test_move_out_container(): """Properties use the `reference_internal` policy by default. If the underlying function returns an rvalue, the policy is automatically changed to `move` to avoid referencing a temporary. In case the return value is a container of user-defined types, the policy also needs to be applied to the elements, not just the container.""" c = m.MoveOutContainer() moved_out_list = c.move_list assert [x.value for x in moved_out_list] == [0, 1, 2] @pytest.mark.skipif(not hasattr(m, "has_optional"), reason="no ") def test_optional(): assert m.double_or_zero(None) == 0 assert m.double_or_zero(42) == 84 pytest.raises(TypeError, m.double_or_zero, "foo") assert m.half_or_none(0) is None assert m.half_or_none(42) == 21 pytest.raises(TypeError, m.half_or_none, "foo") assert m.test_nullopt() == 42 assert m.test_nullopt(None) == 42 assert m.test_nullopt(42) == 42 assert m.test_nullopt(43) == 43 assert m.test_no_assign() == 42 assert m.test_no_assign(None) == 42 assert m.test_no_assign(m.NoAssign(43)) == 43 pytest.raises(TypeError, m.test_no_assign, 43) assert m.nodefer_none_optional(None) holder = m.OptionalHolder() mvalue = holder.member assert mvalue.initialized assert holder.member_initialized() @pytest.mark.skipif( not hasattr(m, "has_exp_optional"), reason="no " ) def test_exp_optional(): assert m.double_or_zero_exp(None) == 0 assert m.double_or_zero_exp(42) == 84 pytest.raises(TypeError, m.double_or_zero_exp, "foo") assert m.half_or_none_exp(0) is None assert m.half_or_none_exp(42) == 21 pytest.raises(TypeError, m.half_or_none_exp, "foo") assert m.test_nullopt_exp() == 42 assert m.test_nullopt_exp(None) == 42 assert m.test_nullopt_exp(42) == 42 assert m.test_nullopt_exp(43) == 43 assert m.test_no_assign_exp() == 42 assert m.test_no_assign_exp(None) == 42 assert m.test_no_assign_exp(m.NoAssign(43)) == 43 pytest.raises(TypeError, m.test_no_assign_exp, 43) holder = m.OptionalExpHolder() mvalue = holder.member assert mvalue.initialized assert holder.member_initialized() @pytest.mark.skipif(not hasattr(m, "load_variant"), reason="no ") def test_variant(doc): assert m.load_variant(1) == "int" assert m.load_variant("1") == "std::string" assert m.load_variant(1.0) == "double" assert m.load_variant(None) == "std::nullptr_t" assert m.load_variant_2pass(1) == "int" assert m.load_variant_2pass(1.0) == "double" assert m.cast_variant() == (5, "Hello") assert ( doc(m.load_variant) == "load_variant(arg0: Union[int, str, float, None]) -> str" ) def test_vec_of_reference_wrapper(): """#171: Can't return reference wrappers (or STL structures containing them)""" assert ( str(m.return_vec_of_reference_wrapper(UserType(4))) == "[UserType(1), UserType(2), UserType(3), UserType(4)]" ) def test_stl_pass_by_pointer(msg): """Passing nullptr or None to an STL container pointer is not expected to work""" with pytest.raises(TypeError) as excinfo: m.stl_pass_by_pointer() # default value is `nullptr` assert ( msg(excinfo.value) == """ stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported: 1. (v: List[int] = None) -> List[int] Invoked with: """ # noqa: E501 line too long ) with pytest.raises(TypeError) as excinfo: m.stl_pass_by_pointer(None) assert ( msg(excinfo.value) == """ stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported: 1. (v: List[int] = None) -> List[int] Invoked with: None """ # noqa: E501 line too long ) assert m.stl_pass_by_pointer([1, 2, 3]) == [1, 2, 3] def test_missing_header_message(): """Trying convert `list` to a `std::vector`, or vice versa, without including should result in a helpful suggestion in the error message""" import pybind11_cross_module_tests as cm expected_message = ( "Did you forget to `#include `? Or ,\n" ", , etc. Some automatic\n" "conversions are optional and require extra headers to be included\n" "when compiling your pybind11 module." ) with pytest.raises(TypeError) as excinfo: cm.missing_header_arg([1.0, 2.0, 3.0]) assert expected_message in str(excinfo.value) with pytest.raises(TypeError) as excinfo: cm.missing_header_return() assert expected_message in str(excinfo.value) def test_function_with_string_and_vector_string_arg(): """Check if a string is NOT implicitly converted to a list, which was the behavior before fix of issue #1258""" assert m.func_with_string_or_vector_string_arg_overload(("A", "B")) == 2 assert m.func_with_string_or_vector_string_arg_overload(["A", "B"]) == 2 assert m.func_with_string_or_vector_string_arg_overload("A") == 3 def test_stl_ownership(): cstats = ConstructorStats.get(m.Placeholder) assert cstats.alive() == 0 r = m.test_stl_ownership() assert len(r) == 1 del r assert cstats.alive() == 0 def test_array_cast_sequence(): assert m.array_cast_sequence((1, 2, 3)) == [1, 2, 3] def test_issue_1561(): """ check fix for issue #1561 """ bar = m.Issue1561Outer() bar.list = [m.Issue1561Inner("bar")] bar.list assert bar.list[0].data == "bar" /a> 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
/*
             LUFA Library
     Copyright (C) Dean Camera, 2017.

  dean [at] fourwalledcubicle [dot] com
           www.lufa-lib.org
*/

/*
  Copyright 2017  Dean Camera (dean [at] fourwalledcubicle [dot] com)

  Permission to use, copy, modify, distribute, and sell this
  software and its documentation for any purpose is hereby granted
  without fee, provided that the above copyright notice appear in
  all copies and that both that the copyright notice and this
  permission notice and warranty disclaimer appear in supporting
  documentation, and that the name of the author not be used in
  advertising or publicity pertaining to distribution of the
  software without specific, written prior permission.

  The author disclaims all warranties with regard to this
  software, including all implied warranties of merchantability
  and fitness.  In no event shall the author be liable for any
  special, indirect or consequential damages or any damages
  whatsoever resulting from loss of use, data or profits, whether
  in an action of contract, negligence or other tortious action,
  arising out of or in connection with the use or performance of
  this software.
*/

/** \file
 *
 *  SCSI command processing routines, for SCSI commands issued by the host. Mass Storage
 *  devices use a thin "Bulk-Only Transport" protocol for issuing commands and status information,
 *  which wrap around standard SCSI device commands for controlling the actual storage medium.
 */

#define  INCLUDE_FROM_SCSI_C
#include "SCSI.h"

/** Structure to hold the SCSI response data to a SCSI INQUIRY command. This gives information about the device's
 *  features and capabilities.
 */
static const SCSI_Inquiry_Response_t InquiryData =
	{
		.DeviceType          = DEVICE_TYPE_BLOCK,
		.PeripheralQualifier = 0,

		.Removable           = true,

		.Version             = 0,

		.ResponseDataFormat  = 2,
		.NormACA             = false,
		.TrmTsk              = false,
		.AERC                = false,

		.AdditionalLength    = 0x1F,

		.SoftReset           = false,
		.CmdQue              = false,
		.Linked              = false,
		.Sync                = false,
		.WideBus16Bit        = false,
		.WideBus32Bit        = false,
		.RelAddr             = false,

		.VendorID            = "LUFA",
		.ProductID           = "Dataflash Disk",
		.RevisionID          = {'0','.','0','0'},
	};

/** Structure to hold the sense data for the last issued SCSI command, which is returned to the host after a SCSI REQUEST SENSE
 *  command is issued. This gives information on exactly why the last command failed to complete.
 */
static SCSI_Request_Sense_Response_t SenseData =
	{
		.ResponseCode        = 0x70,
		.AdditionalLength    = 0x0A,
	};


/** Main routine to process the SCSI command located in the Command Block Wrapper read from the host. This dispatches
 *  to the appropriate SCSI command handling routine if the issued command is supported by the device, else it returns
 *  a command failure due to a ILLEGAL REQUEST.
 *
 *  \param[in] MSInterfaceInfo  Pointer to the Mass Storage class interface structure that the command is associated with
 *
 *  \return Boolean \c true if the command completed successfully, \c false otherwise
 */
bool SCSI_DecodeSCSICommand(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
{
	bool CommandSuccess = false;

	/* Run the appropriate SCSI command hander function based on the passed command */
	switch (MSInterfaceInfo->State.CommandBlock.SCSICommandData[0])
	{
		case SCSI_CMD_INQUIRY:
			CommandSuccess = SCSI_Command_Inquiry(MSInterfaceInfo);
			break;
		case SCSI_CMD_REQUEST_SENSE:
			CommandSuccess = SCSI_Command_Request_Sense(MSInterfaceInfo);
			break;
		case SCSI_CMD_READ_CAPACITY_10:
			CommandSuccess = SCSI_Command_Read_Capacity_10(MSInterfaceInfo);
			break;
		case SCSI_CMD_SEND_DIAGNOSTIC:
			CommandSuccess = SCSI_Command_Send_Diagnostic(MSInterfaceInfo);
			break;
		case SCSI_CMD_WRITE_10:
			CommandSuccess = SCSI_Command_ReadWrite_10(MSInterfaceInfo, DATA_WRITE);
			break;
		case SCSI_CMD_READ_10:
			CommandSuccess = SCSI_Command_ReadWrite_10(MSInterfaceInfo, DATA_READ);
			break;
		case SCSI_CMD_MODE_SENSE_6:
			CommandSuccess = SCSI_Command_ModeSense_6(MSInterfaceInfo);
			break;
		case SCSI_CMD_START_STOP_UNIT:
		case SCSI_CMD_TEST_UNIT_READY:
		case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL: