Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-74696: Pass root_dir to custom archivers which support it#94251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
27 commits
Select commitHold shift + click to select a range
e5ebb8d
gh-74696: Pass root_dir to custom archivers which support it
serhiy-storchakaJun 22, 2022
563b0f5
Update Doc/library/shutil.rst
serhiy-storchakaJun 25, 2022
2190304
Merge branch 'main' into shutil-register_archive_format-supports_root…
merwokSep 29, 2022
7dc8fb3
closes gh-97650: correct sphinx executable (gh-97651)
NoSuckSep 29, 2022
679cf96
gh-96397: Document that attributes need not be identifiers (#96454)
jeff5Sep 29, 2022
3c84af2
gh-96348: Deprecate the 3-arg signature of coroutine.throw and genera…
ofey404Sep 30, 2022
8a0ad46
Use SyntaxError invalid range in tutorial introduction example (GH-93…
ehebertSep 30, 2022
182755f
gh-97649: The Tools directory is no longer installed on Windows (GH-9…
zoobaSep 30, 2022
b1a9de0
gh-90989: Install Windows launcher per-user, and clarify some install…
zoobaSep 30, 2022
cc1e8e0
gh-94526: getpath_dirname() no longer encodes the path (#97645)
vstinnerSep 30, 2022
8fd0e86
bpo-35675: IDLE - separate config_key window and frame (#11427)
csabellaSep 30, 2022
d1d6b31
gh-87597: Document TimeoutExpired.stdout & .stderr types (#97685)
gpsheadSep 30, 2022
c6203f8
GH-96827: Don't touch closed loops from executor threads (#96837)
gvanrossumSep 30, 2022
67851dc
GH-97592: Fix crash in C remove_done_callback due to evil code (#97660)
gvanrossumSep 30, 2022
4c95e50
gh-90110: Update the c-analyzer Tool (gh-97695)
ericsnowcurrentlyOct 1, 2022
fb39e7f
gh-90908: Document asyncio.Task.cancelling() and asyncio.Task.uncance…
ambvOct 1, 2022
0bb7e71
Fix capitalization of Unix in documentation (#96913)
hawkinswOct 1, 2022
5ffc011
gh-95588: Drop the safety claim from `ast.literal_eval` docs. (#95919)
gpsheadOct 2, 2022
e401b65
gh-97591: In `Exception.__setstate__()` acquire strong references bef…
ofey404Oct 2, 2022
879e866
gh-95975: Move except/*/finally ref labels to more precise locations …
CAM-GerlachOct 2, 2022
299dd41
gh-97607: Fix content parsing in the impl-detail reST directive (#97652)
CAM-GerlachOct 2, 2022
551b707
[docs] Update logging cookbook with recipe for using a logger like an…
vsajipOct 2, 2022
b37f2cd
Refactor tests.
serhiy-storchakaOct 2, 2022
bf58b11
Merge branch 'main' into shutil-register_archive_format-supports_root…
serhiy-storchakaOct 2, 2022
9980c8c
Apply suggestions from code review
serhiy-storchakaOct 4, 2022
5160cb7
fix markup
merwokOct 4, 2022
701f896
Update Doc/whatsnew/3.12.rst
serhiy-storchakaOct 5, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletionsDoc/library/shutil.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -575,9 +575,10 @@ provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules.
..note::

This function is not thread-safe when custom archivers registered
with:func:`register_archive_format` are used. In this case it
with:func:`register_archive_format` do not support the *root_dir*
argument. In this case it
temporarily changes the current working directory of the process
to perform archiving.
to*root_dir* toperform archiving.

..versionchanged::3.8
The modern pax (POSIX.1-2001) format is now used instead of
Expand DownExpand Up@@ -614,12 +615,21 @@ provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules.
Further arguments are passed as keyword arguments: *owner*, *group*,
*dry_run* and *logger* (as passed in:func:`make_archive`).

If *function* has the custom attribute ``function.supports_root_dir`` set to ``True``,
the *root_dir* argument is passed as a keyword argument.
Otherwise the current working directory of the process is temporarily
changed to *root_dir* before calling *function*.
In this case:func:`make_archive` is not thread-safe.

If given, *extra_args* is a sequence of ``(name, value)`` pairs that will be
used as extra keywords arguments when the archiver callable is used.

*description* is used by:func:`get_archive_formats` which returns the
list of archivers. Defaults to an empty string.

..versionchanged::3.12
Added support for functions supporting the *root_dir* argument.


..function::unregister_archive_format(name)

Expand Down
9 changes: 9 additions & 0 deletionsDoc/whatsnew/3.12.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -127,6 +127,15 @@ os
for a process with :func:`os.pidfd_open` in non-blocking mode.
(Contributed by Kumar Aditya in :gh:`93312`.)

shutil
------

* :func:`shutil.make_archive` now passes the *root_dir* argument to custom
archivers which support it.
In this case it no longer temporarily changes the current working directory
of the process to *root_dir* to perform archiving.
(Contributed by Serhiy Storchaka in :gh:`74696`.)


sqlite3
-------
Expand Down
20 changes: 11 additions & 9 deletionsLib/shutil.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1023,28 +1023,30 @@ def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0,
zip_filename=os.path.abspath(zip_filename)
returnzip_filename

_make_tarball.supports_root_dir=True
_make_zipfile.supports_root_dir=True

# Maps the name of the archive format to a tuple containing:
# * the archiving function
# * extra keyword arguments
# * description
# * does it support the root_dir argument?
_ARCHIVE_FORMATS= {
'tar': (_make_tarball, [('compress',None)],
"uncompressed tar file",True),
"uncompressed tar file"),
}

if_ZLIB_SUPPORTED:
_ARCHIVE_FORMATS['gztar']= (_make_tarball, [('compress','gzip')],
"gzip'ed tar-file",True)
_ARCHIVE_FORMATS['zip']= (_make_zipfile, [],"ZIP file",True)
"gzip'ed tar-file")
_ARCHIVE_FORMATS['zip']= (_make_zipfile, [],"ZIP file")

if_BZ2_SUPPORTED:
_ARCHIVE_FORMATS['bztar']= (_make_tarball, [('compress','bzip2')],
"bzip2'ed tar-file",True)
"bzip2'ed tar-file")

if_LZMA_SUPPORTED:
_ARCHIVE_FORMATS['xztar']= (_make_tarball, [('compress','xz')],
"xz'ed tar-file",True)
"xz'ed tar-file")

defget_archive_formats():
"""Returns a list of supported formats for archiving and unarchiving.
Expand DownExpand Up@@ -1075,7 +1077,7 @@ def register_archive_format(name, function, extra_args=None, description=''):
ifnotisinstance(element, (tuple,list))orlen(element)!=2:
raiseTypeError('extra_args elements are : (arg_name, value)')

_ARCHIVE_FORMATS[name]= (function,extra_args,description,False)
_ARCHIVE_FORMATS[name]= (function,extra_args,description)

defunregister_archive_format(name):
del_ARCHIVE_FORMATS[name]
Expand DownExpand Up@@ -1114,10 +1116,10 @@ def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0,
ifbase_dirisNone:
base_dir=os.curdir

support_root_dir=format_info[3]
supports_root_dir=getattr(func,'supports_root_dir',False)
save_cwd=None
ifroot_dirisnotNone:
ifsupport_root_dir:
ifsupports_root_dir:
# Support path-like base_name here for backwards-compatibility.
base_name=os.fspath(base_name)
kwargs['root_dir']=root_dir
Expand Down
49 changes: 43 additions & 6 deletionsLib/test/test_shutil.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1568,28 +1568,65 @@ def test_tarfile_root_owner(self):
finally:
archive.close()

deftest_make_archive_cwd_default(self):
current_dir=os.getcwd()
defarchiver(base_name,base_dir,**kw):
self.assertNotIn('root_dir',kw)
self.assertEqual(base_name,'basename')
self.assertEqual(os.getcwd(),current_dir)
raiseRuntimeError()

register_archive_format('xxx',archiver, [],'xxx file')
try:
withno_chdir:
withself.assertRaises(RuntimeError):
make_archive('basename','xxx')
self.assertEqual(os.getcwd(),current_dir)
finally:
unregister_archive_format('xxx')

deftest_make_archive_cwd(self):
current_dir=os.getcwd()
root_dir=self.mkdtemp()
def_breaks(*args,**kw):
defarchiver(base_name,base_dir,**kw):
self.assertNotIn('root_dir',kw)
self.assertEqual(base_name,os.path.join(current_dir,'basename'))
self.assertEqual(os.getcwd(),root_dir)
raiseRuntimeError()
dirs= []
def_chdir(path):
dirs.append(path)
orig_chdir(path)

register_archive_format('xxx',_breaks, [],'xxx file')
register_archive_format('xxx',archiver, [],'xxx file')
try:
withsupport.swap_attr(os,'chdir',_chdir)asorig_chdir:
try:
make_archive('xxx','xxx',root_dir=root_dir)
exceptException:
pass
withself.assertRaises(RuntimeError):
make_archive('basename','xxx',root_dir=root_dir)
self.assertEqual(os.getcwd(),current_dir)
self.assertEqual(dirs, [root_dir,current_dir])
finally:
unregister_archive_format('xxx')

deftest_make_archive_cwd_supports_root_dir(self):
current_dir=os.getcwd()
root_dir=self.mkdtemp()
defarchiver(base_name,base_dir,**kw):
self.assertEqual(base_name,'basename')
self.assertEqual(kw['root_dir'],root_dir)
self.assertEqual(os.getcwd(),current_dir)
raiseRuntimeError()
archiver.supports_root_dir=True

register_archive_format('xxx',archiver, [],'xxx file')
try:
withno_chdir:
withself.assertRaises(RuntimeError):
make_archive('basename','xxx',root_dir=root_dir)
self.assertEqual(os.getcwd(),current_dir)
finally:
unregister_archive_format('xxx')

deftest_make_tarfile_in_curdir(self):
# Issue #21280
root_dir=self.mkdtemp()
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
:func:`shutil.make_archive` now passes the *root_dir* argument to custom
archivers which support it.

[8]ページ先頭

©2009-2025 Movatter.jp