diff options
author | Jeremy Hylton <jeremy@alum.mit.edu> | 2001-08-27 22:56:16 (GMT) |
---|---|---|
committer | Jeremy Hylton <jeremy@alum.mit.edu> | 2001-08-27 22:56:16 (GMT) |
commit | c59e22000082722655517942d7662eec5a4a70ce (patch) | |
tree | 9657ac8cb34b3b319ac0333ae0f5fe0239f8ccd0 /Lib/compiler/misc.py | |
parent | e7d8322630f82d22984987a97c11371ddacba59e (diff) | |
download | cpython-c59e22000082722655517942d7662eec5a4a70ce.zip cpython-c59e22000082722655517942d7662eec5a4a70ce.tar.gz cpython-c59e22000082722655517942d7662eec5a4a70ce.tar.bz2 |
Handle private names
(Hard to believe these were never handled before)
Add misc.mangle() that mangles based on the rules in compile.c.
XXX Need to test the corner cases
Update CodeGenerator with a class_name attribute bound to None. If a
particular instance is created within a class scope, the instance's
class_name is bound to that class's name.
Add mangle() method to CodeGenerator that mangles if the class_name
has a class_name in it.
Modify the FunctionCodeGenerator family to handle an extra argument--
the class_name.
Wrap all name ops and attrnames in calls to self.mangle()
Diffstat (limited to 'Lib/compiler/misc.py')
-rw-r--r-- | Lib/compiler/misc.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/Lib/compiler/misc.py b/Lib/compiler/misc.py index 29ff866..b834a2e 100644 --- a/Lib/compiler/misc.py +++ b/Lib/compiler/misc.py @@ -39,3 +39,26 @@ class Stack: self.stack.append(elt) def top(self): return self.stack[-1] + +MANGLE_LEN = 256 # magic constant from compile.c + +def mangle(name, klass): + if not name.startswith('__'): + return name + if len(name) + 2 >= MANGLE_LEN: + return name + if name.endswith('__'): + return name + try: + i = 0 + while klass[i] == '_': + i = i + 1 + except IndexError: + return name + klass = klass[i:] + + tlen = len(klass) + len(name) + if tlen > MANGLE_LEN: + klass = klass[:MANGLE_LEN-tlen] + + return "_%s%s" % (klass, name) |