66import struct
77import subprocess
88import sys
9+ from collections .abc import Iterable , Sequence
910from functools import partial
11+ from pathlib import Path
1012
1113
1214__all__ = [
@@ -85,18 +87,6 @@ def add_excepthook(hook):
8587 sys .excepthook = __call_excepthooks
8688
8789
88- def exc_message (e ):
89- """
90- In python 3.x, when an exception is reraised it saves original
91- exception in its args, therefore in order to find the actual
92- message, we need to unpack arguments recursively.
93- """
94- msg = e .args [0 ]
95- if isinstance (msg , Exception ):
96- return exc_message (msg )
97- return msg
98-
99-
10090def get_unbound_function (unbound ):
10191 # Op.make_thunk isn't bound, so don't have a __func__ attr.
10292 # But bound method, have a __func__ method that point to the
@@ -106,8 +96,9 @@ def get_unbound_function(unbound):
10696 return unbound
10797
10898
109- def maybe_add_to_os_environ_pathlist (var , newpath ):
110- """Unfortunately, Conda offers to make itself the default Python
99+ def maybe_add_to_os_environ_pathlist (var : str , newpath : Path | str ) -> None :
100+ """
101+ Unfortunately, Conda offers to make itself the default Python
111102 and those who use it that way will probably not activate envs
112103 correctly meaning e.g. mingw-w64 g++ may not be on their PATH.
113104
@@ -118,18 +109,18 @@ def maybe_add_to_os_environ_pathlist(var, newpath):
118109 The reason we check first is because Windows environment vars
119110 are limited to 8191 characters and it is easy to hit that.
120111
121- `var` will typically be 'PATH'."""
122-
123- import os
112+ `var` will typically be 'PATH'.
113+ """
114+ if not Path (newpath ).is_absolute ():
115+ return
124116
125- if os .path .isabs (newpath ):
126- try :
127- oldpaths = os .environ [var ].split (os .pathsep )
128- if newpath not in oldpaths :
129- newpaths = os .pathsep .join ([newpath , * oldpaths ])
130- os .environ [var ] = newpaths
131- except Exception :
132- pass
117+ try :
118+ oldpaths = os .environ [var ].split (os .pathsep )
119+ if str (newpath ) not in oldpaths :
120+ newpaths = os .pathsep .join ([str (newpath ), * oldpaths ])
121+ os .environ [var ] = newpaths
122+ except Exception :
123+ pass
133124
134125
135126def subprocess_Popen (command , ** params ):
@@ -210,7 +201,7 @@ def output_subprocess_Popen(command, **params):
210201 return (* out , p .returncode )
211202
212203
213- def hash_from_code (msg ) :
204+ def hash_from_code (msg : str | bytes ) -> str :
214205 """Return the SHA256 hash of a string or bytes."""
215206 # hashlib.sha256() requires an object that supports buffer interface,
216207 # but Python 3 (unicode) strings don't.
@@ -221,27 +212,7 @@ def hash_from_code(msg):
221212 return "m" + hashlib .sha256 (msg ).hexdigest ()
222213
223214
224- def memoize (f ):
225- """
226- Cache the return value for each tuple of arguments (which must be hashable).
227-
228- """
229- cache = {}
230-
231- def rval (* args , ** kwargs ):
232- kwtup = tuple (kwargs .items ())
233- key = (args , kwtup )
234- if key not in cache :
235- val = f (* args , ** kwargs )
236- cache [key ] = val
237- else :
238- val = cache [key ]
239- return val
240-
241- return rval
242-
243-
244- def uniq (seq ):
215+ def uniq (seq : Sequence ) -> list :
245216 """
246217 Do not use set, this must always return the same value at the same index.
247218 If we just exchange other values, but keep the same pattern of duplication,
@@ -253,11 +224,12 @@ def uniq(seq):
253224 return [x for i , x in enumerate (seq ) if seq .index (x ) == i ]
254225
255226
256- def difference (seq1 , seq2 ):
227+ def difference (seq1 : Iterable , seq2 : Iterable ):
257228 r"""
258229 Returns all elements in seq1 which are not in seq2: i.e ``seq1\seq2``.
259230
260231 """
232+ seq2 = list (seq2 )
261233 try :
262234 # try to use O(const * len(seq1)) algo
263235 if len (seq2 ) < 4 : # I'm guessing this threshold -JB
@@ -285,7 +257,7 @@ def from_return_values(values):
285257 return [values ]
286258
287259
288- def flatten (a ):
260+ def flatten (a ) -> list :
289261 """
290262 Recursively flatten tuple, list and set in a list.
291263
0 commit comments