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
64581041
Kaydet (Commit)
64581041
authored
Mar 03, 2010
tarafından
Lars Gustäbel
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Issue #7232: Add support for the context manager protocol
to the TarFile class.
üst
8af970ab
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
92 additions
and
0 deletions
+92
-0
tarfile.rst
Doc/library/tarfile.rst
+15
-0
tarfile.py
Lib/tarfile.py
+14
-0
test_tarfile.py
Lib/test/test_tarfile.py
+60
-0
NEWS
Misc/NEWS
+3
-0
No files found.
Doc/library/tarfile.rst
Dosyayı görüntüle @
64581041
...
@@ -234,6 +234,14 @@ a header block followed by data blocks. It is possible to store a file in a tar
...
@@ -234,6 +234,14 @@ a header block followed by data blocks. It is possible to store a file in a tar
archive several times. Each archive member is represented by a :class:`TarInfo`
archive several times. Each archive member is represented by a :class:`TarInfo`
object, see :ref:`tarinfo-objects` for details.
object, see :ref:`tarinfo-objects` for details.
A :class:`TarFile` object can be used as a context manager in a :keyword:`with`
statement. It will automatically be closed when the block is completed. Please
note that in the event of an exception an archive opened for writing will not
be finalized, only the internally used file object will be closed. See the
:ref:`tar-examples` section for a use case.
.. versionadded:: 2.7
Added support for the context manager protocol.
.. class:: TarFile(name=None, mode='r', fileobj=None, format=DEFAULT_FORMAT, tarinfo=TarInfo, dereference=False, ignore_zeros=False, encoding=ENCODING, errors=None, pax_headers=None, debug=0, errorlevel=0)
.. class:: TarFile(name=None, mode='r', fileobj=None, format=DEFAULT_FORMAT, tarinfo=TarInfo, dereference=False, ignore_zeros=False, encoding=ENCODING, errors=None, pax_headers=None, debug=0, errorlevel=0)
...
@@ -650,6 +658,13 @@ How to create an uncompressed tar archive from a list of filenames::
...
@@ -650,6 +658,13 @@ How to create an uncompressed tar archive from a list of filenames::
tar.add(name)
tar.add(name)
tar.close()
tar.close()
The same example using the :keyword:`with` statement::
import tarfile
with tarfile.open("sample.tar", "w") as tar:
for name in ["foo", "bar", "quux"]:
tar.add(name)
How to read a gzip compressed tar archive and display some member information::
How to read a gzip compressed tar archive and display some member information::
import tarfile
import tarfile
...
...
Lib/tarfile.py
Dosyayı görüntüle @
64581041
...
@@ -2411,6 +2411,20 @@ class TarFile(object):
...
@@ -2411,6 +2411,20 @@ class TarFile(object):
"""
"""
if
level
<=
self
.
debug
:
if
level
<=
self
.
debug
:
print
>>
sys
.
stderr
,
msg
print
>>
sys
.
stderr
,
msg
def
__enter__
(
self
):
self
.
_check
()
return
self
def
__exit__
(
self
,
type
,
value
,
traceback
):
if
type
is
None
:
self
.
close
()
else
:
# An exception occurred. We must not call close() because
# it would try to write end-of-archive blocks and padding.
if
not
self
.
_extfileobj
:
self
.
fileobj
.
close
()
self
.
closed
=
True
# class TarFile
# class TarFile
class
TarIter
:
class
TarIter
:
...
...
Lib/test/test_tarfile.py
Dosyayı görüntüle @
64581041
...
@@ -1292,6 +1292,65 @@ class LimitsTest(unittest.TestCase):
...
@@ -1292,6 +1292,65 @@ class LimitsTest(unittest.TestCase):
tarinfo
.
tobuf
(
tarfile
.
PAX_FORMAT
)
tarinfo
.
tobuf
(
tarfile
.
PAX_FORMAT
)
class
ContextManagerTest
(
unittest
.
TestCase
):
def
test_basic
(
self
):
with
tarfile
.
open
(
tarname
)
as
tar
:
self
.
assertFalse
(
tar
.
closed
,
"closed inside runtime context"
)
self
.
assertTrue
(
tar
.
closed
,
"context manager failed"
)
def
test_closed
(
self
):
# The __enter__() method is supposed to raise IOError
# if the TarFile object is already closed.
tar
=
tarfile
.
open
(
tarname
)
tar
.
close
()
with
self
.
assertRaises
(
IOError
):
with
tar
:
pass
def
test_exception
(
self
):
# Test if the IOError exception is passed through properly.
with
self
.
assertRaises
(
Exception
)
as
exc
:
with
tarfile
.
open
(
tarname
)
as
tar
:
raise
IOError
self
.
assertIsInstance
(
exc
.
exception
,
IOError
,
"wrong exception raised in context manager"
)
self
.
assertTrue
(
tar
.
closed
,
"context manager failed"
)
def
test_no_eof
(
self
):
# __exit__() must not write end-of-archive blocks if an
# exception was raised.
try
:
with
tarfile
.
open
(
tmpname
,
"w"
)
as
tar
:
raise
Exception
except
:
pass
self
.
assertEqual
(
os
.
path
.
getsize
(
tmpname
),
0
,
"context manager wrote an end-of-archive block"
)
self
.
assertTrue
(
tar
.
closed
,
"context manager failed"
)
def
test_eof
(
self
):
# __exit__() must write end-of-archive blocks, i.e. call
# TarFile.close() if there was no error.
with
tarfile
.
open
(
tmpname
,
"w"
):
pass
self
.
assertNotEqual
(
os
.
path
.
getsize
(
tmpname
),
0
,
"context manager wrote no end-of-archive block"
)
def
test_fileobj
(
self
):
# Test that __exit__() did not close the external file
# object.
fobj
=
open
(
tmpname
,
"wb"
)
try
:
with
tarfile
.
open
(
fileobj
=
fobj
,
mode
=
"w"
)
as
tar
:
raise
Exception
except
:
pass
self
.
assertFalse
(
fobj
.
closed
,
"external file object was closed"
)
self
.
assertTrue
(
tar
.
closed
,
"context manager failed"
)
fobj
.
close
()
class
GzipMiscReadTest
(
MiscReadTest
):
class
GzipMiscReadTest
(
MiscReadTest
):
tarname
=
gzipname
tarname
=
gzipname
mode
=
"r:gz"
mode
=
"r:gz"
...
@@ -1371,6 +1430,7 @@ def test_main():
...
@@ -1371,6 +1430,7 @@ def test_main():
PaxUnicodeTest
,
PaxUnicodeTest
,
AppendTest
,
AppendTest
,
LimitsTest
,
LimitsTest
,
ContextManagerTest
,
]
]
if
hasattr
(
os
,
"link"
):
if
hasattr
(
os
,
"link"
):
...
...
Misc/NEWS
Dosyayı görüntüle @
64581041
...
@@ -38,6 +38,9 @@ Core and Builtins
...
@@ -38,6 +38,9 @@ Core and Builtins
Library
Library
-------
-------
- Issue #7232: Add support for the context manager protocol to the TarFile
class.
- Issue #7250: Fix info leak of os.environ across multi-run uses of
- Issue #7250: Fix info leak of os.environ across multi-run uses of
wsgiref.handlers.CGIHandler.
wsgiref.handlers.CGIHandler.
...
...
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