PyXR

c:\python24\lib \ compiler \ misc.py



0001 import types
0002 
0003 def flatten(tup):
0004     elts = []
0005     for elt in tup:
0006         if type(elt) == types.TupleType:
0007             elts = elts + flatten(elt)
0008         else:
0009             elts.append(elt)
0010     return elts
0011 
0012 class Set:
0013     def __init__(self):
0014         self.elts = {}
0015     def __len__(self):
0016         return len(self.elts)
0017     def __contains__(self, elt):
0018         return self.elts.has_key(elt)
0019     def add(self, elt):
0020         self.elts[elt] = elt
0021     def elements(self):
0022         return self.elts.keys()
0023     def has_elt(self, elt):
0024         return self.elts.has_key(elt)
0025     def remove(self, elt):
0026         del self.elts[elt]
0027     def copy(self):
0028         c = Set()
0029         c.elts.update(self.elts)
0030         return c
0031 
0032 class Stack:
0033     def __init__(self):
0034         self.stack = []
0035         self.pop = self.stack.pop
0036     def __len__(self):
0037         return len(self.stack)
0038     def push(self, elt):
0039         self.stack.append(elt)
0040     def top(self):
0041         return self.stack[-1]
0042     def __getitem__(self, index): # needed by visitContinue()
0043         return self.stack[index]
0044 
0045 MANGLE_LEN = 256 # magic constant from compile.c
0046 
0047 def mangle(name, klass):
0048     if not name.startswith('__'):
0049         return name
0050     if len(name) + 2 >= MANGLE_LEN:
0051         return name
0052     if name.endswith('__'):
0053         return name
0054     try:
0055         i = 0
0056         while klass[i] == '_':
0057             i = i + 1
0058     except IndexError:
0059         return name
0060     klass = klass[i:]
0061 
0062     tlen = len(klass) + len(name)
0063     if tlen > MANGLE_LEN:
0064         klass = klass[:MANGLE_LEN-tlen]
0065 
0066     return "_%s%s" % (klass, name)
0067 
0068 def set_filename(filename, tree):
0069     """Set the filename attribute to filename on every node in tree"""
0070     worklist = [tree]
0071     while worklist:
0072         node = worklist.pop(0)
0073         node.filename = filename
0074         worklist.extend(node.getChildNodes())
0075 

Generated by PyXR 0.9.4
SourceForge.net Logo