def get_parent(globals, level):
 orig_level = level
 if ((not level) or (not isinstance(globals, dict))):
  return (None, '')
 pkgname = globals.get('__package__', None)
 if (pkgname is not None):
  if (not hasattr(pkgname, 'rindex')):
   raise ValueError('__package__ set to non-string')
  if (len(pkgname) == 0):
   if (level > 0):
    raise ValueError('Attempted relative import in non-package')
   return (None, '')
  name = pkgname
 else:
  if ('__name__' not in globals):
   return (None, '')
  modname = globals['__name__']
  if ('__path__' in globals):
   globals['__package__'] = name = modname
  else:
   lastdot = modname.rfind('.')
   if (lastdot < 0 < level):
    raise ValueError('Attempted relative import in non-package')
   if (lastdot < 0):
    globals['__package__'] = None
    return (None, '')
   globals['__package__'] = name = modname[:lastdot]
 dot = len(name)
 for x in range(level, 1, (-1)):
  try:
   dot = name.rindex('.', 0, dot)
  except ValueError:
   raise ValueError('attempted relative import beyond top-level package')
 name = name[:dot]
 try:
  parent = sys.modules[name]
 except:
  if (orig_level < 1):
   warn(("Parent module '%.200s' not found while handling absolute import" % name))
   parent = None
  else:
   raise SystemError(("Parent module '%.200s' not loaded, cannot perform relative import" % name))
 return (parent, name)

def get_parent ( globals , level ) : orig_level = level if ( ( not level ) or ( not isinstance ( globals , dict ) ) ) : return ( None , '' ) pkgname = globals . get ( '__package__' , None ) if ( pkgname is not None ) : if ( not hasattr ( pkgname , 'rindex' ) ) : raise ValueError ( '__package__ set to non-string' ) if ( len ( pkgname ) == 0 ) : if ( level > 0 ) : raise ValueError ( 'Attempted relative import in non-package' ) return ( None , '' ) name = pkgname else : if ( '__name__' not in globals ) : return ( None , '' ) modname = globals [ '__name__' ] if ( '__path__' in globals ) : globals [ '__package__' ] = name = modname else : lastdot = modname . rfind ( '.' ) if ( lastdot < 0 < level ) : raise ValueError ( 'Attempted relative import in non-package' ) if ( lastdot < 0 ) : globals [ '__package__' ] = None return ( None , '' ) globals [ '__package__' ] = name = modname [ : lastdot ] dot = len ( name ) for x in range ( level , 1 , ( - 1 ) ) : try : dot = name . rindex ( '.' , 0 , dot ) except ValueError : raise ValueError ( 'attempted relative import beyond top-level package' ) name = name [ : dot ] try : parent = sys . modules [ name ] except : if ( orig_level < 1 ) : warn ( ( "Parent module '%.200s' not found while handling absolute import" % name ) ) parent = None else : raise SystemError ( ( "Parent module '%.200s' not loaded, cannot perform relative import" % name ) ) return ( parent , name )
retrieves a representation of the parent object .