Skip to content

Commit

Permalink
Merge pull request #1015 from mathics/fixneeds
Browse files Browse the repository at this point in the history
Fixneeds
  • Loading branch information
rocky authored Dec 24, 2020
2 parents 21d25fe + 3f8d244 commit 2b545d7
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 21 deletions.
4 changes: 2 additions & 2 deletions mathics/builtin/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,15 @@ def apply(self, symbols, evaluation):
protected = Symbol("System`Protected")
items = []

if isinstance(symbols ,Symbol):
if isinstance(symbols, Symbol):
symbols = [symbols]
elif isinstance(symbols, String):
symbols = [symbols]
elif isinstance(symbols, Expression):
if symbols.get_head_name() in ("System`Sequence", "System`List"):
symbols = symbols.get_leaves()
else:
evaluation.message('Protect', 'ssym', symbol)
evaluation.message('Protect', 'ssym', symbols)
return Symbol("Null")

for symbol in symbols:
Expand Down
2 changes: 1 addition & 1 deletion mathics/builtin/exptrig.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
r"""
Exponential, Trigonometric and Hyperbolic Functions
\Mathics basically supports all important trigonometric and hyperbolic functions.
Expand Down
29 changes: 22 additions & 7 deletions mathics/builtin/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -3692,7 +3692,7 @@ def apply_name(self, name, evaluation):


class Compress(Builtin):
"""
u"""
<dl>
<dt>'Compress[$expr$]'
<dd>gives a compressed string representation of $expr$.
Expand Down Expand Up @@ -4800,7 +4800,6 @@ def apply(self, pathname, evaluation):
return SymbolTrue
return SymbolFalse


class Needs(Builtin):
"""
<dl>
Expand Down Expand Up @@ -4912,18 +4911,34 @@ class Needs(Builtin):
}

def apply(self, context, evaluation):
"Needs[context_String]"

if not valid_context_name(context.get_string_value()):
evaluation.message("Needs", "ctx", Expression("Needs", context), 1, "`")
'Needs[context_String]'
contextstr = context.get_string_value()
if contextstr == "":
return SymbolNull
if contextstr[0]=="`":
curr_ctxt = evaluation.definitions.get_current_context()
contextstr = curr_ctxt + contextstr[1:]
context = String(contextstr)

if not valid_context_name(contextstr):
evaluation.message('Needs', 'ctx', Expression(
'Needs', context), 1, '`')
return
test_loaded = Expression("MemberQ", Symbol("$Packages"), context)
test_loaded = test_loaded.evaluate(evaluation)
if test_loaded.is_true():
# Already loaded
return SymbolNull

result = Expression("Get", context).evaluate(evaluation)
# TODO: Look why this rises the message
# "Select::normal: Nonatomic expression expected."
already_loaded = Expression('MemberQ',
Symbol('System`$Packages'), context)
already_loaded = already_loaded.evaluate(evaluation).is_true()
if already_loaded:
return SymbolNull

result = Expression('Get', context).evaluate(evaluation)

if result == Symbol("$Failed"):
evaluation.message("Needs", "nocont", context)
Expand Down
3 changes: 1 addition & 2 deletions mathics/builtin/scoping.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class With(Builtin):
Use 'With' to insert values into held expressions
>> With[{x=y}, Hold[x]]
= Hold[y]
>> Table[With[{i=j}, Hold[i]],{j,1,4}]
= {Hold[1], Hold[2], Hold[3], Hold[4]}
>> x=5; With[{x=x}, Hold[x]]
Expand Down Expand Up @@ -465,7 +465,6 @@ class Contexts(Builtin):
## this assignment makes sure that a definition in Global` exists
>> x = 5;
X> Contexts[] // InputForm
"""

def apply(self, evaluation):
Expand Down
11 changes: 3 additions & 8 deletions mathics/builtin/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,18 +230,13 @@ class Packages(Predefined):
</dl>
X> $Packages
= {CombinatoricaOld,ImportExport,Internal,System,XML}
#> MemberQ[$Packages, "System"]
= {ImportExport`,XML`,Internal`,System`,Global`}
#> MemberQ[$Packages, "System`"]
= True
"""

name = "$Packages"

def evaluate(self, evaluation):
return Expression(
"List",
*(String(name) for name in evaluation.definitions.get_package_names()),
)
rules = {'$Packages': '{"ImportExport`", "XML`","Internal`", "System`", "Global`"}',}


class ParentProcessID(Predefined):
Expand Down
8 changes: 7 additions & 1 deletion mathics/core/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,13 @@ def lookup_name(self, name) -> str:
return with_context

def get_package_names(self) -> typing.List[str]:
return sorted({name.split("`")[0] for name in self.get_names()})
packages = self.get_ownvalue("System`$Packages")
packages = packages.replace
assert packages.has_form("System`List", None)
packages = [c.get_string_value() for c in packages.leaves]
return packages

#return sorted({name.split("`")[0] for name in self.get_names()})

def shorten_name(self, name_with_ctx) -> str:
if "`" not in name_with_ctx:
Expand Down

0 comments on commit 2b545d7

Please sign in to comment.