Commit f3845fec authored by Matthieu Gonnet's avatar Matthieu Gonnet Committed by Seblu
Browse files

Fix fd argument allocation in out and err Printer methods

The fd default argument in the out and err fonction causes bad issues because it was initialized
when the class was instanciated. Consequently this patch set the default argument to None and permit you
to change the stdout and stderr file descriptor without causing troubles
parent 368b704c
Loading
Loading
Loading
Loading
+19 −15
Original line number Diff line number Diff line
@@ -70,43 +70,47 @@ class Printer(object):
        # enable completion
        self.completion.readline = readline

    def out(self, message="", fd=sys.stdout, nl=os.linesep, flush=True):
    def out(self, message="", fd=None, nl=os.linesep, flush=True):
        '''Print a message in fd ended by nl'''
        if fd is None:
            fd = sys.stdout
        fd.write("%s%s"%(message, nl))
        if flush:
            fd.flush()

    def err(self, message, fd=sys.stderr, nl=os.linesep):
    def err(self, message, fd=None, nl=os.linesep):
        if fd is None:
            fd = sys.stderr
        self.out(message, fd, nl)

    def fatal(self, message, quit=True, fd=sys.stderr, nl=os.linesep):
        self.out("%sFatal%s: %s%s"%(color["lred"],color["red"],message, color["reset"]),
    def fatal(self, message, quit=True, fd=None, nl=os.linesep):
        self.err("%sFatal%s: %s%s"%(color["lred"],color["red"],message, color["reset"]),
                 fd,
                 nl)
        if quit:
            os.kill(0, signal.SIGKILL)
            os._exit()

    def error(self, message, fd=sys.stderr, nl=os.linesep):
        self.out("%sError%s: %s%s"%(color["lred"],color["red"],message,color["reset"]),
    def error(self, message, fd=None, nl=os.linesep):
        self.err("%sError%s: %s%s"%(color["lred"],color["red"],message,color["reset"]),
                 fd,
                 nl)

    def warn(self, message, fd=sys.stderr, nl=os.linesep):
        self.out("%sWarning%s: %s%s"%(color["lyellow"],color["yellow"],message,color["reset"]),
    def warn(self, message, fd=None, nl=os.linesep):
        self.err("%sWarning%s: %s%s"%(color["lyellow"],color["yellow"],message,color["reset"]),
                 fd,
                 nl)

    def debug(self, message, fd=sys.stderr, nl=os.linesep):
    def debug(self, message, fd=None, nl=os.linesep):
        if cccli.debug:
            self.out("%s%s%s"%(color["lgrey"],message,color["reset"]), fd, nl)
            self.err("%s%s%s"%(color["lgrey"],message,color["reset"]), fd, nl)

    def isinteractive(self):
        return self.readline is not None

    def interactive(self, message, fd=sys.stderr, nl=os.linesep):
    def interactive(self, message, fd=None, nl=os.linesep):
        '''Print only in interactive mode'''
        if self.readline is not None:
            self.out(message, fd, nl)
            self.err(message, fd, nl)

    def getline(self, prompt):
        '''Read a line from stdin'''