Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4015948
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:40:51+00:00 2026-05-20T09:40:51+00:00

When working through MathLink with slave kernel I have a problem with correct parsing

  • 0

When working through MathLink with slave kernel I have a problem with correct parsing TextPackets. In particular when such packet corresponds to a Message generated by the slave kernel I do not understand how to handle it correctly at all. I need such Messages to be printed in the evaluation notebook as if they were generated by master kernel (but with some mark to make clear that it comes from the slave). And I need to separate TextPackets corresponding to Messages from just to Print[] commands. The latter I need to parse correctly too, printing them in the evaluation notebook with a little mark that it is from the slave kernel.

Here is an example of what happens:

link = LinkLaunch[First[$CommandLine] <> " -mathlink"]
Print@LinkRead[link]
LinkWrite[link, 
 Unevaluated[EnterExpressionPacket[Print[a]; 1/0; Print[b]]]]
While[Not@MatchQ[packet = LinkRead[link], InputNamePacket[_]], 
 Print[packet]]

The Message by default comes through MathLink in the form:

TextPacket[                                 1
Power::infy: Infinite expression - encountered.
                                 0]

It looks ugly. The only way to make it better I have found is to evaluate in the slave kernel

$MessagePrePrint = InputForm;

But I think there should be more straightforward solution. In particular when dealing this way I get TextPackets with HoldForms inside:

TextPacket[Power::infy: Infinite expression HoldForm[0^(-1)] encountered.]

I do not know how to convert such string into a form appropriate for printing as a Message.

P.S. This question comes from that question.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:40:51+00:00Added an answer on May 20, 2026 at 9:40 am

    I would like to share a nice hack proposed by Todd Gayley (Wolfram Research) in connection with the given question. Perhaps for somebody it will be useful as also for me. This hack solves the problem in question in rather elegant way.

    One technique is to leave the
    FormatType at OutputForm for
    computations, but override the
    handling of Message to temporarily
    switch to StandardForm, so that only
    Message output comes back in
    StandardForm:

    LinkWrite[link,
            Unevaluated[EnterExpressionPacket[
                Unprotect[Message];
                Message[args___]:=
                   Block[{$inMsg = True, result},
                      SetOptions[$Output, FormatType->StandardForm];
                      result = Message[args];
                      SetOptions[$Output, FormatType->OutputForm];
                      result
                   ] /; !TrueQ[$inMsg]
               ]
            ]]
    

    You will get back an ExpressionPacket for the content of a
    message. To print that as a Message cell in the
    notebook:

    cell = Cell[<the ExpressionPacket>, "Message", "MSG"]
    CellPrint[cell]
    

    Advanced approach: everything is printed in the StandardForm

    For having everything except output returned in StandardForm we could redefine variables $Pre and $Post in the slave kernel in a special way (the following code should be evaluated in the slave kernel):

    SetOptions[$Output, {PageWidth -> 72, FormatType -> StandardForm}];
    (*$inPost is needed for tracing mode compatibility 
    (could be switched on by evaluating On[] in the slave kernel) 
    in which Messages are printed during evaluation of $Post.*)
    $inPost = False; Protect[$inPost];
    $Pre := Function[inputexpr, 
      SetOptions[$Output, FormatType -> StandardForm]; 
      Unevaluated[inputexpr], HoldAllComplete];
    $Post := Function[outputexpr, 
      Block[{$inPost = True}, 
       SetOptions[$Output, FormatType -> OutputForm]; 
       Unevaluated[outputexpr]], HoldAllComplete];
    Protect[$Pre]; Protect[$Post];
    $inMsg = False; Protect[$inMsg];
    Unprotect[Message];
    Message[args___] /; $inPost := Block[{$inMsg = True},
        SetOptions[$Output, FormatType -> StandardForm];
        Message[args];
        SetOptions[$Output, FormatType -> OutputForm]] /; ! $inMsg;
    Protect[Message];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working through Practical Web 2.0 Appications currently and have hit a bit of
Working through a zend tutorial & get this message, & not sure where to
I've been working through Practical Common Lisp and as an exercise decided to write
I'm working through previous years ACM Programming Competition problems trying to get better at
I'm currently working through an assignment that deals with Big-O and running times. I
I've been working through a recent Computer Science homework involving recursion and big-O notation.
I'm working through some homework and a question on a previous exam paper asks
I just got done working through the Django tutorials for the second time, and
I am working through some of the exercises in The C++ Programming Language by
I'm working through Cocoa Programming for Mac OS X (3rd ed) and in chapter

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.