Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
c610aba1
Kaydet (Commit)
c610aba1
authored
Kas 17, 2013
tarafından
Nick Coghlan
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Close #19282: Native context management in dbm
üst
eb8ea265
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
88 additions
and
3 deletions
+88
-3
dbm.rst
Doc/library/dbm.rst
+9
-3
dumb.py
Lib/dbm/dumb.py
+6
-0
test_dbm_dumb.py
Lib/test/test_dbm_dumb.py
+13
-0
test_dbm_gnu.py
Lib/test/test_dbm_gnu.py
+11
-0
test_dbm_ndbm.py
Lib/test/test_dbm_ndbm.py
+13
-0
NEWS
Misc/NEWS
+3
-0
_dbmmodule.c
Modules/_dbmmodule.c
+17
-0
_gdbmmodule.c
Modules/_gdbmmodule.c
+16
-0
No files found.
Doc/library/dbm.rst
Dosyayı görüntüle @
c610aba1
...
...
@@ -73,13 +73,20 @@ Key and values are always stored as bytes. This means that when
strings are used they are implicitly converted to the default encoding before
being stored.
These objects also support being used in a :keyword:`with` statement, which
will automatically close them when done.
.. versionchanged:: 3.4
Added native support for the context management protocol to the objects
returned by :func:`.open`.
The following example records some hostnames and a corresponding title, and
then prints out the contents of the database::
import dbm
# Open database, creating it if necessary.
db = dbm.open('cache', 'c')
with dbm.open('cache', 'c') as db:
# Record some values
db[b'hello'] = b'there'
...
...
@@ -98,8 +105,7 @@ then prints out the contents of the database::
# likely a TypeError).
db['www.yahoo.com'] = 4
# Close when done.
db.close()
# db is automatically closed when leaving the with statement.
.. seealso::
...
...
Lib/dbm/dumb.py
Dosyayı görüntüle @
c610aba1
...
...
@@ -236,6 +236,12 @@ class _Database(collections.MutableMapping):
if
hasattr
(
self
.
_os
,
'chmod'
):
self
.
_os
.
chmod
(
file
,
self
.
_mode
)
def
__enter__
(
self
):
return
self
def
__exit__
(
self
,
*
args
):
self
.
close
()
def
open
(
file
,
flag
=
None
,
mode
=
0
o666
):
"""Open the database file, filename, and return corresponding object.
...
...
Lib/test/test_dbm_dumb.py
Dosyayı görüntüle @
c610aba1
...
...
@@ -184,6 +184,19 @@ class DumbDBMTestCase(unittest.TestCase):
self
.
assertEqual
(
expected
,
got
)
f
.
close
()
def
test_context_manager
(
self
):
with
dumbdbm
.
open
(
_fname
,
'c'
)
as
db
:
db
[
"dumbdbm context manager"
]
=
"context manager"
with
dumbdbm
.
open
(
_fname
,
'r'
)
as
db
:
self
.
assertEqual
(
list
(
db
.
keys
()),
[
b
"dumbdbm context manager"
])
# This currently just raises AttributeError rather than a specific
# exception like the GNU or NDBM based implementations. See
# http://bugs.python.org/issue19385 for details.
with
self
.
assertRaises
(
Exception
):
db
.
keys
()
def
tearDown
(
self
):
_delete_files
()
...
...
Lib/test/test_dbm_gnu.py
Dosyayı görüntüle @
c610aba1
...
...
@@ -81,6 +81,17 @@ class TestGdbm(unittest.TestCase):
size2
=
os
.
path
.
getsize
(
filename
)
self
.
assertTrue
(
size1
>
size2
>=
size0
)
def
test_context_manager
(
self
):
with
gdbm
.
open
(
filename
,
'c'
)
as
db
:
db
[
"gdbm context manager"
]
=
"context manager"
with
gdbm
.
open
(
filename
,
'r'
)
as
db
:
self
.
assertEqual
(
list
(
db
.
keys
()),
[
b
"gdbm context manager"
])
with
self
.
assertRaises
(
gdbm
.
error
)
as
cm
:
db
.
keys
()
self
.
assertEqual
(
str
(
cm
.
exception
),
"GDBM object has already been closed"
)
if
__name__
==
'__main__'
:
unittest
.
main
()
Lib/test/test_dbm_ndbm.py
Dosyayı görüntüle @
c610aba1
...
...
@@ -37,5 +37,18 @@ class DbmTestCase(unittest.TestCase):
except
error
:
self
.
fail
()
def
test_context_manager
(
self
):
with
dbm
.
ndbm
.
open
(
self
.
filename
,
'c'
)
as
db
:
db
[
"ndbm context manager"
]
=
"context manager"
with
dbm
.
ndbm
.
open
(
self
.
filename
,
'r'
)
as
db
:
self
.
assertEqual
(
list
(
db
.
keys
()),
[
b
"ndbm context manager"
])
with
self
.
assertRaises
(
dbm
.
ndbm
.
error
)
as
cm
:
db
.
keys
()
self
.
assertEqual
(
str
(
cm
.
exception
),
"DBM object has already been closed"
)
if
__name__
==
'__main__'
:
unittest
.
main
()
Misc/NEWS
Dosyayı görüntüle @
c610aba1
...
...
@@ -50,6 +50,9 @@ Core and Builtins
Library
-------
-
Issue
#
19282
:
dbm
.
open
now
supports
the
context
manager
protocol
.
(
Inital
patch
by
Claudiu
Popa
)
-
Issue
#
8311
:
Added
support
for
writing
any
bytes
-
like
objects
in
the
aifc
,
sunau
,
and
wave
modules
.
...
...
Modules/_dbmmodule.c
Dosyayı görüntüle @
c610aba1
...
...
@@ -313,6 +313,21 @@ dbm_setdefault(dbmobject *dp, PyObject *args)
return
defvalue
;
}
static
PyObject
*
dbm__enter__
(
PyObject
*
self
,
PyObject
*
args
)
{
Py_INCREF
(
self
);
return
self
;
}
static
PyObject
*
dbm__exit__
(
PyObject
*
self
,
PyObject
*
args
)
{
_Py_IDENTIFIER
(
close
);
return
_PyObject_CallMethodId
(
self
,
&
PyId_close
,
NULL
);
}
static
PyMethodDef
dbm_methods
[]
=
{
{
"close"
,
(
PyCFunction
)
dbm__close
,
METH_NOARGS
,
"close()
\n
Close the database."
},
...
...
@@ -325,6 +340,8 @@ static PyMethodDef dbm_methods[] = {
"setdefault(key[, default]) -> value
\n
"
"Return the value for key if present, otherwise default. If key
\n
"
"is not in the database, it is inserted with default as the value."
},
{
"__enter__"
,
dbm__enter__
,
METH_NOARGS
,
NULL
},
{
"__exit__"
,
dbm__exit__
,
METH_VARARGS
,
NULL
},
{
NULL
,
NULL
}
/* sentinel */
};
...
...
Modules/_gdbmmodule.c
Dosyayı görüntüle @
c610aba1
...
...
@@ -425,6 +425,20 @@ dbm_sync(dbmobject *dp, PyObject *unused)
return
Py_None
;
}
static
PyObject
*
dbm__enter__
(
PyObject
*
self
,
PyObject
*
args
)
{
Py_INCREF
(
self
);
return
self
;
}
static
PyObject
*
dbm__exit__
(
PyObject
*
self
,
PyObject
*
args
)
{
_Py_IDENTIFIER
(
close
);
return
_PyObject_CallMethodId
(
self
,
&
PyId_close
,
NULL
);
}
static
PyMethodDef
dbm_methods
[]
=
{
{
"close"
,
(
PyCFunction
)
dbm_close
,
METH_NOARGS
,
dbm_close__doc__
},
{
"keys"
,
(
PyCFunction
)
dbm_keys
,
METH_NOARGS
,
dbm_keys__doc__
},
...
...
@@ -434,6 +448,8 @@ static PyMethodDef dbm_methods[] = {
{
"sync"
,
(
PyCFunction
)
dbm_sync
,
METH_NOARGS
,
dbm_sync__doc__
},
{
"get"
,
(
PyCFunction
)
dbm_get
,
METH_VARARGS
,
dbm_get__doc__
},
{
"setdefault"
,(
PyCFunction
)
dbm_setdefault
,
METH_VARARGS
,
dbm_setdefault__doc__
},
{
"__enter__"
,
dbm__enter__
,
METH_NOARGS
,
NULL
},
{
"__exit__"
,
dbm__exit__
,
METH_VARARGS
,
NULL
},
{
NULL
,
NULL
}
/* sentinel */
};
...
...
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