Skip to content

Commit ed46c86

Browse files
authored
Merge pull request #687 from Mathics3/add-PythonImplementation
Add $PythonImplementation...
2 parents 4b35611 + 1b9e53a commit ed46c86

File tree

5 files changed

+59
-14
lines changed

5 files changed

+59
-14
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ New Builtins
1212
#. ``$BoxForms``
1313
#. ``$OutputForms``
1414
#. ``$PrintForms``
15+
#. ``$PythonImplementation``
1516
#. ``Accuracy``
1617
#. ``ClebschGordan``
1718
#. ``Curl`` (2-D and 3-D vector forms only)

SYMBOLS_MANIFEST.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ System`$PreRead
5656
System`$PrintForms
5757
System`$ProcessID
5858
System`$ProcessorType
59+
System`$PythonImplementation
5960
System`$RandomState
6061
System`$RecursionLimit
6162
System`$RootDirectory

mathics/builtin/system.py

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class Environment(Builtin):
7575

7676
summary_text = "list the system environment variables"
7777

78-
def apply(self, var, evaluation):
78+
def eval(self, var, evaluation):
7979
"Environment[var_String]"
8080
env_var = var.get_string_value()
8181
if env_var not in os.environ:
@@ -116,7 +116,7 @@ class GetEnvironment(Builtin):
116116

117117
summary_text = "retrieve the value of a system environment variable"
118118

119-
def apply(self, var, evaluation):
119+
def eval(self, var, evaluation):
120120
"GetEnvironment[var___]"
121121
if isinstance(var, String):
122122
env_var = var.get_string_value()
@@ -262,24 +262,49 @@ def evaluate(self, evaluation) -> Integer:
262262

263263
class ProcessorType(Predefined):
264264
r"""
265-
<url>:WMA link:https://reference.wolfram.com/language/ref/ProcessorType.html</url>
265+
<url>
266+
:WMA link:
267+
https://reference.wolfram.com/language/ref/ProcessorType.html</url>
266268
267269
<dl>
268-
<dt>'$ProcessorType'
269-
<dd>gives a string giving the architecture of the processor on which the \Mathics is being run.
270+
<dt>'$ProcessorType'
271+
<dd>gives a string giving the architecture of the processor on which the \Mathics is being run.
270272
</dl>
271-
X> $ProcessorType
272-
= x86_64
273+
274+
>> $ProcessorType
275+
= ...
273276
"""
277+
name = "$ProcessorType"
278+
274279
summary_text = (
275280
"name of the architecture of the processor over which Mathics is running"
276281
)
277-
name = "$ProcessorType"
278282

279283
def evaluate(self, evaluation):
280284
return String(platform.machine())
281285

282286

287+
class PythonImplementation(Predefined):
288+
r"""
289+
## <url>:PythonImplementation native symbol:</url>
290+
291+
<dl>
292+
<dt>'$PythonImplementation'
293+
<dd>gives a string indication the Python implementation used to run \Mathics.
294+
</dl>
295+
>> $PythonImplementation
296+
= ...
297+
"""
298+
name = "$PythonImplementation"
299+
300+
summary_text = "name of the Python implementation running Mathics"
301+
302+
def evaluate(self, evaluation):
303+
from mathics.system_info import python_implementation
304+
305+
return String(python_implementation())
306+
307+
283308
class ScriptCommandLine(Predefined):
284309
"""
285310
<url>:WMA link:https://reference.wolfram.com/language/ref/ScriptCommandLine.html</url>
@@ -320,7 +345,7 @@ class Run(Builtin):
320345

321346
summary_text = "run a system command"
322347

323-
def apply(self, command, evaluation):
348+
def eval(self, command, evaluation):
324349
"Run[command_String]"
325350
command_str = command.to_python()
326351
return Integer(subprocess.call(command_str, shell=True))
@@ -480,7 +505,7 @@ class MemoryAvailable(Builtin):
480505

481506
summary_text = "the available amount of physical memory in the system"
482507

483-
def apply(self, evaluation) -> Integer:
508+
def eval(self, evaluation) -> Integer:
484509
"""MemoryAvailable[]"""
485510
totalmem = psutil.virtual_memory().available
486511
return Integer(totalmem)
@@ -523,7 +548,7 @@ class MemoryAvailable(Builtin):
523548

524549
summary_text = "the available amount of physical memory in the system"
525550

526-
def apply(self, evaluation) -> Integer:
551+
def eval(self, evaluation) -> Integer:
527552
"""MemoryAvailable[]"""
528553
return Integer(-1)
529554

@@ -543,7 +568,7 @@ class MemoryInUse(Builtin):
543568

544569
summary_text = "number of bytes of memory currently being used by Mathics"
545570

546-
def apply_0(self, evaluation) -> Integer:
571+
def eval_0(self, evaluation) -> Integer:
547572
"""MemoryInUse[]"""
548573
# Partially borrowed from https://code.activestate.com/recipes/577504/
549574
from itertools import chain
@@ -596,7 +621,7 @@ class Share(Builtin):
596621

597622
summary_text = "force Python garbage collection"
598623

599-
def apply(self, evaluation) -> Integer:
624+
def eval(self, evaluation) -> Integer:
600625
"""Share[]"""
601626
# TODO: implement a routine that swap all the definitions,
602627
# collecting repeated symbols and expressions, and then
@@ -610,7 +635,7 @@ def apply(self, evaluation) -> Integer:
610635
gc.collect()
611636
return Integer0
612637

613-
def apply_with_symbol(self, symbol, evaluation) -> Integer:
638+
def eval_with_symbol(self, symbol, evaluation) -> Integer:
614639
"""Share[symbol_Symbol]"""
615640
# TODO: implement a routine that swap all the definitions,
616641
# collecting repeated symbols and expressions, and then

mathics/system_info.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@
1111
from mathics.core.evaluation import Evaluation
1212

1313

14+
def python_implementation() -> str:
15+
"""
16+
Returns the Python implemnetation, e.g Pyston, PyPy, CPython...
17+
"""
18+
if hasattr(sys, "pyston_version_info"):
19+
custom_version_info = sys.pyston_version_info
20+
python_implementation = "Pyston"
21+
elif hasattr(sys, "pypy_version_info"):
22+
custom_version_info = sys.pypy_version_info
23+
python_implementation = "PyPy"
24+
else:
25+
custom_version_info = sys.version_info
26+
python_implementation = platform.python_implementation()
27+
return f"{python_implementation} {'.'.join((str(i) for i in custom_version_info))}"
28+
29+
1430
def mathics_system_info(defs):
1531
def eval(name, needs_head=True):
1632
evaled = name().evaluate(evaluation)
@@ -28,6 +44,7 @@ def eval(name, needs_head=True):
2844
"$MachineName": platform.uname().node,
2945
"$ProcessID": os.getppid(),
3046
"$ProcessorType": platform.machine(),
47+
"$PythonImplementation": python_implementation(),
3148
"$RootDirectory": eval(filesystem.RootDirectory),
3249
"$SystemID": sys.platform,
3350
"$SystemMemory": eval(msystem.SystemMemory),

test/test_system_info.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def test_system_info():
2020
"$MachineName",
2121
"$ProcessID",
2222
"$ProcessorType",
23+
"$PythonImplementation",
2324
"$RootDirectory",
2425
"$SystemID",
2526
"$SystemMemory",

0 commit comments

Comments
 (0)