16
$\begingroup$

I am trying to understand the following behaviour, and determine if it's a bug in Mathematica 10. I am using Mathematica 10.3, and can't reproduce this behaviour under Mathematica 9. The test outlined below shows that evaluating Clear["Global`*"] under one kernel clears the Global context in another kernel, which I don't understand - I thought each kernel had it's own Global context.

To demonstrate, I have created a simple notebook called clear.nb containing:

Clear["Global`*"]

and nothing else. I have also created two new kernels called Test1 and Test2.

Now in another notebook I have the following:

f = 99.0

nb = NotebookOpen["/path/to/clear.nb"]

SetOptions[nb, Evaluator -> "Test1"]

NotebookEvaluate[nb]

Evaluating this notebook under kernel Test2, I find that immediately after the NotebookEvaluate[nb] both nb and f are no longer defined. In other words the Clear["Global`*"] which has been evaluated by kernel Test1 has cleared the Global context of kernel Test2.

This is not what I expected at all, and now how it worked in Mathematica 9, where the Global context of kernel Test2 is unaffected.

Is this a bug in Mathematica 10, or a bug in my understanding/expectation?

$\endgroup$
  • 2
    $\begingroup$ This is a great question and I am sorry to see that it has been widely ignored up until now! $\endgroup$ – Mr.Wizard Jul 17 '16 at 9:13
8
$\begingroup$

If you look at the definition of NotebookEvaluate, by means of

Needs["GeneralUtilities`"];
GeneralUtilities`PrintDefinitions[NotebookEvaluate]

you see that the design is strange: always $ParentLink is set to First@$FrontEnd, and the designer obviously never thought about your use case.

However, it is easy to achieve what you want, just define e.g.

nbEval[nb_NotebookObject] := (SelectionMove[nb, All, Notebook]; 
   SelectionEvaluate[nb]);

and now use nbEval instead of NotebookEvaluate and all is good.

$\endgroup$
  • $\begingroup$ Can you contrast the code from version 9? It might be telling if there is a clear design change regarding the evaluation. $\endgroup$ – Mr.Wizard Jul 18 '16 at 4:44
  • $\begingroup$ Same situation with version 9. At least on Windows. Did not check other operating systems. $\endgroup$ – Rolf Mertig Jul 18 '16 at 7:54
  • $\begingroup$ Thank you. So at least on Windows this statement is incorrect?: "This is not what I expected at all, and now how it worked in Mathematica 9, where the Global context of kernel Test2 is unaffected." $\endgroup$ – Mr.Wizard Jul 18 '16 at 7:55
  • $\begingroup$ @Mr.Wizard With version 8.0.4 I get the same behavior as with version 10.4.1 on Win7 x64 (both with InsertResults -> True and without it): the "clear.nb" Notebook is evaluated using the Kernel "Test2" and its "Global`" context is cleared. So the Evaluator option of "clear.nb" is ignored. $\endgroup$ – Alexey Popkov Jul 18 '16 at 9:02
  • $\begingroup$ I should have mentioned I was working on Linux, rather than Windows in case that matters (I doubt it does). $\endgroup$ – JonathanU Dec 5 '17 at 23:17
6
+50
$\begingroup$

In addition to Rolf's workaround.

This is expected and works the same in V9 and V10.

NotebookEvaluate / Details & Options:

By default, NotebookEvaluate evaluates the cells of a notebook in the same way that Get evaluates the lines of a package file. Messages, print output, and other side effects will be placed as output to the cell that called NotebookEvaluate rather than the specified notebook. Existing output cells in the notebook will not be updated or deleted.

So you are not running the clear.nb, NotebookEvaluate reads it and evaluates it itself in the parent kernel.


Though one may get confused because the next bullet point says:

NotebookEvaluate[notebook,InsertResults->True] evaluates the notebook as if all cells had been evaluated with Shift+Enter. Messages, print output, and other side effects are placed in the notebook along with outputs.

and this is really misleading, outputs are inserted but the first sentence should be changed.

WRI Support agrees

CASE:3673620

[...] I have filed a suggestion with the appropriate development team requesting we review the documentation on NotebookEvaluate. As particularly outlined in the StackExchange thread, there is room for confusion here with more advanced uses. [...]

$\endgroup$
  • $\begingroup$ "and this is really misleading, outputs are inserted but the first sentence should be changed." -- I read that too, and I wondered if it actually was supposed to work that way but someone made a mistake? As usual when the documentation and implementation disagree we are left guessing as to which is "correct." $\endgroup$ – Mr.Wizard Jul 18 '16 at 8:15
  • 7
    $\begingroup$ @Mr.Wizard Documentation is usually correct from a perspective of a beginner who doesn't care/know context/kernels/links etc. When you try to develop something complex then those reckless sentences have major implications. Sometimes it is a matter of few tests and sometimes you waste two days on debugging and discussions here. $\endgroup$ – Kuba Jul 18 '16 at 8:19
  • $\begingroup$ I would have thought that if you kept reading, it would have been clarified by a later bullet point..."The cells of the notebook are evaluated in a dialog subsession." But I'll tweak it. $\endgroup$ – John Fultz Oct 24 '16 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.