File tree Expand file tree Collapse file tree 4 files changed +44
-32
lines changed Expand file tree Collapse file tree 4 files changed +44
-32
lines changed Original file line number Diff line number Diff line change 28
28
import basilisp .reader as reader
29
29
from basilisp .lang .runtime import Var
30
30
from basilisp .lang .typing import LispForm
31
- from basilisp .lang .util import genname
32
- from basilisp .util import Maybe , munge
31
+ from basilisp .lang .util import genname , munge
32
+ from basilisp .util import Maybe
33
33
34
34
_CORE_NS = 'basilisp.core'
35
35
_DEFAULT_FN = '__lisp_expr__'
Original file line number Diff line number Diff line change
1
+ import builtins
1
2
import datetime
3
+ import keyword
2
4
import re
3
5
import uuid
4
6
from typing import Pattern
@@ -30,6 +32,39 @@ def lrepr(f) -> str:
30
32
return repr (f )
31
33
32
34
35
+ _MUNGE_REPLACEMENTS = {
36
+ '+' : '__PLUS__' ,
37
+ '-' : '_' ,
38
+ '*' : '__STAR__' ,
39
+ '/' : '__DIV__' ,
40
+ '>' : '__GT__' ,
41
+ '<' : '__LT__' ,
42
+ '!' : '__BANG__' ,
43
+ '=' : '__EQ__' ,
44
+ '?' : '__Q__' ,
45
+ '\\ ' : '__IDIV__' ,
46
+ '&' : '__AMP__'
47
+ }
48
+
49
+
50
+ def munge (s : str , allow_builtins : bool = True ) -> str :
51
+ """Replace characters which are not valid in Python symbols
52
+ with valid replacement strings."""
53
+ new_str = []
54
+ for c in s :
55
+ new_str .append (_MUNGE_REPLACEMENTS .get (c , c ))
56
+
57
+ new_s = '' .join (new_str )
58
+
59
+ if keyword .iskeyword (new_s ):
60
+ return f"{ new_s } _"
61
+
62
+ if not allow_builtins and new_s in builtins .__dict__ :
63
+ return f"{ new_s } _"
64
+
65
+ return new_s
66
+
67
+
33
68
# Use an atomically incremented integer as a suffix for all
34
69
# user-defined function and variable names compiled into Python
35
70
# code so no conflicts occur
Original file line number Diff line number Diff line change 1
1
import functools
2
2
import inspect
3
- import keyword
4
3
import os .path
5
4
from typing import Optional , Callable , TypeVar , Generic
6
5
9
8
10
9
from basilisp .lang .util import lrepr
11
10
12
- _MUNGE_REPLACEMENTS = {
13
- '+' : '__PLUS__' ,
14
- '-' : '_' ,
15
- '*' : '__STAR__' ,
16
- '/' : '__DIV__' ,
17
- '>' : '__GT__' ,
18
- '<' : '__LT__' ,
19
- '!' : '__BANG__' ,
20
- '=' : '__EQ__' ,
21
- '?' : '__Q__' ,
22
- '\\ ' : '__IDIV__' ,
23
- '&' : '__AMP__'
24
- }
25
-
26
11
27
12
def trace (f ):
28
13
@functools .wraps (f )
@@ -102,17 +87,3 @@ def stream(self) -> Sequence:
102
87
@property
103
88
def is_present (self ) -> bool :
104
89
return self ._inner is not None
105
-
106
-
107
- def munge (s : str ) -> str :
108
- """Replace characters which are not valid in Python symbols
109
- with valid replacement strings."""
110
- new_str = []
111
- for c in s :
112
- new_str .append (_MUNGE_REPLACEMENTS .get (c , c ))
113
-
114
- new_s = '' .join (new_str )
115
-
116
- if keyword .iskeyword (new_s ):
117
- return f"{ new_s } _"
118
- return new_s
Original file line number Diff line number Diff line change
1
+ import builtins
1
2
import keyword
2
3
3
- from basilisp .util import munge
4
+ from basilisp .lang . util import munge
4
5
5
6
6
7
def test_munge_disallows_syms ():
@@ -17,6 +18,11 @@ def test_munge_disallows_syms():
17
18
assert "__AMP__form" == munge ("&form" )
18
19
19
20
21
+ def test_munge_disallows_python_builtins ():
22
+ for name in builtins .__dict__ .keys ():
23
+ assert f"{ name } _" == munge (name , allow_builtins = False )
24
+
25
+
20
26
def test_munge_disallows_python_kws ():
21
27
for kw in keyword .kwlist :
22
28
assert f"{ kw } _" == munge (kw )
You can’t perform that action at this time.
0 commit comments