RISC OS Open
Safeguarding the past, present and future of RISC OS for everyone
ROOL
Home | News | Downloads | Bugs | Bounties | Forum | Documents | Photos | Contact us
Account
Forums → General →

Python3 documentation and advanced SWI stuff

Subscribe to Python3 documentation and advanced SWI stuff 11 posts, 7 voices

 
May 14, 2023 8:32pm
Avatar Lauren Etc. (8147) 15 posts

Hi,

I’m playing around this week with porting some stuff I did in Python over to RISC, but I wanted to slap a Toolbox interface on it and yet I’m getting hung up on the platform-specific features which I’m having trouble finding much in the way of documentation for. The only guide for swi.swi() I could find is a one-line example in the release notes inside the app bundle using OS_Write0 or something, but I’m wondering how I would do a call like, say, this where I’m having to pass it an entire memory block (which would be defined with some kind of struct in C):

DIM id_block% 32, msg_block% 4
SYS “Toolbox_Initialise”,0,350,wimp_mask%,tbox_mask%,msg_block%,id_block% TO ,handle%,spritep%

My inclination was to use bytearrays for the blocks I’m trying to pass but it doesn’t like that at all and I just get a TypeError. I then tried some more baroque stuff like defining a class with ctypes fields in it but no good. I feel a bit stupid, but has anyone used swi.swi() in the Python3 port? Or is there more documentation I’m missing somewhere?

By the way, I know there is a Toolbox module as well but a.) the demo code for that was having issues and I haven’t looked into it yet and b.) I’ll still likely need to do something like this regardless so I’m wondering how it works. Thanks!

 
May 14, 2023 10:06pm
Avatar Andrew Conroy (370) 714 posts

he only guide for swi.swi() I could find is a one-line example in the release notes inside the app bundle using OS_Write0 or something, but I’m wondering how I would do a call like, say, this where I’m having to pass it an entire memory block

Did you find the StrongHelp manual included with Python3 Alpha 4? That goes into more detail about swi.swi and how to pass it a pointer to a block. Not sure why this isn’t included with the release version too.

 
May 15, 2023 5:39am
Avatar Lauren Etc. (8147) 15 posts

> Did you find the StrongHelp manual included with Python3 Alpha 4?

Thanks for the lead, I haven’t. I’ve actually been searching for a while now and can’t locate it, I found the 3.8a4 release on Github but didn’t see it there either. Does anyone have a link? I’ll keep searching in the meantime but so far I’m empty handed.

 
May 15, 2023 1:09pm
Avatar John Rickman (71) 493 posts

You might find this useful, but it comes with a warning. It is old,I don’t know how accurately it reflects the current toolbox swi and I can’t remember where it came from.
toolbox swi documentation

 
May 15, 2023 1:13pm
Avatar Sprow (202) 1048 posts
DIM id_block% 32, msg_block% 4
SYS “Toolbox_Initialise”,0,350,wimp_mask%,tbox_mask%,msg_block%,id_block% TO ,handle%,spritep%

While this isn’t addressing your python question, I should say that that doesn’t look right for BBC BASIC. In particular the directory name in R4 is missing, more like

SYS “Toolbox_Initialise”,0,350,wimp_mask%,tbox_mask%,"<MyApp$Dir>",msg_block%,id_block% TO ,handle%,spritep%

It’s also worth a mention that wimp_mask% and tbox_mask% are pointers to lists of words of Wimp or Toolbox reason codes rather than bitmasks (just guessing from the variable names here!).

 
May 15, 2023 5:47pm
Avatar Andrew Conroy (370) 714 posts

Thanks for the lead, I haven’t. I’ve actually been searching for a while now and can’t locate it, I found the 3.8a4 release on Github but didn’t see it there either. Does anyone have a link? I’ll keep searching in the meantime but so far I’m empty handed.

I can’t find it now either, so I’ve temporarily put a copy up here: http://www.owlart.co.uk/temp/Python38a4.zip

Let me know once you’ve got it and I’ll take it down again.

 
May 15, 2023 11:41pm
Avatar Lauren Etc. (8147) 15 posts

You might find this useful, but it comes with a warning.

I’ll take it with a grain of salt then, but this does look helpful.

While this isn’t addressing your python question, I should say that that doesn’t look right for BBC BASIC.

I should have specified, for purposes of my question imagine my code example is correct ;)

I can’t find it now either, so I’ve temporarily put a copy up here:

Got it, thanks! On a cursory glance it looks like it has the info I need, hopefully. But the heatwave here has taken a few IQ points off me and might fry my Pi too, so I’ll examine it more later. Thanks again! But you should be good to take it down for now, I’ve got a copy saved.

 
May 21, 2023 2:23pm
Avatar Mark Beerling (8582) 3 posts

This looks like the right forum topic for me

I’ve also been trying to convert some RISC OS C/C++ programs over to Python

I’m having problems passing a string over to swi.swi when the string contains characters with the top bit set.

for example ‘öüä’ & the Euro sign. These come out garbled in the corresponding wimp gadget. This seems to be a problem with the swi library, the simplest example of the problem is below.

*python3
Python 3.8.8+ (riscos-1, Feb 22 2021, 21:48:54)
[GCC 4.7.4] on riscos
Type "help", "copyright", "credits" or "license" bc. for more information.
>>> import swi
>>> x=swi.block(2,"öüä")
>>> x.nullstring()
'öÃ'

There is a workaround in that I pass the string as a block (‘b’ instead of ‘s’). This works for some gadgets but not for others (eg ‘StringSet’).

I’ve had a look at using Gerph’s “rocodecs/py” but even the enclosed check & test programs don’t work for me (see below).

BTW. I’m using “*Country Germany” and “Alphabet Latin9”.

Perhaps someone can help me.

*check_mappings/py
Checking encoding riscos-latin1
Traceback (most recent call last):
  File "RAM::RamDisc0.$.rocodecs/py", line 116, in decode
    return (''.join(decoder(x) for x in binary), len(binary))
  File "RAM::RamDisc0.$.rocodecs/py", line 116, in <genexpr>
    return (''.join(decoder(x) for x in binary), len(binary))
  File "RAM::RamDisc0.$.rocodecs/py", line 121, in _decode_strict
    raise UnicodeDecodeError("Cannot decode character {!r} from encoding '{}'".format(c, self.name))
TypeError: function takes exactly 5 arguments (1 given)


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "RAM::RamDisc0.$.check_mappings/py", line 15, in <module>
    encoded = rocodecs.RISCOSAlphabet.base_array.decode(encoding.name)
TypeError: decoding with 'riscos-latin1' codec failed (TypeError: function takes exactly 5 arguments (1 given))


*manual_test/py
Traceback (most recent call last):
  File "RAM::RamDisc0.$.manual_test/py", line 24, in <module>
    main()
  File "RAM::RamDisc0.$.manual_test/py", line 14, in main
    print(repr(text))
UnicodeEncodeError: 'latin-1' codec can't encode character '\u20ac' in position 8: ordinal not in range(256)
 
May 21, 2023 2:25pm
Avatar Mark Beerling (8582) 3 posts

Please ignore the strike though above. I don’t know what happend there!

 
May 21, 2023 3:38pm
Avatar Steve Fryatt (216) 1865 posts

Please ignore the strike though above. I don’t know what happend there!

You need to prefix your code with a bc.._ (and start a following paragraph, if there is one, with a p._) where the _’s are actually spaces: see section 3.3 of https://www.riscosopen.org/wiki/documentation/show/Forum

 
2 days ago
Avatar Chris Johns (8262) 231 posts

Late to the thread here. There is a new toolbox library out now (1.0.1) which has had a major overhaul. Those who attended the WROCC presentation will have an idea about them.

SWI blocks. There is the swi.block, which can be passed to swi.swi by specifing a ‘b’ in the registers string. You create a block by gizing it the number of WORDS you want, eg.

b = swi.block(64) # 64 words (256 bytes)

you can then pass it to the swi with

swu.swi(“Foo_Bar”, “b”, b)

to get things in/out of the block, you can use

b.signed(byte-offset, value) and b.tosigned(byte-offset)

there are similar methods for unsigned, and there is padstring(string, pad-char, start, end) to fill in a string, nullstring(offset) / ctrlstring(offset) to get a null or ctrl terminated string from the block.

The other option is to use ctypes, and pass in the address of the ctypes struct as an unsigned integer. The new toolbox libraby does this in sevral places.

Reply

To post replies, please first log in.

Forums → General →

Search forums

Social

Follow us on and

ROOL Store

Buy RISC OS Open merchandise here, including SD cards for Raspberry Pi and more.

Donate! Why?

Help ROOL make things happen – please consider donating!

RISC OS IPR

RISC OS is an Open Source operating system owned by RISC OS Developments Ltd and licensed primarily under the Apache 2.0 license.

Description

General discussions.

Voices

  • Lauren Etc. (8147)
  • Andrew Conroy (370)
  • John Rickman (71)
  • Sprow (202)
  • Mark Beerling (8582)
  • Steve Fryatt (216)
  • Chris Johns (8262)

Options

  • Forums
  • Login
Site design © RISC OS Open Limited 2018 except where indicated
The RISC OS Open Beast theme is based on Beast's default layout

Valid XHTML 1.0  |  Valid CSS

Powered by Beast © 2006 Josh Goebel and Rick Olson
This site runs on Rails

Hosted by Arachsys