From 1572885b4f85d0fe5bcc5d9e609eb0564a430f86 Mon Sep 17 00:00:00 2001 From: Huanyi Xie <74809554+xiehuanyi@users.noreply.github.com> Date: Sun, 11 Oct 2026 11:48:18 +0300 Subject: [PATCH] docs: read binary buffers in usage examples --- magic/__init__.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/magic/__init__.py b/magic/__init__.py index 14d18968..60581069 100644 --- a/magic/__init__.py +++ b/magic/__init__.py @@ -10,8 +10,9 @@ 'PDF document, version 1.2' >>> magic.from_file("testdata/test.pdf", mime=True) 'application/pdf' ->>> magic.from_buffer(open("testdata/test.pdf").read(1024)) -'PDF document, version 1.2' +>>> with open("testdata/test.pdf", "rb") as pdf: +... magic.from_buffer(pdf.read(2048), mime=True) +'application/pdf' >>> """ @@ -223,8 +224,9 @@ def from_buffer(buffer, mime=False): value is the mimetype if mime=True, otherwise a human readable name. - >>> magic.from_buffer(open("testdata/test.pdf").read(1024)) - 'PDF document, version 1.2' + >>> with open("testdata/test.pdf", "rb") as pdf: + ... magic.from_buffer(pdf.read(2048), mime=True) + 'application/pdf' """ m = _get_magic_type(mime) return m.from_buffer(buffer)