Skip to content

Commit

Permalink
Fix TypeError: OSError object is not subscriptable under Python 3
Browse files Browse the repository at this point in the history
Fix the following exception:

```
test/integration/test_cli_runners.py:90:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
lib/galaxy_test/driver/integration_util.py:73: in setUpClass
    cls._test_driver.setup(config_object=cls)
lib/galaxy_test/driver/driver_util.py:942: in setup
    self._register_and_run_servers(config_object)
lib/galaxy_test/driver/driver_util.py:1008: in _register_and_run_servers
    config_object=config_object,
lib/galaxy_test/driver/driver_util.py:827: in launch_server
    host=host, port=port
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

webapp = <paste.urlmap.URLMap object at 0x7f011b68c110>, port = '8845'
host = '127.0.0.1'

    def serve_webapp(webapp, port=None, host=None):
        """Serve the webapp on a recommend port or a free one.

        Return the port the webapp is running on.
        """
        server = None
        for port in attempt_ports(port):
            try:
                server = httpserver.serve(webapp, host=host, port=port, start_loop=False)
                break
            except socket.error as e:
>               if e[0] == 98:
E               TypeError: 'OSError' object is not subscriptable
```

Also use `e.errno` instead of `e.args[0]` to get the errno of
`socket.error`, since the latter may return an error string depending on
how the exception was raised.

Also:
- move standard library imports to the top
- fix issues reported by flake8-bugbear
  • Loading branch information
nsoranzo committed Apr 21, 2020
1 parent 7adb2a3 commit dc5791c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
17 changes: 7 additions & 10 deletions lib/galaxy/util/pastescript/serve.py
Expand Up @@ -21,11 +21,15 @@

import atexit
import errno
import grp
import logging
import optparse
import os
import pwd
import re
import resource
import signal
import socket
import subprocess
import sys
import textwrap
Expand Down Expand Up @@ -662,7 +666,6 @@ def serve():
except AttributeError as e:
# Capturing bad error response from paste
if str(e) == "'WSGIThreadPoolServer' object has no attribute 'thread_pool'":
import socket
raise socket.error(98, 'Address already in use')
else:
raise AttributeError(e)
Expand Down Expand Up @@ -699,7 +702,6 @@ def daemonize(self):

# @@: Should we set the umask and cwd now?

import resource # Resource usage information.
maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
if maxfd == resource.RLIM_INFINITY:
maxfd = MAXFD
Expand Down Expand Up @@ -746,7 +748,7 @@ def stop_daemon(self):
print("Could not delete: %s" % e)
return 2
return 1
for j in range(10):
for _i in range(10):
if not live_pidfile(pid_file):
break
os.kill(pid, signal.SIGTERM)
Expand Down Expand Up @@ -820,15 +822,12 @@ def restart_with_monitor(self, reloader=False):
def change_user_group(self, user, group):
if not user and not group:
return
import pwd
import grp
uid = gid = None
if group:
try:
gid = int(group)
group = grp.getgrgid(gid).gr_name
except ValueError:
import grp
try:
entry = grp.getgrnam(group)
except KeyError:
Expand Down Expand Up @@ -983,15 +982,13 @@ def ensure_port_cleanup(bound_addresses, maxtries=30, sleeptime=2):

def _cleanup_ports(bound_addresses, maxtries=30, sleeptime=2):
# Wait for the server to bind to the port.
import socket
import errno
for bound_address in bound_addresses:
for attempt in range(maxtries):
for _i in range(maxtries):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect(bound_address)
except socket.error as e:
if e.args[0] != errno.ECONNREFUSED:
if e.errno != errno.ECONNREFUSED:
raise
break
else:
Expand Down
16 changes: 10 additions & 6 deletions test/base/driver_util.py
Expand Up @@ -310,13 +310,15 @@ def _tool_data_table_config_path(default_tool_data_table_config_path=None):
return tool_data_table_config_path


def nose_config_and_run(argv=None, env=None, ignore_files=[], plugins=None):
def nose_config_and_run(argv=None, env=None, ignore_files=None, plugins=None):
"""Setup a nose context and run tests.
Tests are specified by argv (defaulting to sys.argv).
"""
if env is None:
env = os.environ
if ignore_files is None:
ignore_files = []
if plugins is None:
plugins = nose.plugins.manager.DefaultPluginManager()
if argv is None:
Expand Down Expand Up @@ -472,7 +474,7 @@ def get_webapp_global_conf():
def wait_for_http_server(host, port, sleep_amount=0.1, sleep_tries=150):
"""Wait for an HTTP server to boot up."""
# Test if the server is up
for i in range(sleep_tries):
for _ in range(sleep_tries):
# directly test the app, not the proxy
conn = http_client.HTTPConnection(host, port)
try:
Expand All @@ -497,7 +499,7 @@ def attempt_ports(port):
raise Exception("An existing process seems bound to specified test server port [%s]" % port)
else:
random.seed()
for i in range(0, 9):
for _ in range(0, 9):
port = str(random.randint(8000, 10000))
yield port

Expand All @@ -515,7 +517,7 @@ def serve_webapp(webapp, port=None, host=None):
server = httpserver.serve(webapp, host=host, port=port, start_loop=False)
break
except socket.error as e:
if e[0] == 98:
if e.errno == 98:
continue
raise

Expand Down Expand Up @@ -947,7 +949,7 @@ def _register_and_run_servers(self, config_object=None, handle_config=None):
# one - other just read the properties above and use the default
# implementation from this file.
galaxy_config = getattr(config_object, "galaxy_config", None)
if hasattr(galaxy_config, '__call__'):
if callable(galaxy_config):
galaxy_config = galaxy_config()
if galaxy_config is None:
setup_galaxy_config_kwds = dict(
Expand Down Expand Up @@ -1038,7 +1040,9 @@ def build_tool_tests(self, testing_shed_tools=None, return_test_classes=False):
return test_classes
return functional.test_toolbox

def run_tool_test(self, tool_id, index=0, resource_parameters={}):
def run_tool_test(self, tool_id, index=0, resource_parameters=None):
if resource_parameters is None:
resource_parameters = {}
host, port, url = target_url_parts()
galaxy_interactor_kwds = {
"galaxy_url": url,
Expand Down

0 comments on commit dc5791c

Please sign in to comment.