Discussion:
Python integration examples
(too old to reply)
Marc Van Dyck
2020-12-15 13:48:31 UTC
Permalink
Apparently it is possible to write code so that a set of routines
present in a shareable image can be called from within a Python
program.
Could someone shed some light, provide examples, any sort of guidance,
about how I can actually achieve this ?

Many thanks in advance,
Marc.
--
Marc Van Dyck
Richard Brodie
2020-12-15 14:19:38 UTC
Permalink
Post by Marc Van Dyck
Apparently it is possible to write code so that a set of routines
present in a shareable image can be called from within a Python
program.
Could someone shed some light, provide examples, any sort of guidance,
about how I can actually achieve this ?
The easiest way to go is through the ctypes module. Once you have a handle
on how it works, it's not too hard to make it work on VMS.
https://docs.python.org/3/library/ctypes.html
Post by Marc Van Dyck
from ctypes import c_int, cdll, byref
librtl = cdll.librtl
get_day = librtl.lib$day
File "<stdin>", line 1
get_day = librtl.lib$day

OK, dollar in the identifier is a problem. More than one way to do it though.
^
Post by Marc Van Dyck
get_day = librtl['lib$day']
day = c_int()
status = get_day(byref(day))
print (status, day)
1 c_long(59198)
Arne Vajhøj
2020-12-16 01:44:29 UTC
Permalink
Post by Marc Van Dyck
Apparently it is possible to write code so that a set of routines
present in a shareable image can be called from within a Python program.
Could someone shed some light, provide examples, any sort of guidance,
about how I can actually achieve this ?
Obviously there are the docs. But sometimes a bit extra
info helps.

I have written a little example:

Python code:

https://wwww.vajhoej.dk/arne/articles/call2.html#py_ctypes

C code & VMS build:

https://www.vajhoej.dk/arne/articles/call2.html#common

One change to the Python code for VMS:

DISK2:[ARNE.py]pydemolib.py;1

cdll.LoadLibrary('stddemolib') ## cdll.LoadLibrary('stddemolib.dll')

(that one should be pretty obvious)

And if your VMS Python does not have numpy module (my 2.7 does not)
the sum function does not work so:

DISK2:[ARNE.py]pydemolib.py;1

##from numpy import *
##demolib.std_sum.argtypes = [c_int, ctypeslib.ndpointer(dtype=int32)]
##demolib.std_sum.restype = c_int
##def sum(a):
## return demolib.std_sum(len(a), array(a))

DISK2:[ARNE.py]TestPy.py;1

##six = [1, 2, 3, 4, 5, 6]
##allsix = pydemolib.sum(six)
##print(allsix)

Arne
Arne Vajhøj
2020-12-16 01:46:07 UTC
Permalink
Post by Arne Vajhøj
https://wwww.vajhoej.dk/arne/articles/call2.html#py_ctypes
https://www.vajhoej.dk/arne/articles/call2.html#py_ctypes

It is not Weird World Wide Web ...

:-)
Post by Arne Vajhøj
https://www.vajhoej.dk/arne/articles/call2.html#common
Arne
Marc Van Dyck
2020-12-16 14:18:32 UTC
Permalink
Post by Marc Van Dyck
Apparently it is possible to write code so that a set of routines
present in a shareable image can be called from within a Python program.
Could someone shed some light, provide examples, any sort of guidance,
about how I can actually achieve this ?
Many thanks in advance,
Marc.
Thank you both. Will look into that and come back here if I need more
info.
--
Marc Van Dyck
Loading...