2

I'm currently trying to port an older python application to version 3. In the process of doing so, the application now crashes when attempting to output data to a text file:

parser.py, line 110 

AttributeError("'HTMLPlusParser' object has no attribute 'rawdata'",),None)

I'm not really sure how to make a MVC example for this paticular situation, here is the relivent code:

HTMLPlus.py

class HTMLPlusParser(HTMLParser):   
    def __init__(self, formatter, verbose = None):
        if verbose and not hasattr(verbose, "write"):
            verbose = sys.stderr
            HTMLParser.__init__(self, formatter, verbose)  

        self.reparsestack = []
        self.tdelt = []

ReportWindow.py

 def parseConfigReport(self, filename, scxmldoc):
    source = etree.fromstring(scxmldoc.toxml())

    try:
        xslt_xml = etree.parse(filename)
        transform = etree.XSLT(xslt_xml)
        report = transform(source)
        self.reportHtml = str(report)

def saveToText(self):
    filename = os.path.join(self.parent.ReportPath, str(self.parent.CharName.text()) + "_report.txt")
    filename, filters = QFileDialog.getSaveFileName(self, "Save Report", filename, "Text (*.txt);;All Files (*.*)")

    if filename is not None and str(filename) != '':

        try:
            if re.compile('\.txt$').search(str(filename)) is None:
                filename = str(filename)
                filename += '.txt'
            f = open(str(filename), 'w')
            w = DimWriter(f)
            s = ObtuseFormatter(w)
            p = HTMLPlusParser(s)
            p.feed(self.reportHtml)
            p.close()
            w.flush()
            f.close()
            self.parent.ReportPath = os.path.dirname(os.path.abspath(str(filename)))

        except IOError:
            QMessageBox.critical(None, 'Error!', 'Error writing to file: ' + filename, 'OK')

def handle_data(self, data):
    if len(self.reparsestack):
        self.reparsestack[-1].append((getattr(self, 'handle_data'), data,))
    HTMLParser.handle_data(self, data)

def handle_starttag(self, tag, method, attrs):
    if self.reparsestack:
        self.reparsestack[-1].append((method, attrs,))
    HTMLParser.handle_starttag(self, tag, method, attrs)

def handle_endtag(self, tag, method):
    if self.tdelt:
        self.tdelt[-1].append((method,))
    HTMLParser.handle_endtag(self, tag, method)

I'm new to python, this is not my application (open source), and I don't really have much experience with outputting data to a file.

With that said, I don't really understand why I am getting a crash, especially in a file that is part of a library, and I don't know what I need to do to fix this issue.

I have tried changing:

HTMLParser.__init__(self, formatter, verbose) --> HTMLParser.__init__(self)

as it was suggested in another thread, but that didn't work. I'm not sure where to go from here. Any ideas?

I know formatter has been depreciated.. not sure what to use to replace it with. This would probably be easier if this was my own code.

UPDATE:

Moving the HTMLPlus init out of the if statement allowed me to push past this exception, but caused another exception

TypeError("handle_starttag() missing 1 required positional argument: 'attrs'",) in parse_starttag.parser.py:345

Again this exception happens in a library file parser.py and it appears that those variables are assigned within that file.

UPDATE2:

I realize that htmllib is now html.parser. This was a changed that I needed to make coming from Python 2.x to Python 3.x. Given that change, was there something that changed in the API perhaps? I can't seem to find any literature that suggests so, and this worked prior to the 2to3 conversion.

artomason
  • 3,625
  • 5
  • 20
  • 43
  • I'd try moving `HTMLParser.__init__(...)` outside the `if` statement. I mean, if the `if` doesn't get executed, the parent class doesn't get initiated. – ForceBru Nov 04 '17 at 17:07
  • @ForceBru That gets me past that exception, then a new one pops up. TypeError("handle_starttag() missing 1 required positional argument: 'attrs', but the code should be overriding that. I'm updating to reflect overrides. – artomason Nov 04 '17 at 17:22
  • I think you can contact the author of this lib directly and ask about the errors you're getting. For example, you can open an issue on GitHub. It's difficult to judge how this code should work. Maybe it was written for Python 2.x, or your Python installation's been corrupted, or you're passing invalid arguments to the constructor... – ForceBru Nov 04 '17 at 17:36
  • @ForceBru the source code I'm working from was originally made in Python 2.x, but I'm trying to convert it. Thanks for the suggestion, I will attempt to reach out to the author. – artomason Nov 04 '17 at 17:41

0 Answers0