Edgewall Software

Ticket #221 (closed defect: fixed)

Opened 7 years ago

Last modified 7 years ago

<?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

genshi-py24-star-import.patch Download (2.0 KB) - added by aronacher 7 years ago.
genshi-py24-star-import-2.patch Download (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 Download (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

Change History

follow-up: ↓ 2   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

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.

  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'

follow-up: ↓ 5   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.

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.

  Changed 7 years ago by cmlenz

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

  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

  Changed 7 years ago by aronacher

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

  Changed 7 years ago by cmlenz

  • status changed from new to closed
  • resolution set to fixed
  • component changed from General to Template processing

Patch applied in [855]. Thanks Armin!

  Changed 7 years ago by dan.colascione@…

  • status changed from closed to reopened
  • resolution fixed deleted

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

  Changed 7 years ago by athomas

  • status changed from reopened to closed
  • resolution set to fixed

Hopefully r857 fixes this for Python 2.3.

  Changed 7 years ago by cmlenz

Yet more fixes in [860].

Add/Change #221 (<?python ... ?> is broken --- "from module import *" doesn't work)

Author


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


Change Properties
<Author field>
Action
as closed
The resolution will be deleted. Next status will be 'reopened'
 
Note: See TracTickets for help on using tickets.