Re: NameError when generating Python code

"R. Tyler Ballance" <[email protected]>
Newsgroups gmane.comp.python.cheetah
Message-ID <[email protected]>
On Thu, Apr 16, 2009 at 06:17:51PM +0100, Martin Skøtt wrote:
>    Hi,
> 
>    I'm trying to use Cheetah 2.1.0.1 to generate some Python stub classes,
>    but I have encountered a problem which to me looks like Cheetah is
>    actually evaluating the code I generate. Here is the error I get: (Python
>    2.6 on Win XP):
>    Traceback (most recent call last):
>      File "stubGen.py", line 64, in <module>
>        print(t)
>      File "C:\Python26\Lib\site-
>    packages\Cheetah\Template.py", line 982, in __str__
>        def __str__(self): return getattr(self, mainMethName)()
>      File "_HOME_Generator_src_templates_class_tmpl.py", line 89, in respond
>    NameError: global name 'BaseClass' is not defined
> 
>    Here is the template I'm using:
>    from BaseClass import BaseClass
>    class $module.module(BaseClass):
>        pass
> 
>    If I comment out (##) the two first lines it works fine. Any suggestions
>    on how to solve this?

I'm impressed! You found the same issue we found tuesday ;)

Anyways, the cause of the issue is the inline import support, part of
what allows Cheetah to do things like this:

	#set $myName = 'Pirate Jake'
	Hello $myName

ANd turn that into a runnable template is that the Cheetah compiler
considers everything "lying around" not in a defined method as part of
the "writeBody" metho, this kind of default uber-method.

So with your case, what's happening is that because the imports aren't
at the top of the fiule, the compiler/parser think that you're adding
imports into the writeBody() method instead of the module level itself.


The fix I think works is to ignore comments as part of the method's body
and ignore them for the sake of imports. (this should go into the final
v2.1.1 I'll release today)

Are you familiar with Git enough to where I could convince you to beta
test the patch this afternoon?


Cheers
-- 
-R. Tyler Ballance
Slide, Inc.

------------------------------------------------------------------------------
Stay on top of everything new and different, both inside and 
around Java (TM) technology - register by April 22, and save
$200 on the JavaOne (SM) conference, June 2-5, 2009, San Francisco.
300 plus technical and hands-on sessions. Register today. 
Use priority code J9JMT32. http://p.sf.net/sfu/p

_______________________________________________
Cheetahtemplate-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
signature.asc (application/pgp-signature, 197 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)

iEYEARECAAYFAknniUMACgkQFCbH3D9R4W+dMACgq/gbVDnUteHzvMeVQeY0DpEP
cEAAoJGZN+2hKaTV1m0IWRxLp5oHl3vJ
=DrcR
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.