##// END OF EJS Templates
typing: add type hints to mercurial/win32.py...
Matt Harbison -
r50705:a9faacdc default
parent child Browse files
Show More
@@ -625,7 +625,7 b' def groupmembers(name):'
625 625 return pycompat.rapply(pycompat.fsencode, list(grp.getgrnam(name).gr_mem))
626 626
627 627
628 def spawndetached(args):
628 def spawndetached(args) -> int:
629 629 return os.spawnvp(os.P_NOWAIT | getattr(os, 'P_DETACH', 0), args[0], args)
630 630
631 631
@@ -585,7 +585,7 b' def hgcmd():'
585 585 return _gethgcmd()
586 586
587 587
588 def rundetached(args, condfn):
588 def rundetached(args, condfn) -> int:
589 589 """Execute the argument list in a detached process.
590 590
591 591 condfn is a callable which is called repeatedly and should return
@@ -621,6 +621,12 b' def rundetached(args, condfn):'
621 621 if prevhandler is not None:
622 622 signal.signal(signal.SIGCHLD, prevhandler)
623 623
624 # pytype seems to get confused by not having a return in the finally
625 # block, and thinks the return value should be Optional[int] here. It
626 # appears to be https://github.com/google/pytype/issues/938, without
627 # the `with` clause.
628 pass # pytype: disable=bad-return-type
629
624 630
625 631 @contextlib.contextmanager
626 632 def uninterruptible(warn):
@@ -14,6 +14,13 b' import os'
14 14 import random
15 15 import subprocess
16 16
17 from typing import (
18 List,
19 NoReturn,
20 Optional,
21 Tuple,
22 )
23
17 24 from . import (
18 25 encoding,
19 26 pycompat,
@@ -356,7 +363,7 b' except AttributeError:'
356 363 _kernel32.PeekNamedPipe.restype = _BOOL
357 364
358 365
359 def _raiseoserror(name):
366 def _raiseoserror(name: bytes) -> NoReturn:
360 367 # Force the code to a signed int to avoid an 'int too large' error.
361 368 # See https://bugs.python.org/issue28474
362 369 code = _kernel32.GetLastError()
@@ -368,7 +375,7 b' def _raiseoserror(name):'
368 375 )
369 376
370 377
371 def _getfileinfo(name):
378 def _getfileinfo(name: bytes) -> _BY_HANDLE_FILE_INFORMATION:
372 379 fh = _kernel32.CreateFileA(
373 380 name,
374 381 0,
@@ -389,7 +396,7 b' def _getfileinfo(name):'
389 396 _kernel32.CloseHandle(fh)
390 397
391 398
392 def checkcertificatechain(cert, build=True):
399 def checkcertificatechain(cert: bytes, build: bool = True) -> bool:
393 400 """Tests the given certificate to see if there is a complete chain to a
394 401 trusted root certificate. As a side effect, missing certificates are
395 402 downloaded and installed unless ``build=False``. True is returned if a
@@ -439,7 +446,7 b' def checkcertificatechain(cert, build=Tr'
439 446 _crypt32.CertFreeCertificateContext(certctx)
440 447
441 448
442 def oslink(src, dst):
449 def oslink(src: bytes, dst: bytes) -> None:
443 450 try:
444 451 if not _kernel32.CreateHardLinkA(dst, src, None):
445 452 _raiseoserror(src)
@@ -447,12 +454,12 b' def oslink(src, dst):'
447 454 _raiseoserror(src)
448 455
449 456
450 def nlinks(name):
457 def nlinks(name: bytes) -> int:
451 458 '''return number of hardlinks for the given file'''
452 459 return _getfileinfo(name).nNumberOfLinks
453 460
454 461
455 def samefile(path1, path2):
462 def samefile(path1: bytes, path2: bytes) -> bool:
456 463 '''Returns whether path1 and path2 refer to the same file or directory.'''
457 464 res1 = _getfileinfo(path1)
458 465 res2 = _getfileinfo(path2)
@@ -463,14 +470,14 b' def samefile(path1, path2):'
463 470 )
464 471
465 472
466 def samedevice(path1, path2):
473 def samedevice(path1: bytes, path2: bytes) -> bool:
467 474 '''Returns whether path1 and path2 are on the same device.'''
468 475 res1 = _getfileinfo(path1)
469 476 res2 = _getfileinfo(path2)
470 477 return res1.dwVolumeSerialNumber == res2.dwVolumeSerialNumber
471 478
472 479
473 def peekpipe(pipe):
480 def peekpipe(pipe) -> int:
474 481 handle = msvcrt.get_osfhandle(pipe.fileno()) # pytype: disable=module-attr
475 482 avail = _DWORD()
476 483
@@ -485,14 +492,14 b' def peekpipe(pipe):'
485 492 return avail.value
486 493
487 494
488 def lasterrorwaspipeerror(err):
495 def lasterrorwaspipeerror(err) -> bool:
489 496 if err.errno != errno.EINVAL:
490 497 return False
491 498 err = _kernel32.GetLastError()
492 499 return err == _ERROR_BROKEN_PIPE or err == _ERROR_NO_DATA
493 500
494 501
495 def testpid(pid):
502 def testpid(pid: int) -> bool:
496 503 """return True if pid is still running or unable to
497 504 determine, False otherwise"""
498 505 h = _kernel32.OpenProcess(_PROCESS_QUERY_INFORMATION, False, pid)
@@ -506,7 +513,7 b' def testpid(pid):'
506 513 return _kernel32.GetLastError() != _ERROR_INVALID_PARAMETER
507 514
508 515
509 def executablepath():
516 def executablepath() -> bytes:
510 517 '''return full path of hg.exe'''
511 518 size = 600
512 519 buf = ctypes.create_string_buffer(size + 1)
@@ -520,7 +527,7 b' def executablepath():'
520 527 return buf.value
521 528
522 529
523 def getvolumename(path):
530 def getvolumename(path: bytes) -> Optional[bytes]:
524 531 """Get the mount point of the filesystem from a directory or file
525 532 (best-effort)
526 533
@@ -541,7 +548,7 b' def getvolumename(path):'
541 548 return buf.value
542 549
543 550
544 def getfstype(path):
551 def getfstype(path: bytes) -> Optional[bytes]:
545 552 """Get the filesystem type name from a directory or file (best-effort)
546 553
547 554 Returns None if we are unsure. Raises OSError on ENOENT, EPERM, etc.
@@ -572,7 +579,7 b' def getfstype(path):'
572 579 return name.value
573 580
574 581
575 def getuser():
582 def getuser() -> bytes:
576 583 '''return name of current user'''
577 584 size = _DWORD(300)
578 585 buf = ctypes.create_string_buffer(size.value + 1)
@@ -581,10 +588,10 b' def getuser():'
581 588 return buf.value
582 589
583 590
584 _signalhandler = []
591 _signalhandler: List[_SIGNAL_HANDLER] = []
585 592
586 593
587 def setsignalhandler():
594 def setsignalhandler() -> None:
588 595 """Register a termination handler for console events including
589 596 CTRL+C. python signal handlers do not work well with socket
590 597 operations.
@@ -601,7 +608,7 b' def setsignalhandler():'
601 608 raise ctypes.WinError() # pytype: disable=module-attr
602 609
603 610
604 def hidewindow():
611 def hidewindow() -> None:
605 612 def callback(hwnd, pid):
606 613 wpid = _DWORD()
607 614 _user32.GetWindowThreadProcessId(hwnd, ctypes.byref(wpid))
@@ -614,7 +621,7 b' def hidewindow():'
614 621 _user32.EnumWindows(_WNDENUMPROC(callback), pid)
615 622
616 623
617 def termsize():
624 def termsize() -> Tuple[int, int]:
618 625 # cmd.exe does not handle CR like a unix console, the CR is
619 626 # counted in the line length. On 80 columns consoles, if 80
620 627 # characters are written, the following CR won't apply on the
@@ -635,7 +642,7 b' def termsize():'
635 642 return width, height
636 643
637 644
638 def enablevtmode():
645 def enablevtmode() -> bool:
639 646 """Enable virtual terminal mode for the associated console. Return True if
640 647 enabled, else False."""
641 648
@@ -661,7 +668,7 b' def enablevtmode():'
661 668 return True
662 669
663 670
664 def spawndetached(args):
671 def spawndetached(args: List[bytes]) -> int:
665 672 # No standard library function really spawns a fully detached
666 673 # process under win32 because they allocate pipes or other objects
667 674 # to handle standard streams communications. Passing these objects
@@ -703,7 +710,7 b' def spawndetached(args):'
703 710 return pi.dwProcessId
704 711
705 712
706 def unlink(f):
713 def unlink(f: bytes) -> None:
707 714 '''try to implement POSIX' unlink semantics on Windows'''
708 715
709 716 if os.path.isdir(f):
@@ -758,7 +765,7 b' def unlink(f):'
758 765 pass
759 766
760 767
761 def makedir(path, notindexed):
768 def makedir(path: bytes, notindexed: bool) -> None:
762 769 os.mkdir(path)
763 770 if notindexed:
764 771 _kernel32.SetFileAttributesA(path, _FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)
General Comments 0
You need to be logged in to leave comments. Login now