Bug 143989 - calling python from calc user function fails at document load
Summary: calling python from calc user function fails at document load
Status: REOPENED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: BASIC (show other bugs)
Version:
(earliest affected)
7.4.3.2 release
Hardware: All Windows (All)
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords: skillPython
Depends on:
Blocks: Calculate
  Show dependency treegraph
 
Reported: 2021-08-21 07:46 UTC by Nerijus
Modified: 2024-12-26 16:02 UTC (History)
4 users (show)

See Also:
Crash report or crash signature:


Attachments
Test case for Python called from formula function (12.65 KB, application/vnd.oasis.opendocument.spreadsheet)
2022-12-23 09:41 UTC, ms777
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Nerijus 2021-08-21 07:46:02 UTC
Description:
=Call_VOL(1,2,3)'6
Later when run 
Data->Calculate->Recalculate Hard
is ok
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Function Call_VOL(x,y,z)
	Dim oScript: oScript = GetPythonScript("WinTool.py$VOL","document")
	Call_VOL=convPy2Basic(oScript.invoke(array(x,y,z), array(), array()))
End Function

Public Function GetPythonScript(macro As String, Optional location As String) As com.sun.star.script.provider.Xscript
    ''' Grab Python script object before execution
    ' Arguments:
    '    macro   : as "library/module.py$macro" or "module.py$macro"
    '    location: as "document", "share", "user" or ENUM(eration)
    ' Result:
    '    located com.sun.star.script.provider.XScript UNO service'''
    If IsMissing(location) Then location = "user"
    Dim mspf As Object ' com.sun.star.script.provider.MasterScriptProviderFactory
    Dim sp As Object ' com.sun.star.script.provider.XScriptProvider compatible
    Dim uri As String
    If location="document" Then
        sp = ThisComponent.getScriptProvider()
    Else
        mspf = CreateUNOService("com.sun.star.script.provider.MasterScriptProviderFactory")
        sp = mspf.createScriptProvider("")
    End If
    uri = "vnd.sun.star.script:"& macro &"?language=Python&location="& location
    GetPythonScript = sp.getScript(uri)
End Function ' GetPythonScript

Actual Results:
=Call_VOL(1,2,3)'6
Error

Expected Results:
=Call_VOL(1,2,3)'6


Reproducible: Always


User Profile Reset: No



Additional Info:
Later when run 
Data->Calculate->Recalculate Hard
is ok
Comment 1 Roman Kuznetsov 2021-08-21 20:44:10 UTC
Can you provide a file with your macro?
Comment 2 QA Administrators 2022-02-18 03:42:24 UTC Comment hidden (obsolete)
Comment 3 QA Administrators 2022-03-21 03:27:01 UTC Comment hidden (obsolete)
Comment 4 ms777 2022-12-23 09:41:50 UTC
Created attachment 184325 [details]
Test case for Python called from formula function
Comment 5 ms777 2022-12-23 09:54:20 UTC
I can confirm the behavior and have added a simplified test case. A python function taking no parameters and returning a string is called from A2 through this Basic wrapper:

function callPython()
  oScriptProvider = ThisComponent.getScriptProvider()
On Error goto ErrorExit:
  oScript = oScriptProvider.getScript("vnd.sun.star.script:formulaFunction.py$pythonFormulaFunction?language=Python&location=document")
  if IsNull(oScript) then
    callPython = "error getting python script: oScript is null"
  else
    callPython = oScript.invoke(array(), array(), array())
  endif
  exit function
ErrorExit:
  callPython = "error getting python script: " + chr(10)+chr(13) + Error$
End function

When the document is opened, an error occurs. After recalc (Ctrl+Shift+F9) the function works without error

The below function in B2 tries to track down the error a bit. It seems like the python function is not yet in the document storage when it is called the first time
function findScript()
  ucb = CreateUnoService("com.sun.star.ucb.UniversalContentBroker", Array("Local", "Office"))
  identifier = ucb.createContentIdentifier("vnd.sun.star.tdoc:/1/Scripts/python/formulaFunction.py")
  content = ucb.queryContent(identifier)

  if IsNull(content) then
    findScript = "python script not found"
  else
    findScript = "python script found"
  endif
End function

The behavior is identical on AO 4.1.6
See also https://forum.openoffice.org//en/forum/viewtopic.php?p=532310#p532310
Comment 6 ms777 2023-12-23 12:01:57 UTC
... still the same situation under 7.6.4.1 Win 10 64