Esempio n. 1
0
    def popen(command, mode='r', bufsize=-1):
        """popen(command [, mode='r' [, bufsize]]) -> pipe

        Open a pipe to/from a command returning a file object."""

        from popen2 import MAXFD
        import os, gc

        def try_close(fd):
            try:
                os.close(fd)
            except OSError:
                pass

        if not mode.startswith('r') and not mode.startswith('w'):
            raise ValueError("invalid mode %r" % (mode, ))
        read_end, write_end = os.pipe()
        try:
            gc.disable_finalizers()
            try:
                childpid = os.fork()
                if childpid == 0:
                    # in the child
                    try:
                        if mode.startswith('r'):
                            os.dup2(write_end, 1)
                            os.close(read_end)
                        else:
                            os.dup2(read_end, 0)
                            os.close(write_end)
                        os.closerange(3, MAXFD)
                        cmd = ['/bin/sh', '-c', command]
                        os.execvp(cmd[0], cmd)
                    finally:
                        os._exit(1)
            finally:
                gc.enable_finalizers()

            if mode.startswith('r'):
                os.close(write_end)
                fd = read_end
            else:
                os.close(read_end)
                fd = write_end
            g = popenfile.fdopen(fd, mode, bufsize)
            g._childpid = childpid
            return g

        except Exception, e:
            try_close(write_end)
            try_close(read_end)
            raise Exception, e  # bare 'raise' does not work here :-(
Esempio n. 2
0
    def popen(command, mode='r', bufsize=-1):
        """popen(command [, mode='r' [, bufsize]]) -> pipe

        Open a pipe to/from a command returning a file object."""

        from popen2 import MAXFD
        import os, gc

        def try_close(fd):
            try:
                os.close(fd)
            except OSError:
                pass

        if not mode.startswith('r') and not mode.startswith('w'):
            raise ValueError("invalid mode %r" % (mode,))
        read_end, write_end = os.pipe()
        try:
            gc.disable_finalizers()
            try:
                childpid = os.fork()
                if childpid == 0:
                    # in the child
                    try:
                        if mode.startswith('r'):
                            os.dup2(write_end, 1)
                            os.close(read_end)
                        else:
                            os.dup2(read_end, 0)
                            os.close(write_end)
                        os.closerange(3, MAXFD)
                        cmd = ['/bin/sh', '-c', command]
                        os.execvp(cmd[0], cmd)
                    finally:
                        os._exit(1)
            finally:
                gc.enable_finalizers()

            if mode.startswith('r'):
                os.close(write_end)
                fd = read_end
            else:
                os.close(read_end)
                fd = write_end
            g = popenfile.fdopen(fd, mode, bufsize)
            g._childpid = childpid
            return g

        except Exception, e:
            try_close(write_end)
            try_close(read_end)
            raise Exception, e     # bare 'raise' does not work here :-(
Esempio n. 3
0
 def __init__(self, cmd, bufsize=-1):
     _cleanup()
     self.cmd = cmd
     p2cread, p2cwrite = os.pipe()
     c2pread, c2pwrite = os.pipe()
     gc.disable_finalizers()
     try:
         self.pid = os.fork()
         if self.pid == 0:
             # Child
             os.dup2(p2cread, 0)
             os.dup2(c2pwrite, 1)
             os.dup2(c2pwrite, 2)
             self._run_child(cmd)
     finally:
         gc.enable_finalizers()
     os.close(p2cread)
     self.tochild = os.fdopen(p2cwrite, 'w', bufsize)
     os.close(c2pwrite)
     self.fromchild = os.fdopen(c2pread, 'r', bufsize)
Esempio n. 4
0
 def __init__(self, cmd, bufsize=-1):
     _cleanup()
     p2cread, p2cwrite = os.pipe()
     c2pread, c2pwrite = os.pipe()
     gc.disable_finalizers()
     try:
         self.pid = os.fork()
         if self.pid == 0:
             # Child
             os.dup2(p2cread, 0)
             os.dup2(c2pwrite, 1)
             os.dup2(c2pwrite, 2)
             self._run_child(cmd)
     finally:
         gc.enable_finalizers()
     os.close(p2cread)
     self.tochild = os.fdopen(p2cwrite, 'w', bufsize)
     os.close(c2pwrite)
     self.fromchild = os.fdopen(c2pread, 'r', bufsize)
     _active.append(self)
Esempio n. 5
0
    def test_disable_finalizers(self):
        import gc

        class X(object):
            created = 0
            deleted = 0

            def __init__(self):
                X.created += 1

            def __del__(self):
                X.deleted += 1

        class OldX:
            created = 0
            deleted = 0

            def __init__(self):
                OldX.created += 1

            def __del__(self):
                OldX.deleted += 1

        def runtest(should_be_enabled):
            runtest1(should_be_enabled, X)
            runtest1(should_be_enabled, OldX)

        def runtest1(should_be_enabled, Cls):
            gc.collect()
            if should_be_enabled:
                assert Cls.deleted == Cls.created
            else:
                old_deleted = Cls.deleted
            Cls()
            Cls()
            Cls()
            gc.collect()
            if should_be_enabled:
                assert Cls.deleted == Cls.created
            else:
                assert Cls.deleted == old_deleted

        runtest(True)
        gc.disable_finalizers()
        runtest(False)
        runtest(False)
        gc.enable_finalizers()
        runtest(True)
        # test nesting
        gc.disable_finalizers()
        gc.disable_finalizers()
        runtest(False)
        gc.enable_finalizers()
        runtest(False)
        gc.enable_finalizers()
        runtest(True)
        raises(ValueError, gc.enable_finalizers)
        runtest(True)
Esempio n. 6
0
    def test_disable_finalizers(self):
        import gc

        class X(object):
            created = 0
            deleted = 0

            def __init__(self):
                X.created += 1

            def __del__(self):
                X.deleted += 1

        class OldX:
            created = 0
            deleted = 0

            def __init__(self):
                OldX.created += 1

            def __del__(self):
                OldX.deleted += 1

        def runtest(should_be_enabled):
            runtest1(should_be_enabled, X)
            runtest1(should_be_enabled, OldX)

        def runtest1(should_be_enabled, Cls):
            gc.collect()
            if should_be_enabled:
                assert Cls.deleted == Cls.created
            else:
                old_deleted = Cls.deleted
            Cls()
            Cls()
            Cls()
            gc.collect()
            if should_be_enabled:
                assert Cls.deleted == Cls.created
            else:
                assert Cls.deleted == old_deleted

        runtest(True)
        gc.disable_finalizers()
        runtest(False)
        runtest(False)
        gc.enable_finalizers()
        runtest(True)
        # test nesting
        gc.disable_finalizers()
        gc.disable_finalizers()
        runtest(False)
        gc.enable_finalizers()
        runtest(False)
        gc.enable_finalizers()
        runtest(True)
        raises(ValueError, gc.enable_finalizers)
        runtest(True)
Esempio n. 7
0
 def __init__(self, cmd, capturestderr=False, bufsize=-1):
     """The parameter 'cmd' is the shell command to execute in a
     sub-process.  On UNIX, 'cmd' may be a sequence, in which case arguments
     will be passed directly to the program without shell intervention (as
     with os.spawnv()).  If 'cmd' is a string it will be passed to the shell
     (as with os.system()).   The 'capturestderr' flag, if true, specifies
     that the object should capture standard error output of the child
     process.  The default is false.  If the 'bufsize' parameter is
     specified, it specifies the size of the I/O buffers to/from the child
     process."""
     _cleanup()
     self.cmd = cmd
     p2cread, p2cwrite = os.pipe()
     c2pread, c2pwrite = os.pipe()
     if capturestderr:
         errout, errin = os.pipe()
     gc.disable_finalizers()
     try:
         self.pid = os.fork()
         if self.pid == 0:
             # Child
             os.dup2(p2cread, 0)
             os.dup2(c2pwrite, 1)
             if capturestderr:
                 os.dup2(errin, 2)
             self._run_child(cmd)
     finally:
         gc.enable_finalizers()
     os.close(p2cread)
     self.tochild = os.fdopen(p2cwrite, 'w', bufsize)
     os.close(c2pwrite)
     self.fromchild = os.fdopen(c2pread, 'r', bufsize)
     if capturestderr:
         os.close(errin)
         self.childerr = os.fdopen(errout, 'r', bufsize)
     else:
         self.childerr = None
Esempio n. 8
0
 def __init__(self, cmd, capturestderr=False, bufsize=-1):
     """The parameter 'cmd' is the shell command to execute in a
     sub-process.  On UNIX, 'cmd' may be a sequence, in which case arguments
     will be passed directly to the program without shell intervention (as
     with os.spawnv()).  If 'cmd' is a string it will be passed to the shell
     (as with os.system()).   The 'capturestderr' flag, if true, specifies
     that the object should capture standard error output of the child
     process.  The default is false.  If the 'bufsize' parameter is
     specified, it specifies the size of the I/O buffers to/from the child
     process."""
     _cleanup()
     p2cread, p2cwrite = os.pipe()
     c2pread, c2pwrite = os.pipe()
     if capturestderr:
         errout, errin = os.pipe()
     gc.disable_finalizers()
     try:
         self.pid = os.fork()
         if self.pid == 0:
             # Child
             os.dup2(p2cread, 0)
             os.dup2(c2pwrite, 1)
             if capturestderr:
                 os.dup2(errin, 2)
             self._run_child(cmd)
     finally:
         gc.enable_finalizers()
     os.close(p2cread)
     self.tochild = os.fdopen(p2cwrite, 'w', bufsize)
     os.close(c2pwrite)
     self.fromchild = os.fdopen(c2pread, 'r', bufsize)
     if capturestderr:
         os.close(errin)
         self.childerr = os.fdopen(errout, 'r', bufsize)
     else:
         self.childerr = None
     _active.append(self)
Esempio n. 9
0
def enable():
    global enabled
    import gc
    if not enabled:
        gc.enable_finalizers()
        enabled = True