Re: Running Python code from GPRBuild (Calling from GNAT Studio)

60 views
Skip to first unread message

Dmitry A. Kazakov

unread,
Nov 10, 2022, 4:58:04 PM11/10/22
to
On 2022-11-10 20:25, Rego, P. wrote:

> I'm trying to compile some Python files from an Ada project (in an integration which will consist of Ada, Python and Rust).

Python is not a compiled language.

1. If you want to create a Python module in a form of a dynamically
linked library, you must use C or Ada for that.

2. If you rather meant pre-compiled binary code (*.pyc). Python creates
them by py_compile command. See

https://docs.python.org/3/library/py_compile.html#py_compile.compile

There is also command-line interface:

https://docs.python.org/3/library/compileall.html

You possibly could configure gpr to call compileall on *.py files,
though it would make no sense to me.

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

Rego, P.

unread,
Nov 11, 2022, 5:25:59 AM11/11/22
to
Hi Dmitry,

> Python is not a compiled language.
Indeed. It will not generate an object like any compiled language.
But Since Python is included in Languages list from Project properties, I expected
that the IDE were capable of statically checking the syntax and pointed the
"compilation" errors. Also, I expected that we could run the script from GPS, and
be able to debug it.

> 1. If you want to create a Python module in a form of a dynamically
> linked library, you must use C or Ada for that.

Not the case, I also don't see advantages of it (so I agree, I'd go directly to Ada or C).

> 2. If you rather meant pre-compiled binary code (*.pyc). Python creates
> them by py_compile command. See
> https://docs.python.org/3/library/py_compile.html#py_compile.compile
> There is also command-line interface:
> https://docs.python.org/3/library/compileall.html
> You possibly could configure gpr to call compileall on *.py files,
> though it would make no sense to me.

I think this would be the closer of what I was trying to do.
The idea is to help in the development. Instead of having to develop the
Python part from a separate IDE, being able to code in the same would
speed up things.

Would you have some example of it? (how to configure gpr to call
compileall on *.py files).

Thanks!

Dmitry A. Kazakov

unread,
Nov 11, 2022, 6:04:45 AM11/11/22
to
On 2022-11-11 11:25, Rego, P. wrote:

>> 1. If you want to create a Python module in a form of a dynamically
>> linked library, you must use C or Ada for that.
>
> Not the case, I also don't see advantages of it (so I agree, I'd go directly to Ada or C).

Actually this is the way of doing scripting from Ada, like GPS IDE does.
You write a module, e.g. in Ada, and then you can import it in your
Python script. The module must be a DLL.

>> 2. If you rather meant pre-compiled binary code (*.pyc). Python creates
>> them by py_compile command. See
>> https://docs.python.org/3/library/py_compile.html#py_compile.compile
>> There is also command-line interface:
>> https://docs.python.org/3/library/compileall.html
>> You possibly could configure gpr to call compileall on *.py files,
>> though it would make no sense to me.
>
> I think this would be the closer of what I was trying to do.
> The idea is to help in the development. Instead of having to develop the
> Python part from a separate IDE, being able to code in the same would
> speed up things.
>
> Would you have some example of it? (how to configure gpr to call
> compileall on *.py files).

You could start with something like this:

project Python is
for Languages use ("Python");
package Compiler is
for Driver ("Python") use

"C:/GNAT/2021/libexec/gnatstudio/gnathub/share/gnathub/python/python.exe";
for Required_Switches ("Python") use ("-m", "compileall");
end Compiler;
package Naming is
for Body_Suffix ("Python") use ".py";
end Naming;
end Python;

Rego, P.

unread,
Nov 11, 2022, 6:28:50 AM11/11/22
to
> You could start with something like this:

Thanks Dmitry!

> project Python is
> for Languages use ("Python");
> package Compiler is
> for Driver ("Python") use
> "C:/GNAT/2021/libexec/gnatstudio/gnathub/share/gnathub/python/python.exe";
> for Required_Switches ("Python") use ("-m", "compileall");
> end Compiler;
> package Naming is
> for Body_Suffix ("Python") use ".py";
> end Naming;
> end Python;

Got it. I didn't include naming and compiler options.
Now it runs, but it asks for a linker
> gprbuild: no linker specified and no default linker in the configuration
(where idk if it makes sense since we wont be linking to an object)

For a no error py it "compiles" clean (ok I'll call compilation, but we understand
it's not "the compilation"), but in compilation log it doesn't show the warnings
that are included in the edit window with the opened file. If I include
an error, it shows in the log where the error happened, but doesn't show in
the locations. GPS will be not very useful this way.

Dmitry A. Kazakov

unread,
Nov 11, 2022, 8:30:29 AM11/11/22
to
On 2022-11-11 12:28, Rego, P. wrote:

> Got it. I didn't include naming and compiler options.
> Now it runs, but it asks for a linker
>> gprbuild: no linker specified and no default linker in the configuration
> (where idk if it makes sense since we wont be linking to an object)
>
> For a no error py it "compiles" clean (ok I'll call compilation, but we understand
> it's not "the compilation"), but in compilation log it doesn't show the warnings
> that are included in the edit window with the opened file. If I include
> an error, it shows in the log where the error happened, but doesn't show in
> the locations. GPS will be not very useful this way.

This "compiles" only:

project Python is
for Languages use ("Python");
package Compiler is
for Driver ("Python") use
"C:/GNAT/2021/libexec/gnatstudio/gnathub/share/gnathub/python/python.exe";
for Required_Switches ("Python") use ("-m", "py_compile");
end Compiler;
package Naming is
for Body_Suffix ("Python") use ".py";
end Naming;
end Python;

It should show syntax errors. If you remove -m py_compile switches, then
it will "compile" and execute.

(Python is not the thing you would wish to use in an IDE or anywhere,
pretty much consistently so... (:-))

Dmitry A. Kazakov

unread,
Nov 11, 2022, 8:40:16 AM11/11/22
to
Forgot to mention. If you want to create an executable. You can try to
configure the package Linker using PyInstaller as the driver:

https://pyinstaller.org/en/stable/operating-mode.html

Note that Linker does not take language e.g. ("Python") you just write:

package Linker is
for Driver use "pyinstaller.exe";

You have to tinker a bit since there is no object files to feed the
Linker. Maybe you could sell it *.py as an "archive" (static library).
Whatever... (:-))

Egil H H

unread,
Nov 11, 2022, 10:20:52 AM11/11/22
to
There are a couple of project level attributes you can play around with:

for Object_Generated("Python") use "False";

and

for Objects_Linked("Python") use "False";



Rego, P.

unread,
Nov 11, 2022, 1:08:14 PM11/11/22
to
> It should show syntax errors. If you remove -m py_compile switches, then
> it will "compile" and execute.

Got some error when removed -m switch
C:\GNAT\2021\libexec\gnatstudio\gnathub\share\gnathub\python\python.exe: can't open file 'compileall': [Errno 2] No such file or directory

> (Python is not the thing you would wish to use in an IDE or anywhere,
> pretty much consistently so... (:-))

LOL. I got lazy :-) IDEs make life easier, debugging, autocomplete, syntax highlight,
pretty printing, analysers rsrsrs
even for Py stuff (can't run away from it)

Rego, P.

unread,
Nov 11, 2022, 1:12:34 PM11/11/22
to
> Note that Linker does not take language e.g. ("Python") you just write:
> package Linker is
> for Driver use "pyinstaller.exe";

did that... got some more ugly messages

gprbuild -d -PP:\Gnat\testpy.gpr P:\Gnat\src\Analysis.py
Compile
[python] analysis.py
gprbuild: raised ADA.ASSERTIONS.ASSERTION_ERROR : Invalid Id 0
[C:\GNAT\2021\bin\gprbuild.exe]
0x8db0d6
0x648431
0x648671
0x4832bc
0x492553
0x49260e
0x4932d4
0x413bd5
0x948929
0x401423
0x40113b
[C:\WINDOWS\System32\KERNEL32.DLL]
0x7ffc052b7032
[C:\WINDOWS\SYSTEM32\ntdll.dll]
0x7ffc05e6269f
[2022-11-11 15:10:07] process exited with status 4, elapsed time: 04.03s

and now I oversimplified the Analysis.py to
import os
import sys
print('test')

maybe I'm pushing harder than should (trying to compile/run/whatever Python from GPS),
but still curious.

> You have to tinker a bit since there is no object files to feed the
> Linker. Maybe you could sell it *.py as an "archive" (static library).
> Whatever... (:-))

Who knows :p

Rego, P.

unread,
Nov 11, 2022, 1:14:45 PM11/11/22
to
> There are a couple of project level attributes you can play around with:
> for Object_Generated("Python") use "False";
> and
> for Objects_Linked("Python") use "False";

I had tried those before, removed because it didn't change anything (or it seemed)
Now after including Linker package I put them back, still no effect.

Dmitry A. Kazakov

unread,
Nov 11, 2022, 2:50:17 PM11/11/22
to
On 2022-11-11 19:08, Rego, P. wrote:
>> It should show syntax errors. If you remove -m py_compile switches, then
>> it will "compile" and execute.
>
> Got some error when removed -m switch
> C:\GNAT\2021\libexec\gnatstudio\gnathub\share\gnathub\python\python.exe: can't open file 'compileall': [Errno 2] No such file or directory

Because it is one of these three:

1. for Required_Switches ("Python") use ("-m", "py_compile");
2. for Required_Switches ("Python") use ("-m", "compileall");
3. nothing

#1 results in

python.exe -m py_compile <file>

= compile only. py_compile is the module name that takes <file> as an
argument.

#2 results in

python.exe -m compileall <file>

= compile, generate *.pyc files, store them at the Python library
location. compileall is the module name.

#3 results in

python.exe <file>

= compile/run <file>.

When you remove -m but leave compileall you get

python.exe compileall <file>

and compileall is treated as a file.

Command line-arguments are here:

https://docs.python.org/3/using/cmdline.html

Egil H H

unread,
Nov 11, 2022, 3:05:13 PM11/11/22
to
Project level, not in the Linker package

Rego, P.

unread,
Nov 11, 2022, 4:25:00 PM11/11/22
to
> Project level, not in the Linker package
Yep, as I did.

Rego, P.

unread,
Nov 11, 2022, 4:34:03 PM11/11/22
to
> 1. for Required_Switches ("Python") use ("-m", "py_compile");
> 2. for Required_Switches ("Python") use ("-m", "compileall");
> 3. nothing

Tried all the options, varing w/wo "-m". Not much exciting.

Current version is
--
project Testpy is
for Source_Dirs use ("src");
for Object_Dir use "obj";
for Languages use ("python");
for Objects_Linked ("python") use "False";
for Object_Generated ("python") use "False";

for Source_Files use ("Analysis.py");
for Main use ("Analysis.py");

package Naming is
for Body_Suffix ("python") use ".py";
end Naming;

package Compiler is
for Driver ("python") use "C:\GNAT\2021\libexec\gnatstudio\gnathub\share\gnathub\python\python.exe";
-- for Required_Switches ("python") use ("-m", "compileall");
-- for Required_Switches ("Python") use ("-m", "py_compile");
for Required_Switches ("Python") use ("-m","py_compile");
end Compiler;

package Linker is
for Driver use "C:\GNAT\2021\libexec\gnatstudio\gnathub\share\gnathub\python\python.exe";
end Linker;

for Object_Generated("Python") use "False";
for Objects_Linked("Python") use "False";

end Testpy;
--

Curiosily, compiling Analysis.py with
# Analysis.py
import os
import sys

print('test')
blabla

(so obviously it should not compile), returns no error

gprbuild -ws -c -f -u -PP:\Gnat\testpy.gpr Analysis.py
Compile
[python] analysis.py
[2022-11-11 18:27:03] process terminated successfully, elapsed time: 03.58s

But no problem. I'm convinced that Python (as external code, not GPS internal
script) should be kept very far from GPS.

Thanks!

Stephen Leake

unread,
Nov 12, 2022, 12:41:44 PM11/12/22
to
"Rego, P." <pvr...@gmail.com> writes:

> Hello everyone, long time no see some of you, I hope everyone is fine.
>
> I'm trying to compile some Python files from an Ada project (in an
> integration which will consist of Ada, Python and Rust). But let's
> simplify the things, so I created a Python-only project using this :
>
> -- testpy.gpr
> project Testpy is
> for Source_Dirs use ("src");
> for Object_Dir use "obj";
> for Main use ("Analysis.py");
> for Languages use ("Python");
> for Source_Files use ("Analysis.py");
> end Testpy;
>
> and as I tried to compile `Analysis.py` I got the messages
>> gprbuild -ws -c -f -u -PP:\Gnat\testpy.gpr Analysis.py
>> testpy.gpr:7:26: language unknown for "analysis.py"

So it does not associate ".py" with "Python". Try setting Spec_Suffix,
Body_Suffix in package Naming.

--
-- Stephe
Reply all
Reply to author
Forward
0 new messages