Skip to content

Commit

Permalink
Merge pull request #37 from mahmoud/drop-py2
Browse files Browse the repository at this point in the history
Finally dropping Python 2
  • Loading branch information
mahmoud authored Dec 17, 2023
2 parents 506cb91 + bdf7b42 commit 9c27ac1
Show file tree
Hide file tree
Showing 47 changed files with 95 additions and 200 deletions.
25 changes: 5 additions & 20 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ jobs:
fail-fast: false
matrix:
include:
- {name: Linux, python: '3.9', os: ubuntu-latest, tox: py39}
- {name: Windows, python: '3.9', os: windows-latest, tox: py39}
- {name: Mac, python: '3.9', os: macos-latest, tox: py39}
- {name: Linux, python: '3.11', os: ubuntu-latest, tox: py311}
- {name: Windows, python: '3.11', os: windows-latest, tox: py311}
- {name: Mac, python: '3.11', os: macos-latest, tox: py311}
- {name: '3.10', python: '3.10', os: ubuntu-latest, tox: py310}
- {name: '3.9', python: '3.9', os: ubuntu-latest, tox: py39}
- {name: '3.8', python: '3.8', os: ubuntu-latest, tox: py38}
- {name: '3.7', python: '3.7', os: ubuntu-latest, tox: py37}
- {name: 'PyPy2', python: 'pypy-2.7', os: ubuntu-latest, tox: pypy}
- {name: 'PyPy3', python: 'pypy-3.9', os: ubuntu-latest, tox: pypy3}
steps:
- uses: actions/checkout@v2
Expand All @@ -45,19 +46,3 @@ jobs:
key: pip|${{ runner.os }}|${{ matrix.python }}|${{ hashFiles('setup.py') }}|${{ hashFiles('requirements/*.txt') }}
- run: pip install tox
- run: tox -e ${{ matrix.tox }}
tests-py27:
name: '2.7'
runs-on: ubuntu-20.04
container:
image: python:2.7.18-buster
strategy:
fail-fast: false
steps:
- uses: actions/checkout@v2
- name: update pip
run: |
pip install -U wheel
pip install -U setuptools
python -m pip install -U pip
- run: pip install tox
- run: tox -e py27
3 changes: 1 addition & 2 deletions clastic/_version.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

from __future__ import unicode_literals

version_info = (21, 1, 2, '')
version_info = (23, 0, 0, 'dev')
__version__ = '.'.join(["%s" % part for part in version_info if part or part == 0])
11 changes: 1 addition & 10 deletions clastic/_werkzeug_serving.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
:license: BSD, see LICENSE for more details.
"""
from __future__ import with_statement

import os
import socket
Expand Down Expand Up @@ -69,7 +68,7 @@

import werkzeug
from werkzeug._internal import _log
from werkzeug._compat import iteritems, PY2, reraise, text_type, \
from werkzeug._compat import iteritems, reraise, text_type, \
wsgi_encoding_dance
from werkzeug.urls import url_parse, url_unquote
from werkzeug.exceptions import InternalServerError, BadRequest
Expand Down Expand Up @@ -602,14 +601,6 @@ def restart_with_reloader():
new_environ = os.environ.copy()
new_environ['WERKZEUG_RUN_MAIN'] = 'true'

# a weird bug on windows. sometimes unicode strings end up in the
# environment and subprocess.call does not like this, encode them
# to latin1 and continue.
if os.name == 'nt' and PY2:
for key, value in iteritems(new_environ):
if isinstance(value, text_type):
new_environ[key] = value.encode('iso-8859-1')

exit_code = subprocess.call(args, env=new_environ)
if exit_code != 3:
return exit_code
Expand Down
1 change: 0 additions & 1 deletion clastic/application.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import os
import itertools
Expand Down
1 change: 0 additions & 1 deletion clastic/contrib/obj_browser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import gc
import sys
Expand Down
1 change: 0 additions & 1 deletion clastic/contrib/webtop/top.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import unicode_literals

import os

Expand Down
1 change: 0 additions & 1 deletion clastic/meta.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import os
import sys
Expand Down
1 change: 0 additions & 1 deletion clastic/middleware/context.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import sys
if sys.version_info < (3,3,):
Expand Down
2 changes: 1 addition & 1 deletion clastic/middleware/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from werkzeug.utils import cached_property
from werkzeug.wrappers import BaseResponse

from ..sinter import make_chain, get_arg_names, PY3, compile_code
from ..sinter import make_chain, get_arg_names, compile_code

_INNER_NAME = 'next'

Expand Down
1 change: 0 additions & 1 deletion clastic/middleware/form.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import sys
if sys.version_info < (3,3,):
Expand Down
1 change: 0 additions & 1 deletion clastic/middleware/profile.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import cProfile
from pstats import Stats
Expand Down
1 change: 0 additions & 1 deletion clastic/render/ashes_templates.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from werkzeug.wrappers import Response

Expand Down
1 change: 0 additions & 1 deletion clastic/render/chameleon_templates.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals

import sys
try:
Expand Down
1 change: 0 additions & 1 deletion clastic/render/mako_templates.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import mako
from mako import exceptions
Expand Down
34 changes: 11 additions & 23 deletions clastic/render/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,20 @@
import sys
import itertools
from json import JSONEncoder
import sys
if sys.version_info < (3,3,):
from collections import Mapping, Sized, Iterable
else:
from collections.abc import Mapping, Sized, Iterable
from collections.abc import Mapping, Sized, Iterable

from werkzeug.wrappers import Response

from .tabular import TabularRender

PY2 = (sys.version_info[0] == 2)

try:
unicode
except NameError:
# py3
unicode = str


class ClasticJSONEncoder(JSONEncoder):
def __init__(self, **kw):
self.dev_mode = kw.pop('dev_mode', False)
kw.setdefault('skipkeys', True)
kw.setdefault('ensure_ascii', True)
kw.setdefault('indent', 2)
kw.setdefault('sort_keys', True)
if not PY2:
kw.pop('encoding', None)
kw.pop('encoding', None)
super(ClasticJSONEncoder, self).__init__(**kw)

def default(self, obj):
Expand Down Expand Up @@ -114,10 +100,12 @@ def __init__(self, **kwargs):
raise TypeError('unexpected keyword arguments: %r' % kwargs)

def render_response(self, context, request, _route):
if isinstance(context, (bytes, unicode)): # already serialized
if isinstance(context, str): # already serialized but not encoded
context = context.encode('utf8')
if isinstance(context, bytes): # already serialized and encoded
if self._guess_json(context):
return Response(context, mimetype="application/json")
elif '<html' in context[:168]:
elif b'<html' in context[:168]:
# based on the longest DOCTYPE I found in a brief search
return Response(context, mimetype="text/html")
else:
Expand Down Expand Up @@ -147,7 +135,7 @@ def _serialize_to_resp(self, context, request, _route):
return self.json_render(context)
elif resp_mime == 'text/html':
return self.tabular_render(context, _route)
return Response(unicode(context), mimetype="text/plain")
return Response(str(context), mimetype="text/plain")

@property
def _mime_format_map(self):
Expand All @@ -162,12 +150,12 @@ def mimetypes(self):
return self._format_mime_map.values()

@staticmethod
def _guess_json(text):
if not text:
def _guess_json(bytestr: bytes):
if not bytestr:
return False
elif text[0] == '{' and text[-1] == '}':
elif bytestr[0] == b'{' and bytestr[-1] == b'}':
return True
elif text[0] == '[' and text[-1] == ']':
elif bytestr[0] == b'[' and bytestr[-1] == b']':
return True
else:
return False
Expand Down
1 change: 0 additions & 1 deletion clastic/render/tabular.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import os
import re
Expand Down
1 change: 0 additions & 1 deletion clastic/server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function

from collections import deque
import os
Expand Down
12 changes: 1 addition & 11 deletions clastic/sinter.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
# -*- coding: utf-8 -*-

from __future__ import print_function

import sys
import types
import inspect
import hashlib
import linecache

from boltons import iterutils
from boltons.strutils import camel2under
from boltons.funcutils import FunctionBuilder

PY3 = (sys.version_info[0] == 3)
_VERBOSE = False
_INDENT = ' '

Expand All @@ -39,9 +35,6 @@ def get_fb(f, drop_self=True):


def get_callable_name(f):
if not PY3:
return f.__module__, f.func_name

path = []
if inspect.ismethod(f):
path.append(f.__self__.__class__.__name__)
Expand Down Expand Up @@ -158,10 +151,7 @@ def compile_code(code_str, name, env=None, verbose=_VERBOSE):
code = compile(code_str, unique_filename, 'single')
if verbose:
print(code_str)
if PY3:
exec(code, env)
else:
exec("exec code in env")
exec(code, env)

linecache.cache[unique_filename] = (
len(code_str),
Expand Down
14 changes: 3 additions & 11 deletions clastic/static.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,12 @@
# TODO: default favicon.ico StaticApplication?
# TODO: process paths

try:
unicode = unicode
PY3 = False
except NameError:
# py3
unicode = str
PY3 = True

DEFAULT_MAX_AGE = 360
DEFAULT_TEXT_MIME = 'text/plain'
DEFAULT_BINARY_MIME = 'application/octet-stream'

# string.printable doesn't cut it
_PRINTABLE = b''.join([chr(x).encode('latin-1') if PY3 else chr(x)
_PRINTABLE = b''.join([chr(x).encode('latin-1')
for x in [7, 8, 9, 10, 12, 13, 27] +
list(range(32, 256))])

Expand Down Expand Up @@ -150,7 +142,7 @@ def __init__(self,
cache_timeout=DEFAULT_MAX_AGE,
default_text_mime=DEFAULT_TEXT_MIME,
default_binary_mime=DEFAULT_BINARY_MIME):
if isinstance(search_paths, (unicode, bytes)):
if isinstance(search_paths, (str, bytes)):
search_paths = [search_paths]
self.search_paths = search_paths
self.cache_timeout = cache_timeout
Expand All @@ -161,7 +153,7 @@ def __init__(self,

def get_file_response(self, path, request):
try:
if not isinstance(path, (unicode, bytes)):
if not isinstance(path, (str, bytes)):
path = '/'.join(path)
full_path = find_file(self.search_paths, path)
if full_path is None:
Expand Down
1 change: 0 additions & 1 deletion clastic/tests/common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import clastic
from clastic import Middleware
Expand Down
1 change: 0 additions & 1 deletion clastic/tests/test_ashes_render.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals
import os
from pytest import raises

Expand Down
3 changes: 1 addition & 2 deletions clastic/tests/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals
from pytest import raises


Expand All @@ -11,7 +10,7 @@

def test_create_empty_application():
app = Application()
return app
assert app


def test_create_hw_application():
Expand Down
1 change: 0 additions & 1 deletion clastic/tests/test_chameleon_render.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals
import os

from pytest import raises
Expand Down
1 change: 0 additions & 1 deletion clastic/tests/test_cline.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

from __future__ import unicode_literals

from clastic.cline import Cline

Expand Down
1 change: 0 additions & 1 deletion clastic/tests/test_context_proc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals
from pytest import raises

import json
Expand Down
1 change: 0 additions & 1 deletion clastic/tests/test_cookie.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals

import time

Expand Down
1 change: 0 additions & 1 deletion clastic/tests/test_decorators.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals
from pytest import raises

from functools import wraps
Expand Down
1 change: 0 additions & 1 deletion clastic/tests/test_errors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import json

Expand Down
1 change: 0 additions & 1 deletion clastic/tests/test_flaw.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from clastic.flaw import create_app

Expand Down
1 change: 0 additions & 1 deletion clastic/tests/test_http_routing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals

from clastic import Application, render_basic, RerouteWSGI
from clastic.route import GET, POST, PUT, DELETE
Expand Down
Loading

0 comments on commit 9c27ac1

Please sign in to comment.