util: Make dot_writer ignore NULL simobjects.
[gem5.git] / src / python / m5 / util / code_formatter.py
index 919a6423b9c1ddf1e63d5c7ac4448ee5feceb323..023e189cd984f46d06cc72374b49ae0ad8944ac9 100644 (file)
@@ -24,6 +24,7 @@
 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
+import __builtin__
 import inspect
 import os
 import re
@@ -54,18 +55,17 @@ class lookup(object):
         if item == '__line__':
             return self.frame.f_lineno
 
+        if self.formatter.locals and item in self.frame.f_locals:
+            return self.frame.f_locals[item]
+
         if item in self.dict:
             return self.dict[item]
 
-        if self.formatter.locals or self.formatter.globals:
-            if self.formatter.locals and item in self.frame.f_locals:
-                return self.frame.f_locals[item]
-
-            if self.formatter.globals and item in self.frame.f_globals:
-                return self.frame.f_globals[item]
+        if self.formatter.globals and item in self.frame.f_globals:
+            return self.frame.f_globals[item]
 
-        if item in __builtins__:
-            return __builtins__[item]
+        if item in __builtin__.__dict__:
+            return __builtin__.__dict__[item]
 
         try:
             item = int(item)
@@ -123,7 +123,7 @@ class code_formatter(object):
         self._dict = {}
         self._indent_level = 0
         self._indent_spaces = 4
-        self.globals = kwargs.pop('globals',type(self).globals)
+        self.globals = kwargs.pop('globals', type(self).globals)
         self.locals = kwargs.pop('locals', type(self).locals)
         self._fix_newlines = \
                 kwargs.pop('fix_newlines', type(self).fix_newlines)
@@ -131,12 +131,12 @@ class code_formatter(object):
         if args:
             self.__call__(args)
 
-    def indent(self):
-        self._indent_level += self._indent_spaces
+    def indent(self, count=1):
+        self._indent_level += self._indent_spaces * count
 
-    def dedent(self):
-        assert self._indent_level >= self._indent_spaces
-        self._indent_level -= self._indent_spaces
+    def dedent(self, count=1):
+        assert self._indent_level >= (self._indent_spaces * count)
+        self._indent_level -= self._indent_spaces * count
 
     def fix(self, status):
         previous = self._fix_newlines
@@ -200,10 +200,14 @@ class code_formatter(object):
 
             initial_newline = False
 
-    def insert_newline(self):
-        self._data.append('\n')
+    def __call__(self, *args, **kwargs):
+        if not args:
+            self._data.append('\n')
+            return
+
+        format = args[0]
+        args = args[1:]
 
-    def __call__(self, format, *args, **kwargs):
         frame = inspect.currentframe().f_back
 
         l = lookup(self, frame, *args, **kwargs)