1212
1313from mathics .builtin .box .compilation import CompiledCodeBox
1414from mathics .core .atoms import Integer , String
15- from mathics .core .attributes import A_HOLD_ALL , A_PROTECTED
15+ from mathics .core .attributes import (
16+ A_HOLD_ALL ,
17+ A_N_HOLD_ALL ,
18+ A_PROTECTED ,
19+ A_READ_PROTECTED ,
20+ )
1621from mathics .core .builtin import Builtin
1722from mathics .core .convert .expression import to_mathics_list
1823from mathics .core .convert .function import (
@@ -66,8 +71,10 @@ class Compile(Builtin):
6671 = 2.18888
6772
6873 Loops and variable assignments are supported using Python builtin "compile" function:
69- >> Compile[{{a, _Integer}, {b, _Integer}}, While[b != 0, {a, b} = {b, Mod[a, b]}]; a] (* GCD of a, b *)
70- = CompiledFunction[{a, b}, While[b != 0, {a, b} = {b, Mod[a, b]}] ; a, -PythonizedCode-]
74+ >> gdc = Compile[{{a, _Integer}, {b, _Integer}}, Module[{x=a, y=b}, While[y != 0, {x, y} = {y, Mod[x, y]}]; x]] (* GCD of a, b *)
75+ = CompiledFunction[{a, b}, Module[{x = a, y = b}, While[y != 0, {x, y} = {y, Mod[x, y]}] ; x], -PythonizedCode-]
76+ >> gdc[18, 81]
77+ = 9.
7178 """
7279
7380 attributes = A_HOLD_ALL | A_PROTECTED
@@ -167,7 +174,23 @@ def to_sympy(self, *args, **kwargs):
167174 raise NotImplementedError
168175
169176 def __hash__ (self ):
170- return hash (("CompiledCode" , ctypes .addressof (self .cfunc ))) # XXX hack
177+ cfunc = self .cfunc
178+ if cfunc is None :
179+ hash (
180+ (
181+ "CompiledCode" ,
182+ None ,
183+ )
184+ ) # XXX hack
185+ try :
186+ return hash (("CompiledCode" , ctypes .addressof (cfunc ))) # XXX hack
187+ except TypeError :
188+ return hash (
189+ (
190+ "Pythonized-function" ,
191+ cfunc ,
192+ )
193+ )
171194
172195 def atom_to_boxes (self , f , evaluation : Evaluation ):
173196 return CompiledCodeBox (String (self .__str__ ()), evaluation = evaluation )
@@ -191,6 +214,7 @@ class CompiledFunction(Builtin):
191214
192215 """
193216
217+ attributes = A_HOLD_ALL | A_PROTECTED | A_N_HOLD_ALL | A_READ_PROTECTED
194218 messages = {"argerr" : "Invalid argument `1` should be Integer, Real or boolean." }
195219 summary_text = "A CompiledFunction object."
196220
0 commit comments