Skip to content

Commit

Permalink
Cleanup a few other legacy Python 2 try/except imports
Browse files Browse the repository at this point in the history
  • Loading branch information
sbesson committed Jan 26, 2024
1 parent bc19fe5 commit f054810
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 67 deletions.
9 changes: 2 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,8 @@
find_packages,
)

try:
from StringIO import StringIO
from StringIO import StringIO as BytesIO
except ImportError:
# Python 3
from io import StringIO
from io import BytesIO
from io import StringIO
from io import BytesIO

from shutil import (
copy,
Expand Down
5 changes: 1 addition & 4 deletions src/omero/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,10 +809,7 @@ def sha1(self, filename):
"""
Calculates the local sha1 for a file.
"""
try:
from hashlib import sha1 as sha_new
except ImportError:
from sha import new as sha_new
from hashlib import sha1 as sha_new
digest = sha_new()
file = open(filename, 'rb')
try:
Expand Down
5 changes: 1 addition & 4 deletions src/omero/plugins/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,7 @@ def demo(self, args):
from omero.util.temp_files import create_path
t = create_path("Demo_Script", ".py")

try:
from hashlib import sha1 as sha_new
except ImportError:
from sha import new as sha_new
from hashlib import sha1 as sha_new

digest = sha_new()
digest.update(DEMO_SCRIPT.encode('utf-8'))
Expand Down
9 changes: 2 additions & 7 deletions src/omero/util/concurrency.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,8 @@
import omero.util
import logging.handlers

try:
from threading import _Event
from threading import _Timer
except ImportError:
# Python3
from threading import Event as _Event
from threading import Timer as _Timer
from threading import Event as _Event
from threading import Timer as _Timer


def get_event(name="Unknown"):
Expand Down
25 changes: 3 additions & 22 deletions src/omero_ext/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,10 @@
except ImportError:
pass

try:
import pwd
except ImportError:
pass

try:
str
except NameError:
str = str
import pwd
from functools import reduce

try:
getcwdu = os.getcwd
except AttributeError:
getcwdu = os.getcwd
getcwdu = os.getcwd

def u(x):
return x
Expand All @@ -83,15 +73,6 @@ def u(x):
o766 = 502
o666 = 438
o554 = 364
################################

##########################
# Python 2.5 compatibility
try:
from functools import reduce
except ImportError:
pass
##########################


__version__ = '5.2'
Expand Down
31 changes: 8 additions & 23 deletions src/omero_ext/pyinotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ def __init__(self):

# Check Python version
import sys
if sys.version < '2.4':
raise UnsupportedPythonVersionError(sys.version)


# Import directives
Expand All @@ -85,10 +83,7 @@ def __init__(self):
import asyncore
import glob

try:
from functools import reduce
except ImportError:
pass # Will fail on Python 2.4 which has reduce() builtin anyway.
from functools import reduce

__author__ = "[email protected] (Sebastien Martini)"

Expand Down Expand Up @@ -117,15 +112,11 @@ def load_libc():
except (OSError, IOError):
pass # Will attemp to load it with None anyway.

if sys.version_info[0] >= 2 and sys.version_info[1] >= 6:
LIBC = ctypes.CDLL(libc, use_errno=True)
def _strerrno():
code = ctypes.get_errno()
return ' Errno=%s (%s)' % (os.strerror(code), errno.errorcode[code])
strerrno = _strerrno
else:
LIBC = ctypes.CDLL(libc)
strerrno = lambda : ''
LIBC = ctypes.CDLL(libc, use_errno=True)
def _strerrno():
code = ctypes.get_errno()
return ' Errno=%s (%s)' % (os.strerror(code), errno.errorcode[code])
strerrno = _strerrno

# Check that libc has needed functions inside.
if (not hasattr(LIBC, 'inotify_init') or
Expand Down Expand Up @@ -155,14 +146,8 @@ def makeRecord(self, name, level, fn, lno, msg, args, exc_info, func=None,
class UnicodeLogRecord(logging.LogRecord):
def __init__(self, name, level, pathname, lineno,
msg, args, exc_info, func=None):
py_version = sys.version_info
# func argument was added in Python 2.5, just ignore it otherwise.
if py_version[0] >= 2 and py_version[1] >= 5:
logging.LogRecord.__init__(self, name, level, pathname, lineno,
msg, args, exc_info, func)
else:
logging.LogRecord.__init__(self, name, level, pathname, lineno,
msg, args, exc_info)
logging.LogRecord.__init__(self, name, level, pathname, lineno,
msg, args, exc_info, func)

def getMessage(self):
msg = self.msg
Expand Down

0 comments on commit f054810

Please sign in to comment.