Skip to content

Conversation

d-torrance
Copy link
Contributor

According to PEP 394, the python3 command is expected to be installed on Unix-like systems. Distributors have the option of providing the python command or not.

For example, on Debian/Ubuntu systems, there is no python command by default, and test_cli fails on these systems.

For example:

$ python3 -m pytest mpmath/tests/test_cli.py 
============================= test session starts ==============================
platform linux -- Python 3.10.12, pytest-6.2.5, py-1.10.0, pluggy-0.13.0
mpmath backend: gmpy
mpmath mp class: <mpmath.ctx_mp.MPContext object at 0x77cdb5d62020>
mpmath version: 0.0.0
Python version: 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]
rootdir: /home/dtorrance/src/mpmath/mpmath, configfile: pyproject.toml
collected 8 items                                                              

mpmath/tests/test_cli.py FFsFFException ignored in: <function Console.__del__ at 0x77cdb5ca9900>
Traceback (most recent call last):
  File "/home/dtorrance/src/mpmath/mpmath/mpmath/tests/test_cli.py", line 22, in __del__
    self.send('exit()\r\n')
  File "/usr/lib/python3/dist-packages/pexpect/pty_spawn.py", line 569, in send
    return os.write(self.child_fd, b)
OSError: [Errno 9] Bad file descriptor
FFF                                        [100%]

=================================== FAILURES ===================================
______________________ test_bare_console_no_bare_division ______________________

Exception ignored in: <function Console.__del__ at 0x77cdb5ca9900>
Traceback (most recent call last):
  File "/home/dtorrance/src/mpmath/mpmath/mpmath/tests/test_cli.py", line 22, in __del__
    self.send('exit()\r\n')
  File "/usr/lib/python3/dist-packages/pexpect/pty_spawn.py", line 569, in send
    return os.write(self.child_fd, b)
OSError: [Errno 9] Bad file descriptor
Exception ignored in: <function Console.__del__ at 0x77cdb5ca9900>
Traceback (most recent call last):
  File "/home/dtorrance/src/mpmath/mpmath/mpmath/tests/test_cli.py", line 22, in __del__
    self.send('exit()\r\n')
  File "/usr/lib/python3/dist-packages/pexpect/pty_spawn.py", line 569, in send
    return os.write(self.child_fd, b)
OSError: [Errno 9] Bad file descriptor
Exception ignored in: <function Console.__del__ at 0x77cdb5ca9900>
Traceback (most recent call last):
  File "/home/dtorrance/src/mpmath/mpmath/mpmath/tests/test_cli.py", line 22, in __del__
    self.send('exit()\r\n')
  File "/usr/lib/python3/dist-packages/pexpect/pty_spawn.py", line 569, in send
    return os.write(self.child_fd, b)
OSError: [Errno 9] Bad file descriptor
    def test_bare_console_no_bare_division():
>       c = Console('python -m mpmath --no-ipython --no-wrap-floats')

mpmath/tests/test_cli.py:29: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
mpmath/tests/test_cli.py:19: in __init__
    super().__init__(command, timeout=timeout, encoding='utf-8')
/usr/lib/python3/dist-packages/pexpect/pty_spawn.py:205: in __init__
    self._spawn(command, args, preexec_fn, dimensions)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <mpmath.tests.test_cli.Console object at 0x77cdb5b3e860>
command = 'python -m mpmath --no-ipython --no-wrap-floats', args = []
preexec_fn = None, dimensions = None

    def _spawn(self, command, args=[], preexec_fn=None, dimensions=None):
        '''This starts the given command in a child process. This does all the
        fork/exec type of stuff for a pty. This is called by __init__. If args
        is empty then command will be parsed (split on spaces) and args will be
        set to parsed arguments. '''
    
        # The pid and child_fd of this object get set by this method.
        # Note that it is difficult for this method to fail.
        # You cannot detect if the child process cannot start.
        # So the only way you can tell if the child process started
        # or not is to try to read from the file descriptor. If you get
        # EOF immediately then it means that the child is already dead.
        # That may not necessarily be bad because you may have spawned a child
        # that performs some task; creates no stdout output; and then dies.
    
        # If command is an int type then it may represent a file descriptor.
        if isinstance(command, type(0)):
            raise ExceptionPexpect('Command is an int type. ' +
                    'If this is a file descriptor then maybe you want to ' +
                    'use fdpexpect.fdspawn which takes an existing ' +
                    'file descriptor instead of a command string.')
    
        if not isinstance(args, type([])):
            raise TypeError('The argument, args, must be a list.')
    
        if args == []:
            self.args = split_command_line(command)
            self.command = self.args[0]
        else:
            # Make a shallow copy of the args list.
            self.args = args[:]
            self.args.insert(0, command)
            self.command = command
    
        command_with_path = which(self.command, env=self.env)
        if command_with_path is None:
>           raise ExceptionPexpect('The command was not found or was not ' +
                    'executable: %s.' % self.command)
E           pexpect.exceptions.ExceptionPexpect: The command was not found or was not executable: python.

/usr/lib/python3/dist-packages/pexpect/pty_spawn.py:276: ExceptionPexpect

Some systems (e.g., Debian/Ubuntu) ship a "python3" command instead of
"python" per PEP 394.  But we may not be able to guarantee if
"python3" exists either, so we use sys.executable to determine the
name of the command.
@d-torrance d-torrance changed the title Use "python3" command instead of "python" for test_cli Use sys.executable instead of "python" for test_cli Apr 18, 2024
@skirpichev skirpichev merged commit 4f3c60d into mpmath:master Apr 19, 2024
@skirpichev
Copy link
Collaborator

Thanks.

@skirpichev skirpichev added this to the 1.4 milestone May 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants