Encapsulates Win32 handles to avoid leaking them. @type inherit: bool @ivar inherit: C{True} if the handle is to be inherited by child processes, C{False} otherwise. @type protectFromClose: bool @ivar protectFromClose: Set to C{True} to prevent the handle from being
| 570 | |
| 571 | |
| 572 | class Handle(object): |
| 573 | """ |
| 574 | Encapsulates Win32 handles to avoid leaking them. |
| 575 | |
| 576 | @type inherit: bool |
| 577 | @ivar inherit: C{True} if the handle is to be inherited by child processes, |
| 578 | C{False} otherwise. |
| 579 | |
| 580 | @type protectFromClose: bool |
| 581 | @ivar protectFromClose: Set to C{True} to prevent the handle from being |
| 582 | closed. Must be set to C{False} before you're done using the handle, |
| 583 | or it will be left open until the debugger exits. Use with care! |
| 584 | |
| 585 | @see: |
| 586 | L{ProcessHandle}, L{ThreadHandle}, L{FileHandle}, L{SnapshotHandle} |
| 587 | """ |
| 588 | |
| 589 | # XXX DEBUG |
| 590 | # When this private flag is True each Handle will print a message to |
| 591 | # standard output when it's created and destroyed. This is useful for |
| 592 | # detecting handle leaks within WinAppDbg itself. |
| 593 | __bLeakDetection = False |
| 594 | |
| 595 | def __init__(self, aHandle=None, bOwnership=True): |
| 596 | """ |
| 597 | @type aHandle: int |
| 598 | @param aHandle: Win32 handle value. |
| 599 | |
| 600 | @type bOwnership: bool |
| 601 | @param bOwnership: |
| 602 | C{True} if we own the handle and we need to close it. |
| 603 | C{False} if someone else will be calling L{CloseHandle}. |
| 604 | """ |
| 605 | super(Handle, self).__init__() |
| 606 | self._value = self._normalize(aHandle) |
| 607 | self.bOwnership = bOwnership |
| 608 | if Handle.__bLeakDetection: # XXX DEBUG |
| 609 | print("INIT HANDLE (%r) %r" % (self.value, self)) |
| 610 | |
| 611 | @property |
| 612 | def value(self): |
| 613 | return self._value |
| 614 | |
| 615 | def __del__(self): |
| 616 | """ |
| 617 | Closes the Win32 handle when the Python object is destroyed. |
| 618 | """ |
| 619 | try: |
| 620 | if Handle.__bLeakDetection: # XXX DEBUG |
| 621 | print("DEL HANDLE %r" % self) |
| 622 | self.close() |
| 623 | except Exception: |
| 624 | pass |
| 625 | |
| 626 | def __enter__(self): |
| 627 | """ |
| 628 | Compatibility with the "C{with}" Python statement. |
| 629 | """ |
no outgoing calls
no test coverage detected