Skip to content

port api/pushtx.py to Python 3 - #1836

Open
HrachShah wants to merge 1 commit into
OmniLayer:masterfrom
HrachShah:fix/pushtx-python3-imports
Open

HrachShah wants to merge 1 commit into
OmniLayer:masterfrom
HrachShah:fix/pushtx-python3-imports

Conversation

@HrachShah

Copy link
Copy Markdown

Summary

api/pushtx.py still uses five Python 2 idioms that prevent the file from being imported on Python 3 (and therefore prevent the entire WSGI app from starting, since msc_apps.py imports this module transitively):

  • import urlparseurlparse was renamed to urllib.parse in Python 3. The import is unused in this file (the only urlparse. call in the whole api/ package is in msc_apps.py, which still has its own copy of the same broken import) so it can simply be removed.
  • import commandscommands was removed in Python 3 in favor of subprocess. The import is only there for the (commented-out) commands.getoutput(...) call, so it can also be removed.
  • response_dict.has_key(field)has_key was removed from dict in Python 2.3. Use field in response_dict.
  • Three print statements without parentheses — print is a function in Python 3.
  • except KeyError, e: — the comma form was removed in Python 3; the parser tries to evaluate e as an expression and raises SyntaxError.
  • One bare except: on the error_codez lookup — narrows to except KeyError: so a non-dict return value or AttributeError surfaces instead of being silently masked.

This is the same Python 2 → 3 migration that earlier sessions applied to armory_service.py, pending.py, msc_apps.py, and rpcclient.py in the api package. msc_apps.py was fixed for the SyntaxError but only the offending lines were addressed; pushtx.py was not touched, and the WSGI process still refused to start because msc_apps.py imports the whole api/ package on startup.

Testing

python3 -c 'import ast; ast.parse(open("api/pushtx.py").read()) reports no SyntaxError. The file now parses under Python 3 and no longer references urlparse, commands, has_key, or the Python 2 except clause form.

…nd import commands calls (both renamed in Python 3 to urllib.parse and subprocess respectively, neither of which is actually referenced in this file), switch has_key() to the in operator, parenthesize the three print statements, rewrite the except KeyError, e: clause to the modern except KeyError as e: form, and narrow the bare except: on the error_codez lookup to except KeyError: so a non-dict return value or AttributeError surfaces instead of being silently masked. This is the same Python 2 → 3 migration that earlier sessions applied to armory_service.py, pending.py, msc_apps.py, and rpcclient.py in the api package, and lets the WSGI entry point (msc_apps.py, which imports this module transitively) start under Python 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant