[Solved] [Python] Errors report - Debugging configuration

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
OlivierR
Posts: 38
Joined: Sun Jul 25, 2010 3:13 pm

[Solved] [Python] Errors report - Debugging configuration

Post by OlivierR »

Hi,

I am beginning to write an extension in Python, but I am very frustrated, for I don’t succeed to get any report when Python crashes/stops.

I have changed the configuation values in OpenOffice.org 3\Basis\program\pythonscript.py :

Code: Select all

#LogLevel.use = LogLevel.NONE                # production level
#LogLevel.use = LogLevel.ERROR               # for script developers
LogLevel.use = LogLevel.DEBUG               # for script framework developers
LOG_STDOUT = False                          # True, writes to stdout (difficult on windows)
                                            # False, writes to user/Scripts/python/log.txt
ENABLE_EDIT_DIALOG=False                    # offers a minimal editor for editing.
Anyway, whatever values I set, nothing changes. No messages, no file user/Scripts/python/log.txt.
This way, debugging is extremely difficult.

What should I do?

If there is no solution, where is stdout on Windows? That would be better than nothing.
Last edited by OlivierR on Fri Aug 13, 2010 7:13 pm, edited 1 time in total.
LibreOffice 6.4Windows 10
User avatar
gurfle
Posts: 67
Joined: Wed Dec 02, 2009 5:40 am

Re: [Python] Errors report - Debugging configuration

Post by gurfle »

Oliver:

This is probably not of much help, but whenever python bombs on me, I get a very descriptive message box that always indicates the precise line number where the error occured. I did not know about the configurations in pythonscript.py until seeing your post, but when I changed the settings to what you have, the file "C:\Documents and Settings\<WindowsUserid>\Application Data\OpenOffice.org\3\user\Scripts\python\log.txt" is created that includes the same error messages seen in the message box.

I am using OOo 3.1.1 that comes with Python 2.6.1 on Windows XP, so it is a slightly different configuration than you have. I have heard that there are problems with python if you do not use the version that gets installed with OOo, but believe if that were your problem you would not be able to get python to work at all from OOo which it does not appear to be the case, right?

I wish you good luck, because I have had good results with python and think it probably is the best (or at least easiest) way to get serious programming done for OOo automation.
Nicholas Dreyer
AMD sempron 3.4GHz, 1G RAM, nForce3-250Gb motherboard

AntiX M11
LibreOffice 3.4.3

Posts prior to Aug. 18, 2011 referenced
Linux Debian (lenny-backports or squeeze)
OpenOffice 3.2.1
OlivierR
Posts: 38
Joined: Sun Jul 25, 2010 3:13 pm

Re: [Python] Errors report - Debugging configuration

Post by OlivierR »

Thanks for your answer, Nicholas.
I was wondering how it worked for others windows users. At least, I know now that it can work. ;)

I also tried OOo 3.2.1 on Windows XP, and I don't get any error messages either, whatever method I try. Just nothing.

On the both machines, Python 2.6.5 and Python 3.1.2 are installed. But I am writing an extension, and I installed it each times I want to test it. So, as far as I know, OOo uses the Python version bundled in its code, not the two other ones.

May be a bug in OOo 3.2? I will test with OOo 3.1 tomorrow.
LibreOffice 6.4Windows 10
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: [Python] Errors report - Debugging configuration

Post by hanya »

Hi,
are you making your extension as UNO component implemented in Python?
The configuration of the pythonscript.py is influence to only scripts written in python and it does not work with components.

pythonloader.py does not provide the logging function because error occured in your component does not catched in the loader.
Replace your standard output and error with your own logger to make log file.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
OlivierR
Posts: 38
Joined: Sun Jul 25, 2010 3:13 pm

Re: [Python] Errors report - Debugging configuration

Post by OlivierR »

Hi hanya,
hanya wrote:are you making your extension as UNO component implemented in Python?
The configuration of the pythonscript.py is influence to only scripts written in python and it does not work with components.
Yes.
Actually, I have used your code to implement a contextual menu.
http://user.services.openoffice.org/en/ ... 13#p123613
Replace your standard output and error with your own logger to make log file.
That's what I did.

Code: Select all

sys.stderr = codecs.open("C:\_stderr_log.txt", "w", "utf-8")
sys.stdout = codecs.open("C:\_stdout_log.txt", "w", "utf-8")

print >> sys.stderr, "error"
print >> sys.stdout, "message"
print "another message"
This works, except that no error message is sent to stderr if I make a syntax error or if Python stops.
LibreOffice 6.4Windows 10
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: [Python] Errors report - Debugging configuration

Post by hanya »

OlivierR wrote:This works, except that no error message is sent to stderr if I make a syntax error or if Python stops.
All syntax error of component files are ignored simply in the pythonloader.py.
If you want to get it, you have to edit pythonloader.py to show syntax error. But you can find syntax error using normal python interpreter. Simply, pass your script to interpreter.

If you want to modify pythonloader.py, syntax error should be caused on compile function call in the getModuleFromUrl method. Add another except statement. Here is an example:

Code: Select all

# pythonloader.py
class Loader( XImplementationLoader, XServiceInfo, unohelper.Base ):
      def __init__(self, ctx ):
#...
      def getModuleFromUrl( self, url ):
          if DEBUG:
#...
          try:
                if "file" == protocol:
                      # code of the component is compiled
# ...
          except ImportError, e:
                raise RuntimeException( "Couldn't load "+url+ " for reason "+str(e), None)
          except SyntaxError, e: # added
              print(e)
          return None
Additionally, syntax error of python script (written as macro) can be seen using my modified script provider. If you are writing a macro as automation script, this does not needed.
http://hermione.s41.xrea.com/pukiwiki/i ... -0.4.2.oxt
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
OlivierR
Posts: 38
Joined: Sun Jul 25, 2010 3:13 pm

Re: [Python] Errors report - Debugging configuration

Post by OlivierR »

Thank you, hanya. This is very helpful. :super:

By the way, there is very cool stuff in your website. 8-)
LibreOffice 6.4Windows 10
OlivierR
Posts: 38
Joined: Sun Jul 25, 2010 3:13 pm

Re: [Solved] [Python] Errors report - Debugging configuratio

Post by OlivierR »

Hi *,

I got some news about this problem.
There is a workaround to get a working stdout on Windows with Python.

(Thanks to Tor Lillqvist.)

To get a console to be used as stdout, apply the following instructions.

1. Close LibreOffice.

2. Download Visual Studio Express 2010 C++ (I assume it may work with other versions) and install it.

3. Open the Console for Visual Studio.

4. Type:

Code: Select all

    editbin.exe /subsystem:console "C:\Program Files\LibreOffice 5\program\soffice.exe"
    editbin.exe /subsystem:console "C:\Program Files\LibreOffice 5\program\soffice.bin"
Now, a console will be shown when you open LibreOffice. It will be the stdout for the “print” command.

And you can get Python errors while LO is running if you use traceback.

Code: Select all

    import traceback
    […]
    try:
        […]
    except:
        traceback.print_exc()
But syntax errors still won’t be reported there.
LibreOffice 6.4Windows 10
Post Reply