This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author jhewitt
Recipients jhewitt, matrixise
Date 2018-10-25.19:36:17
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <97f9f76e-d023-6a33-01a7-1f01bdde0a0a@caurinus.com>
In-reply-to <60C83277-724E-4D46-99CA-A299C3713AD6@wirtel.be>
Content
Sure, it's at https://pastebin.com/L1RMPD7K

-James

On 10/25/2018 12:30 PM, Stéphane Wirtel wrote:
> 
> Stéphane Wirtel <stephane@wirtel.be> added the comment:
> 
> Could you share a pastebin? Thank you
> 
>> Le 25 oct. 2018 à 21:28, James Hewitt <report@bugs.python.org> a écrit :
>>
>>
>> New submission from James Hewitt <jgh@caurinus.com>:
>>
>> Having 'import logging.config' in an if statement in a function causes a namespace issue, despite the fact that the import is not reached.
>>
>> Example code:
>>
>> ---
>> #!/usr/bin/env python3
>>
>> # Test weird import bug
>>
>> import logging
>>
>>
>> config = {}
>> config['log'] = {}
>> config['log']['log_type'] = 'file'
>> config['log']['log_file'] = './log'
>> config['log']['config'] = { 'version' : 1 }
>>
>>
>> def do_config_logging():
>>     if config['log']['log_type'] == 'from_config':
>>         import logging.config
>>         logging.config.dictConfig(config['log']['config'])
>>     elif config['log']['log_type'] == 'file':
>>         logging.basicConfig(filename=config['log']['log_file'])
>>         logging.info("start logging")
>>
>>
>> if __name__ == "__main__":
>>     do_config_logging()
>>
>> ---
>>
>>
>> This results in:
>>
>> Traceback (most recent call last):
>>   File "./bug.py", line 25, in <module>
>>     do_config_logging()
>>   File "./bug.py", line 20, in do_config_logging
>>     logging.basicConfig(filename=config['log']['log_file'])
>> UnboundLocalError: local variable 'logging' referenced before assignment
>>
>>
>> Notes:
>>
>> This was run on Ubuntu Linux 18.04 Intel 64-bit, Python version 3.6.6
>>
>> The problem does not occur if the branch is actually taken, and it does not occur if the 'if' statement is not in a function.  It also does not occur if 'logging.config' is imported as some other name, eg. 'configlogging'.
>>
>> virtualenv is installed (via the distribution package) but not in use in the test case.
>>
>> ----------
>> components: Library (Lib)
>> messages: 328470
>> nosy: jhewitt
>> priority: normal
>> severity: normal
>> status: open
>> title: Unexecuted import in function causes UnboundLocalError
>> type: behavior
>> versions: Python 3.6
>>
>> _______________________________________
>> Python tracker <report@bugs.python.org>
>> <https://bugs.python.org/issue35069>
>> _______________________________________
>> _______________________________________________
>> New-bugs-announce mailing list
>> New-bugs-announce@python.org
>> https://mail.python.org/mailman/listinfo/new-bugs-announce
> 
> ----------
> nosy: +matrixise
> 
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue35069>
> _______________________________________
>
History
Date User Action Args
2018-10-25 19:36:17jhewittsetrecipients: + jhewitt, matrixise
2018-10-25 19:36:17jhewittlinkissue35069 messages
2018-10-25 19:36:17jhewittcreate