diff options
author | Ondrej Certik <ondrej.certik@gmail.com> | 2013-03-07 15:40:10 +0100 |
---|---|---|
committer | Ondrej Certik <ondrej.certik@gmail.com> | 2013-03-07 15:40:10 +0100 |
commit | 7c3b6dd33ec8bee517f1333e1dd0d4fb7078d01c (patch) | |
tree | 92854bacdfdb37ac326ca8813ed7a61f339aeefd /numpy/distutils/exec_command.py | |
parent | ff01555976cbbac5cb1ee9ecabc170a9d6886641 (diff) | |
download | numpy-7c3b6dd33ec8bee517f1333e1dd0d4fb7078d01c.tar.gz |
FIX: add _supports_fileno() for testing fileno()
The new _supports_fileno() function works in all versions of Python.
The problem was that in Python 2.x, the fileno() method of StringIO() is
missing, while in Python 3.x, it is present, but raises an exception.
Diffstat (limited to 'numpy/distutils/exec_command.py')
-rw-r--r-- | numpy/distutils/exec_command.py | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/numpy/distutils/exec_command.py b/numpy/distutils/exec_command.py index 9707f360d..c6635b6e0 100644 --- a/numpy/distutils/exec_command.py +++ b/numpy/distutils/exec_command.py @@ -141,6 +141,19 @@ def _update_environment( **env ): for name,value in env.items(): os.environ[name] = value or '' +def _supports_fileno(stream): + """ + Returns True if 'stream' supports the file descriptor and allows fileno(). + """ + if hasattr(stream, 'fileno'): + try: + r = stream.fileno() + return True + except IOError: + return False + else: + return False + def exec_command( command, execute_in='', use_shell=None, use_tee = None, _with_python = 1, @@ -195,7 +208,7 @@ def exec_command( command, # _exec_command_posix uses os.system and is faster # but not on all platforms os.system will return # a correct status. - if _with_python and hasattr(sys.stdout, 'fileno') and \ + if _with_python and _supports_fileno(sys.stdout) and \ sys.stdout.fileno() == -1: st = _exec_command_python(command, exec_command_dir = exec_dir, @@ -350,7 +363,7 @@ def _exec_command( command, use_shell=None, use_tee = None, **env ): argv = [os.environ['COMSPEC'],'/C'] + argv using_command = 1 - _has_fileno = hasattr(sys.stdout, 'fileno') + _has_fileno = _supports_fileno(sys.stdout) if _has_fileno: so_fileno = sys.stdout.fileno() se_fileno = sys.stderr.fileno() |