GeezerVison is showing github.com — gh-110383: Remove references to removed popen[234] (GH-112783) · python/cpython@b1374aa
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: Doc/faq/library.rst -78Lines changed: 0 additions & 78 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -541,84 +541,6 @@ Thus, to read *n* bytes from a pipe *p* created with :func:`os.popen`, you need | 541 | 541 | use ``p.read(n)``. | 542 | 542 |
| 543 | 543 |
| 544 | | -.. XXX update to use subprocess. See the :ref:`subprocess-replacements` section. | 545 | | -
| 546 | | - How do I run a subprocess with pipes connected to both input and output? | 547 | | - ------------------------------------------------------------------------ | 548 | | -
| 549 | | - Use the :mod:`popen2` module. For example:: | 550 | | -
| 551 | | - import popen2 | 552 | | - fromchild, tochild = popen2.popen2("command") | 553 | | - tochild.write("input\n") | 554 | | - tochild.flush() | 555 | | - output = fromchild.readline() | 556 | | -
| 557 | | - Warning: in general it is unwise to do this because you can easily cause a | 558 | | - deadlock where your process is blocked waiting for output from the child | 559 | | - while the child is blocked waiting for input from you. This can be caused | 560 | | - by the parent expecting the child to output more text than it does or | 561 | | - by data being stuck in stdio buffers due to lack of flushing. | 562 | | - The Python parent can of course explicitly flush the data it sends to the | 563 | | - child before it reads any output, but if the child is a naive C program it | 564 | | - may have been written to never explicitly flush its output, even if it is | 565 | | - interactive, since flushing is normally automatic. | 566 | | -
| 567 | | - Note that a deadlock is also possible if you use :func:`popen3` to read | 568 | | - stdout and stderr. If one of the two is too large for the internal buffer | 569 | | - (increasing the buffer size does not help) and you ``read()`` the other one | 570 | | - first, there is a deadlock, too. | 571 | | -
| 572 | | - Note on a bug in popen2: unless your program calls ``wait()`` or | 573 | | - ``waitpid()``, finished child processes are never removed, and eventually | 574 | | - calls to popen2 will fail because of a limit on the number of child | 575 | | - processes. Calling :func:`os.waitpid` with the :const:`os.WNOHANG` option can | 576 | | - prevent this; a good place to insert such a call would be before calling | 577 | | - ``popen2`` again. | 578 | | -
| 579 | | - In many cases, all you really need is to run some data through a command and | 580 | | - get the result back. Unless the amount of data is very large, the easiest | 581 | | - way to do this is to write it to a temporary file and run the command with | 582 | | - that temporary file as input. The standard module :mod:`tempfile` exports a | 583 | | - :func:`~tempfile.mktemp` function to generate unique temporary file names. :: | 584 | | -
| 585 | | - import tempfile | 586 | | - import os | 587 | | -
| 588 | | - class Popen3: | 589 | | - """ | 590 | | - This is a deadlock-safe version of popen that returns | 591 | | - an object with errorlevel, out (a string) and err (a string). | 592 | | - (capturestderr may not work under windows.) | 593 | | - Example: print(Popen3('grep spam','\n\nhere spam\n\n').out) | 594 | | - """ | 595 | | - def __init__(self,command,input=None,capturestderr=None): | 596 | | - outfile=tempfile.mktemp() | 597 | | - command="( %s ) > %s" % (command,outfile) | 598 | | - if input: | 599 | | - infile=tempfile.mktemp() | 600 | | - open(infile,"w").write(input) | 601 | | - command=command+" <"+infile | 602 | | - if capturestderr: | 603 | | - errfile=tempfile.mktemp() | 604 | | - command=command+" 2>"+errfile | 605 | | - self.errorlevel=os.system(command) >> 8 | 606 | | - self.out=open(outfile,"r").read() | 607 | | - os.remove(outfile) | 608 | | - if input: | 609 | | - os.remove(infile) | 610 | | - if capturestderr: | 611 | | - self.err=open(errfile,"r").read() | 612 | | - os.remove(errfile) | 613 | | -
| 614 | | - Note that many interactive programs (e.g. vi) don't work well with pipes | 615 | | - substituted for standard input and output. You will have to use pseudo ttys | 616 | | - ("ptys") instead of pipes. Or you can use a Python interface to Don Libes' | 617 | | - "expect" library. A Python extension that interfaces to expect is called | 618 | | - "expy" and available from https://expectpy.sourceforge.net. A pure Python | 619 | | - solution that works like expect is :pypi:`pexpect`. | 620 | | -
| 621 | | -
| 622 | 544 | How do I access the serial (RS232) port? | 623 | 545 | ---------------------------------------- | 624 | 546 |
| |
Collapse file Copy file name to clipboardExpand all lines: Doc/library/subprocess.rst +2-68Lines changed: 2 additions & 68 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -1443,36 +1443,8 @@ Environment example:: | 1443 | 1443 |
| 1444 | 1444 |
| 1445 | 1445 |
| 1446 | | -Replacing :func:`os.popen`, :func:`os.popen2`, :func:`os.popen3` | 1447 | | -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | 1448 | | - | 1449 | | -:: | 1450 | | - | 1451 | | - (child_stdin, child_stdout) = os.popen2(cmd, mode, bufsize) | 1452 | | - ==> | 1453 | | - p = Popen(cmd, shell=True, bufsize=bufsize, | 1454 | | - stdin=PIPE, stdout=PIPE, close_fds=True) | 1455 | | - (child_stdin, child_stdout) = (p.stdin, p.stdout) | 1456 | | - | 1457 | | -:: | 1458 | | - | 1459 | | - (child_stdin, | 1460 | | - child_stdout, | 1461 | | - child_stderr) = os.popen3(cmd, mode, bufsize) | 1462 | | - ==> | 1463 | | - p = Popen(cmd, shell=True, bufsize=bufsize, | 1464 | | - stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True) | 1465 | | - (child_stdin, | 1466 | | - child_stdout, | 1467 | | - child_stderr) = (p.stdin, p.stdout, p.stderr) | 1468 | | - | 1469 | | -:: | 1470 | | - | 1471 | | - (child_stdin, child_stdout_and_stderr) = os.popen4(cmd, mode, bufsize) | 1472 | | - ==> | 1473 | | - p = Popen(cmd, shell=True, bufsize=bufsize, | 1474 | | - stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True) | 1475 | | - (child_stdin, child_stdout_and_stderr) = (p.stdin, p.stdout) | | 1446 | +Replacing :func:`os.popen` | | 1447 | +^^^^^^^^^^^^^^^^^^^^^^^^^^ | 1476 | 1448 |
| 1477 | 1449 | Return code handling translates as follows:: | 1478 | 1450 |
| @@ -1489,44 +1461,6 @@ Return code handling translates as follows:: | 1489 | 1461 | print("There were some errors") | 1490 | 1462 |
| 1491 | 1463 |
| 1492 | | -Replacing functions from the :mod:`!popen2` module | 1493 | | -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | 1494 | | - | 1495 | | -.. note:: | 1496 | | - | 1497 | | - If the cmd argument to popen2 functions is a string, the command is executed | 1498 | | - through /bin/sh. If it is a list, the command is directly executed. | 1499 | | - | 1500 | | -:: | 1501 | | - | 1502 | | - (child_stdout, child_stdin) = popen2.popen2("somestring", bufsize, mode) | 1503 | | - ==> | 1504 | | - p = Popen("somestring", shell=True, bufsize=bufsize, | 1505 | | - stdin=PIPE, stdout=PIPE, close_fds=True) | 1506 | | - (child_stdout, child_stdin) = (p.stdout, p.stdin) | 1507 | | - | 1508 | | -:: | 1509 | | - | 1510 | | - (child_stdout, child_stdin) = popen2.popen2(["mycmd", "myarg"], bufsize, mode) | 1511 | | - ==> | 1512 | | - p = Popen(["mycmd", "myarg"], bufsize=bufsize, | 1513 | | - stdin=PIPE, stdout=PIPE, close_fds=True) | 1514 | | - (child_stdout, child_stdin) = (p.stdout, p.stdin) | 1515 | | - | 1516 | | -:class:`popen2.Popen3` and :class:`popen2.Popen4` basically work as | 1517 | | -:class:`subprocess.Popen`, except that: | 1518 | | - | 1519 | | -* :class:`Popen` raises an exception if the execution fails. | 1520 | | - | 1521 | | -* The *capturestderr* argument is replaced with the *stderr* argument. | 1522 | | - | 1523 | | -* ``stdin=PIPE`` and ``stdout=PIPE`` must be specified. | 1524 | | - | 1525 | | -* popen2 closes all file descriptors by default, but you have to specify | 1526 | | - ``close_fds=True`` with :class:`Popen` to guarantee this behavior on | 1527 | | - all platforms or past Python versions. | 1528 | | - | 1529 | | - | 1530 | 1464 | Legacy Shell Invocation Functions | 1531 | 1465 | --------------------------------- | 1532 | 1466 |
| |
|