Menu

accessing session from extensions?

2020-10-30
2020-11-03
  • Justin R. Miller

    I’ve looked at the source quite a bit, but I’m not sure if the intention is there to be able to access the session object from within Python extensions themselves — for example, to get at the program in memory or to manipulate the pixel buffer. Would this be possible, or are there any plans to add such a feature?

    JM

     
    • Rob Hagemans

      Rob Hagemans - 2020-10-30

      Hi Justin, If you're working from Python it's possible to access the session object by defining a child class with your own methods and starting BASIC from there rather than the regular Session. You could then provide that child object (or some proxy object around it so you control the namespace) as an extension to itself. Some simple examples can be found in tests/unit/test_extensions.py However all this is still in experimental state and for now undocumented; your code may stop working with the next (even minor) version upgrade...

      Note I don't really want to encourage people to depend on accessing the session object's internals as the was it's set up can change quite dramatically from version to version as the code is refactored. So the recommended way is to use BASIC only to change the internal state of the interpreter - i.e. use POKE to manipulate memory or the pixel buffer, along with the small number of methods defined in api.py- but even that API is not stable yet and might still change.

       
      • Justin R. Miller

        Awesome, thanks for this. I had seen test_extensions.py, but going back at it with new eyes helped. I'm able to do as you suggested. I've manipulated things with s.execute(), s.set_variable(), etc.

        I guess the problem I'm having is that I need to do this from inside a graphical interactive session, not just a programmatic session. I hear you on the API internals. Really all I need right now is a reasonably stable way to get the current program listing; not much more. I'd like to create an extension function which acts on this listing. Whether that's via a currently-private session API or through executing a LIST from the session, no huge matter. I've done something like import and call run to launch such a session, but I can't then get the session object as I can the other way. Any suggestions?

         
        • Rob Hagemans

          Rob Hagemans - 2020-10-31

          I see. I can't think of a way to do that through the interface that's exposed in 'regular' PC-BASIC. You could start a graphical session from a custom Python script - essentially you'd need to copy _run_session from main.py and adapt it to start a derived Session object. Next problem is that the listing itself isn't really directly accessible through the API or through BASIC - however you could SAVE "FILENAME", A to a temporary file or a StringIO stream (using Session.bind_file) and read back from that?

           
          • Justin R. Miller

            Thanks for this, Rob! I pushed through with that and was able to load a subclassed Session and then use its methods to SAVE and otherwise get to what I needed.

             
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.