class stdout(object):
    def __init__(self,realout,prefix):
        self.file = realout
        self.at_start_of_line = True
        self.prefix=prefix
    def write(self, text):
        if len(text):
            if self.at_start_of_line:
                text = self.prefix + text

            self.file.write(text)

            self.at_start_of_line = (text[-1] == '\n')
sys.stdout=stdout(sys.stdout,"OUT  ["+f+"]> ")




  File "C:\Users\James\Desktop\run.py", line 13, in write
    self.file.write(text)
  File "C:\Users\James\Desktop\run.py", line 10, in write
    if self.at_start_of_line:
  AttributeError: 'stdout' object has no attribute 'prefix'

It's not the whole thing but the error is really confusing

Dani AI

Generated

The traceback shows a telltale pattern: write calls self.file.write(...) and that call re-enters a write method on a stdout object that lacks prefix. In other words, the object stored in self.file is not the original underlying stream but another wrapper (or an instance that wasn't fully initialized), so calling it causes recursion and then an AttributeError when prefix is accessed.

Concrete diagnostics (safe to run while the wrapper is active):

  • Inspect the real target with the real stdout (bypass the wrapper): sys.__stdout__.write("DBG: file=%r prefix=%r\n" % (getattr(self,'file',None), getattr(self,'prefix',None))).
    Using sys.__stdout__ avoids triggering the wrapper and lets the program report what self.file actually is.
  • Check whether the wrapper is created before the prefix/variables used to build it exist; that can cause the replacement to use the wrong values or happen at the wrong time.

Practical fixes and best practices:

  • Save the original stream first (for example orig = sys.__stdout__ or orig = sys.stdout before any reassignment) and always pass that original to the wrapper. Never pass a stream that might already be the wrapper itself.
  • If the prefix needs to change per-loop, create one wrapper once and update a prefix attribute (or a set_prefix() method) instead of recreating sys.stdout repeatedly.
  • Avoid calling print() or writing to sys.stdout from inside the wrapper; always write to the saved original stream to prevent re-entry.

This lines up with ’s comment about an uninitialized instance: the error is consistent with a write call happening on an object that didn’t get its prefix set (either because the wrapper wrapped itself or the replacement ran at the wrong time). ’s request for a full snippet is the right move for reproducing the exact timing issue; the simplest immediate change is to capture the original stdout and update the wrapper’s prefix after the raw_input is read.

Recommended Answers

All 4 Replies

Can you post a full snippet replicating the error ?

This looks like you are using class, not class instance, so the instance variable is not initialized.

Your code is really odd.

from subprocess import *
import sys
class stdout(object):
    def __init__(self,realout,prefix):
        self.file = realout
        self.at_start_of_line = True
        self.prefix=prefix
    def write(self, text):
        if len(text):
            if self.at_start_of_line:
                text = self.prefix + text

            self.file.write(text)

            self.at_start_of_line = (text[-1] == '\n')
sys.stdout=stdout(sys.stdout,"OUT  ["+f+"]> ")
while True:
    f = raw_input("URL:")
    p = Popen("C:/python27/python.exe "+f,shell=True)#,stdin=PIPE,stdout=PIPE,stderr=PIPE)
    print"Press enter to read from pipes"
    print"Press Control-C to terminate"
    while not p.poll():
        try:
            d = raw_input("IN   ["+f+"]> ")
            if d == '':
                d = None
            try:
                output,errors = p.communicate(d)
            except:
                pass
            if errors.strip():
                print "ERROR["+f+"]>"
                print errors
        except KeyboardInterrupt:
            print"Terminating..."
            p.terminate()
            print"Terminated."
            break
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.