View Issue Details

IDProjectCategoryView StatusLast Update
0001287Scribus-public2005-01-04 12:20
Reporterzitter Assigned Toplinnell 
PrioritynormalSeverityminorReproducibilityalways
Status closedResolutionfixed 
PlatformlinuxOSdebianOS Versionsid
Fixed in Version1.2.1cvs 
Summary0001287: calendier.py doesn't work
DescriptionTraceback (most recent call last):
  File "<string>", line 4, in ?
  File "/usr/lib/python2.3/warnings.py", line 53, in warn
    filename = sys.argv[0]
AttributeError: 'module' object has no attribute 'argv'
TagsNo tags attached.
Patch

Relationships

related to 0001283 closedsubik Scripter code cleanup 
has duplicate 0001336 closed python script 
child of 0003813 acknowledged Metabug: Scripter 

Activities

ringerc

2004-11-08 10:56

reporter   ~0002810

Weird.... works fine here.

I'm a little puzzed as to how that error could be arising, too. The only case I can imagine is if the `sys' module fails to import, resulting in an empty module dictionary - but I can't imagine how that could happen.

The script you're using is Calendier.py from the 'samples' directory in the Scribus source distribution, yes? If not, it'd be much appreciated if you could let me know where you got it.

As I can't reproduce the problem, it'd also be really helpful if you could give a description of what your're doing, step by step, that leads up to the problem. Knowing what Python version you're using would also help.

ringerc

2004-11-08 11:19

reporter   ~0002811

My error - calendier not Calendar. I'll grab it from the scribus downloads and see if I can figure out what's going on.

ringerc

2004-11-08 12:34

reporter   ~0002812

OK, the problem is a warning being thrown by Python right at the very initial loading of the script, presumably before the sys module has finished loading. I don't know why it used to work or why it stopped working and will look into that, but the immediate issue is with the way the coding is specified in the script.

If you are going to use an encoding line in the script, it _must_ be the second line, and must come immediately before the #! line. So your script needs to start with these two lines:

#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-

before anything else, even comments.

With that change the error you report goes away. I'm still having other issues with the script, though.

I'm still hitting some character encoding problems here when I try to run your script, even with the encoding change (though Python stops complaining about the script encoding once that line is moved to to the second-top line).

Traceback (most recent call last):
  File "<string>", line 4, in ?
  File "/home/craig/.scribus/scripts/calendrier.py", line 201, in ?
    main()
  File "/home/craig/.scribus/scripts/calendrier.py", line 164, in main
    insertText(mois[j], 0, a)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 1: ordinal not in range(128)

I had to modify the scripter slightly to be able to get that error - the shipped version hides that message, instead telling you you've used the wrong arguments :-(

I need to do some more research into these encoding issues, as I just know too little about them. I would be interested to know if you have any success with moving the encoding line up to the second line as I showed.

On another note, I think it might be a good idea to add a check before any Scribus API calls are made to make sure there's an open document. Something like this:

if not haveDoc():
   # display a dialog warning the user they must have a document open already
   messageBox(....)

Otherwise some of your setup functions, like defineColor, might affect the global preferences. This probably isn't what the user wants.

ringerc

2004-11-10 13:31

reporter   ~0002854

Zitter: Are you still active here? I can not find your account in the user list. It would be very helpful if you could let me know if you have any luck changing the encoding as I suggested.

zitter

2004-11-10 14:47

reporter   ~0002856

HI, I've tried the english version of the script (calender.py) and it works.
The french one doesn't.
Here the first lines fo the script:
<code>
########################
#
# Encodage et imports

#!/usr/bin/python
# -*- coding: iso-8859-1 -*-

from scribus import *
from calendar import monthrange
from time import localtime

#
</code>

You ask me if my calendier.py is in the "sample" dir od scribus source distribution... yes it is there.

cbradney

2004-11-10 14:54

administrator   ~0002857

Its the calendrier.py from www.scribus.net downloads section.

ringerc

2004-11-10 15:01

reporter   ~0002858

cbradney: yeah, I figured that out as soon as I posted my first comment. I've been testing with that version ever since.

zitter: Can you try making the start of the script look like this instead:

#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-

########################
#
# Encodage et imports


and see if that helps? It does here, though I'm still having other problems with both the unpatched and newly patched scripter. There _is_ a bug in the recent scripter changes that's causing it to crash, but the encoding line is also not in the right place in your script currently.

The encoding line must be the second line of the script, before any comments or anything except the #! line.

zitter

2004-11-10 15:26

reporter   ~0002860

first lines of the "calendierer.py" script:

<code>
#!/usr/bin/python
# -*- coding: iso-8859-1 -*-

from scribus import *
from calendar import monthrange
from time import localtime
</code>

it gives me another error:

<error>
Traceback (most recent call last):
  File "<string>", line 4, in ?
  File "/home/danilo/scribus/Scribus/scribus/plugins/scriptplugin/samples/calendrier.py", line 151, in ?
    InsertText(mois[j], 0, a)
  File "<string>", line 4, in InsertText
Exception: Ups! Argomenti sbagliati! Chiama: insertText(unicodetext, position [, objectname])
</error>

"argomenti sbagliati" is italian way to say something like "invalid arguments".

Anyway, in the page appears a calendar with *only* one month, Janvier, with days in a column.

Hope this helps
zitter

ringerc

2004-11-10 16:12

reporter   ~0002864

Last edited: 2004-11-10 16:14

OK, thanks for that. I was also having that problem, but was hoping it was something wrong with my local Python setup. I pointed this out in an earlier note. The real error you are getting, that the scripter is hiding from you, is:

 UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 1: ordinal not in range(128)

Just to check, this script used to work correctly, right? Did it stop working a few days ago, when bug 0001276 was comitted? That code didn't receive as much testing as it could have used before being committed, but I don't see how it could be causing this problem either. It was definitely at fault for the first one, but I don't see how it could be for this one.

I'm going to have to do some more research to understand what's going on here properly, because I just don't understand enough about how Python handles text encodings.

edited on: 04-11-10 17:14

zitter

2004-11-10 21:32

reporter   ~0002871

HI, don't know if script works right because it shows only january 2005...
the script calender.py (english one) shows the current month of the current year

ringerc

2004-11-11 02:40

reporter   ~0002873

Sort-of. The script crashes when inserting the french name for the month of April. It only crashes there because that is the first non-ASCII text it encounters that isn't a comment - it's a general problem with the text encoding handling rather than anything specific to that bit of code.

Now I just need to understand why.

Did the French version used to work on older versions of Scribus?
If so, did it stop working a few days ago, when bug 0001276 was comitted?

ringerc

2004-11-11 08:30

reporter   ~0002877

Confirming: The initial bug reported by zitter is definitely the fault of the patches I submitted. I did say I wasn't entirely sure they were ready for inclusion yet ;-)

I will investigate and fix that bug shortly. Putting the encoding line in the correct place seems to fix the problem anyway, but if the encoding line is in the wrong place or missing it should not be crashing.

From IRC discussion it appears that the next problem (the wrong arguments error which is actually " 'ascii' codec can't decode byte 0xe9 in position 1: ordinal not in range(128)") is unrelated to the scripter changes. I'm pretty sure zitter confirmed that the script does not work in an older version of Scribus either.

I'm not sure what the problem is with the encodings yet. A workaround is to insert non-ascii text as escape sequences, for example:

mois = ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre']

becomes:

mois = ['Janvier', 'F\xc3\xa9vrier', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet','Ao\xc3\xbbt', 'Septembre', 'Octobre', 'Novembre', 'D\xc3\xa9cembre']

This is clumsy, but effective for now.

ringerc

2004-11-11 11:10

reporter   ~0002878

zitter, I just tested your script with 1.2RC and it works fine there. I need to test a week-ish old CVS version next.

Can you please answer the questions I asked earlier, as clearly as possible? Talk to me as if I don't know anything about your script or details of what the development of the scripter has been.

Another question: In what version of Scribus do you _know_ your script last worked? If it was a CVS version, knowing what date or build number would be extremely useful.

ringerc

2004-11-11 11:32

reporter   ~0002879

Last edited: 2004-11-16 10:15

While your script works correctly in 1.2RC1, it doesn't work (inserts gibberish) if I use iconv to convert it to utf8 and change the encoding line. Strange.

Now that I have a couple of test cases I can start investigating this properly.

edited on: 04-11-16 11:15

zitter

2004-11-11 11:33

reporter   ~0002880

the French version of the script doesn't work on older versions of Scribus, too.

You asked me: "In what version of Scribus do you _know_ your script last worked?". The script calendier (french version) *never* worked to me.

Another thing... the script is not mine, I'm not the author of the script. I'm not python dev, I'm a simple scribus-user :)

Ciao
Zitter

ringerc

2004-11-12 12:32

reporter   ~0002885

OK, I think I've narrowed down the issue. It's to do with some changes made in the scripter to better support unicode text. Here are two altered versions of the script - both run fine with a current Scribus version. The only difference between them is that one is latin-1 encoded and one is utf-8 encoded.

The changes required were:
   Put encoding specifier in the right place (second line)
   Explicitly create unicode strings in mois list by using the u'' constructor
   Don't try to use unicode names for objects, because they're not supported by
      the scripter - instead use English month name abbreviations.

This version will _not_ run on an older Scribus.

2004-11-12 12:33

 

calendrier.py.gz (1,827 bytes)

2004-11-12 12:33

 

calendrier_utf.py.gz (1,837 bytes)

zitter

2004-11-15 09:50

reporter   ~0002890

OK... It works :-)

2004-11-16 10:05

 

encoding_warning_fix.diff (1,836 bytes)   
Index: scriptplugin.cpp
===================================================================
RCS file: /cvs/Scribus/scribus/plugins/scriptplugin/scriptplugin.cpp,v
retrieving revision 1.42
diff -u -r1.42 scriptplugin.cpp
--- scriptplugin.cpp	14 Nov 2004 15:56:18 -0000	1.42
+++ scriptplugin.cpp	16 Nov 2004 10:02:44 -0000
@@ -242,10 +242,18 @@
 	{
 		PyObject* globals = PyModule_GetDict(m);
 		// Build the Python code to run the script
-		QString cm = QString("import sys,StringIO,traceback\n");
+		QString cm = QString("import sys\n");
+		cm        += QString("import traceback\n");
+		cm        += QString("import StringIO\n");
+		cm        += QString("import warnings\n");
+		// Put the dir containing the script into the module search path
 		cm        += QString("sys.path[0] = \"%1\"\n").arg(fi.dirPath(true));
 		cm        += QString("try:\n");
+		// This next line is a hack for some weird issues in warnings.py, see bug 1287 and warnings.py line 46-53.
+		cm        += QString("    __file__ = '%1'\n").arg(fileName);
+		// Run the darn thing:
 		cm        += QString("    execfile(\"%1\")\n").arg(fileName);
+		// Prevent it from terminating Scribus when it exits
 		cm        += QString("except SystemExit:\n");
 		cm        += QString("    pass\n");
 		// Capture the text of any other exception that's raised by the interpreter
@@ -456,7 +464,7 @@
 // is provided to direct the user to the correct function.
 // By default the warning gets output only on the first use of the function in a given
 // interpreter instance, but user scripts can change this.
-void deprecatedFunctionAlias(PyObject* scribusdict, char* oldName, char* newName)
+void deprecatedFunctionAlias(PyObject* scribusdict, const char* oldName, const char* newName)
 {
 	// Build the Python code to create the wrapper function
 	QString wrapperFunc = "";
encoding_warning_fix.diff (1,836 bytes)   

ringerc

2004-11-16 10:09

reporter   ~0002895

Bug fix for missing/incorrect encodings line crash attached - encoding_warning_fix.diff. Scribus will now print a warning to stderr like it should (instead of aborting the script with a weird exception). I wish I understood what was going on here a bit better in particular why 'sys' lacks an 'argv' attribute at that particular point in execution. This change does work around the problem, however, and results in the desired behaviour. There should be no side-effects.

This diff also changes deprecatedFunctionAlias to take 'const char*' arguments, knocking out some 200 warnings in the scripter code with no behavioural change at all.

ringerc

2004-11-21 14:36

reporter   ~0002926

Petr - the attatched patch should take care of the issue this bug was initially reporting. I'm re-assigning it to you now that I've tracked down the issue.

The second problem was really a script programming issue, though I suspect some more thought about encodings may be needed in the scripter. For now, users should make sure to always create Unicode strings ( u'blah' ), especially if they use non-unicode characters. Python will eventually go all-Unicode anyway.

ringerc

2004-12-08 11:42

reporter   ~0003023

Petr has applied the patch to fix the issue this bug was reported for, reassigning to me. I'm closing this bug now - the remaining unicode issues are separate, and will be taken care of during the unicode handling audit for 1.3 scripter.

ringerc

2004-12-08 11:42

reporter   ~0003024

zitter, please close this bug if you're satisfied that things are all sorted out now.

cbradney

2004-12-08 19:58

administrator   ~0003055

CR, do we have the new file in CVS?

ringerc

2004-12-09 05:11

reporter   ~0003067

No, the file is not in CVS. The script's author has not submitted the original script or updated version for inclusion. I have spoken with him, however, and new versions of the script have been mentioned on the mailing list - with the right encoding declaration.

It might be a good idea to offer the updated file in the downloads section of scribus.net though.

I'll get in touch with the author.

cbradney

2004-12-09 07:05

administrator   ~0003069

Yours is better, lets put it in instead.

ringerc

2005-01-02 12:08

reporter   ~0003317

Assigning to you, cbradney. The attached script needs to replace the calendrier.py currently on the downloads section of the website. No changes are needed for scribus, 1.2.1 or otherwise.

Issue History

Date Modified Username Field Change
2004-11-08 10:35 zitter New Issue
2004-11-08 10:47 plinnell Relationship added related to 0001283
2004-11-08 10:56 ringerc Note Added: 0002810
2004-11-08 11:19 ringerc Note Added: 0002811
2004-11-08 12:34 ringerc Note Added: 0002812
2004-11-10 13:31 ringerc Note Added: 0002854
2004-11-10 14:47 zitter Note Added: 0002856
2004-11-10 14:54 cbradney Note Added: 0002857
2004-11-10 15:01 ringerc Note Added: 0002858
2004-11-10 15:26 zitter Note Added: 0002860
2004-11-10 16:12 ringerc Note Added: 0002864
2004-11-10 16:14 ringerc Note Edited: 0002864
2004-11-10 21:32 zitter Note Added: 0002871
2004-11-11 02:40 ringerc Note Added: 0002873
2004-11-11 08:30 ringerc Note Added: 0002877
2004-11-11 11:10 ringerc Note Added: 0002878
2004-11-11 11:32 ringerc Note Added: 0002879
2004-11-11 11:33 zitter Note Added: 0002880
2004-11-12 12:32 ringerc Note Added: 0002885
2004-11-12 12:33 ringerc File Added: calendrier.py.gz
2004-11-12 12:33 ringerc File Added: calendrier_utf.py.gz
2004-11-15 09:50 zitter Note Added: 0002890
2004-11-16 10:05 ringerc File Added: encoding_warning_fix.diff
2004-11-16 10:09 ringerc Note Added: 0002895
2004-11-16 10:10 ringerc Status new => assigned
2004-11-16 10:10 ringerc Assigned To => ringerc
2004-11-16 10:15 ringerc Note Edited: 0002879
2004-11-21 14:33 ringerc Assigned To ringerc => subik
2004-11-21 14:36 ringerc Note Added: 0002926
2004-11-21 23:59 cbradney Relationship added has duplicate 0001336
2004-12-08 11:40 ringerc Assigned To subik => ringerc
2004-12-08 11:42 ringerc Note Added: 0003023
2004-12-08 11:42 ringerc Status assigned => resolved
2004-12-08 11:42 ringerc Fixed in Version => 1.2.1cvs
2004-12-08 11:42 ringerc Resolution open => fixed
2004-12-08 11:42 ringerc Note Added: 0003024
2004-12-08 19:58 cbradney Note Added: 0003055
2004-12-09 05:11 ringerc Status resolved => feedback
2004-12-09 05:11 ringerc Resolution fixed => reopened
2004-12-09 05:11 ringerc Note Added: 0003067
2004-12-09 07:05 cbradney Note Added: 0003069
2004-12-09 08:58 ringerc Status feedback => acknowledged
2005-01-02 12:08 ringerc Note Added: 0003317
2005-01-02 12:08 ringerc Assigned To ringerc => cbradney
2005-01-02 12:08 ringerc Status acknowledged => assigned
2005-01-02 12:08 ringerc Resolution reopened => open
2005-01-02 12:08 ringerc Product Version 1.2.1cvs =>
2005-01-02 12:08 ringerc Fixed in Version 1.2.1cvs =>
2005-01-03 22:36 plinnell Assigned To cbradney => plinnell
2005-01-03 22:56 cbradney Status assigned => resolved
2005-01-03 22:56 cbradney Fixed in Version => 1.2.1cvs
2005-01-03 22:56 cbradney Resolution open => fixed
2005-01-04 12:20 cbradney Status resolved => closed
2006-05-13 21:52 christoph_s Relationship added child of 0003813