Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4aec9a8

Browse files
bpo-29901: Improve support of path-like objects in zipapp. (#815)
Now general path-like objects are supported, not just pathlib.Path.
1 parent 3c749fc commit 4aec9a8

3 files changed

Lines changed: 11 additions & 11 deletions

File tree

Doc/library/zipapp.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ The module defines two convenience functions:
103103
Create an application archive from *source*. The source can be any
104104
of the following:
105105

106-
* The name of a directory, or a :class:`pathlib.Path` object referring
106+
* The name of a directory, or a :term:`path-like object` referring
107107
to a directory, in which case a new application archive will be
108108
created from the content of that directory.
109-
* The name of an existing application archive file, or a :class:`pathlib.Path`
110-
object referring to such a file, in which case the file is copied to
109+
* The name of an existing application archive file, or a :term:`path-like object`
110+
referring to such a file, in which case the file is copied to
111111
the target (modifying it to reflect the value given for the *interpreter*
112112
argument). The file name should include the ``.pyz`` extension, if required.
113113
* A file object open for reading in bytes mode. The content of the
@@ -117,7 +117,7 @@ The module defines two convenience functions:
117117
The *target* argument determines where the resulting archive will be
118118
written:
119119

120-
* If it is the name of a file, or a :class:`pathlb.Path` object,
120+
* If it is the name of a file, or a :term:`path-like object`,
121121
the archive will be written to that file.
122122
* If it is an open file object, the archive will be written to that
123123
file object, which must be open for writing in bytes mode.

Lib/zipapp.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ class ZipAppError(ValueError):
3636

3737
@contextlib.contextmanager
3838
def _maybe_open(archive, mode):
39-
if isinstance(archive, pathlib.Path):
40-
archive = str(archive)
41-
if isinstance(archive, str):
39+
if isinstance(archive, (str, os.PathLike)):
4240
with open(archive, mode) as f:
4341
yield f
4442
else:
@@ -135,10 +133,9 @@ def create_archive(source, target=None, interpreter=None, main=None):
135133
with _maybe_open(target, 'wb') as fd:
136134
_write_file_prefix(fd, interpreter)
137135
with zipfile.ZipFile(fd, 'w') as z:
138-
root = pathlib.Path(source)
139-
for child in root.rglob('*'):
140-
arcname = str(child.relative_to(root))
141-
z.write(str(child), arcname)
136+
for child in source.rglob('*'):
137+
arcname = child.relative_to(source).as_posix()
138+
z.write(child, arcname)
142139
if main_py:
143140
z.writestr('__main__.py', main_py.encode('utf-8'))
144141

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ Extension Modules
291291
Library
292292
-------
293293

294+
- bpo-29901: The zipapp module now supports general path-like objects, not
295+
just pathlib.Path.
296+
294297
- bpo-25803: Avoid incorrect errors raised by Path.mkdir(exist_ok=True)
295298
when the OS gives priority to errors such as EACCES over EEXIST.
296299

0 commit comments

Comments
 (0)