Both the subprocess and the popen2 modules use the range(...) function to iterate over and close inherited file descriptors when a subprocess is spawned. In the OS environment, it possible (using ulimit -n) to set SC_OPEN_MAX (and thus MAXFD in these modules) to a very high value. This can exhaust the limitations of the range() function, producing a MemoryError. The better thing to do would be to use xrange() instead. In particular, this bug occurs in subprocess._close_fds() and popen2._run_child().