[Python-checkins] gh-85984: Remove legacy Lib/pty.py code. (#92365)

gpshead webhook-mailer at python.org
Wed Feb 8 20:00:48 EST 2023


https://github.com/python/cpython/commit/244d4cd9d22d73fb3c0938937c4f435bd68f32d4
commit: 244d4cd9d22d73fb3c0938937c4f435bd68f32d4
branch: main
author: Soumendra Ganguly <67527439+8vasu at users.noreply.github.com>
committer: gpshead <greg at krypto.org>
date: 2023-02-08T17:00:17-08:00
summary:

gh-85984: Remove legacy Lib/pty.py code. (#92365)

Refactored the implementation of pty.fork to use os.login_tty.

A DeprecationWarning is now raised by pty.master_open() and pty.slave_open(). They were
undocumented and deprecated long long ago in the docstring in favor of pty.openpty.

Signed-off-by: Soumendra Ganguly <soumendraganguly at gmail.com>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg at krypto.org>

files:
A Misc/NEWS.d/next/Library/2023-02-05-21-40-15.gh-issue-85984.Kfzbb2.rst
M Doc/whatsnew/3.12.rst
M Lib/pty.py

diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst
index b723b70154f0..45a5e5062d55 100644
--- a/Doc/whatsnew/3.12.rst
+++ b/Doc/whatsnew/3.12.rst
@@ -512,6 +512,10 @@ Pending Removal in Python 3.14
   :func:`~multiprocessing.set_start_method` APIs to explicitly specify when
   your code *requires* ``'fork'``.  See :ref:`multiprocessing-start-methods`.
 
+* :mod:`pty` has two undocumented ``master_open()`` and ``slave_open()``
+  functions that have been deprecated since Python 2 but only gained a
+  proper :exc:`DeprecationWarning` in 3.12. Remove them in 3.14.
+
 Pending Removal in Future Versions
 ----------------------------------
 
diff --git a/Lib/pty.py b/Lib/pty.py
index 03073f07c92c..6571050886bd 100644
--- a/Lib/pty.py
+++ b/Lib/pty.py
@@ -40,6 +40,9 @@ def master_open():
     Open a pty master and return the fd, and the filename of the slave end.
     Deprecated, use openpty() instead."""
 
+    import warnings
+    warnings.warn("Use pty.openpty() instead.", DeprecationWarning, stacklevel=2)  # Remove API in 3.14
+
     try:
         master_fd, slave_fd = os.openpty()
     except (AttributeError, OSError):
@@ -69,6 +72,9 @@ def slave_open(tty_name):
     opened filedescriptor.
     Deprecated, use openpty() instead."""
 
+    import warnings
+    warnings.warn("Use pty.openpty() instead.", DeprecationWarning, stacklevel=2)  # Remove API in 3.14
+
     result = os.open(tty_name, os.O_RDWR)
     try:
         from fcntl import ioctl, I_PUSH
@@ -101,20 +107,8 @@ def fork():
     master_fd, slave_fd = openpty()
     pid = os.fork()
     if pid == CHILD:
-        # Establish a new session.
-        os.setsid()
         os.close(master_fd)
-
-        # Slave becomes stdin/stdout/stderr of child.
-        os.dup2(slave_fd, STDIN_FILENO)
-        os.dup2(slave_fd, STDOUT_FILENO)
-        os.dup2(slave_fd, STDERR_FILENO)
-        if slave_fd > STDERR_FILENO:
-            os.close(slave_fd)
-
-        # Explicitly open the tty to make it become a controlling tty.
-        tmp_fd = os.open(os.ttyname(STDOUT_FILENO), os.O_RDWR)
-        os.close(tmp_fd)
+        os.login_tty(slave_fd)
     else:
         os.close(slave_fd)
 
diff --git a/Misc/NEWS.d/next/Library/2023-02-05-21-40-15.gh-issue-85984.Kfzbb2.rst b/Misc/NEWS.d/next/Library/2023-02-05-21-40-15.gh-issue-85984.Kfzbb2.rst
new file mode 100644
index 000000000000..c91829f2c739
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-02-05-21-40-15.gh-issue-85984.Kfzbb2.rst
@@ -0,0 +1,4 @@
+Refactored the implementation of :func:`pty.fork` to use :func:`os.login_tty`.
+
+A :exc:`DeprecationWarning` is now raised by ``pty.master_open()`` and ``pty.slave_open()``. They were
+undocumented and deprecated long long ago in the docstring in favor of :func:`pty.openpty`.



More information about the Python-checkins mailing list