PyCG icon indicating copy to clipboard operation
PyCG copied to clipboard

AttributeError: 'NoneType' object has no attribute 'reset_counters'

Open AllenSun1024 opened this issue 3 years ago • 3 comments

Hi,

I encountered the following error when using PyCG to parse tensorflow models:

Original Error Description

  • AttributeError: 'NoneType' object has no attribute 'reset_counters'
  • The problem occurs in /pycg/processing/base.py/self.scope_manager.get_scope(self.current_ns).reset_counters()

Traceback

  • Error in sys.excepthook, exactly, in /usr/lib/python3/dist-packages/apport_python_hook.py/from apport.fileutils import likely_packaged, get_recent_crashes)
  • ImportError: cannot import name 'likely_packaged'

So, I'm confused about the following two questions:

  1. What is the cause of the above error?
  2. Can it be fixed or avoided?

AllenSun1024 avatar Nov 16 '21 08:11 AllenSun1024

Hello, anyone can answer the above question? @vitsalis

AllenSun1024 avatar Dec 27 '21 09:12 AllenSun1024

Yep, this should not happen. self.scope_manager.get_scope(self.current_ns) should never return null. Would appreciate some more information about the error (whole error statck + python file that leads to the error).

vitsalis avatar Jan 02 '22 08:01 vitsalis

I got a similar issue AttributeError: 'NoneType' object has no attribute 'add_def'

Let's say I want to get a call graph for a file in which there is a function named 'top'.

def top():
    pass

Then PyCG will throw an AttributeError: 'NoneType' object has no attribute 'add_def'

Traceback (most recent call last):
  File "/home/xu/.local/bin/pycg", line 8, in <module>
    sys.exit(main())
  File "/home/xu/.local/lib/python3.8/site-packages/pycg/__main__.py", line 79, in main
    cg.analyze()
  File "/home/xu/.local/lib/python3.8/site-packages/pycg/pycg.py", line 155, in analyze
    self.do_pass(PreProcessor, True,
  File "/home/xu/.local/lib/python3.8/site-packages/pycg/pycg.py", line 148, in do_pass
    processor.analyze()
  File "/home/xu/.local/lib/python3.8/site-packages/pycg/processing/preprocessor.py", line 375, in analyze
    self.visit(ast.parse(self.contents, self.filename))
  File "/usr/lib/python3.8/ast.py", line 371, in visit
    return visitor(node)
  File "/home/xu/.local/lib/python3.8/site-packages/pycg/processing/preprocessor.py", line 107, in visit_Module
    iterate_mod_items(items["functions"], utils.constants.FUN_DEF)
  File "/home/xu/.local/lib/python3.8/site-packages/pycg/processing/preprocessor.py", line 79, in iterate_mod_items
    self.scope_manager.get_scope(parentns).add_def(name, defi)
AttributeError: 'NoneType' object has no attribute 'add_def'
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 57, in apport_excepthook
    apt_pkg.DATE
AttributeError: module 'apt_pkg' has no attribute 'DATE'

Original exception was:
Traceback (most recent call last):
  File "/home/xu/.local/bin/pycg", line 8, in <module>
    sys.exit(main())
  File "/home/xu/.local/lib/python3.8/site-packages/pycg/__main__.py", line 79, in main
    cg.analyze()
  File "/home/xu/.local/lib/python3.8/site-packages/pycg/pycg.py", line 155, in analyze
    self.do_pass(PreProcessor, True,
  File "/home/xu/.local/lib/python3.8/site-packages/pycg/pycg.py", line 148, in do_pass
    processor.analyze()
  File "/home/xu/.local/lib/python3.8/site-packages/pycg/processing/preprocessor.py", line 375, in analyze
    self.visit(ast.parse(self.contents, self.filename))
  File "/usr/lib/python3.8/ast.py", line 371, in visit
    return visitor(node)
  File "/home/xu/.local/lib/python3.8/site-packages/pycg/processing/preprocessor.py", line 107, in visit_Module
    iterate_mod_items(items["functions"], utils.constants.FUN_DEF)
  File "/home/xu/.local/lib/python3.8/site-packages/pycg/processing/preprocessor.py", line 79, in iterate_mod_items
    self.scope_manager.get_scope(parentns).add_def(name, defi)
AttributeError: 'NoneType' object has no attribute 'add_def'

I might be wrong but I think the cause for this is on line 34 at pycg/machinery/scopes.py:

name = table.get_name() if table.get_name() != 'top' else ''

class ScopeManager(object):
    """Manages the scope entries"""

    def __init__(self):
        self.scopes = {}

    def handle_module(self, modulename, filename, contents):
        functions = []
        classes = []
        def process(namespace, parent, table):
            name = table.get_name() if table.get_name() != 'top' else ''
            if name:
                fullns = utils.join_ns(namespace, name)
            else:
                fullns = namespace

            if table.get_type() == "function":
                functions.append(fullns)

            if table.get_type() == "class":
                classes.append(fullns)

            sc = self.create_scope(fullns, parent)

            for t in table.get_children():
                process(fullns, sc, t)

        process(modulename, None, symtable.symtable(contents, filename, compile_type="exec"))

        return {"functions": functions, "classes": classes}

Might be a similar cause to AttributeError: 'NoneType' object has no attribute 'reset_counters' since there is a function named top in tensorflow/models code base

Since 'top' is kind of a reserved keyword in symtable, I guess it can be fixed by additionally checking the line number.

So change

name = table.get_name() if table.get_name() != 'top' else ''

to

            if table.get_name() == 'top' and table.get_lineno() == 0:
                name = ''
            else:
                name = table.get_name()

Any ideas @vitsalis ?

seanxiaoyan avatar Mar 07 '22 00:03 seanxiaoyan

Closing due to archival of repository.

vitsalis avatar Nov 26 '23 07:11 vitsalis