Ticket #2947: management_import_patch.diff

File management_import_patch.diff, 2.0 KB (added by paul@…, 18 years ago)

Patch to fix management.py import bug

  • django/core/management.py

     
    429429get_sql_all.help_doc = "Prints the CREATE TABLE, initial-data and CREATE INDEX SQL statements for the given model module name(s)."
    430430get_sql_all.args = APP_ARGS
    431431
     432def _find_module_with_dots(name, paths=None):
     433    """Like imp.find_module, but supports module names with zero or more dots."""
     434    import imp
     435    dotindex = name.find('.')
     436    if dotindex == -1:
     437        return imp.find_module(name, paths)
     438    else:
     439        parent_name = name[:dotindex]
     440        parent_path = imp.find_module(parent_name, paths)[1]
     441        return _find_module_with_dots(name[dotindex+1:], [parent_path])
     442
     443def _module_exists(modulename):
     444    try:
     445        _find_module_with_dots(modulename)
     446        return True
     447    except ImportError:
     448        return False
     449
     450
     451def _import_module_if_it_exists(modname, *args, **kwargs):
     452    try:
     453        return __import__(modname, *args, **kwargs)
     454    except ImportError, e:
     455        if _module_exists(modname):
     456            # the module exists and yet raised an ImportError. Why?
     457            raise e
     458        else:
     459            # it's okay if the module doesn't exist
     460            return False
     461   
    432462def syncdb(verbosity=1, interactive=True):
    433463    "Creates the database tables for all apps in INSTALLED_APPS whose tables haven't already been created."
    434464    from django.db import connection, transaction, models, get_creation_module
     
    444474    # Import the 'management' module within each installed app, to register
    445475    # dispatcher events.
    446476    for app_name in settings.INSTALLED_APPS:
    447         try:
    448             __import__(app_name + '.management', '', '', [''])
    449         except ImportError:
    450             pass
    451 
     477        modname = app_name + '.management'
     478        _import_module_if_it_exists(modname)
     479           
    452480    data_types = get_creation_module().DATA_TYPES
    453481
    454482    cursor = connection.cursor()
Back to Top