17
17
import warnings
18
18
from contextlib import contextmanager
19
19
from pathlib import Path
20
+ from typing import Optional
20
21
21
22
pjoin = os .path .join
22
23
@@ -43,7 +44,7 @@ def get_home_dir():
43
44
return homedir
44
45
45
46
46
- _dtemps = {}
47
+ _dtemps : dict = {}
47
48
48
49
49
50
def _mkdtemp_once (name ):
@@ -159,7 +160,7 @@ def jupyter_path(*subdirs):
159
160
['~/.local/jupyter/kernels', '/usr/local/share/jupyter/kernels']
160
161
"""
161
162
162
- paths = []
163
+ paths : list = []
163
164
164
165
# highest priority is explicit environment variable
165
166
if os .environ .get ("JUPYTER_PATH" ):
@@ -170,13 +171,16 @@ def jupyter_path(*subdirs):
170
171
if site .ENABLE_USER_SITE :
171
172
# Check if site.getuserbase() exists to be compatible with virtualenv,
172
173
# which often does not have this method.
174
+ userbase : Optional [str ]
173
175
if hasattr (site , "getuserbase" ):
174
176
userbase = site .getuserbase ()
175
177
else :
176
178
userbase = site .USER_BASE
177
- userdir = os .path .join (userbase , "share" , "jupyter" )
178
- if userdir not in user :
179
- user .append (userdir )
179
+
180
+ if userbase :
181
+ userdir = os .path .join (userbase , "share" , "jupyter" )
182
+ if userdir not in user :
183
+ user .append (userdir )
180
184
181
185
env = [p for p in ENV_JUPYTER_PATH if p not in SYSTEM_JUPYTER_PATH ]
182
186
@@ -225,7 +229,7 @@ def jupyter_config_path():
225
229
# jupyter_config_dir makes a blank config when JUPYTER_NO_CONFIG is set.
226
230
return [jupyter_config_dir ()]
227
231
228
- paths = []
232
+ paths : list = []
229
233
230
234
# highest priority is explicit environment variable
231
235
if os .environ .get ("JUPYTER_CONFIG_PATH" ):
@@ -234,16 +238,18 @@ def jupyter_config_path():
234
238
# Next is environment or user, depending on the JUPYTER_PREFER_ENV_PATH flag
235
239
user = [jupyter_config_dir ()]
236
240
if site .ENABLE_USER_SITE :
241
+ userbase : Optional [str ]
237
242
# Check if site.getuserbase() exists to be compatible with virtualenv,
238
243
# which often does not have this method.
239
244
if hasattr (site , "getuserbase" ):
240
245
userbase = site .getuserbase ()
241
246
else :
242
247
userbase = site .USER_BASE
243
248
244
- userdir = os .path .join (userbase , "etc" , "jupyter" )
245
- if userdir not in user :
246
- user .append (userdir )
249
+ if userbase :
250
+ userdir = os .path .join (userbase , "etc" , "jupyter" )
251
+ if userdir not in user :
252
+ user .append (userdir )
247
253
248
254
env = [p for p in ENV_CONFIG_PATH if p not in SYSTEM_CONFIG_PATH ]
249
255
@@ -298,7 +304,7 @@ def is_file_hidden_win(abs_path, stat_res=None):
298
304
raise
299
305
300
306
try :
301
- if stat_res .st_file_attributes & stat .FILE_ATTRIBUTE_HIDDEN :
307
+ if stat_res .st_file_attributes & stat .FILE_ATTRIBUTE_HIDDEN : # type:ignore[attr-defined]
302
308
return True
303
309
except AttributeError :
304
310
# allow AttributeError on PyPy for Windows
@@ -424,12 +430,12 @@ def win32_restrict_file_to_user(fname):
424
430
The path to the file to secure
425
431
"""
426
432
try :
427
- import win32api
433
+ import win32api # type:ignore[import]
428
434
except ImportError :
429
435
return _win32_restrict_file_to_user_ctypes (fname )
430
436
431
- import ntsecuritycon as con
432
- import win32security
437
+ import ntsecuritycon as con # type:ignore[import]
438
+ import win32security # type:ignore[import]
433
439
434
440
# everyone, _domain, _type = win32security.LookupAccountName("", "Everyone")
435
441
admins = win32security .CreateWellKnownSid (win32security .WinBuiltinAdministratorsSid )
@@ -470,8 +476,8 @@ def _win32_restrict_file_to_user_ctypes(fname):
470
476
import ctypes
471
477
from ctypes import wintypes
472
478
473
- advapi32 = ctypes .WinDLL ("advapi32" , use_last_error = True )
474
- secur32 = ctypes .WinDLL ("secur32" , use_last_error = True )
479
+ advapi32 = ctypes .WinDLL ("advapi32" , use_last_error = True ) # type:ignore[attr-defined]
480
+ secur32 = ctypes .WinDLL ("secur32" , use_last_error = True ) # type:ignore[attr-defined]
475
481
476
482
NameSamCompatible = 2
477
483
WinBuiltinAdministratorsSid = 26
@@ -520,7 +526,7 @@ class ACL(ctypes.Structure):
520
526
521
527
def _nonzero_success (result , func , args ):
522
528
if not result :
523
- raise ctypes .WinError (ctypes .get_last_error ())
529
+ raise ctypes .WinError (ctypes .get_last_error ()) # type:ignore[attr-defined]
524
530
return args
525
531
526
532
secur32 .GetUserNameExW .errcheck = _nonzero_success
@@ -627,7 +633,7 @@ def CreateWellKnownSid(WellKnownSidType):
627
633
try :
628
634
advapi32 .CreateWellKnownSid (WellKnownSidType , None , pSid , ctypes .byref (cbSid ))
629
635
except OSError as e :
630
- if e .winerror != ERROR_INSUFFICIENT_BUFFER :
636
+ if e .winerror != ERROR_INSUFFICIENT_BUFFER : # type:ignore[attr-defined]
631
637
raise
632
638
pSid = (ctypes .c_char * cbSid .value )()
633
639
advapi32 .CreateWellKnownSid (WellKnownSidType , None , pSid , ctypes .byref (cbSid ))
@@ -640,7 +646,7 @@ def GetUserNameEx(NameFormat):
640
646
try :
641
647
secur32 .GetUserNameExW (NameFormat , None , nSize )
642
648
except OSError as e :
643
- if e .winerror != ERROR_MORE_DATA :
649
+ if e .winerror != ERROR_MORE_DATA : # type:ignore[attr-defined]
644
650
raise
645
651
if not nSize .contents .value :
646
652
return None
@@ -665,7 +671,7 @@ def LookupAccountName(lpSystemName, lpAccountName):
665
671
ctypes .byref (peUse ),
666
672
)
667
673
except OSError as e :
668
- if e .winerror != ERROR_INSUFFICIENT_BUFFER :
674
+ if e .winerror != ERROR_INSUFFICIENT_BUFFER : # type:ignore[attr-defined]
669
675
raise
670
676
Sid = ctypes .create_unicode_buffer ("" , cbSid .value )
671
677
pSid = ctypes .cast (ctypes .pointer (Sid ), wintypes .LPVOID )
@@ -680,7 +686,7 @@ def LookupAccountName(lpSystemName, lpAccountName):
680
686
ctypes .byref (peUse ),
681
687
)
682
688
if not success :
683
- raise ctypes .WinError ()
689
+ raise ctypes .WinError () # type:ignore[attr-defined]
684
690
return pSid , lpReferencedDomainName .value , peUse .value
685
691
686
692
def AddAccessAllowedAce (pAcl , dwAceRevision , AccessMask , pSid ):
@@ -700,7 +706,7 @@ def GetFileSecurity(lpFileName, RequestedInformation):
700
706
ctypes .byref (nLength ),
701
707
)
702
708
except OSError as e :
703
- if e .winerror != ERROR_INSUFFICIENT_BUFFER :
709
+ if e .winerror != ERROR_INSUFFICIENT_BUFFER : # type:ignore[attr-defined]
704
710
raise
705
711
if not nLength .value :
706
712
return None
@@ -750,7 +756,7 @@ def MakeAbsoluteSD(pSelfRelativeSecurityDescriptor):
750
756
ctypes .byref (lpdwPrimaryGroupSize ),
751
757
)
752
758
except OSError as e :
753
- if e .winerror != ERROR_INSUFFICIENT_BUFFER :
759
+ if e .winerror != ERROR_INSUFFICIENT_BUFFER : # type:ignore[attr-defined]
754
760
raise
755
761
pAbsoluteSecurityDescriptor = (wintypes .BYTE * lpdwAbsoluteSecurityDescriptorSize .value )()
756
762
pDaclData = (wintypes .BYTE * lpdwDaclSize .value )()
@@ -788,7 +794,7 @@ def MakeSelfRelativeSD(pAbsoluteSecurityDescriptor):
788
794
ctypes .byref (lpdwBufferLength ),
789
795
)
790
796
except OSError as e :
791
- if e .winerror != ERROR_INSUFFICIENT_BUFFER :
797
+ if e .winerror != ERROR_INSUFFICIENT_BUFFER : # type:ignore[attr-defined]
792
798
raise
793
799
pSelfRelativeSecurityDescriptor = (wintypes .BYTE * lpdwBufferLength .value )()
794
800
advapi32 .MakeSelfRelativeSD (
@@ -907,7 +913,7 @@ def issue_insecure_write_warning():
907
913
def format_warning (msg , * args , ** kwargs ):
908
914
return str (msg ) + "\n "
909
915
910
- warnings .formatwarning = format_warning
916
+ warnings .formatwarning = format_warning # type:ignore[assignment]
911
917
warnings .warn (
912
918
"WARNING: Insecure writes have been enabled via environment variable "
913
919
"'JUPYTER_ALLOW_INSECURE_WRITES'! If this is not intended, remove the "
0 commit comments