Skip to content

Commit b882cc9

Browse files
committed
Remove "Protect" Attribute hackiness on Builtin
There are a number of built-ins that don't want to be Protected by default, and we currently have no way to indicate that. Currently "MakeBoxes" is hacked in to change this and "assignment" has another hack to change this as well. And they do this using the `List#clear()` function which removes *all* attributes, not just Protect. In this change, setting the `attribute = ("Unprotect", ...)` indicates to Builtin we don't want to add the "Protect" attribute. Note however this isn't a true attribute like "Protect" is - it only means something in the declaration of a Builtin, and doesn't exist at run time. The need for this change becomes all the more important as we move things into PyMathics because a growing number of these for variable-like definition will be desired. Another Note: in the future we may want to add a class specificaly for variable-like definition.
1 parent 6f7f836 commit b882cc9

File tree

6 files changed

+29
-21
lines changed

6 files changed

+29
-21
lines changed

mathics/builtin/__init__.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,6 @@ def contribute(definitions):
114114
if name != 'System`MakeBoxes':
115115
item.contribute(definitions)
116116

117-
# Is there another way to Unprotect these symbols at initialization?
118-
definitions.get_attributes('System`$PreRead').clear()
119-
definitions.get_attributes('System`$Pre').clear()
120-
definitions.get_attributes('System`$Post').clear()
121-
definitions.get_attributes('System`$PrePrint').clear()
122-
definitions.get_attributes('System`$SyntaxHandler').clear()
123-
definitions.get_attributes('System`$TimeZone').clear()
124-
definitions.get_attributes('System`$UseSansSerif').clear()
125117
from mathics.core.expression import ensure_context
126118
from mathics.core.parser import all_operator_names
127119
from mathics.core.definitions import Definition

mathics/builtin/assignment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1712,7 +1712,7 @@ def apply(self, module, evaluation):
17121712
return Symbol('$Failed')
17131713
else:
17141714
# Add PyMathics` to $ContextPath so that when user don't
1715-
# have to qualify PyMathics variables and functions,
1715+
# have to qualify PyMathics variables and functions,
17161716
# as the those in teh module just loaded.
17171717

17181718
# Following the example of $ContextPath in the WL

mathics/builtin/base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,13 @@ def contextify_form_name(f):
225225
)
226226
)
227227

228-
if name == "System`MakeBoxes":
228+
if "Unprotected" in self.attributes:
229229
attributes = []
230+
self.attributes = list(self.attributes)
231+
self.attributes.remove("Unprotected")
230232
else:
231233
attributes = ["System`Protected"]
234+
232235
attributes += list(ensure_context(a) for a in self.attributes)
233236
options = {}
234237
for option, value in self.options.items():

mathics/builtin/datentime.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ class TimeZone(Predefined):
571571

572572
name = "$TimeZone"
573573
value = SystemTimeZone.value.copy()
574+
attributes = ("Unprotected",)
574575

575576
rules = {
576577
"$TimeZone": str(value),

mathics/builtin/inout.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class UseSansSerif(Predefined):
4646
"""
4747
context = "System`"
4848
name = '$UseSansSerif'
49+
attributes = ("Unprotected",)
4950
value = True
5051

5152
rules = {
@@ -462,7 +463,7 @@ class MakeBoxes(Builtin):
462463
= RowBox[{a, ,, b}]
463464
"""
464465

465-
attributes = ('HoldAllComplete',)
466+
attributes = ('HoldAllComplete', "Unprotected")
466467

467468
rules = {
468469
'MakeBoxes[Infix[head_[leaves___]], '

mathics/builtin/iohooks.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,21 @@
66

77

88
class IOHookPreRead(Builtin):
9-
'''
9+
"""
1010
<dl>
1111
<dt>$PreRead
1212
<dt> is a global variable whose value, if set, is applied to the \
1313
text or box form of every input expression before it is fed to the parser.
1414
<dt>(Not implemented yet)
1515
</dl>
16-
'''
16+
"""
17+
1718
name = "$PreRead"
19+
attributes = ("Unprotected",)
20+
1821

1922
class IOHookPre(Builtin):
20-
'''
23+
"""
2124
<dl>
2225
<dt>$Pre
2326
<dt>is a global variable whose value, if set,
@@ -42,39 +45,47 @@ class IOHookPre(Builtin):
4245
| [Processing input...]
4346
>> 2 + 2
4447
= 4
45-
'''
48+
"""
49+
4650
name = "$Pre"
51+
attributes = ("Unprotected",)
4752

4853

4954
class IOHookPost(Builtin):
50-
'''
55+
"""
5156
<dl>
5257
<dt>$Post
5358
<dt>is a global variable whose value, if set,
5459
is applied to every output expression.
5560
</dl>
56-
'''
61+
"""
62+
5763
name = "$Post"
64+
attributes = ("Unprotected",)
5865

5966

6067
class IOHookPrePrint(Builtin):
61-
'''
68+
"""
6269
<dl>
6370
<dt>$PrePrint
6471
<dt>is a global variable whose value, if set,
6572
is applied to every output expression before it is printed.
6673
</dl>
67-
'''
74+
"""
75+
6876
name = "$PrePrint"
77+
attributes = ("Unprotected",)
6978

7079

7180
class IOHookSyntaxHandler(Builtin):
72-
'''
81+
"""
7382
<dl>
7483
<dt>$SyntaxHandler
7584
<dt>is a global variable whose value, if set,
7685
is applied to any input string that is found to contain a syntax error.
7786
<dt>(Not implemented yet)
7887
</dl>
79-
'''
88+
"""
89+
8090
name = "$SyntaxHandler"
91+
attributes = ("Unprotected",)

0 commit comments

Comments
 (0)