Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Be my human compiler: What is wrong with this Python 2.5 code?

My framework is raising a syntax error when I try to execute this code:

    from django.template import Template, TemplateSyntaxError
    try:
        Template(value)
    except TemplateSyntaxError as error:
        raise forms.ValidationError(error)
    return value

And here's the error:

    from template_field import TemplateTextField, TemplateCharField
      File "C:\django\internal\..\internal\cmsplugins\form_designer\template_field.py", line 14
        except TemplateSyntaxError as error:
                                    ^
    SyntaxError: invalid syntax

What's going on?

like image 651
Brian D Avatar asked Aug 12 '10 19:08

Brian D


People also ask

Does python turn into C?

Python code can make calls directly into C modules. Those C modules can be either generic C libraries or libraries built specifically to work with Python. Cython generates the second kind of module: C libraries that talk to Python's internals, and that can be bundled with existing Python code.


2 Answers

The alternate syntax except SomeException as err is new in 2.6. You should use except SomeException, err in 2.5.

like image 185
Josh Lee Avatar answered Oct 02 '22 14:10

Josh Lee


You can't have an empty try block like that in Python. If you just want to do nothing in the block (for prototyping code, say), use the pass keyword:

from django.template import Template, TemplateSyntaxError
try:
    pass
except TemplateSyntaxError as error:
    Template(value)
    raise forms.ValidationError(error)
return value

Edit: This answers the original version of the question. I'll leave it up for posterity, but the question has now been edited, and @jleedev has the correct answer to the revised question.

like image 30
bcat Avatar answered Oct 02 '22 12:10

bcat