Commits

bentrofatter  committed 02ca9c5 Draft

Finalized future_imports feature.

Changed keyword argument from futures to future_imports for clarity.
Added docstring describing use.

  • Participants
  • Parent commits 481b6f2

Comments (0)

Files changed (3)

File mako/lookup.py

                         buffer_filters=(),
                         strict_undefined=False,
                         imports=None,
+                        future_imports=None,
                         enable_loop=True,
                         input_encoding=None,
-                        preprocessor=None,
-                        futures=None):
+                        preprocessor=None):
 
         self.directories = [posixpath.normpath(d) for d in
                             util.to_list(directories, ())
             'buffer_filters':buffer_filters,
             'strict_undefined':strict_undefined,
             'imports':imports,
+            'future_imports':future_imports,
             'enable_loop':enable_loop,
-            'preprocessor':preprocessor,
-            'futures': futures}
+            'preprocessor':preprocessor}
 
         if collection_size == -1:
             self._collection = {}

File mako/template.py

      preamble of all generated Python modules. See the example
      in :ref:`filtering_default_filters`.
 
+    :param future_imports: String list of names to import from `__future__`.
+     These will be concatenated into a comma-separated string and inserted
+     into the beginning of the template, e.g. ``futures_imports=['FOO',
+     'BAR']`` results in ``from __future__ import FOO, BAR``.  If you're
+     interested in using features like the new division operator, you must
+     use future_imports to convey that to the renderer, as otherwise the
+     import will not appear as the first executed statement in the generated
+     code and will therefore not have the desired effect.
+
     :param input_encoding: Encoding of the template's source code.  Can
      be used in lieu of the coding comment. See
      :ref:`usage_unicode` as well as :ref:`unicode_toplevel` for
                     buffer_filters=(),
                     strict_undefined=False,
                     imports=None,
+                    future_imports=None,
                     enable_loop=True,
-                    preprocessor=None,
-                    futures=None):
+                    preprocessor=None):
         if uri:
             self.module_id = re.sub(r'\W', "_", uri)
             self.uri = uri
         self.enable_loop = enable_loop
         self.strict_undefined = strict_undefined
         self.module_writer = module_writer
-        self.futures = futures
 
         if util.py3k and disable_unicode:
             raise exceptions.UnsupportedError(
         self.buffer_filters = buffer_filters
 
         self.imports = imports
+        self.future_imports = future_imports
         self.preprocessor = preprocessor
 
         # if plain text, compile code in memory only
                             default_filters=template.default_filters,
                             buffer_filters=template.buffer_filters,
                             imports=template.imports,
-                            future_imports=template.futures,
+                            future_imports=template.future_imports,
                             source_encoding=lexer.encoding,
                             generate_magic_comment=generate_magic_comment,
                             disable_unicode=template.disable_unicode,

File test/test_template.py

 class FuturesTest(TemplateTest):
 
     def test_future_import(self):
-        t = Template("${ x / y }", futures=["division"])
+        t = Template("${ x / y }", future_imports=["division"])
         assert result_lines(t.render(x=12, y=5)) == ["2.4"]