Edgewall Software
Modify

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#221 closed defect (fixed)

<?python ... ?> is broken --- "from module import *" doesn't work

Reported by: mats_a@… Owned by: cmlenz
Priority: major Milestone: 0.5
Component: Template processing Version: devel
Keywords: Cc:

Description

Goal: Write python code with "from modulename import *"

_

Problem: Something fails with the following error:

File "/home/username/.../mytemplate.html", line 123, in <Suite u'import turbogears as tg ...'>

print "this is the line above the error"

SystemError?: Objects/dictobject.c:599: bad argument to internal function

Reproduction:

  • Make a template
  • Add this to it:

<?python from sys import * ?> _

Possible cause?: Perhaps this may be occurring because genshi seems to be eval'ing lines one by one... and normally python doesn't allow an eval("import sys/os/etc.")... and perhaps Genshi is doing something behind-the-scenes to make it work... _

Suggested priority if reproducible: very high / blocker: prevents one from using very common python code

Attachments (3)

genshi-py24-star-import.patch (2.0 KB) - added by aronacher 7 years ago.
genshi-py24-star-import-2.patch (2.1 KB) - added by aronacher 7 years ago.
alternative patch that doesn't do weird things with the context
genshi-py24-star-import-3.patch (2.5 KB) - added by aronacher 7 years ago.
This patch doesn't expect a context object any more and fixes a bug with modules without all deifned. However for performance reasons it implements update() on Context

Download all attachments as: .zip

Change History (15)

comment:1 follow-up: Changed 7 years ago by mats_a@…

[apologies for the bad formatting... did not preview the wiki formatting; would be great if there was a plaintext / HTML-PRE-tag edit mode]

Goal:

Write python code with "from modulename import *"


Problem:

Something fails with the following error:

File "/home/username/.../mytemplate.html", line 123, in <Suite u'import turbogears as tg ...'>

print "this is the line above the error"

SystemError?: Objects/dictobject.c:599: bad argument to internal function


Reproduction:

  • Make a template
  • Add this to it:

<?python

from sys import *

?>


Possible cause?:

Perhaps this may be occurring because genshi seems to be eval'ing lines one by one... and normally python doesn't allow an eval("import sys/os/etc.")... and perhaps Genshi is doing something behind-the-scenes to make it work...


Suggested priority if reproducible:

very high / blocker: prevents one from using very common python code

comment:2 in reply to: ↑ 1 Changed 7 years ago by anonymous

Replying to mats_a@mit.edu:

SystemError?: Objects/dictobject.c:599: bad argument to internal function

Are you using Python 2.3?

Reproduction:

  • Make a template
  • Add this to it:

<?python from sys import * ?>

The following script works for me:

from genshi.template import MarkupTemplate

tmpl = MarkupTemplate('''
<?python
  from sys import *
?>
<info>
  $version
</info>
''')
print tmpl.generate()

Perhaps this may be occurring because genshi seems to be eval'ing lines one by one... and normally python doesn't allow an eval("import sys/os/etc.")... and perhaps Genshi is doing something behind-the-scenes to make it work...

Genshi does not eval code line by line. That would not even work.

comment:3 Changed 7 years ago by anonymous

Definitely not using Python 2.3...

That script definitely does not work for me on the command line. Traceback is:

Traceback (most recent call last):

File "<stdin>", line 1, in <module> File "build/bdist.linux-i686/egg/genshi/core.py", line 243, in str File "build/bdist.linux-i686/egg/genshi/core.py", line 179, in render File "build/bdist.linux-i686/egg/genshi/output.py", line 60, in encode File "build/bdist.linux-i686/egg/genshi/output.py", line 203, in call File "build/bdist.linux-i686/egg/genshi/output.py", line 566, in call File "build/bdist.linux-i686/egg/genshi/output.py", line 672, in call File "build/bdist.linux-i686/egg/genshi/output.py", line 506, in call File "build/bdist.linux-i686/egg/genshi/core.py", line 264, in _ensure File "build/bdist.linux-i686/egg/genshi/template/markup.py", line 246, in _match File "build/bdist.linux-i686/egg/genshi/template/base.py", line 541, in _exec File "build/bdist.linux-i686/egg/genshi/template/base.py", line 298, in _exec_suite File "build/bdist.linux-i686/egg/genshi/template/eval.py", line 179, in execute File "<string>", line 2, in <Suite u'from sys import *'>

SystemError?: Objects/dictobject.c:599: bad argument to internal function

genshi.version

'0.5dev-r832'

comment:4 follow-up: Changed 7 years ago by anonymous

I was able to reproduce this with the test code in comment 2. This is with r805 of Genshi with Python 2.4.3. Let my try updating to a more recent revision and see if that helps.

comment:5 in reply to: ↑ 4 Changed 7 years ago by anonymous

Replying to anonymous:

I was able to reproduce this with the test code in comment 2. This is with r805 of Genshi with Python 2.4.3. Let my try updating to a more recent revision and see if that helps.

Same thing with r852.

comment:6 Changed 7 years ago by cmlenz

Darn, looks like a bug in Python 2.4. Looking into it.

comment:7 Changed 7 years ago by aronacher

I have a patch for that that works around the problem for affected Python versions. The problem happens because in Python 2.4 the star-import function is directly calling the dict interface instead of using the corresponding mapping method.

What the patch does is testing for the bug and if it happens it adds a _star_import_compat method to the context and rewrites the import statement to a call to that method which then performs the update.

Changed 7 years ago by aronacher

Changed 7 years ago by aronacher

alternative patch that doesn't do weird things with the context

Changed 7 years ago by aronacher

This patch doesn't expect a context object any more and fixes a bug with modules without all deifned. However for performance reasons it implements update() on Context

comment:8 Changed 7 years ago by aronacher

My latest version implements update() on Context as side-effect.

comment:9 Changed 7 years ago by cmlenz

  • Component changed from General to Template processing
  • Resolution set to fixed
  • Status changed from new to closed

Patch applied in [855]. Thanks Armin!

comment:10 Changed 7 years ago by dan.colascione@…

  • Resolution fixed deleted
  • Status changed from closed to reopened

The fix uses a generator expression and breaks Python 2.3 support.

comment:11 Changed 7 years ago by athomas

  • Resolution set to fixed
  • Status changed from reopened to closed

Hopefully r857 fixes this for Python 2.3.

comment:12 Changed 7 years ago by cmlenz

Yet more fixes in [860].

Add Comment

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain cmlenz.
The resolution will be deleted. Next status will be 'reopened'.
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.