[Tutor] Question on os.popen

eryksun eryksun at gmail.com
Mon Jan 20 05:33:46 CET 2014


On Sun, Jan 19, 2014 at 6:36 PM, SM <sunithanc at gmail.com> wrote:
>
> This time it probably ran for a few more iterations than before and stopped
> with the same error message. This time it also output the following
> messages:
>
> IOError: [Errno 4] Interrupted system call
> Attribute not found in file (tsk_fs_attrlist_get: Attribute 128 not found)
> Attribute not found in file (tsk_fs_attrlist_get: Attribute 128 not found)
> Attribute not found in file (tsk_fs_attrlist_get: Attribute 128 not found)

I can't help with these (NTFS?) Attribute errors from "The Sleuth Kit"
digital forensics tools.

In Python 3.3, `IOError` is an alias for `OSError`, and EINTR (i.e.
errno.errorcode[4]) is exposed directly as `InterruptedError`. So you
must be running a previous version. I see you're using `print` as a
function, so I'll guess you're using 3.2.

In 3.2, `os.popen` is implemented via `subprocess.Popen`:

http://hg.python.org/cpython/file/cef745775b65/Lib/os.py#l776

For example, it uses the following for 'r' mode:

    proc = subprocess.Popen(cmd,
                            shell=True,
                            stdout=subprocess.PIPE,
                            bufsize=buffering)
   return _wrap_close(io.TextIOWrapper(proc.stdout), proc)

If you're sticking to `os.popen`, you'll need to retry the read in
case of an interrupted system call.

I recommend you switch to `Popen` directly and call `communicate`.
This retries reading `stdout` using the helper function
`_eintr_retry_call`:

http://hg.python.org/cpython/file/cef745775b65/Lib/subprocess.py#l452

    def _eintr_retry_call(func, *args):
        while True:
            try:
                return func(*args)
            except (OSError, IOError) as e:
                if e.errno == errno.EINTR:
                    continue
                raise

More simply, use `subprocess.check_output`, which calls `communicate`
for you. You can pass `shell=True` if you really need it.


More information about the Tutor mailing list