blob: 15cce34e822cfae8ffe04d9c7247227d503b38e3 [file] [log] [blame]
Victor Stinnerb907abc2017-08-17 16:40:51 +02001"""
2Collect various informations about Python to help debugging test failures.
3"""
4from __future__ import print_function
5import re
6import sys
7import traceback
8
9
10def normalize_text(text):
11 if text is None:
12 return None
13 text = str(text)
14 text = re.sub(r'\s+', ' ', text)
15 return text.strip()
16
17
18class PythonInfo:
19 def __init__(self):
20 self.info = {}
21
22 def add(self, key, value):
23 if key in self.info:
24 raise ValueError("duplicate key: %r" % key)
25
Victor Stinnerad7eaed2017-08-18 12:08:47 +020026 if value is None:
27 return
28
29 if not isinstance(value, int):
30 if not isinstance(value, str):
31 # convert other objects like sys.flags to string
32 value = str(value)
33
Victor Stinnerb907abc2017-08-17 16:40:51 +020034 value = value.strip()
35 if not value:
36 return
Victor Stinnerb907abc2017-08-17 16:40:51 +020037
38 self.info[key] = value
39
40 def get_infos(self):
41 """
42 Get informations as a key:value dictionary where values are strings.
43 """
44 return {key: str(value) for key, value in self.info.items()}
45
46
47def copy_attributes(info_add, obj, name_fmt, attributes, *, formatter=None):
48 for attr in attributes:
49 value = getattr(obj, attr, None)
50 if value is None:
51 continue
52 name = name_fmt % attr
53 if formatter is not None:
54 value = formatter(attr, value)
55 info_add(name, value)
56
57
Victor Stinnerad7eaed2017-08-18 12:08:47 +020058def call_func(info_add, name, mod, func_name, *, formatter=None):
59 try:
60 func = getattr(mod, func_name)
61 except AttributeError:
62 return
63 value = func()
64 if formatter is not None:
65 value = formatter(value)
66 info_add(name, value)
Victor Stinnerb907abc2017-08-17 16:40:51 +020067
Victor Stinnerad7eaed2017-08-18 12:08:47 +020068
69def collect_sys(info_add):
Victor Stinnerb907abc2017-08-17 16:40:51 +020070 attributes = (
71 '_framework',
Victor Stinnerad7eaed2017-08-18 12:08:47 +020072 'abiflags',
73 'api_version',
74 'builtin_module_names',
Victor Stinnerb907abc2017-08-17 16:40:51 +020075 'byteorder',
Victor Stinnerad7eaed2017-08-18 12:08:47 +020076 'dont_write_bytecode',
Victor Stinnerb907abc2017-08-17 16:40:51 +020077 'executable',
78 'flags',
Victor Stinnerad7eaed2017-08-18 12:08:47 +020079 'float_info',
80 'float_repr_style',
81 'hash_info',
82 'hexversion',
83 'implementation',
84 'int_info',
Victor Stinnerb907abc2017-08-17 16:40:51 +020085 'maxsize',
86 'maxunicode',
Victor Stinnerad7eaed2017-08-18 12:08:47 +020087 'path',
88 'platform',
89 'prefix',
90 'thread_info',
Victor Stinnerb907abc2017-08-17 16:40:51 +020091 'version',
Victor Stinnerad7eaed2017-08-18 12:08:47 +020092 'version_info',
93 'winver',
Victor Stinnerb907abc2017-08-17 16:40:51 +020094 )
Victor Stinnerad7eaed2017-08-18 12:08:47 +020095 copy_attributes(info_add, sys, 'sys.%s', attributes)
96
97 call_func(info_add, 'sys.androidapilevel', sys, 'getandroidapilevel')
98 call_func(info_add, 'sys.windowsversion', sys, 'getwindowsversion')
Victor Stinnerb907abc2017-08-17 16:40:51 +020099
100 encoding = sys.getfilesystemencoding()
101 if hasattr(sys, 'getfilesystemencodeerrors'):
102 encoding = '%s/%s' % (encoding, sys.getfilesystemencodeerrors())
103 info_add('sys.filesystem_encoding', encoding)
104
105 for name in ('stdin', 'stdout', 'stderr'):
106 stream = getattr(sys, name)
107 if stream is None:
108 continue
109 encoding = getattr(stream, 'encoding', None)
110 if not encoding:
111 continue
112 errors = getattr(stream, 'errors', None)
113 if errors:
114 encoding = '%s/%s' % (encoding, errors)
115 info_add('sys.%s.encoding' % name, encoding)
116
Victor Stinnerb907abc2017-08-17 16:40:51 +0200117
118def collect_platform(info_add):
119 import platform
120
121 arch = platform.architecture()
122 arch = ' '.join(filter(bool, arch))
123 info_add('platform.architecture', arch)
124
125 info_add('platform.python_implementation',
126 platform.python_implementation())
127 info_add('platform.platform',
128 platform.platform(aliased=True))
129
130
131def collect_locale(info_add):
132 import locale
133
134 info_add('locale.encoding', locale.getpreferredencoding(False))
135
136
137def collect_os(info_add):
138 import os
139
Victor Stinnerad7eaed2017-08-18 12:08:47 +0200140 def format_attr(attr, value):
141 if attr in ('supports_follow_symlinks', 'supports_fd',
142 'supports_effective_ids'):
143 return str(sorted(func.__name__ for func in value))
144 else:
145 return value
146
147 attributes = (
148 'name',
149 'supports_bytes_environ',
150 'supports_effective_ids',
151 'supports_fd',
152 'supports_follow_symlinks',
153 )
154 copy_attributes(info_add, os, 'os.%s', attributes, formatter=format_attr)
Victor Stinnerb907abc2017-08-17 16:40:51 +0200155
156 info_add("os.cwd", os.getcwd())
157
Victor Stinnerad7eaed2017-08-18 12:08:47 +0200158 call_func(info_add, 'os.uid', os, 'getuid')
159 call_func(info_add, 'os.gid', os, 'getgid')
160 call_func(info_add, 'os.uname', os, 'uname')
Victor Stinnerb907abc2017-08-17 16:40:51 +0200161
162 if hasattr(os, 'getgroups'):
163 groups = os.getgroups()
164 groups = map(str, groups)
165 groups = ', '.join(groups)
166 info_add("os.groups", groups)
167
168 if hasattr(os, 'getlogin'):
169 try:
170 login = os.getlogin()
171 except OSError:
172 # getlogin() fails with "OSError: [Errno 25] Inappropriate ioctl
173 # for device" on Travis CI
174 pass
175 else:
176 info_add("os.login", login)
177
178 if hasattr(os, 'cpu_count'):
179 cpu_count = os.cpu_count()
180 if cpu_count:
181 info_add('os.cpu_count', cpu_count)
182
Victor Stinnerad7eaed2017-08-18 12:08:47 +0200183 call_func(info_add, 'os.loadavg', os, 'getloadavg')
Victor Stinnerb907abc2017-08-17 16:40:51 +0200184
185 # Get environment variables: filter to list
186 # to not leak sensitive information
187 ENV_VARS = (
188 "CC",
189 "COMSPEC",
190 "DISPLAY",
191 "DISTUTILS_USE_SDK",
192 "DYLD_LIBRARY_PATH",
193 "HOME",
194 "HOMEDRIVE",
195 "HOMEPATH",
196 "LANG",
197 "LD_LIBRARY_PATH",
198 "MACOSX_DEPLOYMENT_TARGET",
199 "MAKEFLAGS",
200 "MSSDK",
201 "PATH",
202 "SDK_TOOLS_BIN",
203 "SHELL",
204 "TEMP",
205 "TERM",
206 "TMP",
207 "TMPDIR",
208 "USERPROFILE",
209 "WAYLAND_DISPLAY",
210 )
211 for name, value in os.environ.items():
212 uname = name.upper()
213 if (uname in ENV_VARS or uname.startswith(("PYTHON", "LC_"))
214 # Visual Studio: VS140COMNTOOLS
215 or (uname.startswith("VS") and uname.endswith("COMNTOOLS"))):
216 info_add('os.environ[%s]' % name, value)
217
Victor Stinnerad7eaed2017-08-18 12:08:47 +0200218 if hasattr(os, 'umask'):
219 mask = os.umask(0)
220 os.umask(mask)
221 info_add("os.umask", '%03o' % mask)
222
223 if hasattr(os, 'getrandom'):
224 # PEP 524: Check if system urandom is initialized
225 try:
226 os.getrandom(1, os.GRND_NONBLOCK)
227 state = 'ready (initialized)'
228 except BlockingIOError as exc:
229 state = 'not seeded yet (%s)' % exc
230 info_add('os.getrandom', state)
231
Victor Stinnerb907abc2017-08-17 16:40:51 +0200232
233def collect_readline(info_add):
234 try:
235 import readline
236 except ImportError:
237 return
238
239 def format_attr(attr, value):
240 if isinstance(value, int):
241 return "%#x" % value
242 else:
243 return value
244
245 attributes = (
246 "_READLINE_VERSION",
247 "_READLINE_RUNTIME_VERSION",
248 "_READLINE_LIBRARY_VERSION",
249 )
250 copy_attributes(info_add, readline, 'readline.%s', attributes,
251 formatter=format_attr)
252
253
254def collect_gdb(info_add):
255 import subprocess
256
257 try:
258 proc = subprocess.Popen(["gdb", "-nx", "--version"],
259 stdout=subprocess.PIPE,
260 stderr=subprocess.PIPE,
261 universal_newlines=True)
262 version = proc.communicate()[0]
263 except OSError:
264 return
265
266 # Only keep the first line
267 version = version.splitlines()[0]
268 info_add('gdb_version', version)
269
270
271def collect_tkinter(info_add):
272 try:
273 import _tkinter
274 except ImportError:
275 pass
276 else:
277 attributes = ('TK_VERSION', 'TCL_VERSION')
278 copy_attributes(info_add, _tkinter, 'tkinter.%s', attributes)
279
280 try:
281 import tkinter
282 except ImportError:
283 pass
284 else:
285 tcl = tkinter.Tcl()
286 patchlevel = tcl.call('info', 'patchlevel')
287 info_add('tkinter.info_patchlevel', patchlevel)
288
289
290def collect_time(info_add):
291 import time
292
Victor Stinnerad7eaed2017-08-18 12:08:47 +0200293 attributes = (
294 'altzone',
295 'daylight',
296 'timezone',
297 'tzname',
298 )
299 copy_attributes(info_add, time, 'time.%s', attributes)
300
Victor Stinnerb907abc2017-08-17 16:40:51 +0200301 if not hasattr(time, 'get_clock_info'):
302 return
303
304 for clock in ('time', 'perf_counter'):
305 tinfo = time.get_clock_info(clock)
Victor Stinnerad7eaed2017-08-18 12:08:47 +0200306 info_add('time.%s' % clock, tinfo)
Victor Stinnerb907abc2017-08-17 16:40:51 +0200307
308
309def collect_sysconfig(info_add):
310 import sysconfig
311
312 for name in (
313 'ABIFLAGS',
314 'ANDROID_API_LEVEL',
315 'CC',
316 'CCSHARED',
317 'CFLAGS',
318 'CFLAGSFORSHARED',
319 'PY_LDFLAGS',
320 'CONFIG_ARGS',
321 'HOST_GNU_TYPE',
322 'MACHDEP',
323 'MULTIARCH',
324 'OPT',
325 'PY_CFLAGS',
326 'PY_CFLAGS_NODIST',
327 'Py_DEBUG',
328 'Py_ENABLE_SHARED',
329 'SHELL',
330 'SOABI',
331 'prefix',
332 ):
333 value = sysconfig.get_config_var(name)
334 if name == 'ANDROID_API_LEVEL' and not value:
335 # skip ANDROID_API_LEVEL=0
336 continue
337 value = normalize_text(value)
338 info_add('sysconfig[%s]' % name, value)
339
340
341def collect_ssl(info_add):
342 try:
343 import ssl
344 except ImportError:
345 return
346
347 def format_attr(attr, value):
348 if attr.startswith('OP_'):
349 return '%#8x' % value
350 else:
Victor Stinnerad7eaed2017-08-18 12:08:47 +0200351 return value
Victor Stinnerb907abc2017-08-17 16:40:51 +0200352
353 attributes = (
354 'OPENSSL_VERSION',
355 'OPENSSL_VERSION_INFO',
356 'HAS_SNI',
357 'OP_ALL',
358 'OP_NO_TLSv1_1',
359 )
360 copy_attributes(info_add, ssl, 'ssl.%s', attributes, formatter=format_attr)
361
362
363def collect_socket(info_add):
364 import socket
365
366 hostname = socket.gethostname()
367 info_add('socket.hostname', hostname)
368
369
370def collect_sqlite(info_add):
371 try:
372 import sqlite3
373 except ImportError:
374 return
375
376 attributes = ('version', 'sqlite_version')
377 copy_attributes(info_add, sqlite3, 'sqlite3.%s', attributes)
378
379
380def collect_zlib(info_add):
381 try:
382 import zlib
383 except ImportError:
384 return
385
386 attributes = ('ZLIB_VERSION', 'ZLIB_RUNTIME_VERSION')
387 copy_attributes(info_add, zlib, 'zlib.%s', attributes)
388
389
Victor Stinnerf6ebd832017-08-17 22:13:11 +0200390def collect_expat(info_add):
391 try:
392 from xml.parsers import expat
393 except ImportError:
394 return
395
396 attributes = ('EXPAT_VERSION',)
397 copy_attributes(info_add, expat, 'expat.%s', attributes)
398
399
400def collect_decimal(info_add):
401 try:
402 import _decimal
403 except ImportError:
404 return
405
406 attributes = ('__libmpdec_version__',)
407 copy_attributes(info_add, _decimal, '_decimal.%s', attributes)
408
409
Victor Stinnerb907abc2017-08-17 16:40:51 +0200410def collect_info(info):
411 error = False
412 info_add = info.add
413
414 for collect_func in (
415 # collect_os() should be the first, to check the getrandom() status
416 collect_os,
417
418 collect_gdb,
419 collect_locale,
420 collect_platform,
421 collect_readline,
422 collect_socket,
423 collect_sqlite,
424 collect_ssl,
425 collect_sys,
426 collect_sysconfig,
427 collect_time,
428 collect_tkinter,
429 collect_zlib,
Victor Stinnerf6ebd832017-08-17 22:13:11 +0200430 collect_expat,
431 collect_decimal,
Victor Stinnerb907abc2017-08-17 16:40:51 +0200432 ):
433 try:
434 collect_func(info_add)
435 except Exception as exc:
436 error = True
437 print("ERROR: %s() failed" % (collect_func.__name__),
438 file=sys.stderr)
439 traceback.print_exc(file=sys.stderr)
440 print(file=sys.stderr)
441 sys.stderr.flush()
442
443 return error
444
445
446def dump_info(info, file=None):
447 title = "Python debug information"
448 print(title)
449 print("=" * len(title))
450 print()
451
452 infos = info.get_infos()
453 infos = sorted(infos.items())
454 for key, value in infos:
455 value = value.replace("\n", " ")
456 print("%s: %s" % (key, value))
457 print()
458
459
460def main():
461 info = PythonInfo()
462 error = collect_info(info)
463 dump_info(info)
464
465 if error:
466 print("Collection failed: exit with error", file=sys.stderr)
467 sys.exit(1)
468
469
470if __name__ == "__main__":
471 main()