LEFT | RIGHT |
1 """Core implementation of import. | 1 """Core implementation of import. |
2 | 2 |
3 This module is NOT meant to be directly imported! It has been designed such | 3 This module is NOT meant to be directly imported! It has been designed such |
4 that it can be bootstrapped into Python as the implementation of import. As | 4 that it can be bootstrapped into Python as the implementation of import. As |
5 such it requires the injection of specific modules and attributes in order to | 5 such it requires the injection of specific modules and attributes in order to |
6 work. One should use importlib as the public-facing version of this module. | 6 work. One should use importlib as the public-facing version of this module. |
7 | 7 |
8 """ | 8 """ |
9 | 9 |
10 # Injected modules are '_warnings', 'imp', 'sys', 'marshal', 'errno', '_io', | 10 # Injected modules are '_warnings', 'imp', 'sys', 'marshal', 'errno', '_io', |
(...skipping 471 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
482 | 482 |
483 """ | 483 """ |
484 return self._is_pkg | 484 return self._is_pkg |
485 | 485 |
486 | 486 |
487 class _PyPycFileLoader(PyPycLoader, _PyFileLoader): | 487 class _PyPycFileLoader(PyPycLoader, _PyFileLoader): |
488 | 488 |
489 """Load a module from a source or bytecode file.""" | 489 """Load a module from a source or bytecode file.""" |
490 | 490 |
491 def _find_path(self, ext_type): | 491 def _find_path(self, ext_type): |
| 492 """Return PEP 3147 path if ext_type is PY_COMPILED, otherwise |
| 493 super()._find_path() is called.""" |
492 if ext_type == imp.PY_COMPILED: | 494 if ext_type == imp.PY_COMPILED: |
| 495 # We don't really care what the extension on self._base_path is, |
| 496 # as long as it has exactly one dot. |
493 bytecode_path = imp.cache_from_source(self._base_path + '.py') | 497 bytecode_path = imp.cache_from_source(self._base_path + '.py') |
494 return (bytecode_path if _path_exists(bytecode_path) else None) | 498 return (bytecode_path if _path_exists(bytecode_path) else None) |
495 return super()._find_path(ext_type) | 499 return super()._find_path(ext_type) |
496 | 500 |
497 @_check_name | 501 @_check_name |
498 def source_mtime(self, name): | 502 def source_mtime(self, name): |
499 """Return the modification time of the source for the specified | 503 """Return the modification time of the source for the specified |
500 module.""" | 504 module.""" |
501 source_path = self.source_path(name) | 505 source_path = self.source_path(name) |
502 if not source_path: | 506 if not source_path: |
(...skipping 11 matching lines...) Expand all Loading... |
514 def write_bytecode(self, name, data): | 518 def write_bytecode(self, name, data): |
515 """Write out 'data' for the specified module, returning a boolean | 519 """Write out 'data' for the specified module, returning a boolean |
516 signifying if the write-out actually occurred. | 520 signifying if the write-out actually occurred. |
517 | 521 |
518 Raises ImportError (just like get_source) if the specified module | 522 Raises ImportError (just like get_source) if the specified module |
519 cannot be handled by the loader. | 523 cannot be handled by the loader. |
520 | 524 |
521 """ | 525 """ |
522 bytecode_path = self.bytecode_path(name) | 526 bytecode_path = self.bytecode_path(name) |
523 if not bytecode_path: | 527 if not bytecode_path: |
524 bytecode_path = self._base_path + _suffix_list(imp.PY_COMPILED)[0] | 528 source_path = self.source_path(name) |
| 529 bytecode_path = imp.cache_from_source(source_path) |
| 530 # Ensure that the __pycache__ directory exists. We can't use |
| 531 # os.path.dirname() here. |
| 532 dirname, sep, basename = bytecode_path.rpartition(path_sep) |
| 533 try: |
| 534 _os.mkdir(dirname) |
| 535 except OSError as error: |
| 536 if error.errno != errno.EEXIST: |
| 537 raise |
525 try: | 538 try: |
526 # Assuming bytes. | 539 # Assuming bytes. |
527 with _closing(_io.FileIO(bytecode_path, 'w')) as bytecode_file: | 540 with _closing(_io.FileIO(bytecode_path, 'w')) as bytecode_file: |
528 bytecode_file.write(data) | 541 bytecode_file.write(data) |
529 return True | 542 return True |
530 except IOError as exc: | 543 except IOError as exc: |
531 if exc.errno == errno.EACCES: | 544 if exc.errno == errno.EACCES: |
532 return False | 545 return False |
533 else: | 546 else: |
534 raise | 547 raise |
(...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
947 if '*' in fromlist and hasattr(module, '__all__'): | 960 if '*' in fromlist and hasattr(module, '__all__'): |
948 fromlist = list(fromlist) | 961 fromlist = list(fromlist) |
949 fromlist.remove('*') | 962 fromlist.remove('*') |
950 fromlist.extend(module.__all__) | 963 fromlist.extend(module.__all__) |
951 for x in (y for y in fromlist if not hasattr(module,y)): | 964 for x in (y for y in fromlist if not hasattr(module,y)): |
952 try: | 965 try: |
953 _gcd_import('{0}.{1}'.format(module.__name__, x)) | 966 _gcd_import('{0}.{1}'.format(module.__name__, x)) |
954 except ImportError: | 967 except ImportError: |
955 pass | 968 pass |
956 return module | 969 return module |
LEFT | RIGHT |