forked from cool-RR/python_toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathname_mangling.py
More file actions
51 lines (32 loc) · 1.63 KB
/
name_mangling.py
File metadata and controls
51 lines (32 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Copyright 2009-2017 Ram Rachum.
# This program is distributed under the MIT license.
'''Defines tools for name-mangling.'''
from python_toolbox import string_tools
MANGLE_LEN = 256
def mangle_attribute_name_if_needed(attribute_name, class_name):
# Ruling out four cases in which we do not mangle:
if ((not attribute_name.startswith('__')) or
(len(attribute_name) + 2 >= MANGLE_LEN) or
(attribute_name.endswith('__')) or
set(class_name) == {'_'}):
return attribute_name
cleaned_class_name = class_name.lstrip('_')
total_length = len(cleaned_class_name) + len(attribute_name)
if total_length > MANGLE_LEN:
cleaned_class_name = cleaned_class_name[:(MANGLE_LEN - total_length)]
return f'_{cleaned_class_name}{attribute_name}'
def will_attribute_name_be_mangled(attribute_name, class_name):
return mangle_attribute_name_if_needed(attribute_name, class_name) != \
attribute_name
def unmangle_attribute_name_if_needed(attribute_name, class_name):
# Ruling out four cases in which mangling wouldn't have happened:
if ((string_tools.get_n_identical_edge_characters(attribute_name,
'_') != 1) or
(len(attribute_name) >= MANGLE_LEN) or
(attribute_name.endswith('__')) or
set(class_name) == set('_')):
return attribute_name
cleaned_class_name = class_name.lstrip('_')
if not attribute_name[1:].startswith(cleaned_class_name + '__'):
return attribute_name
return attribute_name[(len(cleaned_class_name) + 1):]