Movatterモバイル変換


[0]ホーム

URL:


homepage

Issue34066

This issue trackerhas been migrated toGitHub, and is currentlyread-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title:Possible resource warning in "with open()"
Type:resource usageStage:resolved
Components:Interpreter CoreVersions:Python 3.8, Python 3.7, Python 3.6
process
Status:closedResolution:fixed
Dependencies:Superseder:
Assigned To:Nosy List: Mark.Shannon, benjamin.peterson, jwilk, miss-islington, ncoghlan, serhiy.storchaka, yselivanov
Priority:normalKeywords:patch

Created on2018-07-07 17:11 byserhiy.storchaka, last changed2022-04-11 14:59 byadmin. This issue is nowclosed.

Pull Requests
URLStatusLinkedEdit
PR 8159mergedserhiy.storchaka,2018-07-07 17:21
PR 8197mergedmiss-islington,2018-07-09 12:41
PR 8198mergedserhiy.storchaka,2018-07-09 12:51
Messages (5)
msg321224 -(view)Author: Serhiy Storchaka (serhiy.storchaka)*(Python committer)Date: 2018-07-07 17:11
The bytecode generated for "with open()":with open(path) as file:    data = file.read()  1           0 LOAD_NAME                0 (open)              2 LOAD_NAME                1 (path)              4 CALL_FUNCTION            1              6 SETUP_WITH              14 (to 22)              8 STORE_NAME               2 (file)  2          10 LOAD_NAME                2 (file)             12 LOAD_METHOD              3 (read)             14 CALL_METHOD              0             16 STORE_NAME               4 (data)             18 POP_BLOCK             20 BEGIN_FINALLY        >>   22 WITH_CLEANUP_START             24 WITH_CLEANUP_FINISH             26 END_FINALLY             28 LOAD_CONST               0 (None)             30 RETURN_VALUEThe execution can be interrupted by Ctrl-C between calling open() and entering the 'with' block. In this case the file object will be created, but its __enter__ and __exit__ methods will be not executed. As a result it will be closed after disappearing a reference to it and a ResourceWarning will be emitted.The solution is disabling interruption before the SETUP_WITH opcode. It is already disabled before SETUP_FINALLY and YIELD_FROM. It is worth to disable it before BEFORE_ASYNC_WITH for consistency although I don't have examples for it.See alsoissue29988.
msg321288 -(view)Author: Serhiy Storchaka (serhiy.storchaka)*(Python committer)Date: 2018-07-08 20:07
Maybe it is worth to disable interrupting before more opcodes. For example for fixing a problem with the contextlib.nullcontext example (issue34067) we need to disable interrupting before STORE_FAST, LOAD_FAST and JUMP_FORWARD.
msg321314 -(view)Author: Alyssa Coghlan (ncoghlan)*(Python committer)Date: 2018-07-09 12:40
New changeset3f4d90d4d72921f16babd3f52d7df804916af224 by Nick Coghlan (Serhiy Storchaka) in branch 'master':bpo-34066: Disabled interruption before SETUP_WITH and BEFORE_ASYNC_WITH. (GH-8159)https://github.com/python/cpython/commit/3f4d90d4d72921f16babd3f52d7df804916af224
msg321316 -(view)Author: miss-islington (miss-islington)Date: 2018-07-09 13:31
New changesetf5197ddfd0f2c5da848af57196448810bd18bb82 by Miss Islington (bot) in branch '3.7':bpo-34066: Disabled interruption before SETUP_WITH and BEFORE_ASYNC_WITH. (GH-8159)https://github.com/python/cpython/commit/f5197ddfd0f2c5da848af57196448810bd18bb82
msg321328 -(view)Author: Serhiy Storchaka (serhiy.storchaka)*(Python committer)Date: 2018-07-09 16:02
New changeseteeaae26ce5abce56292330c29459337d6bd57aaf by Serhiy Storchaka in branch '3.6':[3.6]bpo-34066: Disabled interruption before SETUP_WITH and BEFORE_ASYNC_WITH. (GH-8159) (GH-8198)https://github.com/python/cpython/commit/eeaae26ce5abce56292330c29459337d6bd57aaf
History
DateUserActionArgs
2022-04-11 14:59:02adminsetgithub: 78247
2018-07-13 16:19:58jwilksetnosy: +jwilk
2018-07-09 16:03:41serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2018-07-09 16:02:28serhiy.storchakasetmessages: +msg321328
2018-07-09 13:31:07miss-islingtonsetnosy: +miss-islington
messages: +msg321316
2018-07-09 12:51:37serhiy.storchakasetpull_requests: +pull_request7750
2018-07-09 12:41:28miss-islingtonsetpull_requests: +pull_request7749
2018-07-09 12:40:17ncoghlansetmessages: +msg321314
2018-07-08 20:07:04serhiy.storchakasetmessages: +msg321288
2018-07-07 17:21:22serhiy.storchakasetkeywords: +patch
stage: patch review
pull_requests: +pull_request7723
2018-07-07 17:11:52serhiy.storchakacreate
Supported byThe Python Software Foundation,
Powered byRoundup
Copyright © 1990-2022,Python Software Foundation
Legal Statements

[8]ページ先頭

©2009-2026 Movatter.jp