Fix variable use before definition. (#78500)

* Fix variable use before definition.

* Include mkstemp in exception handler.

Also remove two pointless variable assignments.
pull/78528/head
Matt Clay 2 years ago committed by GitHub
parent 7f69629fa7
commit 09d0df1d87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- Fix potential, but unlikely, cases of variable use before definition.

@ -90,10 +90,10 @@ class ConnectionProcess(object):
self._ansible_playbook_pid = ansible_playbook_pid
def start(self, variables):
try:
messages = list()
result = {}
try:
messages.append(('vvvv', 'control socket path is %s' % self.socket_path))
# If this is a relative path (~ gets expanded later) then plug the

@ -745,8 +745,9 @@ class TaskExecutor:
# if we didn't skip this task, use the helpers to evaluate the changed/
# failed_when properties
if 'skipped' not in result:
try:
condname = 'changed'
try:
_evaluate_changed_when_result(result)
condname = 'failed'
_evaluate_failed_when_result(result)

@ -1592,10 +1592,13 @@ def _resolve_depenency_map(
raise AnsibleError("Failed to import resolvelib, check that a supported version is installed")
if not HAS_PACKAGING:
raise AnsibleError("Failed to import packaging, check that a supported version is installed")
req = None
try:
dist = distribution('ansible-core')
except Exception:
req = None
pass
else:
req = next((rr for r in (dist.requires or []) if (rr := PkgReq(r)).name == 'resolvelib'), None)
finally:

@ -467,6 +467,9 @@ def write_file(module, dest, content, resp):
"""
Create temp file and write content to dest file only if content changed
"""
tmpsrc = None
try:
fd, tmpsrc = tempfile.mkstemp(dir=module.tmpdir)
with os.fdopen(fd, 'wb') as f:
@ -475,14 +478,11 @@ def write_file(module, dest, content, resp):
else:
shutil.copyfileobj(content, f)
except Exception as e:
if os.path.exists(tmpsrc):
if tmpsrc and os.path.exists(tmpsrc):
os.remove(tmpsrc)
msg = format_message("Failed to create temporary content file: %s" % to_native(e), resp)
module.fail_json(msg=msg, **resp)
checksum_src = None
checksum_dest = None
checksum_src = module.sha1(tmpsrc)
checksum_dest = module.sha1(dest)

@ -179,6 +179,7 @@ try:
from winrm.protocol import Protocol
import requests.exceptions
HAS_WINRM = True
WINRM_IMPORT_ERR = None
except ImportError as e:
HAS_WINRM = False
WINRM_IMPORT_ERR = e
@ -186,6 +187,7 @@ except ImportError as e:
try:
import xmltodict
HAS_XMLTODICT = True
XMLTODICT_IMPORT_ERR = None
except ImportError as e:
HAS_XMLTODICT = False
XMLTODICT_IMPORT_ERR = e

@ -80,8 +80,9 @@ def _list_plugins_from_paths(ptype, dirs, collection, depth=0):
else:
raise AnsibleError('how did you get here?')
try:
added = False
try:
if path not in ploader._extra_dirs:
ploader.add_directory(path)
added = True

Loading…
Cancel
Save