Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
Merge branch 'main' into gh-73991-rmtree
  • Loading branch information
barneygale committed Jun 18, 2024
commit 5d2472e1cb3a542134751d98c95dbd456bbb69d3
183 changes: 37 additions & 146 deletions Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1507,6 +1507,43 @@ Copying, renaming and deleting
Remove this directory. The directory must be empty.


.. method:: Path.rmtree(ignore_errors=False, on_error=None)

Delete this entire directory tree. The path must not refer to a symlink.
Comment thread
barneygale marked this conversation as resolved.
Outdated

If *ignore_errors* is true, errors resulting from failed removals will be
ignored. If *ignore_errors* is false or omitted, and a function is given to
*on_error*, it will be called each time an exception is raised. If neither
*ignore_errors* nor *on_error* are supplied, exceptions are propagated to
the caller.

.. note::

On platforms that support the necessary fd-based functions a symlink
Comment thread
barneygale marked this conversation as resolved.
Outdated
attack resistant version of :meth:`~Path.rmtree` is used by default. On
Comment thread
barneygale marked this conversation as resolved.
Outdated
other platforms, the :func:`~Path.rmtree` implementation is susceptible
to a symlink attack: given proper timing and circumstances, attackers
can manipulate symlinks on the filesystem to delete files they wouldn't
Comment thread
barneygale marked this conversation as resolved.
Outdated
be able to access otherwise. Applications can use the
:data:`Path.rmtree.avoids_symlink_attacks` function attribute to
determine which case applies.

If the optional argument *on_error* is specified, it should be a callable;
it will be called with one argument, an :exc:`OSError` instance. The
Comment thread
barneygale marked this conversation as resolved.
Outdated
callable can handle the error to continue the deletion process or re-raise
it to stop. Note that the filename is available as the ``filename``
Comment thread
barneygale marked this conversation as resolved.
Outdated
attribute of the exception object.

.. attribute:: Path.rmtree.avoids_symlink_attacks

Indicates whether the current platform and implementation provides a
symlink attack resistant version of :meth:`~Path.rmtree`. Currently
this is only true for platforms supporting fd-based directory access
functions.

.. versionadded:: 3.14


Other methods
^^^^^^^^^^^^^

Expand Down Expand Up @@ -1665,152 +1702,6 @@ Other methods
strict mode, and no exception is raised in non-strict mode. In previous
versions, :exc:`RuntimeError` is raised no matter the value of *strict*.

.. method:: Path.rmdir()

Remove this directory. The directory must be empty.


.. method:: Path.rmtree(ignore_errors=False, on_error=None)

Delete this entire directory tree. The path must not refer to a symlink.

If *ignore_errors* is true, errors resulting from failed removals will be
ignored. If *ignore_errors* is false or omitted, and a function is given to
*on_error*, it will be called each time an exception is raised. If neither
*ignore_errors* nor *on_error* are supplied, exceptions are propagated to
the caller.

.. note::

On platforms that support the necessary fd-based functions a symlink
attack resistant version of :meth:`~Path.rmtree` is used by default. On
other platforms, the :func:`~Path.rmtree` implementation is susceptible
to a symlink attack: given proper timing and circumstances, attackers
can manipulate symlinks on the filesystem to delete files they wouldn't
be able to access otherwise. Applications can use the
:data:`Path.rmtree.avoids_symlink_attacks` function attribute to
determine which case applies.

If the optional argument *on_error* is specified, it should be a callable;
it will be called with one argument, an :exc:`OSError` instance. The
callable can handle the error to continue the deletion process or re-raise
it to stop. Note that the filename is available as the ``filename``
attribute of the exception object.

.. attribute:: Path.rmtree.avoids_symlink_attacks

Indicates whether the current platform and implementation provides a
symlink attack resistant version of :meth:`~Path.rmtree`. Currently
this is only true for platforms supporting fd-based directory access
functions.

.. versionadded:: 3.14


.. method:: Path.symlink_to(target, target_is_directory=False)

Make this path a symbolic link pointing to *target*.

On Windows, a symlink represents either a file or a directory, and does not
morph to the target dynamically. If the target is present, the type of the
symlink will be created to match. Otherwise, the symlink will be created
as a directory if *target_is_directory* is ``True`` or a file symlink (the
default) otherwise. On non-Windows platforms, *target_is_directory* is ignored.

::

>>> p = Path('mylink')
>>> p.symlink_to('setup.py')
>>> p.resolve()
PosixPath('/home/antoine/pathlib/setup.py')
>>> p.stat().st_size
956
>>> p.lstat().st_size
8

.. note::
The order of arguments (link, target) is the reverse
of :func:`os.symlink`'s.

.. versionchanged:: 3.13
Raises :exc:`UnsupportedOperation` if :func:`os.symlink` is not
available. In previous versions, :exc:`NotImplementedError` was raised.


.. method:: Path.hardlink_to(target)

Make this path a hard link to the same file as *target*.

.. note::
The order of arguments (link, target) is the reverse
of :func:`os.link`'s.

.. versionadded:: 3.10

.. versionchanged:: 3.13
Raises :exc:`UnsupportedOperation` if :func:`os.link` is not
available. In previous versions, :exc:`NotImplementedError` was raised.


.. method:: Path.touch(mode=0o666, exist_ok=True)

Create a file at this given path. If *mode* is given, it is combined
with the process' ``umask`` value to determine the file mode and access
flags. If the file already exists, the function succeeds if *exist_ok*
is true (and its modification time is updated to the current time),
otherwise :exc:`FileExistsError` is raised.


.. method:: Path.unlink(missing_ok=False)

Remove this file or symbolic link. If the path points to a directory,
use :func:`Path.rmdir` instead.

If *missing_ok* is false (the default), :exc:`FileNotFoundError` is
raised if the path does not exist.

If *missing_ok* is true, :exc:`FileNotFoundError` exceptions will be
ignored (same behavior as the POSIX ``rm -f`` command).

.. versionchanged:: 3.8
The *missing_ok* parameter was added.


.. method:: Path.write_bytes(data)

Open the file pointed to in bytes mode, write *data* to it, and close the
file::

>>> p = Path('my_binary_file')
>>> p.write_bytes(b'Binary file contents')
20
>>> p.read_bytes()
b'Binary file contents'

An existing file of the same name is overwritten.

.. versionadded:: 3.5


.. method:: Path.write_text(data, encoding=None, errors=None, newline=None)

Open the file pointed to in text mode, write *data* to it, and close the
file::

>>> p = Path('my_text_file')
>>> p.write_text('Text file contents')
18
>>> p.read_text()
'Text file contents'

An existing file of the same name is overwritten. The optional parameters
have the same meaning as in :func:`open`.

.. versionadded:: 3.5

.. versionchanged:: 3.10
The *newline* parameter was added.


.. _pathlib-pattern-language:

Expand Down
2 changes: 1 addition & 1 deletion Lib/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ def _fwalk(stack, isbytes, topdown, onerror, follow_symlinks):
if isbytes:
name = fsencode(name)
try:
if entry.is_dir(follow_symlinks=follow_symlinks is not _walk_symlinks_as_files):
if entry.is_dir():
dirs.append(name)
if entries is not None:
entries.append(entry)
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.