149
155
proc = win32process.GetCurrentProcess()
150
156
info = win32process.GetProcessMemoryInfo(proc)
152
trace.note('Cannot debug memory on win32 without ctypes'
158
trace.note(gettext('Cannot debug memory on win32 without ctypes'
156
162
# using base-2 units (see HACKING.txt).
157
trace.note('WorkingSize %7dKiB'
158
'\tPeakWorking %7dKiB\t%s',
163
trace.note(gettext('WorkingSize {0:>7}KiB'
164
'\tPeakWorking {1:>7}KiB\t{2}').format(
159
165
info['WorkingSetSize'] / 1024,
160
166
info['PeakWorkingSetSize'] / 1024,
164
170
trace.note('%s', message)
165
trace.note('WorkingSize %8d KiB', info['WorkingSetSize'] / 1024)
166
trace.note('PeakWorking %8d KiB', info['PeakWorkingSetSize'] / 1024)
167
trace.note('PagefileUsage %8d KiB', info.get('PagefileUsage', 0) / 1024)
168
trace.note('PeakPagefileUsage %8d KiB',
171
trace.note(gettext('WorkingSize %8d KiB'), info['WorkingSetSize'] / 1024)
172
trace.note(gettext('PeakWorking %8d KiB'), info['PeakWorkingSetSize'] / 1024)
173
trace.note(gettext('PagefileUsage %8d KiB'), info.get('PagefileUsage', 0) / 1024)
174
trace.note(gettext('PeakPagefileUsage %8d KiB'),
169
175
info.get('PeakPagefileUsage', 0) / 1024)
170
trace.note('PrivateUsage %8d KiB', info.get('PrivateUsage', 0) / 1024)
171
trace.note('PageFaultCount %8d', info.get('PageFaultCount', 0))
176
trace.note(gettext('PrivateUsage %8d KiB'), info.get('PrivateUsage', 0) / 1024)
177
trace.note(gettext('PageFaultCount %8d'), info.get('PageFaultCount', 0))
174
180
def get_console_size(defaultx=80, defaulty=25):
536
542
:return: A list of unicode strings.
544
# First, spit the command line
538
545
s = cmdline.Splitter(command_line, single_quotes_allowed=single_quotes_allowed)
539
# Now that we've split the content, expand globs if necessary
547
# Bug #587868 Now make sure that the length of s agrees with sys.argv
548
# we do this by simply counting the number of arguments in each. The counts should
549
# agree no matter what encoding sys.argv is in (AFAIK)
550
# len(arguments) < len(sys.argv) should be an impossibility since python gets
551
# args from the very same PEB as does GetCommandLineW
554
# Now shorten the command line we get from GetCommandLineW to match sys.argv
555
if len(arguments) < len(argv):
556
raise AssertionError("Split command line can't be shorter than argv")
557
arguments = arguments[len(arguments) - len(argv):]
559
# Carry on to process globs (metachars) in the command line
560
# expand globs if necessary
540
561
# TODO: Use 'globbing' instead of 'glob.glob', this gives us stuff like
541
562
# '**/' style globs
543
for is_quoted, arg in s:
564
for is_quoted, arg in arguments:
544
565
if is_quoted or not glob.has_magic(arg):
557
578
if command_line is None:
558
579
raise ctypes.WinError()
559
580
# Skip the first argument, since we only care about parameters
560
argv = _command_line_to_argv(command_line)[1:]
561
if getattr(sys, 'frozen', None) is None:
562
# Invoked via 'python.exe' which takes the form:
563
# python.exe [PYTHON_OPTIONS] C:\Path\bzr [BZR_OPTIONS]
564
# we need to get only BZR_OPTIONS part,
565
# We already removed 'python.exe' so we remove everything up to and
566
# including the first non-option ('-') argument.
567
for idx in xrange(len(argv)):
568
if argv[idx][:1] != '-':
581
argv = _command_line_to_argv(command_line, sys.argv)[1:]
585
def get_environ_unicode(key, default=None):
586
"""Get `key` from environment as unicode or `default` if unset
588
A large enough buffer will be allocated to retrieve the value, though
589
it may take two calls to the underlying library function.
591
This needs ctypes because pywin32 does not expose the wide version.
593
cfunc = getattr(get_environ_unicode, "_c_function", None)
595
from ctypes.wintypes import DWORD, LPCWSTR, LPWSTR
596
cfunc = ctypes.WINFUNCTYPE(DWORD, LPCWSTR, LPWSTR, DWORD)(
597
("GetEnvironmentVariableW", ctypes.windll.kernel32))
598
get_environ_unicode._c_function = cfunc
599
buffer_size = 256 # heuristic, 256 characters often enough
601
buffer = ctypes.create_unicode_buffer(buffer_size)
602
length = cfunc(key, buffer, buffer_size)
604
code = ctypes.GetLastError()
605
if code == 203: # ERROR_ENVVAR_NOT_FOUND
607
raise ctypes.WinError(code)
608
if buffer_size > length:
609
return buffer[:length]
573
get_unicode_argv = None
612
get_unicode_argv = get_environ_unicode = None
616
def _pywin32_is_local_pid_dead(pid):
617
"""True if pid doesn't correspond to live process on this machine"""
619
handle = win32api.OpenProcess(1, False, pid) # PROCESS_TERMINATE
620
except pywintypes.error, e:
621
if e[0] == 5: # ERROR_ACCESS_DENIED
622
# Probably something alive we're not allowed to kill
624
elif e[0] == 87: # ERROR_INVALID_PARAMETER
629
is_local_pid_dead = _pywin32_is_local_pid_dead
630
elif has_ctypes and sys.platform == 'win32':
631
from ctypes.wintypes import BOOL, DWORD, HANDLE
632
_kernel32 = ctypes.windll.kernel32
633
_CloseHandle = ctypes.WINFUNCTYPE(BOOL, HANDLE)(
634
("CloseHandle", _kernel32))
635
_OpenProcess = ctypes.WINFUNCTYPE(HANDLE, DWORD, BOOL, DWORD)(
636
("OpenProcess", _kernel32))
637
def _ctypes_is_local_pid_dead(pid):
638
"""True if pid doesn't correspond to live process on this machine"""
639
handle = _OpenProcess(1, False, pid) # PROCESS_TERMINATE
641
errorcode = ctypes.GetLastError()
642
if errorcode == 5: # ERROR_ACCESS_DENIED
643
# Probably something alive we're not allowed to kill
645
elif errorcode == 87: # ERROR_INVALID_PARAMETER
647
raise ctypes.WinError(errorcode)
650
is_local_pid_dead = _ctypes_is_local_pid_dead
653
def _is_pywintypes_error(evalue):
654
"""True if exception instance is an error from pywin32"""
655
if has_pywintypes and isinstance(evalue, pywintypes.error):