From f3bf8033276aa4fc272940e650c319c517bb3479 Mon Sep 17 00:00:00 2001
From: Lumir Balhar <lbalhar@redhat.com>
Date: Tue, 7 Apr 2026 09:16:08 +0200
Subject: [PATCH] Python 3.15+ requires C-contiguous buffers for
 int.from_bytes()

---
 src/websockets/utils.py | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/websockets/utils.py b/src/websockets/utils.py
index b2a90e52..84904ea0 100644
--- a/src/websockets/utils.py
+++ b/src/websockets/utils.py
@@ -47,6 +47,14 @@ def apply_mask(data: BytesLike, mask: bytes | bytearray) -> bytes:
     if len(mask) != 4:
         raise ValueError("mask must contain 4 bytes")
 
+    # Python 3.15+ requires C-contiguous buffers for int.from_bytes()
+    # CPython (https://github.com/python/cpython/pull/132109) optimized
+    # int.from_bytes() to use the buffer protocol with PyBUF_SIMPLE, which requires
+    # C-contiguous buffers. Non-contiguous memoryviews (e.g., created by [::-1])
+    # now raise BufferError. Convert to bytes to create a contiguous copy.
+    if isinstance(data, memoryview) and not data.c_contiguous:
+        data = bytes(data)
+
     data_int = int.from_bytes(data, sys.byteorder)
     mask_repeated = mask * (len(data) // 4) + mask[: len(data) % 4]
     mask_int = int.from_bytes(mask_repeated, sys.byteorder)
