|
5 | 5 | import mathics.builtin |
6 | 6 | from mathics.builtin.base import ( |
7 | 7 | Builtin, BinaryOperator, PostfixOperator, PrefixOperator) |
8 | | -from mathics.core.expression import (Expression, Symbol, SymbolFailed, valid_context_name, |
| 8 | +from mathics.core.expression import (Expression, Symbol, SymbolFailed, SymbolNull, valid_context_name, |
9 | 9 | system_symbols, String) |
10 | 10 | from mathics.core.rules import Rule, BuiltinRule |
11 | 11 | from mathics.builtin.patterns import RuleDelayed |
@@ -833,23 +833,34 @@ def _get_usage_string(symbol, evaluation, htmlout=False): |
833 | 833 | definition = evaluation.definitions.get_definition(symbol.name) |
834 | 834 | ruleusage = definition.get_values_list('messages') |
835 | 835 | usagetext = None |
836 | | - from mathics.builtin import builtins |
837 | 836 | import re |
838 | | - bio = builtins.get(definition.name) |
839 | | - |
| 837 | + # First look at user definitions: |
| 838 | + for rulemsg in ruleusage: |
| 839 | + if rulemsg.pattern.expr.leaves[1].__str__() == "\"usage\"": |
| 840 | + usagetext = rulemsg.replace.value |
| 841 | + if usagetext is not None: |
| 842 | + # Maybe, if htmltout is True, we should convert |
| 843 | + # the value to a HTML form... |
| 844 | + return usagetext |
| 845 | + # Otherwise, look at the pymathics, and builtin docstrings: |
| 846 | + builtins = evaluation.definitions.builtin |
| 847 | + pymathics = evaluation.definitions.pymathics |
| 848 | + bio = pymathics.get(definition.name) |
| 849 | + if bio is None: |
| 850 | + bio = builtins.get(definition.name) |
| 851 | + |
840 | 852 | if bio is not None: |
841 | 853 | from mathics.doc.doc import Doc |
| 854 | + docstr = bio.builtin.__class__.__doc__ |
| 855 | + if docstr is None: |
| 856 | + return None |
842 | 857 | if htmlout: |
843 | | - usagetext = Doc(bio.__class__.__doc__).text(0) |
| 858 | + usagetext = Doc(docstr).html() |
844 | 859 | else: |
845 | | - usagetext = Doc(bio.__class__.__doc__).text(0) |
| 860 | + usagetext = Doc(docstr).text(0) |
846 | 861 | usagetext = re.sub(r'\$([0-9a-zA-Z]*)\$', r'\1', usagetext) |
847 | | - # For built-in symbols, looks for a docstring. |
848 | | - # Looks for the "usage" message. For built-in symbols, if there is an "usage" chain, overwrite the __doc__ information. |
849 | | - for rulemsg in ruleusage: |
850 | | - if rulemsg.pattern.expr.leaves[1].__str__() == "\"usage\"": |
851 | | - usagetext = rulemsg.replace.value |
852 | | - return usagetext |
| 862 | + return usagetext |
| 863 | + return None |
853 | 864 |
|
854 | 865 |
|
855 | 866 | class Information(PrefixOperator): |
@@ -919,7 +930,7 @@ class Information(PrefixOperator): |
919 | 930 |
|
920 | 931 | def format_definition(self, symbol, evaluation, options, grid=True): |
921 | 932 | 'StandardForm,TraditionalForm,OutputForm: Information[symbol_, OptionsPattern[Information]]' |
922 | | - ret = Symbol('Null') |
| 933 | + ret = SymbolNull |
923 | 934 | lines = [] |
924 | 935 | if isinstance(symbol, String): |
925 | 936 | evaluation.print_out(symbol) |
@@ -1014,7 +1025,7 @@ def rhs(expr): |
1014 | 1025 | def format_definition_input(self, symbol, evaluation, options): |
1015 | 1026 | 'InputForm: Information[symbol_, OptionsPattern[Information]]' |
1016 | 1027 | self.format_definition(symbol, evaluation, options, grid=False) |
1017 | | - ret = Symbol("Null") |
| 1028 | + ret = SymbolNull |
1018 | 1029 | return ret |
1019 | 1030 |
|
1020 | 1031 |
|
@@ -1757,17 +1768,19 @@ def apply(self, module, evaluation): |
1757 | 1768 | except ImportError as e: |
1758 | 1769 | evaluation.message(self.get_name(), 'notfound', module) |
1759 | 1770 | return SymbolFailed |
1760 | | - |
| 1771 | + except PyMathicsLoadException as e: |
| 1772 | + evaluation.message(self.get_name(), 'notmathicslib', module) |
| 1773 | + return SymbolFailed |
| 1774 | + else: |
1761 | 1775 | # Add Pymathics` to $ContextPath so that when user don't |
1762 | 1776 | # have to qualify Pymathics variables and functions, |
1763 | 1777 | # as the those in the module just loaded. |
1764 | 1778 | # Following the example of $ContextPath in the WL |
1765 | 1779 | # reference manual where PackletManager appears first in |
1766 | 1780 | # the list, it seems to be preferable to add this PyMathics |
1767 | 1781 | # at the beginning. |
1768 | | - context_path = evaluation.definitions.get_context_path() |
1769 | | - if "Pymathics`" not in context_path: |
1770 | | - context_path.insert(0, "Pymathics`") |
1771 | | - evaluation.definitions.set_context_path(context_path) |
1772 | | - |
| 1782 | + context_path = evaluation.definitions.get_context_path() |
| 1783 | + if "Pymathics`" not in context_path: |
| 1784 | + context_path.insert(0, "Pymathics`") |
| 1785 | + evaluation.definitions.set_context_path(context_path) |
1773 | 1786 | return module |
0 commit comments