10
10
import os
11
11
import sys
12
12
import tokenize
13
-
14
- try :
15
- from importlib .util import find_spec # type:ignore
16
- except ImportError :
17
- import typing as t
18
- from collections import namedtuple
19
- from imp import find_module # type:ignore
20
-
21
- ModuleSpec = namedtuple ("ModuleSpec" , ["origin" , "has_location" , "submodule_search_locations" ])
22
-
23
- def find_spec ( # type:ignore
24
- name : str ,
25
- package : t .Optional [str ] = None ,
26
- ) -> t .Optional [ModuleSpec ]:
27
- """Minimal implementation as required by `find`."""
28
- try :
29
- f , path , _ = find_module (name )
30
- except ImportError :
31
- return None
32
- has_location = path is not None
33
- if f is None :
34
- return ModuleSpec (None , has_location , [path ])
35
- f .close ()
36
- return ModuleSpec (path , has_location , None )
13
+ from importlib .util import find_spec
37
14
38
15
39
16
class ArgcompleteMarkerNotFound (RuntimeError ):
@@ -42,7 +19,11 @@ class ArgcompleteMarkerNotFound(RuntimeError):
42
19
43
20
def find (name , return_package = False ):
44
21
names = name .split ("." )
45
- spec = find_spec (names [0 ])
22
+ for package_name_boundary in range (len (names )):
23
+ spec = find_spec ("." .join (names [:package_name_boundary + 1 ]))
24
+ if spec is not None and spec .origin is not None :
25
+ break
26
+
46
27
if spec is None :
47
28
raise ArgcompleteMarkerNotFound ('no module named "{}"' .format (names [0 ]))
48
29
if not spec .has_location :
@@ -53,7 +34,7 @@ def find(name, return_package=False):
53
34
return spec .origin
54
35
if len (spec .submodule_search_locations ) != 1 :
55
36
raise ArgcompleteMarkerNotFound ("expecting one search location" )
56
- path = os .path .join (spec .submodule_search_locations [0 ], * names [1 :])
37
+ path = os .path .join (spec .submodule_search_locations [0 ], * names [package_name_boundary + 1 :])
57
38
if os .path .isdir (path ):
58
39
filename = "__main__.py"
59
40
if return_package :
0 commit comments