Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

referencing variable out of scope #31786

Closed
lavie opened this issue Oct 16, 2017 · 4 comments · Fixed by #31792
Closed

referencing variable out of scope #31786

lavie opened this issue Oct 16, 2017 · 4 comments · Fixed by #31792
Labels
affects_2.5 This issue/PR affects Ansible v2.5 bug This issue/PR relates to a bug. support:core This issue/PR relates to code supported by the Ansible Engineering Team.

Comments

@lavie
Copy link

lavie commented Oct 16, 2017

out_dest = open(dest, 'wb')

In the following code:

            try:
                out_dest = open(dest, 'wb')
                in_src = open(src, 'rb')
                shutil.copyfileobj(in_src, out_dest)
            finally:  # assuring closed files in 2.4 compatible way
                if out_dest:
                    out_dest.close()

If the first open raises an exception, then the if out_dest fails with either a "trying to reference undefined variable" or a "dereferencing variable before assignment" error, depending on the python version I guess. I ran into this corner case in a situation that's a bit hard to recreate while using lineinfile and unsafe writes. However, you can test a simplified version of this code in interactive python to see that the issue does exist (works for me with python 2.7).

SUMMARY

referencing undefined variable

ISSUE TYPE
  • Bug Report
COMPONENT NAME

module_utils/basic.py

ANSIBLE VERSION

master

OS / ENVIRONMENT

OSX

STEPS TO REPRODUCE
@ansibot
Copy link
Contributor

ansibot commented Oct 16, 2017

@lavie Greetings! Thanks for taking the time to open this issue. In order for the community to handle your issue effectively, we need a bit more information.

Here are the items we could not find in your description:

  • issue type
  • ansible version
  • component name

Please set the description of this issue with this template:
https://raw.githubusercontent.com/ansible/ansible/devel/.github/ISSUE_TEMPLATE.md

click here for bot help

@ansibot ansibot added affects_2.5 This issue/PR affects Ansible v2.5 needs_info This issue requires further information. Please answer any outstanding questions. needs_template This issue/PR has an incomplete description. Please fill in the proposed template correctly. needs_triage Needs a first human triage before being processed. support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Oct 16, 2017
@lavie
Copy link
Author

lavie commented Oct 16, 2017

ISSUE TYPE
  • Bug Report
COMPONENT NAME

module_utils/basic.py

ANSIBLE VERSION

master

CONFIGURATION
OS / ENVIRONMENT

OSX

SUMMARY

referencing undefined variable

STEPS TO REPRODUCE

out_dest = open(dest, 'wb')

In the following code:

            try:
                out_dest = open(dest, 'wb')
                in_src = open(src, 'rb')
                shutil.copyfileobj(in_src, out_dest)
            finally:  # assuring closed files in 2.4 compatible way
                if out_dest:
                    out_dest.close()

If the first open raises an exception, then the if out_dest fails with either a "trying to reference undefined variable" or a "dereferencing variable before assignment" error, depending on the python version I guess. I ran into this corner case in a situation that's a bit hard to recreate while using lineinfile and unsafe writes. However, you can test a simplified version of this code in interactive python to see that the issue does exist (works for me with python 2.7).

@alikins
Copy link
Contributor

alikins commented Oct 16, 2017

(I updated the initial bug comment with info from #31786 (comment))

@alikins
Copy link
Contributor

alikins commented Oct 16, 2017

pinging @bcoca, @abadger (basic.py atomic_move/_unsafe_writes)

@alikins alikins removed needs_triage Needs a first human triage before being processed. needs_template This issue/PR has an incomplete description. Please fill in the proposed template correctly. needs_info This issue requires further information. Please answer any outstanding questions. labels Oct 16, 2017
bcoca added a commit to bcoca/ansible that referenced this issue Oct 16, 2017
@bcoca bcoca added this to Nominated in 2.4.x Blocker List Oct 16, 2017
s-hertel pushed a commit to s-hertel/ansible that referenced this issue Oct 16, 2017
@abadger abadger removed this from Nominated in 2.4.x Blocker List Oct 16, 2017
abadger pushed a commit that referenced this issue Oct 18, 2017
fixes #31786

(cherry picked from commit a26a284)
@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 7, 2018
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.5 This issue/PR affects Ansible v2.5 bug This issue/PR relates to a bug. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants