1
1
import inspect
2
- import os
3
2
import re
4
3
import warnings
5
4
from collections .abc import Callable , Collection
5
+ from pathlib import Path
6
6
from re import Pattern
7
7
from typing import TYPE_CHECKING , Any , ClassVar , cast
8
8
@@ -288,28 +288,32 @@ class ExternalCOp(COp):
288
288
_cop_num_outputs : int | None = None
289
289
290
290
@classmethod
291
- def get_path (cls , f : str ) -> str :
291
+ def get_path (cls , f : Path ) -> Path :
292
292
"""Convert a path relative to the location of the class file into an absolute path.
293
293
294
294
Paths that are already absolute are passed through unchanged.
295
295
296
296
"""
297
- if not os . path . isabs ( f ):
297
+ if not f . is_absolute ( ):
298
298
class_file = inspect .getfile (cls )
299
- class_dir = os . path . dirname (class_file )
300
- f = os . path . realpath ( os . path . join ( class_dir , f ) )
299
+ class_dir = Path (class_file ). parent
300
+ f = ( class_dir / f ). resolve ( )
301
301
return f
302
302
303
- def __init__ (self , func_files : str | list [str ], func_name : str | None = None ):
303
+ def __init__ (
304
+ self ,
305
+ func_files : str | Path | list [str ] | list [Path ],
306
+ func_name : str | None = None ,
307
+ ):
304
308
"""
305
309
Sections are loaded from files in order with sections in later
306
310
files overriding sections in previous files.
307
311
308
312
"""
309
313
if not isinstance (func_files , list ):
310
- self .func_files = [func_files ]
314
+ self .func_files = [Path ( func_files ) ]
311
315
else :
312
- self .func_files = func_files
316
+ self .func_files = [ Path ( func_file ) for func_file in func_files ]
313
317
314
318
self .func_codes : list [str ] = []
315
319
# Keep the original name. If we reload old pickle, we want to
@@ -334,12 +338,11 @@ def __init__(self, func_files: str | list[str], func_name: str | None = None):
334
338
"Cannot have an `op_code_cleanup` section and specify `func_name`"
335
339
)
336
340
337
- def load_c_code (self , func_files : list [str ]) -> None :
341
+ def load_c_code (self , func_files : list [Path ]) -> None :
338
342
"""Loads the C code to perform the `Op`."""
339
- func_files = [self .get_path (f ) for f in func_files ]
340
343
for func_file in func_files :
341
- with open (func_file ) as f :
342
- self .func_codes .append (f . read ( ))
344
+ func_file = self . get_path (func_file )
345
+ self .func_codes .append (func_file . read_text ( encoding = "utf-8" ))
343
346
344
347
# If both the old section markers and the new section markers are
345
348
# present, raise an error because we don't know which ones to follow.
0 commit comments