MCPcopy Index your code
hub / github.com/python/cpython / get_data

Function get_data

Lib/pkgutil.py:358–398  ·  view source on GitHub ↗

Get a resource from a package. This is a wrapper round the PEP 302 loader get_data API. The package argument should be the name of a package, in standard module format (foo.bar). The resource argument should be in the form of a relative filename, using '/' as the path separator. The

(package, resource)

Source from the content-addressed store, hash-verified

356
357
358def get_data(package, resource):
359 """Get a resource from a package.
360
361 This is a wrapper round the PEP 302 loader get_data API. The package
362 argument should be the name of a package, in standard module format
363 (foo.bar). The resource argument should be in the form of a relative
364 filename, using '/' as the path separator. The parent directory name '..'
365 is not allowed, and nor is a rooted name (starting with a '/').
366
367 The function returns a binary string, which is the contents of the
368 specified resource.
369
370 For packages located in the filesystem, which have already been imported,
371 this is the rough equivalent of
372
373 d = os.path.dirname(sys.modules[package].__file__)
374 data = open(os.path.join(d, resource), 'rb').read()
375
376 If the package cannot be located or loaded, or it uses a PEP 302 loader
377 which does not support get_data(), then None is returned.
378 """
379
380 spec = importlib.util.find_spec(package)
381 if spec is None:
382 return None
383 loader = spec.loader
384 if loader is None or not hasattr(loader, 'get_data'):
385 return None
386 # XXX needs test
387 mod = (sys.modules.get(package) or
388 importlib._bootstrap._load(spec))
389 if mod is None or not hasattr(mod, '__file__'):
390 return None
391
392 # Modify the resource name to be compatible with the loader.get_data
393 # signature - an os.path format "filename" starting with the dirname of
394 # the package's __file__
395 parts = resource.split('/')
396 parts.insert(0, os.path.dirname(mod.__file__))
397 resource_name = os.path.join(*parts)
398 return loader.get_data(resource_name)
399
400
401_NAME_PATTERN = None

Callers

nothing calls this directly

Calls 8

_loadMethod · 0.80
find_specMethod · 0.45
getMethod · 0.45
splitMethod · 0.45
insertMethod · 0.45
dirnameMethod · 0.45
joinMethod · 0.45
get_dataMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…