From 903d1682d49877930828d2ea7ffff7e3e9c8291f Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Fri, 23 May 2008 00:51:58 +0200 Subject: [PATCH] variables starting with one or more underscores are not exported --HG-- branch : trunk --- docs/templates.rst | 3 +++ jinja2/compiler.py | 16 ++++++++-------- jinja2/parser.py | 4 ++-- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/docs/templates.rst b/docs/templates.rst index 63e4294..1c1b8a7 100644 --- a/docs/templates.rst +++ b/docs/templates.rst @@ -706,6 +706,9 @@ namespace::

{{ textarea('comment') }}

+Macros and variables starting with one ore more underscores are private and +cannot be imported. + .. _import-visibility: diff --git a/jinja2/compiler.py b/jinja2/compiler.py index cc1e4ae..8fcb30f 100644 --- a/jinja2/compiler.py +++ b/jinja2/compiler.py @@ -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)) diff --git a/jinja2/parser.py b/jinja2/parser.py index a4ce491..86ee570 100644 --- a/jinja2/parser.py +++ b/jinja2/parser.py @@ -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'): -- 2.26.2