Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
ef08fb1f
Commit
ef08fb1f
authored
Oct 06, 2012
by
Andrew Svetlov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #13896: Make shelf instances work with 'with' as context managers.
Original patch by Filip Gruszczyński.
parent
dc22587d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
23 additions
and
0 deletions
+23
-0
Lib/shelve.py
Lib/shelve.py
+7
-0
Lib/test/test_shelve.py
Lib/test/test_shelve.py
+13
-0
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Lib/shelve.py
View file @
ef08fb1f
...
...
@@ -131,6 +131,12 @@ class Shelf(collections.MutableMapping):
except
KeyError
:
pass
def
__enter__
(
self
):
return
self
def
__exit__
(
self
,
type
,
value
,
traceback
):
self
.
close
()
def
close
(
self
):
self
.
sync
()
try
:
...
...
@@ -147,6 +153,7 @@ class Shelf(collections.MutableMapping):
def
__del__
(
self
):
if
not
hasattr
(
self
,
'writeback'
):
# __init__ didn't succeed, so don't bother closing
# see http://bugs.python.org/issue1339007 for details
return
self
.
close
()
...
...
Lib/test/test_shelve.py
View file @
ef08fb1f
...
...
@@ -148,6 +148,19 @@ class TestCase(unittest.TestCase):
p2
=
d
[
encodedkey
]
self
.
assertNotEqual
(
p1
,
p2
)
# Write creates new object in store
def
test_with
(
self
):
d1
=
{}
with
shelve
.
Shelf
(
d1
,
protocol
=
2
,
writeback
=
False
)
as
s
:
s
[
'key1'
]
=
[
1
,
2
,
3
,
4
]
self
.
assertEqual
(
s
[
'key1'
],
[
1
,
2
,
3
,
4
])
self
.
assertEqual
(
len
(
s
),
1
)
self
.
assertRaises
(
ValueError
,
len
,
s
)
try
:
s
[
'key1'
]
except
ValueError
:
pass
else
:
self
.
fail
(
'Closed shelf should not find a key'
)
from
test
import
mapping_tests
...
...
Misc/NEWS
View file @
ef08fb1f
...
...
@@ -39,6 +39,9 @@ Core and Builtins
Library
-------
- Issue #13896: Make shelf instances work with 'with' as context managers.
Original patch by Filip Gruszczyński.
- Issue #15417: Add support for csh and fish in venv activation scripts.
- Issue #16123: IDLE - deprecate running without a subprocess.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment