Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/cattrs/gen/_lc.py: 21%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

14 statements  

1"""Line-cache functionality.""" 

2 

3import linecache 

4 

5 

6def generate_unique_filename(cls: type, func_name: str, lines: list[str] = []) -> str: 

7 """ 

8 Create a "filename" suitable for a function being generated. 

9 

10 If *lines* are provided, insert them in the first free spot or stop 

11 if a duplicate is found. 

12 """ 

13 extra = "" 

14 count = 1 

15 

16 while True: 

17 unique_filename = "<cattrs generated {} {}.{}{}>".format( 

18 func_name, cls.__module__, getattr(cls, "__qualname__", cls.__name__), extra 

19 ) 

20 if not lines: 

21 return unique_filename 

22 cache_line = (len("\n".join(lines)), None, lines, unique_filename) 

23 if linecache.cache.setdefault(unique_filename, cache_line) == cache_line: 

24 return unique_filename 

25 

26 # Looks like this spot is taken. Try again. 

27 count += 1 

28 extra = f"-{count}"