@@ -122,37 +122,60 @@ def test_binary_string_encoding():
122122 assert result == b"Hello\n World\t !" , "String with special characters should encode properly"
123123
124124
125+ def test_binary_memoryview ():
126+ """Binary() accepts memoryview (GH-739): Django BinaryField and DB-API buffers."""
127+ result = Binary (memoryview (b"\x01 \x02 \x03 " ))
128+ assert isinstance (result , bytes ), "memoryview should be converted to bytes"
129+ assert result == b"\x01 \x02 \x03 " , "memoryview content should be preserved"
130+
131+ # Empty memoryview
132+ assert Binary (memoryview (b"" )) == b""
133+
134+ # memoryview over a bytearray round-trips the same bytes
135+ assert Binary (memoryview (bytearray (b"hello" ))) == b"hello"
136+
137+
125138def test_binary_unsupported_types_error ():
126139 """Test Binary() TypeError for unsupported types (Lines 138-141)."""
127140 # Test integer type
128141 with pytest .raises (TypeError ) as exc_info :
129142 Binary (123 )
130143 assert "Cannot convert type int to bytes" in str (exc_info .value )
131- assert "Binary() only accepts str, bytes, or bytearray objects" in str (exc_info .value )
144+ assert "Binary() only accepts str, bytes, bytearray, or memoryview objects" in str (
145+ exc_info .value
146+ )
132147
133148 # Test float type
134149 with pytest .raises (TypeError ) as exc_info :
135150 Binary (3.14 )
136151 assert "Cannot convert type float to bytes" in str (exc_info .value )
137- assert "Binary() only accepts str, bytes, or bytearray objects" in str (exc_info .value )
152+ assert "Binary() only accepts str, bytes, bytearray, or memoryview objects" in str (
153+ exc_info .value
154+ )
138155
139156 # Test list type
140157 with pytest .raises (TypeError ) as exc_info :
141158 Binary ([1 , 2 , 3 ])
142159 assert "Cannot convert type list to bytes" in str (exc_info .value )
143- assert "Binary() only accepts str, bytes, or bytearray objects" in str (exc_info .value )
160+ assert "Binary() only accepts str, bytes, bytearray, or memoryview objects" in str (
161+ exc_info .value
162+ )
144163
145164 # Test dict type
146165 with pytest .raises (TypeError ) as exc_info :
147166 Binary ({"key" : "value" })
148167 assert "Cannot convert type dict to bytes" in str (exc_info .value )
149- assert "Binary() only accepts str, bytes, or bytearray objects" in str (exc_info .value )
168+ assert "Binary() only accepts str, bytes, bytearray, or memoryview objects" in str (
169+ exc_info .value
170+ )
150171
151172 # Test None type
152173 with pytest .raises (TypeError ) as exc_info :
153174 Binary (None )
154175 assert "Cannot convert type NoneType to bytes" in str (exc_info .value )
155- assert "Binary() only accepts str, bytes, or bytearray objects" in str (exc_info .value )
176+ assert "Binary() only accepts str, bytes, bytearray, or memoryview objects" in str (
177+ exc_info .value
178+ )
156179
157180 # Test custom object type
158181 class CustomObject :
@@ -161,7 +184,9 @@ class CustomObject:
161184 with pytest .raises (TypeError ) as exc_info :
162185 Binary (CustomObject ())
163186 assert "Cannot convert type CustomObject to bytes" in str (exc_info .value )
164- assert "Binary() only accepts str, bytes, or bytearray objects" in str (exc_info .value )
187+ assert "Binary() only accepts str, bytes, bytearray, or memoryview objects" in str (
188+ exc_info .value
189+ )
165190
166191
167192def test_binary_comprehensive_coverage ():
0 commit comments