@@ -94,21 +94,16 @@ def pytest_configure(config: Config) -> None:
94
94
95
95
96
96
class LsofFdLeakChecker :
97
- def get_open_files (self ):
98
- out = self ._exec_lsof ()
99
- open_files = self ._parse_lsof_output (out )
100
- return open_files
101
-
102
- def _exec_lsof (self ):
103
- pid = os .getpid ()
104
- # py3: use subprocess.DEVNULL directly.
105
- with open (os .devnull , "wb" ) as devnull :
106
- return subprocess .check_output (
107
- ("lsof" , "-Ffn0" , "-p" , str (pid )), stderr = devnull
108
- ).decode ()
109
-
110
- def _parse_lsof_output (self , out ):
111
- def isopen (line ):
97
+ def get_open_files (self ) -> List [Tuple [str , str ]]:
98
+ out = subprocess .run (
99
+ ("lsof" , "-Ffn0" , "-p" , str (os .getpid ())),
100
+ stdout = subprocess .PIPE ,
101
+ stderr = subprocess .DEVNULL ,
102
+ check = True ,
103
+ universal_newlines = True ,
104
+ ).stdout
105
+
106
+ def isopen (line : str ) -> bool :
112
107
return line .startswith ("f" ) and (
113
108
"deleted" not in line
114
109
and "mem" not in line
@@ -130,9 +125,9 @@ def isopen(line):
130
125
131
126
return open_files
132
127
133
- def matching_platform (self ):
128
+ def matching_platform (self ) -> bool :
134
129
try :
135
- subprocess .check_output (("lsof" , "-v" ))
130
+ subprocess .run (("lsof" , "-v" ), check = True )
136
131
except (OSError , subprocess .CalledProcessError ):
137
132
return False
138
133
else :
@@ -149,16 +144,17 @@ def pytest_runtest_protocol(self, item: Item) -> Generator[None, None, None]:
149
144
new_fds = {t [0 ] for t in lines2 } - {t [0 ] for t in lines1 }
150
145
leaked_files = [t for t in lines2 if t [0 ] in new_fds ]
151
146
if leaked_files :
152
- error = []
153
- error .append ("***** %s FD leakage detected" % len (leaked_files ))
154
- error .extend ([str (f ) for f in leaked_files ])
155
- error .append ("*** Before:" )
156
- error .extend ([str (f ) for f in lines1 ])
157
- error .append ("*** After:" )
158
- error .extend ([str (f ) for f in lines2 ])
159
- error .append (error [0 ])
160
- error .append ("*** function %s:%s: %s " % item .location )
161
- error .append ("See issue #2366" )
147
+ error = [
148
+ "***** %s FD leakage detected" % len (leaked_files ),
149
+ * (str (f ) for f in leaked_files ),
150
+ "*** Before:" ,
151
+ * (str (f ) for f in lines1 ),
152
+ "*** After:" ,
153
+ * (str (f ) for f in lines2 ),
154
+ "***** %s FD leakage detected" % len (leaked_files ),
155
+ "*** function %s:%s: %s " % item .location ,
156
+ "See issue #2366" ,
157
+ ]
162
158
item .warn (pytest .PytestWarning ("\n " .join (error )))
163
159
164
160
0 commit comments