Skip to content

Commit

Permalink
special: added len check to special(), test, fixed text, ...
Browse files Browse the repository at this point in the history
  • Loading branch information
YoSTEALTH committed May 2, 2024
1 parent 46ede5d commit fb84b2f
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 10 deletions.
2 changes: 1 addition & 1 deletion dynamic_import/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self, package, info, module, *args, **kwargs):
super().__init__(package, None, *args, **kwargs)
self.__PACKAGE__ = package
self.__INFO__ = info
# note: ^ this needs to mimic magic method name since those are made to raise error
# note: ^ these needs to mimic magic method name since those are made to raise error

# only include special name from previous module, all other names should be
# imported through this `Module`
Expand Down
11 changes: 8 additions & 3 deletions dynamic_import/prep.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ def prep_package(pkg_name, pkg_path, recursive, exclude_file, exclude_dir):
'''
info = {}
dir_mtime = {}
for module, file_path, mtime in prep_files(pkg_name, pkg_path, recursive, dir_mtime, exclude_file, exclude_dir):
for module, file_path, mtime in prep_files(pkg_name,
pkg_path,
recursive,
dir_mtime,
exclude_file,
exclude_dir):
for var, variables in prep_variables(module, file_path):
info[var] = (module, file_path, variables, mtime)
return info, dir_mtime
Expand Down Expand Up @@ -78,8 +83,8 @@ def prep_files(pkg_name, pkg_path, recursive, dir_mtime, exclude_file, exclude_d
yield f'{module_name}', file_path, mtime
else:
yield f'{module_name}.{file.split(".")[0]}', file_path, mtime
# ('pkg.sub.module', '/path/pkg/sub/module.py', 123.45)
# ('pkg.sub.module', '/path/pkg/sub/module.cpython-312-x86_64-linux-gnu.so', 123.45)
# ('pkg.sub.module', '/pkg/sub/module.py', 123.45)
# ('pkg.sub.module', '/pkg/sub/module*.so', 123.45)
if not recursive:
break

Expand Down
2 changes: 1 addition & 1 deletion dynamic_import/special.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def special(iterable, error=False):
- if `error=True` will raise exception if special name is found.
'''
for name in iterable:
if (name[0:2] == '__' == name[-2:]) and (name[2] != '_' != name[-3]):
if len(name) > 4 and (name[0:2] == '__' == name[-2:]) and (name[2] != '_' != name[-3]):
if error:
raise ValueError(f'special name like {name!r} is not supported')
continue
Expand Down
2 changes: 1 addition & 1 deletion dynamic_import/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = '2024.4.2'
version = '2024.5.2'
4 changes: 4 additions & 0 deletions test/conflict/my_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@

def my_func():
return 'my_func() called'


def another_func():
return 'another_func() called'
16 changes: 15 additions & 1 deletion test/conflict_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
import conflict


def test_conflict():
from conflict import my_func
from conflict import my_func, another_func
assert my_func() == 'my_func() called'
assert another_func() == 'another_func() called'


def test_multiple_conflict():
assert conflict.another_func() == 'another_func() called'
assert conflict.my_func() == 'my_func() called'


def test_multiple_conflict2():
assert conflict.another_func() == 'another_func() called'
assert conflict.my_func() == 'my_func() called'
2 changes: 1 addition & 1 deletion test/record_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_add_record(tmp_dir):
with open(record_file, 'rb') as file:
assert file.read() == b'pkg/__pycache__/__init__.importer-313.pyc,,\r\n'

# try again, shouldn't change since cache path alrady exists.
# try again, shouldn't change since cache path already exists.
add_record(pkg_name, cache_path)
with open(record_file, 'rb') as file:
assert file.read() == b'pkg/__pycache__/__init__.importer-313.pyc,,\r\n'
Expand Down
6 changes: 4 additions & 2 deletions test/special_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@


def test_special():
my_list = ['first', '__name__', '_name', 'name_', 'name', 'name__', '__name', '___name___', '', 'last']
result = ('first', '_name', 'name_', 'name', 'name__', '__name', '___name___', '', 'last')
my_list = ['first', '__name__', '_name', 'name_', 'name', 'name__', '__name', '___name___',
'', 'last', '__q__', '____', '___', '__', '_']
result = ('first', '_name', 'name_', 'name', 'name__', '__name', '___name___',
'', 'last', '____', '___', '__', '_')
assert tuple(special(my_list)) == result

with pytest.raises(ValueError, match="special name like '__name__' is not supported"):
Expand Down

0 comments on commit fb84b2f

Please sign in to comment.