|
24 | 24 | PY_EXTENSIONS = tuple(PYTHON_EXTENSIONS) |
25 | 25 |
|
26 | 26 |
|
| 27 | +class InvalidPackageName(Exception): |
| 28 | + """Exception indicating that a package name was invalid.""" |
| 29 | + |
| 30 | + |
27 | 31 | def main(script_path: str, args: List[str] = None) -> None: |
28 | 32 | """Main entry point to the type checker. |
29 | 33 |
|
@@ -459,9 +463,15 @@ def add_invertible_flag(flag: str, |
459 | 463 | targets = [] |
460 | 464 | for f in special_opts.files: |
461 | 465 | if f.endswith(PY_EXTENSIONS): |
462 | | - targets.append(BuildSource(f, crawl_up(f)[1], None)) |
| 466 | + try: |
| 467 | + targets.append(BuildSource(f, crawl_up(f)[1], None)) |
| 468 | + except InvalidPackageName as e: |
| 469 | + fail(str(e)) |
463 | 470 | elif os.path.isdir(f): |
464 | | - sub_targets = expand_dir(f) |
| 471 | + try: |
| 472 | + sub_targets = expand_dir(f) |
| 473 | + except InvalidPackageName as e: |
| 474 | + fail(str(e)) |
465 | 475 | if not sub_targets: |
466 | 476 | fail("There are no .py[i] files in directory '{}'" |
467 | 477 | .format(f)) |
@@ -528,10 +538,14 @@ def crawl_up(arg: str) -> Tuple[str, str]: |
528 | 538 | dir, base = os.path.split(dir) |
529 | 539 | if not base: |
530 | 540 | break |
| 541 | + # Ensure that base is a valid python module name |
| 542 | + if not base.isidentifier(): |
| 543 | + raise InvalidPackageName('{} is not a valid Python package name'.format(base)) |
531 | 544 | if mod == '__init__' or not mod: |
532 | 545 | mod = base |
533 | 546 | else: |
534 | 547 | mod = base + '.' + mod |
| 548 | + |
535 | 549 | return dir, mod |
536 | 550 |
|
537 | 551 |
|
|
0 commit comments