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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
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 inapi.py
- but even that API is not stable yet and might still change.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 withs.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 likeimport
and callrun
to launch such a session, but I can't then get the session object as I can the other way. Any suggestions?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
frommain.py
and adapt it to start a derivedSession
object. Next problem is that the listing itself isn't really directly accessible through the API or through BASIC - however you couldSAVE "FILENAME", A
to a temporary file or aStringIO
stream (usingSession.bind_file
) and read back from that?Thanks for this, Rob! I pushed through with that and was able to load a subclassed
Session
and then use its methods toSAVE
and otherwise get to what I needed.