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
e6568694
Commit
e6568694
authored
Aug 07, 2007
by
Martin v. Löwis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Change dumbdbm to use bytes keys.
parent
45d569b8
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
21 additions
and
18 deletions
+21
-18
Lib/dumbdbm.py
Lib/dumbdbm.py
+5
-2
Lib/test/test_dumbdbm.py
Lib/test/test_dumbdbm.py
+15
-15
Lib/test/test_whichdb.py
Lib/test/test_whichdb.py
+1
-1
No files found.
Lib/dumbdbm.py
View file @
e6568694
...
...
@@ -115,6 +115,7 @@ class _Database(UserDict.DictMixin):
sync
=
_commit
def
__getitem__
(
self
,
key
):
key
=
key
.
decode
(
"latin-1"
)
pos
,
siz
=
self
.
_index
[
key
]
# may raise KeyError
f
=
_io
.
open
(
self
.
_datfile
,
'rb'
)
f
.
seek
(
pos
)
...
...
@@ -159,8 +160,9 @@ class _Database(UserDict.DictMixin):
f
.
close
()
def
__setitem__
(
self
,
key
,
val
):
if
not
isinstance
(
key
,
basestring
):
raise
TypeError
(
"keys must be strings"
)
if
not
isinstance
(
key
,
bytes
):
raise
TypeError
(
"keys must be bytes"
)
key
=
key
.
decode
(
"latin-1"
)
# hashable bytes
if
not
isinstance
(
val
,
(
str8
,
bytes
)):
raise
TypeError
(
"values must be byte strings"
)
if
key
not
in
self
.
_index
:
...
...
@@ -188,6 +190,7 @@ class _Database(UserDict.DictMixin):
# (so that _commit() never gets called).
def
__delitem__
(
self
,
key
):
key
=
key
.
decode
(
"latin-1"
)
# The blocks used by the associated value are lost.
del
self
.
_index
[
key
]
# XXX It's unclear why we do a _commit() here (the code always
...
...
Lib/test/test_dumbdbm.py
View file @
e6568694
...
...
@@ -35,7 +35,7 @@ class DumbDBMTestCase(unittest.TestCase):
f
=
dumbdbm
.
open
(
_fname
,
'c'
)
self
.
assertEqual
(
list
(
f
.
keys
()),
[])
for
key
in
self
.
_dict
:
f
[
key
]
=
self
.
_dict
[
key
]
f
[
key
.
encode
(
"ascii"
)
]
=
self
.
_dict
[
key
]
self
.
read_helper
(
f
)
f
.
close
()
...
...
@@ -65,15 +65,15 @@ class DumbDBMTestCase(unittest.TestCase):
def
test_close_twice
(
self
):
f
=
dumbdbm
.
open
(
_fname
)
f
[
'a'
]
=
b'b'
self
.
assertEqual
(
f
[
'a'
],
b'b'
)
f
[
b
'a'
]
=
b'b'
self
.
assertEqual
(
f
[
b
'a'
],
b'b'
)
f
.
close
()
f
.
close
()
def
test_dumbdbm_modification
(
self
):
self
.
init_db
()
f
=
dumbdbm
.
open
(
_fname
,
'w'
)
self
.
_dict
[
'g'
]
=
f
[
'g'
]
=
b"indented"
self
.
_dict
[
'g'
]
=
f
[
b
'g'
]
=
b"indented"
self
.
read_helper
(
f
)
f
.
close
()
...
...
@@ -92,8 +92,8 @@ class DumbDBMTestCase(unittest.TestCase):
def
test_write_write_read
(
self
):
# test for bug #482460
f
=
dumbdbm
.
open
(
_fname
)
f
[
'1'
]
=
b'hello'
f
[
'1'
]
=
b'hello2'
f
[
b
'1'
]
=
b'hello'
f
[
b
'1'
]
=
b'hello2'
f
.
close
()
f
=
dumbdbm
.
open
(
_fname
)
self
.
assertEqual
(
f
[
'1'
],
b'hello2'
)
...
...
@@ -103,8 +103,8 @@ class DumbDBMTestCase(unittest.TestCase):
# test for bug #1172763: dumbdbm would die if the line endings
# weren't what was expected.
f
=
dumbdbm
.
open
(
_fname
)
f
[
'1'
]
=
b'hello'
f
[
'2'
]
=
b'hello2'
f
[
b
'1'
]
=
b'hello'
f
[
b
'2'
]
=
b'hello2'
f
.
close
()
# Mangle the file by adding \r before each newline
...
...
@@ -113,23 +113,23 @@ class DumbDBMTestCase(unittest.TestCase):
io
.
open
(
_fname
+
'.dir'
,
'wb'
).
write
(
data
)
f
=
dumbdbm
.
open
(
_fname
)
self
.
assertEqual
(
f
[
'1'
],
b'hello'
)
self
.
assertEqual
(
f
[
'2'
],
b'hello2'
)
self
.
assertEqual
(
f
[
b
'1'
],
b'hello'
)
self
.
assertEqual
(
f
[
b
'2'
],
b'hello2'
)
def
read_helper
(
self
,
f
):
keys
=
self
.
keys_helper
(
f
)
for
key
in
self
.
_dict
:
self
.
assertEqual
(
self
.
_dict
[
key
],
f
[
key
])
self
.
assertEqual
(
self
.
_dict
[
key
],
f
[
key
.
encode
(
"ascii"
)
])
def
init_db
(
self
):
f
=
dumbdbm
.
open
(
_fname
,
'w'
)
for
k
in
self
.
_dict
:
f
[
k
]
=
self
.
_dict
[
k
]
f
[
k
.
encode
(
"ascii"
)
]
=
self
.
_dict
[
k
]
f
.
close
()
def
keys_helper
(
self
,
f
):
keys
=
sorted
(
f
.
keys
())
keys
=
sorted
(
k
.
decode
(
"ascii"
)
for
k
in
f
.
keys
())
dkeys
=
sorted
(
self
.
_dict
.
keys
())
self
.
assertEqual
(
keys
,
dkeys
)
return
keys
...
...
@@ -146,11 +146,11 @@ class DumbDBMTestCase(unittest.TestCase):
if
random
.
random
()
<
0.2
:
if
k
in
d
:
del
d
[
k
]
del
f
[
k
]
del
f
[
k
.
encode
(
"ascii"
)
]
else
:
v
=
random
.
choice
((
b'a'
,
b'b'
,
b'c'
))
*
random
.
randrange
(
10000
)
d
[
k
]
=
v
f
[
k
]
=
v
f
[
k
.
encode
(
"ascii"
)
]
=
v
self
.
assertEqual
(
f
[
k
],
v
)
f
.
close
()
...
...
Lib/test/test_whichdb.py
View file @
e6568694
...
...
@@ -51,7 +51,7 @@ for name in anydbm._names:
self
.
assertEqual
(
name
,
whichdb
.
whichdb
(
_fname
))
# Now add a key
f
=
mod
.
open
(
_fname
,
'w'
)
f
[
"1"
]
=
b"1"
f
[
b
"1"
]
=
b"1"
f
.
close
()
self
.
assertEqual
(
name
,
whichdb
.
whichdb
(
_fname
))
setattr
(
WhichDBTestCase
,
"test_whichdb_%s"
%
name
,
test_whichdb_name
)
...
...
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