Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Ensure shortened module path points to expected object. #273

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

Merged
merged 1 commit into from
Jul 31, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion sphinx_gallery/backreferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,23 @@ def get_mapping(self):

def get_short_module_name(module_name, obj_name):
""" Get the shortest possible module name """
scope = {}
try:
# Find out what the real object is supposed to be.
exec('from %s import %s' % (module_name, obj_name), scope, scope)
real_obj = scope[obj_name]
except Exception:
return module_name

parts = module_name.split('.')
short_name = module_name
for i in range(len(parts) - 1, 0, -1):
short_name = '.'.join(parts[:i])
scope = {}
try:
exec('from %s import %s' % (short_name, obj_name))
exec('from %s import %s' % (short_name, obj_name), scope, scope)
# Ensure shortened object is the same as what we expect.
assert real_obj is scope[obj_name]
except Exception: # libraries can throw all sorts of exceptions...
# get the last working module name
short_name = '.'.join(parts[:(i + 1)])
Expand Down