Skip to content

Commit

Permalink
misc bugs + please pyflakes (#89)
Browse files Browse the repository at this point in the history
* add missing "urlopen" import

* fix undefined name "unicode"

* remove extraneous import of deprecated "cgi" module

* pyflakes 'raise NotImplemented' should be 'raise NotImplementedError'

* bug: cmp() does not exist in Python3

https://stackoverflow.com/questions/22490366/how-to-use-cmp-in-python-3

* bug: undefined name 'current_thread'

* remaining pyflakes

* remove usage of "six.moves"

* six: vendor int2byte

* remove more "six" usage

* vendor six.reraise()

* finish removing six
  • Loading branch information
a-detiste authored Mar 31, 2024
1 parent 7f1750e commit 7654dd8
Show file tree
Hide file tree
Showing 37 changed files with 129 additions and 71 deletions.
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
# All configuration values have a default value; values that are commented out
# serve to show the default value.

import sys

# If your extensions are in another directory, add it here.
#import sys
#sys.path.append('some/directory')

# General configuration
Expand Down
7 changes: 4 additions & 3 deletions paste/auth/auth_tkt.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
makes it possible to use the same authentication process with
non-Python code run under Apache.
"""
import six
import time as time_mod
try:
import hashlib
Expand Down Expand Up @@ -202,15 +201,17 @@ def calculate_digest(ip, timestamp, secret, userid, tokens, user_data,
digest = digest_algo(digest0 + secret).hexdigest()
return maybe_encode(digest)

def int2byte(i):
return bytes((i,))

def encode_ip_timestamp(ip, timestamp):
ip_chars = b''.join(map(six.int2byte, map(int, ip.split('.'))))
ip_chars = b''.join(map(int2byte, map(int, ip.split('.'))))
t = int(timestamp)
ts = ((t & 0xff000000) >> 24,
(t & 0xff0000) >> 16,
(t & 0xff00) >> 8,
t & 0xff)
ts_chars = b''.join(map(six.int2byte, ts))
ts_chars = b''.join(map(int2byte, ts))
return (ip_chars + ts_chars)


Expand Down
4 changes: 1 addition & 3 deletions paste/auth/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
"""
from base64 import b64decode

import six

from paste.httpexceptions import HTTPUnauthorized
from paste.httpheaders import (
AUTHORIZATION,
Expand Down Expand Up @@ -53,7 +51,7 @@ def authenticate(self, environ):
(authmeth, auth) = authorization.split(' ', 1)
if 'basic' != authmeth.lower():
return self.build_authentication()
auth = six.ensure_text(b64decode(six.ensure_binary(auth.strip())))
auth = b64decode(auth.strip().encode('ascii')).decode('ascii')
username, password = auth.split(':', 1)
if self.authfunc(environ, username, password):
return username
Expand Down
3 changes: 2 additions & 1 deletion paste/auth/cas.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
authentication methods to be used concurrently.
"""
from urllib.parse import urlencode
from urllib.request import urlopen
from paste.request import construct_url
from paste.httpexceptions import HTTPSeeOther, HTTPForbidden

Expand Down Expand Up @@ -94,6 +95,6 @@ def cas_application(environ, start_response):
authority = "https://secure.its.yale.edu/cas/servlet/"
from paste.wsgilib import dump_environ
from paste.httpserver import serve
from paste.httpexceptions import *
from paste.httpexceptions import HTTPExceptionHandler
serve(HTTPExceptionHandler(
AuthCASHandler(dump_environ, authority)))
5 changes: 3 additions & 2 deletions paste/auth/cookie.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"""

import hmac, base64, random, six, time, warnings
import hmac, base64, random, time, warnings
from functools import reduce
try:
from hashlib import sha1
except ImportError:
Expand All @@ -62,7 +63,7 @@ def make_time(value):
_decode = [(v, k) for (k, v) in _encode]
_decode.reverse()
def encode(s, sublist = _encode):
return six.moves.reduce((lambda a, b: a.replace(b[0], b[1])), sublist, str(s))
return reduce((lambda a, b: a.replace(b[0], b[1])), sublist, str(s))
decode = lambda s: encode(s, _decode)

class CookieTooLarge(RuntimeError):
Expand Down
13 changes: 9 additions & 4 deletions paste/cgiapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@
import select
except ImportError:
select = None
import six

from paste.util import converters

__all__ = ['CGIError', 'CGIApplication']

def ensure_text(s, encoding='utf-8', errors='strict'):
if type(s) is str:
return s
else:
return s.decode(encoding, errors)

class CGIError(Exception):
"""
Raised when the CGI script can't be found or doesn't
Expand All @@ -40,7 +45,7 @@ def __init__(self,
include_os_environ=True,
query_string=None):
if global_conf:
raise NotImplemented(
raise NotImplementedError(
"global_conf is no longer supported for CGIApplication "
"(use make_cgi_application); please pass None instead")
self.script_filename = script
Expand Down Expand Up @@ -158,7 +163,7 @@ def write(self, data):
else:
self.headers.append((name, value))

class StdinReader(object):
class StdinReader:

def __init__(self, stdin, content_length):
self.stdin = stdin
Expand Down Expand Up @@ -252,7 +257,7 @@ def proc_communicate(proc, stdin=None, stdout=None, stderr=None):
read_set.remove(proc.stderr)
if trans_nl:
data = proc._translate_newlines(data)
stderr.write(six.ensure_text(data))
stderr.write(ensure_text(data))

try:
proc.wait()
Expand Down
3 changes: 3 additions & 0 deletions paste/evalexception/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@

limit = 200

def cmp(a, b):
return (a > b) - (a < b)

def html_quote(v):
"""
Escape HTML characters, plus translate None to ''
Expand Down
2 changes: 1 addition & 1 deletion paste/exceptions/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ def safeStr(self, obj):
return str(obj)
except UnicodeEncodeError:
try:
return unicode(obj).encode(FALLBACK_ENCODING, 'replace')
return str(obj).encode(FALLBACK_ENCODING, 'replace')
except UnicodeEncodeError:
# This is when something is really messed up, but this can
# happen when the __str__ of an object has to handle unicode
Expand Down
5 changes: 3 additions & 2 deletions paste/exceptions/serial_number_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
except ImportError:
from md5 import md5

import six
import operator
byte2int = operator.itemgetter(0)

good_characters = "23456789abcdefghjkmnpqrtuvwxyz"

Expand Down Expand Up @@ -71,7 +72,7 @@ def hash_identifier(s, length, pad=True, hasher=md5, prefix='',
modulo = base ** length
number = 0
for c in list(bin_hash):
number = (number * 256 + six.byte2int([c])) % modulo
number = (number * 256 + byte2int([c])) % modulo
ident = make_identifier(number)
if pad:
ident = good_characters[0]*(length-len(ident)) + ident
Expand Down
25 changes: 23 additions & 2 deletions paste/fileapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,29 @@
"""

import os, time, mimetypes, zipfile, tarfile
from paste.httpexceptions import *
from paste.httpheaders import *
from paste.httpexceptions import (
HTTPBadRequest,
HTTPForbidden,
HTTPMethodNotAllowed,
HTTPNotFound,
HTTPRequestRangeNotSatisfiable,
)
from paste.httpheaders import (
get_header,
list_headers,
ACCEPT_RANGES,
CACHE_CONTROL,
CONTENT_DISPOSITION,
CONTENT_LENGTH,
CONTENT_RANGE,
CONTENT_TYPE,
ETAG,
EXPIRES,
IF_MODIFIED_SINCE,
IF_NONE_MATCH,
LAST_MODIFIED,
RANGE,
)

CACHE_SIZE = 4096
BLOCK_SIZE = 4096 * 16
Expand Down
23 changes: 17 additions & 6 deletions paste/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,24 @@
import subprocess
from urllib.parse import urlencode
from urllib import parse as urlparse
from six.moves.http_cookies import BaseCookie
import six
from http.cookies import BaseCookie

from paste import wsgilib
from paste import lint
from paste.response import HeaderDict

def ensure_binary(s):
if isinstance(s, bytes):
return s
else:
return s.encode('utf-8')

def ensure_str(s, encoding='utf-8', errors='strict'):
if type(s) is str:
return s
else:
return s.decode(encoding, errors)

def tempnam_no_warning(*args):
"""
An os.tempnam with the warning turned off, because sometimes
Expand Down Expand Up @@ -333,18 +344,18 @@ def encode_multipart(self, params, files):
lines = []
for key, value in params:
lines.append(b'--'+boundary)
line = b'Content-Disposition: form-data; name="%s"' % six.ensure_binary(key)
line = b'Content-Disposition: form-data; name="%s"' % ensure_binary(key)
lines.append(line)
lines.append(b'')
line = six.ensure_binary(value)
line = ensure_binary(value)
lines.append(line)
for file_info in files:
key, filename, value = self._get_file_info(file_info)
lines.append(b'--'+boundary)
line = (b'Content-Disposition: form-data; name="%s"; filename="%s"'
% (six.ensure_binary(key), six.ensure_binary(filename)))
% (ensure_binary(key), ensure_binary(filename)))
lines.append(line)
fcontent = mimetypes.guess_type(six.ensure_str(filename, 'ascii', 'ignore'))[0]
fcontent = mimetypes.guess_type(ensure_str(filename, 'ascii', 'ignore'))[0]
line = (b'Content-Type: %s'
% (fcontent.encode('ascii') if fcontent else b'application/octet-stream'))
lines.append(line)
Expand Down
8 changes: 4 additions & 4 deletions paste/httpserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
import time
import os
from itertools import count
from six.moves import _thread
from six.moves import queue
from six.moves.BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from six.moves.socketserver import ThreadingMixIn
import _thread
import queue
from http.server import BaseHTTPRequestHandler, HTTPServer
from socketserver import ThreadingMixIn
from urllib.parse import unquote, urlsplit
from paste.util import converters
import logging
Expand Down
2 changes: 1 addition & 1 deletion paste/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"""

from six.moves import http_client as httplib
import http.client as httplib
from urllib import parse as urlparse
from urllib.parse import quote

Expand Down
2 changes: 1 addition & 1 deletion paste/translogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import logging
import time
from six.moves.urllib.parse import quote
from urllib.parse import quote

class TransLogger:
"""
Expand Down
11 changes: 4 additions & 7 deletions paste/util/quoting.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
# (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org)
# Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php

import html
import html.entities
import re
from six.moves import html_entities
from urllib.parse import quote, unquote
import six

import html


__all__ = ['html_quote', 'html_unquote', 'url_quote', 'url_unquote',
'strip_html']
Expand All @@ -30,10 +27,10 @@ def html_quote(v, encoding=None):
return html.escape(str(v), 1)

_unquote_re = re.compile(r'&([a-zA-Z]+);')
def _entity_subber(match, name2c=html_entities.name2codepoint):
def _entity_subber(match, name2c=html.entities.name2codepoint):
code = name2c.get(match.group(1))
if code:
return six.unichr(code)
return chr(code)
else:
return match.group(0)

Expand Down
18 changes: 14 additions & 4 deletions paste/util/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def foo(bar):
"""

import re
import six
import sys
from html import escape
from urllib.parse import quote
Expand All @@ -44,6 +43,17 @@ def foo(bar):
in_re = re.compile(r'\s+in\s+')
var_re = re.compile(r'^[a-z_][a-z0-9_]*$', re.I)

def reraise(tp, value, tb=None):
try:
if value is None:
value = tp()
if value.__traceback__ is not tb:
raise value.with_traceback(tb)
raise value
finally:
value = None
tb = None

class TemplateError(Exception):
"""Exception raised while parsing a template
"""
Expand Down Expand Up @@ -208,7 +218,7 @@ def _eval(self, code, ns, pos):
else:
arg0 = str(e)
e.args = (self._add_line_info(arg0, pos),)
six.reraise(exc_info[0], e, exc_info[2])
reraise(exc_info[0], e, exc_info[2])

def _exec(self, code, ns, pos):
__traceback_hide__ = True
Expand All @@ -218,7 +228,7 @@ def _exec(self, code, ns, pos):
exc_info = sys.exc_info()
e = exc_info[1]
e.args = (self._add_line_info(e.args[0], pos),)
six.reraise(exc_info[0], e, exc_info[2])
reraise(exc_info[0], e, exc_info[2])

def _repr(self, value, pos):
__traceback_hide__ = True
Expand All @@ -230,7 +240,7 @@ def _repr(self, value, pos):
exc_info = sys.exc_info()
e = exc_info[1]
e.args = (self._add_line_info(e.args[0], pos),)
six.reraise(exc_info[0], e, exc_info[2])
reraise(exc_info[0], e, exc_info[2])
else:
if self._unicode and isinstance(value, bytes):
if not self.decode_encoding:
Expand Down
2 changes: 1 addition & 1 deletion paste/util/threadedprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def __init__(self, default=None, factory=None, paramwriter=None,
self._catchers = {}

def write(self, v, currentThread=threading.current_thread):
name = current_thread().name
name = currentThread().name
catchers = self._catchers
if not catchers.has_key(name):
self._defaultfunc(name, v)
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
zip_safe=False,
install_requires=[
'setuptools', # pkg_resources
'six>=1.4.0',
],
extras_require={
'subprocess': [],
Expand Down
1 change: 0 additions & 1 deletion tests/test_auth/test_auth_cookie.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from paste.auth import cookie
from paste.wsgilib import raw_interactive, dump_environ
from paste.response import header_value
from paste.httpexceptions import *

def build(application,setenv, *args, **kwargs):
def setter(environ, start_response):
Expand Down
Loading

0 comments on commit 7654dd8

Please sign in to comment.