variables starting with one or more underscores are not exported
authorArmin Ronacher <armin.ronacher@active-4.com>
Thu, 22 May 2008 22:51:58 +0000 (00:51 +0200)
committerArmin Ronacher <armin.ronacher@active-4.com>
Thu, 22 May 2008 22:51:58 +0000 (00:51 +0200)
--HG--
branch : trunk

docs/templates.rst
jinja2/compiler.py
jinja2/parser.py

index 63e4294cbe431f4c600c0afcb509dec386dd6e39..1c1b8a7bf846236737a93906a99966a9d67dbe20 100644 (file)
@@ -706,6 +706,9 @@ namespace::
     </dl>
     <p>{{ textarea('comment') }}</p>
 
+Macros and variables starting with one ore more underscores are private and
+cannot be imported.
+
 
 .. _import-visibility:
 
index cc1e4ae10d7137bc6de00df1f16f469099e6c867..8fcb30f2d336af5f4f1c99aa4b700c4df4a164f0 100644 (file)
@@ -624,10 +624,10 @@ class CodeGenerator(NodeVisitor):
         for arg in node.defaults:
             self.visit(arg, frame)
             self.write(', ')
-        self.write('), %s, %s, %s)' % (
-            frame.accesses_kwargs and '1' or '0',
-            frame.accesses_varargs and '1' or '0',
-            frame.accesses_caller and '1' or '0'
+        self.write('), %r, %r, %r)' % (
+            bool(frame.accesses_kwargs),
+            bool(frame.accesses_varargs),
+            bool(frame.accesses_caller)
         ))
 
     # -- Statement Visitors
@@ -815,7 +815,7 @@ class CodeGenerator(NodeVisitor):
             self.write('make_module(context.parent, True)')
         else:
             self.write('module')
-        if frame.toplevel and not node.target.startswith('__'):
+        if frame.toplevel and not node.target.startswith('_'):
             self.writeline('context.exported_vars.discard(%r)' % node.target)
 
     def visit_FromImport(self, node, frame):
@@ -848,7 +848,7 @@ class CodeGenerator(NodeVisitor):
             self.outdent()
             if frame.toplevel:
                 var_names.append(alias)
-                if not alias.startswith('__'):
+                if not alias.startswith('_'):
                     discarded_names.append(alias)
 
         if var_names:
@@ -996,7 +996,7 @@ class CodeGenerator(NodeVisitor):
         macro_frame = self.macro_body(node, frame)
         self.newline()
         if frame.toplevel:
-            if not node.name.startswith('__'):
+            if not node.name.startswith('_'):
                 self.write('context.exported_vars.add(%r)' % node.name)
             self.writeline('context.vars[%r] = ' % node.name)
         self.write('l_%s = ' % node.name)
@@ -1149,7 +1149,7 @@ class CodeGenerator(NodeVisitor):
         # make sure toplevel assignments are added to the context.
         if frame.toplevel:
             public_names = [x for x in assignment_frame.assigned_names
-                            if not x.startswith('__')]
+                            if not x.startswith('_')]
             if len(assignment_frame.assigned_names) == 1:
                 name = iter(assignment_frame.assigned_names).next()
                 self.writeline('context.vars[%r] = l_%s' % (name, name))
index a4ce4915349b66c5903e8a1f0d3f050cf00d0216..86ee570aa7088c80fad919ab31d68dde8979acb9 100644 (file)
@@ -199,8 +199,8 @@ class Parser(object):
                 if parse_context():
                     break
                 target = self.parse_assign_target(name_only=True)
-                if target.name.startswith('__'):
-                    self.fail('names starting with two underscores can not '
+                if target.name.startswith('_'):
+                    self.fail('names starting with an underline can not '
                               'be imported', target.lineno,
                               exc=TemplateAssertionError)
                 if self.stream.skip_if('name:as'):