diff options
author | Erlend Egeberg Aasland <erlend.aasland@innova.no> | 2022-04-16 04:21:12 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-16 04:21:12 (GMT) |
commit | a8617566759a07c67d14c9b6ed663e32d3b3f5e7 (patch) | |
tree | 531993ba1515ca7050375c7d840579423dcc4d7f /Doc | |
parent | 4e661cd69164318c1f871faa476c68a04092ddc4 (diff) | |
download | cpython-a8617566759a07c67d14c9b6ed663e32d3b3f5e7.zip cpython-a8617566759a07c67d14c9b6ed663e32d3b3f5e7.tar.gz cpython-a8617566759a07c67d14c9b6ed663e32d3b3f5e7.tar.bz2 |
gh-69093: Add context manager support to sqlite3.Blob (GH-91562)
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/includes/sqlite3/blob.py | 16 | ||||
-rw-r--r-- | Doc/library/sqlite3.rst | 9 |
2 files changed, 15 insertions, 10 deletions
diff --git a/Doc/includes/sqlite3/blob.py b/Doc/includes/sqlite3/blob.py index 61994fb..b3694ad 100644 --- a/Doc/includes/sqlite3/blob.py +++ b/Doc/includes/sqlite3/blob.py @@ -4,9 +4,13 @@ con = sqlite3.connect(":memory:") con.execute("create table test(blob_col blob)") con.execute("insert into test(blob_col) values (zeroblob(10))") -blob = con.blobopen("test", "blob_col", 1) -blob.write(b"Hello") -blob.write(b"World") -blob.seek(0) -print(blob.read()) # will print b"HelloWorld" -blob.close() +# Write to our blob, using two write operations: +with con.blobopen("test", "blob_col", 1) as blob: + blob.write(b"Hello") + blob.write(b"World") + +# Read the contents of our blob +with con.blobopen("test", "blob_col", 1) as blob: + greeting = blob.read() + +print(greeting) # outputs "b'HelloWorld'" diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst index d0274fb..4838db0 100644 --- a/Doc/library/sqlite3.rst +++ b/Doc/library/sqlite3.rst @@ -1115,6 +1115,11 @@ Blob Objects data in an SQLite :abbr:`BLOB (Binary Large OBject)`. Call ``len(blob)`` to get the size (number of bytes) of the blob. + Use the :class:`Blob` as a :term:`context manager` to ensure that the blob + handle is closed after use. + + .. literalinclude:: ../includes/sqlite3/blob.py + .. method:: close() Close the blob. @@ -1149,10 +1154,6 @@ Blob Objects current position) and :data:`os.SEEK_END` (seek relative to the blob’s end). - :class:`Blob` example: - - .. literalinclude:: ../includes/sqlite3/blob.py - .. _sqlite3-types: |