Issue1111

classification
Title: keyword arguments not supported on __import__
Type: Severity: normal
Components: Any Versions: 2.5alpha1
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: fwierzbicki Nosy List: fwierzbicki, nriley, wesleys
Priority: Keywords:

Created on 2008-08-26.10:01:13 by wesleys, last changed 2008-10-13.17:21:38 by fwierzbicki.

Messages
msg3449 (view) Author: Wesley Schwengle (wesleys) Date: 2008-08-26.10:01:12
Code:

def load_modules(dir, module_prefix = None):
    files = os.listdir(dir)
    files.sort()
    for filename in files:
        if not filename.endswith('.py') or not filename[0:1].isupper():
            continue

        file = filename[:-3]
        if module_prefix == None:
            mod = __import__(file, fromlist=filename)
        else:
            mod = __import__('%s.%s' % (module_prefix, file),
fromlist=filename)

        c = getattr(mod, file)
        o = c()
        info(o) // prints information about the module/class

path = "/home/wesleys/sbox/blpython/src/lib/OSS/bladelogic"
load_modules(path, "OSS.bladelogic")

Running this code in python (2.5.1) works as expected, with jython it
fails.. 

2.2.1:
Traceback (innermost last):
  File "test_logic.py", line 48, in ?
  File "test_logic.py", line 38, in load_modules
TypeError: __import__() takes no keyword arguments

2.5a1:
Traceback (most recent call last):
  File "test_logic.py", line 48, in <module>
    load_modules(path, "OSS.bladelogic")
  File "test_logic.py", line 38, in load_modules
    mod = __import__('%s.%s' % (module_prefix, file), fromlist=filename)
TypeError: __import__() takes no keyword arguments
msg3450 (view) Author: Nicholas Riley (nriley) Date: 2008-08-27.03:13:25
You can work around this bug (in ImportFunction.__call__) by using positional 
rather than keyword arguments.  Pass {} for globals and locals in order to get 
to fromlist.

The function doesn't verify the number of positional arguments it's passed, 
either:

Jython trunk:
>>> __import__('sys', {}, {}, [], 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
<module 'sys' (built-in)>

Python 2.5:
>>> __import__('sys', {}, {}, [], 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __import__() takes at most 5 arguments (14 given)
msg3451 (view) Author: Wesley Schwengle (wesleys) Date: 2008-08-27.03:47:30
Thanks,

had to change my code for loading the modules to:

        # Work around for http://bugs.jython.org/issue1111
        # mod = __import__(modules, {}, {}, [ filename ], -1)
        mod = __import__(module_name)
        components = module_name.split('.')
        for comp in components[1:]:
            mod = getattr(mod, comp)

Works for 2.2.1 and 2.5a1.

BTW, from the documentation regarding __import__ I saw these notes:
Changed in version 2.5: The level parameter was added. Changed in
version 2.5: Keyword support for parameters was added.

Don't know if level is supported by jython, but just in case :)
msg3512 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2008-09-13.17:35:21
Relative imports are near the top of my todo list (though the things
above it may take a week or two more.  I've assigned this bug to myself
to get at the same time.
msg3675 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2008-10-13.17:21:38
Fixed in trunk r5384.  The level keyword is accepted but not yet acted upon.
History
Date User Action Args
2008-10-13 17:21:38fwierzbickisetstatus: open -> closed
resolution: fixed
messages: + msg3675
2008-09-13 17:35:21fwierzbickisetassignee: fwierzbicki
messages: + msg3512
nosy: + fwierzbicki
2008-08-27 03:47:31wesleyssetmessages: + msg3451
2008-08-27 03:13:27nrileysetnosy: + nriley
title: __import__() not working in 2.2.1 -> keyword arguments not supported on __import__
messages: + msg3450
versions: + 2.5alpha1, - 2.2.2
2008-08-26 10:01:13wesleyscreate