Skip to content

Commit a1d70af

Browse files
committed
Modernize code
- remove python 2 support - add github workflows - remove disable unicode - cleanup compat file - modernize setup - use pep517 Change-Id: Ic38dbf478046cec5d0815b468f0c235b4ea5e20c
1 parent 09cf4f6 commit a1d70af

57 files changed

Lines changed: 611 additions & 1327 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/run-on-pr.yaml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Run tests on a pr
2+
3+
on:
4+
# run on pull request to master excluding changes that are only on doc or example folders
5+
pull_request:
6+
branches:
7+
- master
8+
paths-ignore:
9+
- "doc/**"
10+
11+
jobs:
12+
run-test:
13+
name: ${{ matrix.python-version }}-${{ matrix.os }}
14+
runs-on: ${{ matrix.os }}
15+
strategy:
16+
# run this job using this matrix
17+
matrix:
18+
os:
19+
- "ubuntu-latest"
20+
python-version:
21+
- "3.9"
22+
23+
fail-fast: false
24+
25+
# steps to run in each job. Some are github actions, others run shell commands
26+
steps:
27+
- name: Checkout repo
28+
uses: actions/checkout@v2
29+
30+
- name: Set up python
31+
uses: actions/setup-python@v2
32+
with:
33+
python-version: ${{ matrix.python-version }}
34+
architecture: ${{ matrix.architecture }}
35+
36+
- name: Install dependencies
37+
run: |
38+
python -m pip install --upgrade pip
39+
pip install --upgrade tox setuptools
40+
pip list
41+
42+
- name: Run tests
43+
run: tox

.github/workflows/run-test.yaml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: Run tests
2+
3+
on:
4+
# run on push in master or rel_* branches excluding changes are only on doc or example folders
5+
push:
6+
branches:
7+
- master
8+
- "rel_*"
9+
# branches used to test the workflow
10+
- "workflow_test_*"
11+
paths-ignore:
12+
- "docs/**"
13+
14+
jobs:
15+
run-test:
16+
name: ${{ matrix.python-version }}-${{ matrix.os }}
17+
runs-on: ${{ matrix.os }}
18+
strategy:
19+
# run this job using this matrix
20+
matrix:
21+
os:
22+
- "ubuntu-latest"
23+
- "windows-latest"
24+
- "macos-latest"
25+
python-version:
26+
- "3.6"
27+
- "3.7"
28+
- "3.8"
29+
- "3.9"
30+
31+
fail-fast: false
32+
33+
# steps to run in each job. Some are github actions, others run shell commands
34+
steps:
35+
- name: Checkout repo
36+
uses: actions/checkout@v2
37+
38+
- name: Set up python
39+
uses: actions/setup-python@v2
40+
with:
41+
python-version: ${{ matrix.python-version }}
42+
architecture: ${{ matrix.architecture }}
43+
44+
- name: Install dependencies
45+
run: |
46+
python -m pip install --upgrade pip
47+
pip install --upgrade tox setuptools
48+
pip list
49+
50+
- name: Run tests
51+
run: tox

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
/man
1414
.tox/
1515
.cache/
16+
.vscode

.travis.yml

Lines changed: 0 additions & 10 deletions
This file was deleted.

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright 2006-2020 the Mako authors and contributors <see AUTHORS file>.
1+
Copyright 2006-2021 the Mako authors and contributors <see AUTHORS file>.
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy of
44
this software and associated documentation files (the "Software"), to deal in

doc/build/filtering.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ In addition to the ``expression_filter`` argument, the
101101
:class:`.TemplateLookup` can specify filtering for all expression tags
102102
at the programmatic level. This array-based argument, when given
103103
its default argument of ``None``, will be internally set to
104-
``["unicode"]`` (or ``["str"]`` on Python 3), except when
105-
``disable_unicode=True`` is set in which case it defaults to
106-
``["str"]``:
104+
``["unicode"]`` (or ``["str"]`` on Python 3):
107105

108106
.. sourcecode:: python
109107

doc/build/unicode.rst

Lines changed: 2 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,7 @@ the storage format for strings.
6262

6363
The "pass through encoded data" scheme is what template
6464
languages like Cheetah and earlier versions of Myghty do by
65-
default. Mako as of version 0.2 also supports this mode of
66-
operation when using Python 2, using the ``disable_unicode=True``
67-
flag. However, when using Mako in its default mode of
68-
unicode-aware, it requires explicitness when dealing with
69-
non-ASCII encodings. Additionally, if you ever need to handle
70-
unicode strings and other kinds of encoding conversions more
71-
intelligently, the usage of raw byte-strings quickly becomes a
72-
nightmare, since you are sending the Python interpreter
73-
collections of bytes for which it can make no intelligent
74-
decisions with regards to encoding. In Python 3 Mako only allows
65+
default. In Python 3 Mako only allows
7566
usage of native, unicode strings.
7667

7768
In normal Mako operation, all parsed template constructs and
@@ -255,82 +246,5 @@ which cannot handle encoding of non-ASCII ``unicode`` objects
255246
buffering. Otherwise, a custom Mako class called
256247
``FastEncodingBuffer`` is used, which essentially is a super
257248
dumbed-down version of ``StringIO`` that gathers all strings into
258-
a list and uses ``u''.join(elements)`` to produce the final output
249+
a list and uses ``''.join(elements)`` to produce the final output
259250
-- it's markedly faster than ``StringIO``.
260-
261-
.. _unicode_disabled:
262-
263-
Saying to Heck with It: Disabling the Usage of Unicode Entirely
264-
===============================================================
265-
266-
Some segments of Mako's userbase choose to make no usage of
267-
Unicode whatsoever, and instead would prefer the "pass through"
268-
approach; all string expressions in their templates return
269-
encoded byte-strings, and they would like these strings to pass
270-
right through. The only advantage to this approach is that
271-
templates need not use ``u""`` for literal strings; there's an
272-
arguable speed improvement as well since raw byte-strings
273-
generally perform slightly faster than unicode objects in
274-
Python. For these users, assuming they're sticking with Python
275-
2, they can hit the ``disable_unicode=True`` flag as so:
276-
277-
.. sourcecode:: python
278-
279-
# -*- coding:utf-8 -*-
280-
from mako.template import Template
281-
282-
t = Template("drôle de petite voix m’a réveillé.", disable_unicode=True, input_encoding='utf-8')
283-
print(t.code)
284-
285-
The ``disable_unicode`` mode is strictly a Python 2 thing. It is
286-
not supported at all in Python 3.
287-
288-
The generated module source code will contain elements like
289-
these:
290-
291-
.. sourcecode:: python
292-
293-
# -*- coding:utf-8 -*-
294-
# ...more generated code ...
295-
296-
def render_body(context,**pageargs):
297-
context.caller_stack.push_frame()
298-
try:
299-
__M_locals = dict(pageargs=pageargs)
300-
# SOURCE LINE 1
301-
context.write('dr\xc3\xb4le de petite voix m\xe2\x80\x99a r\xc3\xa9veill\xc3\xa9.')
302-
return ''
303-
finally:
304-
context.caller_stack.pop_frame()
305-
306-
Where above that the string literal used within :meth:`.Context.write`
307-
is a regular byte-string.
308-
309-
When ``disable_unicode=True`` is turned on, the ``default_filters``
310-
argument which normally defaults to ``["unicode"]`` now defaults
311-
to ``["str"]`` instead. Setting ``default_filters`` to the empty list
312-
``[]`` can remove the overhead of the ``str`` call. Also, in this
313-
mode you **cannot** safely call :meth:`~.Template.render_unicode` -- you'll get
314-
unicode/decode errors.
315-
316-
The ``h`` filter (HTML escape) uses a less performant pure Python
317-
escape function in non-unicode mode. This because
318-
MarkupSafe only supports Python unicode objects for non-ASCII
319-
strings.
320-
321-
.. versionchanged:: 0.3.4
322-
In prior versions, it used ``cgi.escape()``, which has been replaced
323-
with a function that also escapes single quotes.
324-
325-
Rules for using ``disable_unicode=True``
326-
----------------------------------------
327-
328-
* Don't use this mode unless you really, really want to and you
329-
absolutely understand what you're doing.
330-
* Don't use this option just because you don't want to learn to
331-
use Unicode properly; we aren't supporting user issues in this
332-
mode of operation. We will however offer generous help for the
333-
vast majority of users who stick to the Unicode program.
334-
* Python 3 is unicode by default, and the flag is not available
335-
when running on Python 3.
336-
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.. change::
2+
:tags: py3k
3+
4+
Removed ``disable_unicode`` flag, that's no longer used in Python 3.

doc/build/unreleased/py2.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.. change::
2+
:tags: py3k
3+
4+
Remove support for Python 2. Mako now requires Python >= 3.6

examples/bench/basic.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,10 @@
2828
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
2929
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030

31-
from cgi import escape
32-
import os
33-
try:
34-
from StringIO import StringIO
35-
except ImportError:
36-
from io import StringIO
31+
from io import StringIO
3732
import sys
3833
import timeit
3934

40-
def u(stringlit):
41-
if sys.version_info >= (3,):
42-
return stringlit
43-
else:
44-
return stringlit.decode('latin1')
4535

4636
__all__ = ['mako', 'mako_inheritance', 'jinja2', 'jinja2_inheritance',
4737
'cheetah', 'django', 'myghty', 'genshi', 'kid']
@@ -50,7 +40,6 @@ def u(stringlit):
5040
TITLE = 'Just a test'
5141
USER = 'joe'
5242
ITEMS = ['Number %d' % num for num in range(1, 15)]
53-
U_ITEMS = [u(item) for item in ITEMS]
5443

5544
def genshi(dirname, verbose=False):
5645
from genshi.template import TemplateLoader
@@ -79,11 +68,10 @@ def render():
7968
def mako(dirname, verbose=False):
8069
from mako.template import Template
8170
from mako.lookup import TemplateLookup
82-
disable_unicode = (sys.version_info < (3,))
83-
lookup = TemplateLookup(directories=[dirname], filesystem_checks=False, disable_unicode=disable_unicode)
71+
lookup = TemplateLookup(directories=[dirname], filesystem_checks=False)
8472
template = lookup.get_template('template.html')
8573
def render():
86-
return template.render(title=TITLE, user=USER, list_items=U_ITEMS)
74+
return template.render(title=TITLE, user=USER, list_items=ITEMS)
8775
if verbose:
8876
print(template.code + " " + render())
8977
return render
@@ -94,7 +82,7 @@ def jinja2(dirname, verbose=False):
9482
env = Environment(loader=FileSystemLoader(dirname))
9583
template = env.get_template('template.html')
9684
def render():
97-
return template.render(title=TITLE, user=USER, list_items=U_ITEMS)
85+
return template.render(title=TITLE, user=USER, list_items=ITEMS)
9886
if verbose:
9987
print(render())
10088
return render
@@ -106,7 +94,7 @@ def cheetah(dirname, verbose=False):
10694
template = Template(file=filename)
10795
def render():
10896
template.__dict__.update({'title': TITLE, 'user': USER,
109-
'list_items': U_ITEMS})
97+
'list_items': ITEMS})
11098
return template.respond()
11199

112100
if verbose:

0 commit comments

Comments
 (0)