Describe the bug
mssql_python.Binary() accepts only str, bytes, and bytearray. It raises TypeError on a memoryview, which pyodbc and most DB-API drivers accept. memoryview is a standard buffer type, and Django's BinaryField hands it straight to the driver, so any BinaryField write or lookup fails under mssql-python.
Exception message:
TypeError: Cannot convert type memoryview to bytes. Binary() only accepts str, bytes, or bytearray objects.
The check is in mssql_python/type.py (Binary(), line 123): it handles bytes, bytearray, and str, then raises for everything else.
To reproduce
Standalone, no database needed:
import mssql_python
mv = memoryview(b"\x01\x02\x03")
mssql_python.Binary(mv)
# TypeError: Cannot convert type memoryview to bytes.
# Binary() only accepts str, bytes, or bytearray objects.
For contrast, pyodbc accepts it:
import pyodbc
pyodbc.Binary(memoryview(b"\x01\x02\x03")) # returns bytearray(b'\x01\x02\x03')
Through Django on the mssql-django backend, a plain BinaryField save hits the same path and fails:
# class Blob(models.Model): data = models.BinaryField()
Blob.objects.create(data=b"\x00\x01\x02")
# File ".../django/db/models/fields/__init__.py", in get_db_prep_value
# return connection.Database.Binary(value)
# TypeError: Cannot convert type memoryview to bytes. ...
Expected behavior
Binary(memoryview(...)) returns the bytes, matching pyodbc and the DB-API convention of accepting buffer-protocol objects. Django BinaryField writes, lookups, and fixture load/dump then work under mssql-python.
Further technical details
Python version: 3.13 (also reproduces on 3.10 through 3.14)
SQL Server version: SQL Server 2022
Operating system: driver-side, OS-independent (seen on macOS and Ubuntu)
mssql-python version: 1.14.0
Additional context
Found while validating mssql-python as a driver for mssql-django. This accounts for 7 of the 9 Django integration-suite failures: 2 in model_fields.test_binaryfield and 5 in serializers.test_data (json/jsonl/python/xml/yaml), since Django's serializers roundtrip BinaryField data through memoryview.
Suggested fix: accept memoryview in Binary() by returning value.tobytes(). One localized change.
Describe the bug
mssql_python.Binary()accepts onlystr,bytes, andbytearray. It raisesTypeErroron amemoryview, which pyodbc and most DB-API drivers accept.memoryviewis a standard buffer type, and Django'sBinaryFieldhands it straight to the driver, so anyBinaryFieldwrite or lookup fails under mssql-python.Exception message:
The check is in
mssql_python/type.py(Binary(), line 123): it handlesbytes,bytearray, andstr, then raises for everything else.To reproduce
Standalone, no database needed:
For contrast, pyodbc accepts it:
Through Django on the mssql-django backend, a plain
BinaryFieldsave hits the same path and fails:Expected behavior
Binary(memoryview(...))returns the bytes, matching pyodbc and the DB-API convention of accepting buffer-protocol objects. DjangoBinaryFieldwrites, lookups, and fixture load/dump then work under mssql-python.Further technical details
Python version: 3.13 (also reproduces on 3.10 through 3.14)
SQL Server version: SQL Server 2022
Operating system: driver-side, OS-independent (seen on macOS and Ubuntu)
mssql-python version: 1.14.0
Additional context
Found while validating mssql-python as a driver for mssql-django. This accounts for 7 of the 9 Django integration-suite failures: 2 in
model_fields.test_binaryfieldand 5 inserializers.test_data(json/jsonl/python/xml/yaml), since Django's serializers roundtripBinaryFielddata throughmemoryview.Suggested fix: accept
memoryviewinBinary()by returningvalue.tobytes(). One localized change.