Bug 158417 - Calling an embedded document Python script from Basic gives an error
Summary: Calling an embedded document Python script from Basic gives an error
Status: RESOLVED DUPLICATE of bug 122545
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: BASIC (show other bugs)
Version:
(earliest affected)
7.6.3.2 release
Hardware: x86-64 (AMD64) Windows (All)
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-11-28 11:41 UTC by Olivier
Modified: 2023-12-19 16:46 UTC (History)
2 users (show)

See Also:
Crash report or crash signature:


Attachments
This is the error when trying to open an embedded Python script (16.03 KB, image/png)
2023-11-28 11:41 UTC, Olivier
Details
Sample document with a non working Python Call (11.90 KB, application/vnd.oasis.opendocument.spreadsheet)
2023-11-28 13:05 UTC, Olivier
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Olivier 2023-11-28 11:41:59 UTC
Created attachment 191079 [details]
This is the error when trying to open an embedded Python script

Calling an embedded Python script from a Basic script does not work.

The same script can be called without error if it is in the user location.

The problems seems related to the "document" location.
The script has been embedded with APSO v1.3.3.

Here is the calling Basic code :

REM  *****  BASIC  *****

Function bridge(fonction_py, param1, Optional param2,  Optional param3)

    Dim myScript as Object
    Dim chemin As String
    'The user location is working :
    'chemin = "vnd.sun.star.script:Module.py$" & fonction_py & "?language=Python&location=user"
    'The document location does not work
     chemin = "vnd.sun.star.script:Module.py$" & fonction_py & "?language=Python&location=document"

    myScript = ThisComponent.getScriptProvider().getScript(chemin)
    bridge = myScript.invoke(Array(param1, param2, param3), Array(), Array() )
    
End Function
Comment 1 Mike Kaganski 2023-11-28 11:55:43 UTC
(In reply to Olivier from comment #0)
>     'The document location does not work
>      chemin = "vnd.sun.star.script:Module.py$" & fonction_py &
> "?language=Python&location=document"

Where in the document package does is Module.py located? Note https://wiki.documentfoundation.org/Documentation/DevGuide/Scripting_Framework#Python_script , where the common case is described as:

> the Python module myModule.py is stored in directory Scripts/python of the user
> or share directory of the installation, or of the document.

Please provide a sample document with both Python module, and a Basic macro trying to call it.
Comment 2 Olivier 2023-11-28 13:05:54 UTC
Created attachment 191080 [details]
Sample document with a non working Python Call

Here is a sample document where the Basic errors trig.


After opening the document, enabling macros and confirming the Basic errors, i've just discovered that copying the 3 cells D3, D5 and D6 in another other place solve the problem.


After opening, please try to copy D3, D5 and D6 to another location, for example D9, then you should see that the Python script call start to work.

If you did copy the three cells at D9 location, You should get the Python script results inside cells D11 and 12, values should be 100 and 200.

But the initial cells D5 and D6 will still stay empty.
Comment 3 Olivier 2023-11-28 18:00:57 UTC
I've found the root cause of the problem.

When opening the document the Sheet formulas are refreshing before the Basic script got enough time to load the Python Script.


Disabling the autorefresh in the data menu solve the problem. Then pressing F9 run the basic script normally without error. After that, enabling again autorefresh does not cause problems.


This show that the problem exhibit only at the opening of the document.


It probably does come from a missing process synchronization check.
Comment 4 Mike Kaganski 2023-11-29 09:08:18 UTC
See also bug 79588 comment 29.

But that comment only discusses how the UDF are recognized; and this problem here is not about UDF recognition, but about the first execution of spreadsheet formulas.

Likely this is a duplicate of bug 122545.
Comment 5 Olivier 2023-11-29 14:52:17 UTC
You are right this is probably a duplicate of the bug 122545 that is here since 2019 and has not been fixed.


It's probably definitely a problem with a missing process or thread synchronization. Because if autocalculate is disabled when opening the document the error does not exhibit.

Probably, Basic try to execute Python code before the Python object has been loaded.


The processor load during document opening is probably the reason that trig this error.


Should be solved by adding a semaphore so that Basic do not try to run code from another yet unloaded object for another language.
Comment 6 Buovjaga 2023-12-19 16:46:57 UTC

*** This bug has been marked as a duplicate of bug 122545 ***