Mathics3 / mathics-core

An open-source Mathematica. This repository contains the Python modules for WL Built-in functions, variables, core primitives, e.g. Symbol, a parser to create Expressions, and an evaluator to execute them.
https://mathics.org
Other
786 stars 48 forks source link

`WriteString[]` does not handle String filenames. #1195

Open rocky opened 1 day ago

rocky commented 1 day ago

Description

WriteString[] does not find a previously-created Stream by filename.

More generally, finding streams by name in Mathics3 is flaky to put it generously.

How to Reproduce

mathics -e 'OpenWrite["/tmp/foo"]; WriteString["/tmp/foo", "bar\n"]'

Output Given

Traceback (most recent call last):
  File "/tmp/bin/mathics", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/tmp/Mathics3/mathics-core/mathics/main.py", line 532, in main
    result, evaluation = run_it()
                         ^^^^^^^^
  File "/tmp/Mathics3/mathics-core/mathics/main.py", line 524, in run_it
    return evaluation.parse_evaluate(expr, timeout=settings.TIMEOUT), evaluation
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/Mathics3/mathics-core/mathics/core/evaluation.py", line 142, in parse_evaluate
    return self.evaluate(expr, timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/Mathics3/mathics-core/mathics/core/evaluation.py", line 241, in evaluate
    result = evaluate()
             ^^^^^^^^^^
  File "/tmp/Mathics3/mathics-core/mathics/core/evaluation.py", line 214, in evaluate
    self.last_eval = query.evaluate(self)
                     ^^^^^^^^^^^^^^^^^^^^
  File "/tmp/Mathics3/mathics-core/mathics/eval/tracing.py", line 62, in wrapper
    result = func(expr, evaluation)
             ^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/Mathics3/mathics-core/mathics/core/expression.py", line 573, in evaluate
    expr, reevaluate = expr.rewrite_apply_eval_step(evaluation)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/Mathics3/mathics-core/mathics/core/expression.py", line 1408, in rewrite_apply_eval_step
    result = rule.apply(new, evaluation, fully=False)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/Mathics3/mathics-core/mathics/core/rules.py", line 165, in apply
    self.pattern.match(
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 464, in match
    basic_match_expression(self, expression, parms)
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 889, in basic_match_expression
    self.head.match(
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 350, in match_symbol
    pattern_context["yield_func"](pattern_context["vars_dict"], None)
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 874, in yield_head
    self.get_pre_choices(
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 1061, in get_pre_choices_with_order
    pattern_context["yield_choice"](pattern_context["vars_dict"])
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 850, in yield_choice
    self.match_element(
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 620, in match_element
    expression_pattern_match_element_process_items(items, items_rest, parms)
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 1043, in expression_pattern_match_element_process_items
    pattern.get_wrappings(
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 525, in get_wrappings
    yield_func(sequence)
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 1023, in yield_wrapping
    parms["element"].match(
  File "/tmp/Mathics3/mathics-core/mathics/builtin/patterns/composite.py", line 421, in match
    self.pattern.match(
  File "/tmp/Mathics3/mathics-core/mathics/builtin/patterns/basic.py", line 141, in match
    yield_func(vars_dict, None)
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 1020, in match_yield
    yield_func(new_vars, items_rest)
  File "/tmp/Mathics3/mathics-core/mathics/core/rules.py", line 137, in yield_match
    new_expression = apply_fn(expression, vars, options, evaluation)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/Mathics3/mathics-core/mathics/core/rules.py", line 354, in apply_function
    return self.function(evaluation=evaluation, **vars_noctx)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/Mathics3/mathics-core/mathics/builtin/procedural.py", line 217, in eval
    result = expr.evaluate(evaluation)
             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/Mathics3/mathics-core/mathics/eval/tracing.py", line 62, in wrapper
    result = func(expr, evaluation)
             ^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/Mathics3/mathics-core/mathics/core/expression.py", line 573, in evaluate
    expr, reevaluate = expr.rewrite_apply_eval_step(evaluation)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/Mathics3/mathics-core/mathics/core/expression.py", line 1408, in rewrite_apply_eval_step
    result = rule.apply(new, evaluation, fully=False)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/Mathics3/mathics-core/mathics/core/rules.py", line 165, in apply
    self.pattern.match(
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 464, in match
    basic_match_expression(self, expression, parms)
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 889, in basic_match_expression
    self.head.match(
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 350, in match_symbol
    pattern_context["yield_func"](pattern_context["vars_dict"], None)
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 874, in yield_head
    self.get_pre_choices(
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 1061, in get_pre_choices_with_order
    pattern_context["yield_choice"](pattern_context["vars_dict"])
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 850, in yield_choice
    self.match_element(
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 620, in match_element
    expression_pattern_match_element_process_items(items, items_rest, parms)
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 1043, in expression_pattern_match_element_process_items
    pattern.get_wrappings(
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 510, in get_wrappings
    yield_func(items[0])
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 1023, in yield_wrapping
    parms["element"].match(
  File "/tmp/Mathics3/mathics-core/mathics/builtin/patterns/composite.py", line 421, in match
    self.pattern.match(
  File "/tmp/Mathics3/mathics-core/mathics/builtin/patterns/basic.py", line 101, in match
    yield_func(vars_dict, None)
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 1015, in match_yield
    pattern.match_element(
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 620, in match_element
    expression_pattern_match_element_process_items(items, items_rest, parms)
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 1043, in expression_pattern_match_element_process_items
    pattern.get_wrappings(
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 510, in get_wrappings
    yield_func(items[0])
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 1023, in yield_wrapping
    parms["element"].match(
  File "/tmp/Mathics3/mathics-core/mathics/builtin/patterns/composite.py", line 421, in match
    self.pattern.match(
  File "/tmp/Mathics3/mathics-core/mathics/builtin/patterns/basic.py", line 141, in match
    yield_func(vars_dict, None)
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 1020, in match_yield
    yield_func(new_vars, items_rest)
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 998, in element_yield
    yield_func(
  File "/tmp/Mathics3/mathics-core/mathics/core/rules.py", line 137, in yield_match
    new_expression = apply_fn(expression, vars, options, evaluation)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/Mathics3/mathics-core/mathics/core/rules.py", line 354, in apply_function
    return self.function(evaluation=evaluation, **vars_noctx)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/Mathics3/mathics-core/mathics/builtin/files_io/files.py", line 1636, in eval
    if stream is None or stream.io is None or stream.io.closed:
       ^^^^^^
UnboundLocalError: cannot access local variable 'stream' where it is not associated with a value

Expected behavior

Data written to file.

Additional context

But seen trying to run Mathics3 package analytica.