utils.py 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116
  1. import base64
  2. import json
  3. import linecache
  4. import logging
  5. import math
  6. import os
  7. import copy
  8. import random
  9. import re
  10. import subprocess
  11. import sys
  12. import threading
  13. import time
  14. from collections import namedtuple
  15. from datetime import datetime, timezone
  16. from decimal import Decimal
  17. from functools import partial, partialmethod, wraps
  18. from numbers import Real
  19. from urllib.parse import parse_qs, unquote, urlencode, urlsplit, urlunsplit
  20. try:
  21. # Python 3.11
  22. from builtins import BaseExceptionGroup
  23. except ImportError:
  24. # Python 3.10 and below
  25. BaseExceptionGroup = None # type: ignore
  26. from typing import TYPE_CHECKING
  27. import sentry_sdk
  28. from sentry_sdk._compat import PY37
  29. from sentry_sdk._types import SENSITIVE_DATA_SUBSTITUTE, Annotated, AnnotatedValue
  30. from sentry_sdk.consts import (
  31. DEFAULT_ADD_FULL_STACK,
  32. DEFAULT_MAX_STACK_FRAMES,
  33. DEFAULT_MAX_VALUE_LENGTH,
  34. EndpointType,
  35. )
  36. if TYPE_CHECKING:
  37. from types import FrameType, TracebackType
  38. from typing import (
  39. Any,
  40. Callable,
  41. ContextManager,
  42. Dict,
  43. Iterator,
  44. List,
  45. Literal,
  46. NoReturn,
  47. Optional,
  48. ParamSpec,
  49. Set,
  50. Tuple,
  51. Type,
  52. TypeVar,
  53. Union,
  54. cast,
  55. overload,
  56. )
  57. from gevent.hub import Hub
  58. from sentry_sdk._types import (
  59. AttributeValue,
  60. SerializedAttributeValue,
  61. Event,
  62. ExcInfo,
  63. Hint,
  64. Log,
  65. Metric,
  66. )
  67. P = ParamSpec("P")
  68. R = TypeVar("R")
  69. epoch = datetime(1970, 1, 1)
  70. # The logger is created here but initialized in the debug support module
  71. logger = logging.getLogger("sentry_sdk.errors")
  72. _installed_modules = None
  73. BASE64_ALPHABET = re.compile(r"^[a-zA-Z0-9/+=]*$")
  74. FALSY_ENV_VALUES = frozenset(("false", "f", "n", "no", "off", "0"))
  75. TRUTHY_ENV_VALUES = frozenset(("true", "t", "y", "yes", "on", "1"))
  76. MAX_STACK_FRAMES = 2000
  77. """Maximum number of stack frames to send to Sentry.
  78. If we have more than this number of stack frames, we will stop processing
  79. the stacktrace to avoid getting stuck in a long-lasting loop. This value
  80. exceeds the default sys.getrecursionlimit() of 1000, so users will only
  81. be affected by this limit if they have a custom recursion limit.
  82. """
  83. def env_to_bool(value: "Any", *, strict: "Optional[bool]" = False) -> "bool | None":
  84. """Casts an ENV variable value to boolean using the constants defined above.
  85. In strict mode, it may return None if the value doesn't match any of the predefined values.
  86. """
  87. normalized = str(value).lower() if value is not None else None
  88. if normalized in FALSY_ENV_VALUES:
  89. return False
  90. if normalized in TRUTHY_ENV_VALUES:
  91. return True
  92. return None if strict else bool(value)
  93. def json_dumps(data: "Any") -> bytes:
  94. """Serialize data into a compact JSON representation encoded as UTF-8."""
  95. return json.dumps(data, allow_nan=False, separators=(",", ":")).encode("utf-8")
  96. def get_git_revision() -> "Optional[str]":
  97. try:
  98. with open(os.path.devnull, "w+") as null:
  99. # prevent command prompt windows from popping up on windows
  100. startupinfo = None
  101. if sys.platform == "win32" or sys.platform == "cygwin":
  102. startupinfo = subprocess.STARTUPINFO()
  103. startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
  104. revision = (
  105. subprocess.Popen(
  106. ["git", "rev-parse", "HEAD"],
  107. startupinfo=startupinfo,
  108. stdout=subprocess.PIPE,
  109. stderr=null,
  110. stdin=null,
  111. )
  112. .communicate()[0]
  113. .strip()
  114. .decode("utf-8")
  115. )
  116. except (OSError, IOError, FileNotFoundError):
  117. return None
  118. return revision
  119. def get_default_release() -> "Optional[str]":
  120. """Try to guess a default release."""
  121. release = os.environ.get("SENTRY_RELEASE")
  122. if release:
  123. return release
  124. release = get_git_revision()
  125. if release:
  126. return release
  127. for var in (
  128. "HEROKU_SLUG_COMMIT",
  129. "SOURCE_VERSION",
  130. "CODEBUILD_RESOLVED_SOURCE_VERSION",
  131. "CIRCLE_SHA1",
  132. "GAE_DEPLOYMENT_ID",
  133. "K_REVISION",
  134. ):
  135. release = os.environ.get(var)
  136. if release:
  137. return release
  138. return None
  139. def get_sdk_name(installed_integrations: "List[str]") -> str:
  140. """Return the SDK name including the name of the used web framework."""
  141. # Note: I can not use for example sentry_sdk.integrations.django.DjangoIntegration.identifier
  142. # here because if django is not installed the integration is not accessible.
  143. framework_integrations = [
  144. "django",
  145. "flask",
  146. "fastapi",
  147. "bottle",
  148. "falcon",
  149. "quart",
  150. "sanic",
  151. "starlette",
  152. "litestar",
  153. "starlite",
  154. "chalice",
  155. "serverless",
  156. "pyramid",
  157. "tornado",
  158. "aiohttp",
  159. "aws_lambda",
  160. "gcp",
  161. "beam",
  162. "asgi",
  163. "wsgi",
  164. ]
  165. for integration in framework_integrations:
  166. if integration in installed_integrations:
  167. return "sentry.python.{}".format(integration)
  168. return "sentry.python"
  169. class CaptureInternalException:
  170. __slots__ = ()
  171. def __enter__(self) -> "ContextManager[Any]":
  172. return self
  173. def __exit__(
  174. self,
  175. ty: "Optional[Type[BaseException]]",
  176. value: "Optional[BaseException]",
  177. tb: "Optional[TracebackType]",
  178. ) -> bool:
  179. if ty is not None and value is not None:
  180. capture_internal_exception((ty, value, tb))
  181. return True
  182. _CAPTURE_INTERNAL_EXCEPTION = CaptureInternalException()
  183. def capture_internal_exceptions() -> "ContextManager[Any]":
  184. return _CAPTURE_INTERNAL_EXCEPTION
  185. def capture_internal_exception(exc_info: "ExcInfo") -> None:
  186. """
  187. Capture an exception that is likely caused by a bug in the SDK
  188. itself.
  189. These exceptions do not end up in Sentry and are just logged instead.
  190. """
  191. if sentry_sdk.get_client().is_active():
  192. logger.error("Internal error in sentry_sdk", exc_info=exc_info)
  193. def to_timestamp(value: "datetime") -> float:
  194. return (value - epoch).total_seconds()
  195. def format_timestamp(value: "datetime") -> str:
  196. """Formats a timestamp in RFC 3339 format.
  197. Any datetime objects with a non-UTC timezone are converted to UTC, so that all timestamps are formatted in UTC.
  198. """
  199. utctime = value.astimezone(timezone.utc)
  200. # We use this custom formatting rather than isoformat for backwards compatibility (we have used this format for
  201. # several years now), and isoformat is slightly different.
  202. return utctime.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
  203. ISO_TZ_SEPARATORS = frozenset(("+", "-"))
  204. def datetime_from_isoformat(value: str) -> "datetime":
  205. try:
  206. result = datetime.fromisoformat(value)
  207. except (AttributeError, ValueError):
  208. # py 3.6
  209. timestamp_format = (
  210. "%Y-%m-%dT%H:%M:%S.%f" if "." in value else "%Y-%m-%dT%H:%M:%S"
  211. )
  212. if value.endswith("Z"):
  213. value = value[:-1] + "+0000"
  214. if value[-6] in ISO_TZ_SEPARATORS:
  215. timestamp_format += "%z"
  216. value = value[:-3] + value[-2:]
  217. elif value[-5] in ISO_TZ_SEPARATORS:
  218. timestamp_format += "%z"
  219. result = datetime.strptime(value, timestamp_format)
  220. return result.astimezone(timezone.utc)
  221. def event_hint_with_exc_info(
  222. exc_info: "Optional[ExcInfo]" = None,
  223. ) -> "Dict[str, Optional[ExcInfo]]":
  224. """Creates a hint with the exc info filled in."""
  225. if exc_info is None:
  226. exc_info = sys.exc_info()
  227. else:
  228. exc_info = exc_info_from_error(exc_info)
  229. if exc_info[0] is None:
  230. exc_info = None
  231. return {"exc_info": exc_info}
  232. class BadDsn(ValueError):
  233. """Raised on invalid DSNs."""
  234. class Dsn:
  235. """Represents a DSN."""
  236. ORG_ID_REGEX = re.compile(r"^o(\d+)\.")
  237. def __init__(
  238. self, value: "Union[Dsn, str]", org_id: "Optional[str]" = None
  239. ) -> None:
  240. if isinstance(value, Dsn):
  241. self.__dict__ = dict(value.__dict__)
  242. return
  243. parts = urlsplit(str(value))
  244. if parts.scheme not in ("http", "https"):
  245. raise BadDsn("Unsupported scheme %r" % parts.scheme)
  246. self.scheme = parts.scheme
  247. if parts.hostname is None:
  248. raise BadDsn("Missing hostname")
  249. self.host = parts.hostname
  250. if org_id is not None:
  251. self.org_id: "Optional[str]" = org_id
  252. else:
  253. org_id_match = Dsn.ORG_ID_REGEX.match(self.host)
  254. self.org_id = org_id_match.group(1) if org_id_match else None
  255. if parts.port is None:
  256. self.port: int = self.scheme == "https" and 443 or 80
  257. else:
  258. self.port = parts.port
  259. if not parts.username:
  260. raise BadDsn("Missing public key")
  261. self.public_key = parts.username
  262. self.secret_key = parts.password
  263. path = parts.path.rsplit("/", 1)
  264. try:
  265. self.project_id = str(int(path.pop()))
  266. except (ValueError, TypeError):
  267. raise BadDsn("Invalid project in DSN (%r)" % (parts.path or "")[1:])
  268. self.path = "/".join(path) + "/"
  269. @property
  270. def netloc(self) -> str:
  271. """The netloc part of a DSN."""
  272. rv = self.host
  273. if (self.scheme, self.port) not in (("http", 80), ("https", 443)):
  274. rv = "%s:%s" % (rv, self.port)
  275. return rv
  276. def to_auth(self, client: "Optional[Any]" = None) -> "Auth":
  277. """Returns the auth info object for this dsn."""
  278. return Auth(
  279. scheme=self.scheme,
  280. host=self.netloc,
  281. path=self.path,
  282. project_id=self.project_id,
  283. public_key=self.public_key,
  284. secret_key=self.secret_key,
  285. client=client,
  286. )
  287. def __str__(self) -> str:
  288. return "%s://%s%s@%s%s%s" % (
  289. self.scheme,
  290. self.public_key,
  291. self.secret_key and "@" + self.secret_key or "",
  292. self.netloc,
  293. self.path,
  294. self.project_id,
  295. )
  296. class Auth:
  297. """Helper object that represents the auth info."""
  298. def __init__(
  299. self,
  300. scheme: str,
  301. host: str,
  302. project_id: str,
  303. public_key: str,
  304. secret_key: "Optional[str]" = None,
  305. version: int = 7,
  306. client: "Optional[Any]" = None,
  307. path: str = "/",
  308. ) -> None:
  309. self.scheme = scheme
  310. self.host = host
  311. self.path = path
  312. self.project_id = project_id
  313. self.public_key = public_key
  314. self.secret_key = secret_key
  315. self.version = version
  316. self.client = client
  317. def get_api_url(
  318. self,
  319. type: "EndpointType" = EndpointType.ENVELOPE,
  320. ) -> str:
  321. """Returns the API url for storing events."""
  322. return "%s://%s%sapi/%s/%s/" % (
  323. self.scheme,
  324. self.host,
  325. self.path,
  326. self.project_id,
  327. type.value,
  328. )
  329. def to_header(self) -> str:
  330. """Returns the auth header a string."""
  331. rv = [("sentry_key", self.public_key), ("sentry_version", self.version)]
  332. if self.client is not None:
  333. rv.append(("sentry_client", self.client))
  334. if self.secret_key is not None:
  335. rv.append(("sentry_secret", self.secret_key))
  336. return "Sentry " + ", ".join("%s=%s" % (key, value) for key, value in rv)
  337. def get_type_name(cls: "Optional[type]") -> "Optional[str]":
  338. return getattr(cls, "__qualname__", None) or getattr(cls, "__name__", None)
  339. def get_type_module(cls: "Optional[type]") -> "Optional[str]":
  340. mod = getattr(cls, "__module__", None)
  341. if mod not in (None, "builtins", "__builtins__"):
  342. return mod
  343. return None
  344. def should_hide_frame(frame: "FrameType") -> bool:
  345. try:
  346. mod = frame.f_globals["__name__"]
  347. if mod.startswith("sentry_sdk."):
  348. return True
  349. except (AttributeError, KeyError):
  350. pass
  351. for flag_name in "__traceback_hide__", "__tracebackhide__":
  352. try:
  353. if frame.f_locals[flag_name]:
  354. return True
  355. except Exception:
  356. pass
  357. return False
  358. def iter_stacks(tb: "Optional[TracebackType]") -> "Iterator[TracebackType]":
  359. tb_: "Optional[TracebackType]" = tb
  360. while tb_ is not None:
  361. if not should_hide_frame(tb_.tb_frame):
  362. yield tb_
  363. tb_ = tb_.tb_next
  364. def get_lines_from_file(
  365. filename: str,
  366. lineno: int,
  367. max_length: "Optional[int]" = None,
  368. loader: "Optional[Any]" = None,
  369. module: "Optional[str]" = None,
  370. ) -> "Tuple[List[Annotated[str]], Optional[Annotated[str]], List[Annotated[str]]]":
  371. context_lines = 5
  372. source = None
  373. if loader is not None and hasattr(loader, "get_source"):
  374. try:
  375. source_str: "Optional[str]" = loader.get_source(module)
  376. except (ImportError, IOError):
  377. source_str = None
  378. if source_str is not None:
  379. source = source_str.splitlines()
  380. if source is None:
  381. try:
  382. source = linecache.getlines(filename)
  383. except (OSError, IOError):
  384. return [], None, []
  385. if not source:
  386. return [], None, []
  387. lower_bound = max(0, lineno - context_lines)
  388. upper_bound = min(lineno + 1 + context_lines, len(source))
  389. try:
  390. pre_context = [
  391. strip_string(line.strip("\r\n"), max_length=max_length)
  392. for line in source[lower_bound:lineno]
  393. ]
  394. context_line = strip_string(source[lineno].strip("\r\n"), max_length=max_length)
  395. post_context = [
  396. strip_string(line.strip("\r\n"), max_length=max_length)
  397. for line in source[(lineno + 1) : upper_bound]
  398. ]
  399. return pre_context, context_line, post_context
  400. except IndexError:
  401. # the file may have changed since it was loaded into memory
  402. return [], None, []
  403. def get_source_context(
  404. frame: "FrameType",
  405. tb_lineno: "Optional[int]",
  406. max_value_length: "Optional[int]" = None,
  407. ) -> "Tuple[List[Annotated[str]], Optional[Annotated[str]], List[Annotated[str]]]":
  408. try:
  409. abs_path: "Optional[str]" = frame.f_code.co_filename
  410. except Exception:
  411. abs_path = None
  412. try:
  413. module = frame.f_globals["__name__"]
  414. except Exception:
  415. return [], None, []
  416. try:
  417. loader = frame.f_globals["__loader__"]
  418. except Exception:
  419. loader = None
  420. if tb_lineno is not None and abs_path:
  421. lineno = tb_lineno - 1
  422. return get_lines_from_file(
  423. abs_path, lineno, max_value_length, loader=loader, module=module
  424. )
  425. return [], None, []
  426. def safe_str(value: "Any") -> str:
  427. try:
  428. return str(value)
  429. except Exception:
  430. return safe_repr(value)
  431. def safe_repr(value: "Any") -> str:
  432. try:
  433. return repr(value)
  434. except Exception:
  435. return "<broken repr>"
  436. def filename_for_module(
  437. module: "Optional[str]", abs_path: "Optional[str]"
  438. ) -> "Optional[str]":
  439. if not abs_path or not module:
  440. return abs_path
  441. try:
  442. if abs_path.endswith(".pyc"):
  443. abs_path = abs_path[:-1]
  444. base_module = module.split(".", 1)[0]
  445. if base_module == module:
  446. return os.path.basename(abs_path)
  447. base_module_path = sys.modules[base_module].__file__
  448. if not base_module_path:
  449. return abs_path
  450. return abs_path.split(base_module_path.rsplit(os.sep, 2)[0], 1)[-1].lstrip(
  451. os.sep
  452. )
  453. except Exception:
  454. return abs_path
  455. def serialize_frame(
  456. frame: "FrameType",
  457. tb_lineno: "Optional[int]" = None,
  458. include_local_variables: bool = True,
  459. include_source_context: bool = True,
  460. max_value_length: "Optional[int]" = None,
  461. custom_repr: "Optional[Callable[..., Optional[str]]]" = None,
  462. ) -> "Dict[str, Any]":
  463. f_code = getattr(frame, "f_code", None)
  464. if not f_code:
  465. abs_path = None
  466. function = None
  467. else:
  468. abs_path = frame.f_code.co_filename
  469. function = frame.f_code.co_name
  470. try:
  471. module = frame.f_globals["__name__"]
  472. except Exception:
  473. module = None
  474. if tb_lineno is None:
  475. tb_lineno = frame.f_lineno
  476. try:
  477. os_abs_path = os.path.abspath(abs_path) if abs_path else None
  478. except Exception:
  479. os_abs_path = None
  480. rv: "Dict[str, Any]" = {
  481. "filename": filename_for_module(module, abs_path) or None,
  482. "abs_path": os_abs_path,
  483. "function": function or "<unknown>",
  484. "module": module,
  485. "lineno": tb_lineno,
  486. }
  487. if include_source_context:
  488. rv["pre_context"], rv["context_line"], rv["post_context"] = get_source_context(
  489. frame, tb_lineno, max_value_length
  490. )
  491. if include_local_variables:
  492. from sentry_sdk.serializer import serialize
  493. rv["vars"] = serialize(
  494. dict(frame.f_locals), is_vars=True, custom_repr=custom_repr
  495. )
  496. return rv
  497. def current_stacktrace(
  498. include_local_variables: bool = True,
  499. include_source_context: bool = True,
  500. max_value_length: "Optional[int]" = None,
  501. ) -> "Dict[str, Any]":
  502. __tracebackhide__ = True
  503. frames = []
  504. f: "Optional[FrameType]" = sys._getframe()
  505. while f is not None:
  506. if not should_hide_frame(f):
  507. frames.append(
  508. serialize_frame(
  509. f,
  510. include_local_variables=include_local_variables,
  511. include_source_context=include_source_context,
  512. max_value_length=max_value_length,
  513. )
  514. )
  515. f = f.f_back
  516. frames.reverse()
  517. return {"frames": frames}
  518. def get_errno(exc_value: BaseException) -> "Optional[Any]":
  519. return getattr(exc_value, "errno", None)
  520. def get_error_message(exc_value: "Optional[BaseException]") -> str:
  521. message: str = safe_str(
  522. getattr(exc_value, "message", "")
  523. or getattr(exc_value, "detail", "")
  524. or safe_str(exc_value)
  525. )
  526. # __notes__ should be a list of strings when notes are added
  527. # via add_note, but can be anything else if __notes__ is set
  528. # directly. We only support strings in __notes__, since that
  529. # is the correct use.
  530. notes: object = getattr(exc_value, "__notes__", None)
  531. if isinstance(notes, list) and len(notes) > 0:
  532. message += "\n" + "\n".join(note for note in notes if isinstance(note, str))
  533. return message
  534. def single_exception_from_error_tuple(
  535. exc_type: "Optional[type]",
  536. exc_value: "Optional[BaseException]",
  537. tb: "Optional[TracebackType]",
  538. client_options: "Optional[Dict[str, Any]]" = None,
  539. mechanism: "Optional[Dict[str, Any]]" = None,
  540. exception_id: "Optional[int]" = None,
  541. parent_id: "Optional[int]" = None,
  542. source: "Optional[str]" = None,
  543. full_stack: "Optional[list[dict[str, Any]]]" = None,
  544. ) -> "Dict[str, Any]":
  545. """
  546. Creates a dict that goes into the events `exception.values` list and is ingestible by Sentry.
  547. See the Exception Interface documentation for more details:
  548. https://develop.sentry.dev/sdk/event-payloads/exception/
  549. """
  550. exception_value: "Dict[str, Any]" = {}
  551. exception_value["mechanism"] = (
  552. mechanism.copy() if mechanism else {"type": "generic", "handled": True}
  553. )
  554. if exception_id is not None:
  555. exception_value["mechanism"]["exception_id"] = exception_id
  556. if exc_value is not None:
  557. errno = get_errno(exc_value)
  558. else:
  559. errno = None
  560. if errno is not None:
  561. exception_value["mechanism"].setdefault("meta", {}).setdefault(
  562. "errno", {}
  563. ).setdefault("number", errno)
  564. if source is not None:
  565. exception_value["mechanism"]["source"] = source
  566. is_root_exception = exception_id == 0
  567. if not is_root_exception and parent_id is not None:
  568. exception_value["mechanism"]["parent_id"] = parent_id
  569. exception_value["mechanism"]["type"] = "chained"
  570. if is_root_exception and "type" not in exception_value["mechanism"]:
  571. exception_value["mechanism"]["type"] = "generic"
  572. is_exception_group = BaseExceptionGroup is not None and isinstance(
  573. exc_value, BaseExceptionGroup
  574. )
  575. if is_exception_group:
  576. exception_value["mechanism"]["is_exception_group"] = True
  577. exception_value["module"] = get_type_module(exc_type)
  578. exception_value["type"] = get_type_name(exc_type)
  579. exception_value["value"] = get_error_message(exc_value)
  580. if client_options is None:
  581. include_local_variables = True
  582. include_source_context = True
  583. max_value_length = DEFAULT_MAX_VALUE_LENGTH # fallback
  584. custom_repr = None
  585. else:
  586. include_local_variables = client_options["include_local_variables"]
  587. include_source_context = client_options["include_source_context"]
  588. max_value_length = client_options["max_value_length"]
  589. custom_repr = client_options.get("custom_repr")
  590. frames: "List[Dict[str, Any]]" = [
  591. serialize_frame(
  592. tb.tb_frame,
  593. tb_lineno=tb.tb_lineno,
  594. include_local_variables=include_local_variables,
  595. include_source_context=include_source_context,
  596. max_value_length=max_value_length,
  597. custom_repr=custom_repr,
  598. )
  599. # Process at most MAX_STACK_FRAMES + 1 frames, to avoid hanging on
  600. # processing a super-long stacktrace.
  601. for tb, _ in zip(iter_stacks(tb), range(MAX_STACK_FRAMES + 1))
  602. ]
  603. if len(frames) > MAX_STACK_FRAMES:
  604. # If we have more frames than the limit, we remove the stacktrace completely.
  605. # We don't trim the stacktrace here because we have not processed the whole
  606. # thing (see above, we stop at MAX_STACK_FRAMES + 1). Normally, Relay would
  607. # intelligently trim by removing frames in the middle of the stacktrace, but
  608. # since we don't have the whole stacktrace, we can't do that. Instead, we
  609. # drop the entire stacktrace.
  610. exception_value["stacktrace"] = AnnotatedValue.removed_because_over_size_limit(
  611. value=None
  612. )
  613. elif frames:
  614. if not full_stack:
  615. new_frames = frames
  616. else:
  617. new_frames = merge_stack_frames(frames, full_stack, client_options)
  618. exception_value["stacktrace"] = {"frames": new_frames}
  619. return exception_value
  620. HAS_CHAINED_EXCEPTIONS = hasattr(Exception, "__suppress_context__")
  621. if HAS_CHAINED_EXCEPTIONS:
  622. def walk_exception_chain(exc_info: "ExcInfo") -> "Iterator[ExcInfo]":
  623. exc_type, exc_value, tb = exc_info
  624. seen_exceptions = []
  625. seen_exception_ids: "Set[int]" = set()
  626. while (
  627. exc_type is not None
  628. and exc_value is not None
  629. and id(exc_value) not in seen_exception_ids
  630. ):
  631. yield exc_type, exc_value, tb
  632. # Avoid hashing random types we don't know anything
  633. # about. Use the list to keep a ref so that the `id` is
  634. # not used for another object.
  635. seen_exceptions.append(exc_value)
  636. seen_exception_ids.add(id(exc_value))
  637. if exc_value.__suppress_context__:
  638. cause = exc_value.__cause__
  639. else:
  640. cause = exc_value.__context__
  641. if cause is None:
  642. break
  643. exc_type = type(cause)
  644. exc_value = cause
  645. tb = getattr(cause, "__traceback__", None)
  646. else:
  647. def walk_exception_chain(exc_info: "ExcInfo") -> "Iterator[ExcInfo]":
  648. yield exc_info
  649. def exceptions_from_error(
  650. exc_type: "Optional[type]",
  651. exc_value: "Optional[BaseException]",
  652. tb: "Optional[TracebackType]",
  653. client_options: "Optional[Dict[str, Any]]" = None,
  654. mechanism: "Optional[Dict[str, Any]]" = None,
  655. exception_id: int = 0,
  656. parent_id: int = 0,
  657. source: "Optional[str]" = None,
  658. full_stack: "Optional[list[dict[str, Any]]]" = None,
  659. ) -> "Tuple[int, List[Dict[str, Any]]]":
  660. """
  661. Creates the list of exceptions.
  662. This can include chained exceptions and exceptions from an ExceptionGroup.
  663. See the Exception Interface documentation for more details:
  664. https://develop.sentry.dev/sdk/event-payloads/exception/
  665. """
  666. parent = single_exception_from_error_tuple(
  667. exc_type=exc_type,
  668. exc_value=exc_value,
  669. tb=tb,
  670. client_options=client_options,
  671. mechanism=mechanism,
  672. exception_id=exception_id,
  673. parent_id=parent_id,
  674. source=source,
  675. full_stack=full_stack,
  676. )
  677. exceptions = [parent]
  678. parent_id = exception_id
  679. exception_id += 1
  680. should_supress_context = (
  681. hasattr(exc_value, "__suppress_context__") and exc_value.__suppress_context__ # type: ignore
  682. )
  683. if should_supress_context:
  684. # Add direct cause.
  685. # The field `__cause__` is set when raised with the exception (using the `from` keyword).
  686. exception_has_cause = (
  687. exc_value
  688. and hasattr(exc_value, "__cause__")
  689. and exc_value.__cause__ is not None
  690. )
  691. if exception_has_cause:
  692. cause = exc_value.__cause__ # type: ignore
  693. (exception_id, child_exceptions) = exceptions_from_error(
  694. exc_type=type(cause),
  695. exc_value=cause,
  696. tb=getattr(cause, "__traceback__", None),
  697. client_options=client_options,
  698. mechanism=mechanism,
  699. exception_id=exception_id,
  700. source="__cause__",
  701. full_stack=full_stack,
  702. )
  703. exceptions.extend(child_exceptions)
  704. else:
  705. # Add indirect cause.
  706. # The field `__context__` is assigned if another exception occurs while handling the exception.
  707. exception_has_content = (
  708. exc_value
  709. and hasattr(exc_value, "__context__")
  710. and exc_value.__context__ is not None
  711. )
  712. if exception_has_content:
  713. context = exc_value.__context__ # type: ignore
  714. (exception_id, child_exceptions) = exceptions_from_error(
  715. exc_type=type(context),
  716. exc_value=context,
  717. tb=getattr(context, "__traceback__", None),
  718. client_options=client_options,
  719. mechanism=mechanism,
  720. exception_id=exception_id,
  721. source="__context__",
  722. full_stack=full_stack,
  723. )
  724. exceptions.extend(child_exceptions)
  725. # Add exceptions from an ExceptionGroup.
  726. is_exception_group = exc_value and hasattr(exc_value, "exceptions")
  727. if is_exception_group:
  728. for idx, e in enumerate(exc_value.exceptions): # type: ignore
  729. (exception_id, child_exceptions) = exceptions_from_error(
  730. exc_type=type(e),
  731. exc_value=e,
  732. tb=getattr(e, "__traceback__", None),
  733. client_options=client_options,
  734. mechanism=mechanism,
  735. exception_id=exception_id,
  736. parent_id=parent_id,
  737. source="exceptions[%s]" % idx,
  738. full_stack=full_stack,
  739. )
  740. exceptions.extend(child_exceptions)
  741. return (exception_id, exceptions)
  742. def exceptions_from_error_tuple(
  743. exc_info: "ExcInfo",
  744. client_options: "Optional[Dict[str, Any]]" = None,
  745. mechanism: "Optional[Dict[str, Any]]" = None,
  746. full_stack: "Optional[list[dict[str, Any]]]" = None,
  747. ) -> "List[Dict[str, Any]]":
  748. exc_type, exc_value, tb = exc_info
  749. is_exception_group = BaseExceptionGroup is not None and isinstance(
  750. exc_value, BaseExceptionGroup
  751. )
  752. if is_exception_group:
  753. (_, exceptions) = exceptions_from_error(
  754. exc_type=exc_type,
  755. exc_value=exc_value,
  756. tb=tb,
  757. client_options=client_options,
  758. mechanism=mechanism,
  759. exception_id=0,
  760. parent_id=0,
  761. full_stack=full_stack,
  762. )
  763. else:
  764. exceptions = []
  765. for exc_type, exc_value, tb in walk_exception_chain(exc_info):
  766. exceptions.append(
  767. single_exception_from_error_tuple(
  768. exc_type=exc_type,
  769. exc_value=exc_value,
  770. tb=tb,
  771. client_options=client_options,
  772. mechanism=mechanism,
  773. full_stack=full_stack,
  774. )
  775. )
  776. exceptions.reverse()
  777. return exceptions
  778. def to_string(value: str) -> str:
  779. try:
  780. return str(value)
  781. except UnicodeDecodeError:
  782. return repr(value)[1:-1]
  783. def iter_event_stacktraces(event: "Event") -> "Iterator[Annotated[Dict[str, Any]]]":
  784. if "stacktrace" in event:
  785. yield event["stacktrace"]
  786. if "threads" in event:
  787. for thread in event["threads"].get("values") or ():
  788. if "stacktrace" in thread:
  789. yield thread["stacktrace"]
  790. if "exception" in event:
  791. for exception in event["exception"].get("values") or ():
  792. if isinstance(exception, dict) and "stacktrace" in exception:
  793. yield exception["stacktrace"]
  794. def iter_event_frames(event: "Event") -> "Iterator[Dict[str, Any]]":
  795. for stacktrace in iter_event_stacktraces(event):
  796. if isinstance(stacktrace, AnnotatedValue):
  797. stacktrace = stacktrace.value or {}
  798. for frame in stacktrace.get("frames") or ():
  799. yield frame
  800. def handle_in_app(
  801. event: "Event",
  802. in_app_exclude: "Optional[List[str]]" = None,
  803. in_app_include: "Optional[List[str]]" = None,
  804. project_root: "Optional[str]" = None,
  805. ) -> "Event":
  806. for stacktrace in iter_event_stacktraces(event):
  807. if isinstance(stacktrace, AnnotatedValue):
  808. stacktrace = stacktrace.value or {}
  809. set_in_app_in_frames(
  810. stacktrace.get("frames"),
  811. in_app_exclude=in_app_exclude,
  812. in_app_include=in_app_include,
  813. project_root=project_root,
  814. )
  815. return event
  816. def set_in_app_in_frames(
  817. frames: "Any",
  818. in_app_exclude: "Optional[List[str]]",
  819. in_app_include: "Optional[List[str]]",
  820. project_root: "Optional[str]" = None,
  821. ) -> "Optional[Any]":
  822. if not frames:
  823. return None
  824. for frame in frames:
  825. # if frame has already been marked as in_app, skip it
  826. current_in_app = frame.get("in_app")
  827. if current_in_app is not None:
  828. continue
  829. module = frame.get("module")
  830. # check if module in frame is in the list of modules to include
  831. if _module_in_list(module, in_app_include):
  832. frame["in_app"] = True
  833. continue
  834. # check if module in frame is in the list of modules to exclude
  835. if _module_in_list(module, in_app_exclude):
  836. frame["in_app"] = False
  837. continue
  838. # if frame has no abs_path, skip further checks
  839. abs_path = frame.get("abs_path")
  840. if abs_path is None:
  841. continue
  842. if _is_external_source(abs_path):
  843. frame["in_app"] = False
  844. continue
  845. if _is_in_project_root(abs_path, project_root):
  846. frame["in_app"] = True
  847. continue
  848. return frames
  849. def exc_info_from_error(error: "Union[BaseException, ExcInfo]") -> "ExcInfo":
  850. if isinstance(error, tuple) and len(error) == 3:
  851. exc_type, exc_value, tb = error
  852. elif isinstance(error, BaseException):
  853. tb = getattr(error, "__traceback__", None)
  854. if tb is not None:
  855. exc_type = type(error)
  856. exc_value = error
  857. else:
  858. exc_type, exc_value, tb = sys.exc_info()
  859. if exc_value is not error:
  860. tb = None
  861. exc_value = error
  862. exc_type = type(error)
  863. else:
  864. raise ValueError("Expected Exception object to report, got %s!" % type(error))
  865. exc_info = (exc_type, exc_value, tb)
  866. if TYPE_CHECKING:
  867. # This cast is safe because exc_type and exc_value are either both
  868. # None or both not None.
  869. exc_info = cast(ExcInfo, exc_info)
  870. return exc_info
  871. def merge_stack_frames(
  872. frames: "List[Dict[str, Any]]",
  873. full_stack: "List[Dict[str, Any]]",
  874. client_options: "Optional[Dict[str, Any]]",
  875. ) -> "List[Dict[str, Any]]":
  876. """
  877. Add the missing frames from full_stack to frames and return the merged list.
  878. """
  879. frame_ids = {
  880. (
  881. frame["abs_path"],
  882. frame["context_line"],
  883. frame["lineno"],
  884. frame["function"],
  885. )
  886. for frame in frames
  887. }
  888. new_frames = [
  889. stackframe
  890. for stackframe in full_stack
  891. if (
  892. stackframe["abs_path"],
  893. stackframe["context_line"],
  894. stackframe["lineno"],
  895. stackframe["function"],
  896. )
  897. not in frame_ids
  898. ]
  899. new_frames.extend(frames)
  900. # Limit the number of frames
  901. max_stack_frames = (
  902. client_options.get("max_stack_frames", DEFAULT_MAX_STACK_FRAMES)
  903. if client_options
  904. else None
  905. )
  906. if max_stack_frames is not None:
  907. new_frames = new_frames[len(new_frames) - max_stack_frames :]
  908. return new_frames
  909. def event_from_exception(
  910. exc_info: "Union[BaseException, ExcInfo]",
  911. client_options: "Optional[Dict[str, Any]]" = None,
  912. mechanism: "Optional[Dict[str, Any]]" = None,
  913. ) -> "Tuple[Event, Dict[str, Any]]":
  914. exc_info = exc_info_from_error(exc_info)
  915. hint = event_hint_with_exc_info(exc_info)
  916. if client_options and client_options.get("add_full_stack", DEFAULT_ADD_FULL_STACK):
  917. full_stack = current_stacktrace(
  918. include_local_variables=client_options["include_local_variables"],
  919. max_value_length=client_options["max_value_length"],
  920. )["frames"]
  921. else:
  922. full_stack = None
  923. return (
  924. {
  925. "level": "error",
  926. "exception": {
  927. "values": exceptions_from_error_tuple(
  928. exc_info, client_options, mechanism, full_stack
  929. )
  930. },
  931. },
  932. hint,
  933. )
  934. def _module_in_list(name: "Optional[str]", items: "Optional[List[str]]") -> bool:
  935. if name is None:
  936. return False
  937. if not items:
  938. return False
  939. for item in items:
  940. if item == name or name.startswith(item + "."):
  941. return True
  942. return False
  943. def _is_external_source(abs_path: "Optional[str]") -> bool:
  944. # check if frame is in 'site-packages' or 'dist-packages'
  945. if abs_path is None:
  946. return False
  947. external_source = (
  948. re.search(r"[\\/](?:dist|site)-packages[\\/]", abs_path) is not None
  949. )
  950. return external_source
  951. def _is_in_project_root(
  952. abs_path: "Optional[str]", project_root: "Optional[str]"
  953. ) -> bool:
  954. if abs_path is None or project_root is None:
  955. return False
  956. # check if path is in the project root
  957. if abs_path.startswith(project_root):
  958. return True
  959. return False
  960. def _truncate_by_bytes(string: str, max_bytes: int) -> str:
  961. """
  962. Truncate a UTF-8-encodable string to the last full codepoint so that it fits in max_bytes.
  963. """
  964. truncated = string.encode("utf-8")[: max_bytes - 3].decode("utf-8", errors="ignore")
  965. return truncated + "..."
  966. def _get_size_in_bytes(value: str) -> "Optional[int]":
  967. try:
  968. return len(value.encode("utf-8"))
  969. except (UnicodeEncodeError, UnicodeDecodeError):
  970. return None
  971. def strip_string(
  972. value: str, max_length: "Optional[int]" = None
  973. ) -> "Union[AnnotatedValue, str]":
  974. if not value:
  975. return value
  976. if max_length is None:
  977. max_length = DEFAULT_MAX_VALUE_LENGTH
  978. byte_size = _get_size_in_bytes(value)
  979. text_size = len(value)
  980. if byte_size is not None and byte_size > max_length:
  981. # truncate to max_length bytes, preserving code points
  982. truncated_value = _truncate_by_bytes(value, max_length)
  983. elif text_size is not None and text_size > max_length:
  984. # fallback to truncating by string length
  985. truncated_value = value[: max_length - 3] + "..."
  986. else:
  987. return value
  988. return AnnotatedValue(
  989. value=truncated_value,
  990. metadata={
  991. "len": byte_size or text_size,
  992. "rem": [["!limit", "x", max_length - 3, max_length]],
  993. },
  994. )
  995. def parse_version(version: str) -> "Optional[Tuple[int, ...]]":
  996. """
  997. Parses a version string into a tuple of integers.
  998. This uses the parsing loging from PEP 440:
  999. https://peps.python.org/pep-0440/#appendix-b-parsing-version-strings-with-regular-expressions
  1000. """
  1001. VERSION_PATTERN = r""" # noqa: N806
  1002. v?
  1003. (?:
  1004. (?:(?P<epoch>[0-9]+)!)? # epoch
  1005. (?P<release>[0-9]+(?:\.[0-9]+)*) # release segment
  1006. (?P<pre> # pre-release
  1007. [-_\.]?
  1008. (?P<pre_l>(a|b|c|rc|alpha|beta|pre|preview))
  1009. [-_\.]?
  1010. (?P<pre_n>[0-9]+)?
  1011. )?
  1012. (?P<post> # post release
  1013. (?:-(?P<post_n1>[0-9]+))
  1014. |
  1015. (?:
  1016. [-_\.]?
  1017. (?P<post_l>post|rev|r)
  1018. [-_\.]?
  1019. (?P<post_n2>[0-9]+)?
  1020. )
  1021. )?
  1022. (?P<dev> # dev release
  1023. [-_\.]?
  1024. (?P<dev_l>dev)
  1025. [-_\.]?
  1026. (?P<dev_n>[0-9]+)?
  1027. )?
  1028. )
  1029. (?:\+(?P<local>[a-z0-9]+(?:[-_\.][a-z0-9]+)*))? # local version
  1030. """
  1031. pattern = re.compile(
  1032. r"^\s*" + VERSION_PATTERN + r"\s*$",
  1033. re.VERBOSE | re.IGNORECASE,
  1034. )
  1035. try:
  1036. release = pattern.match(version).groupdict()["release"] # type: ignore
  1037. release_tuple: "Tuple[int, ...]" = tuple(map(int, release.split(".")[:3]))
  1038. except (TypeError, ValueError, AttributeError):
  1039. return None
  1040. return release_tuple
  1041. def _is_contextvars_broken() -> bool:
  1042. """
  1043. Returns whether gevent/eventlet have patched the stdlib in a way where thread locals are now more "correct" than contextvars.
  1044. """
  1045. try:
  1046. import gevent
  1047. from gevent.monkey import is_object_patched
  1048. # Get the MAJOR and MINOR version numbers of Gevent
  1049. version_tuple = tuple(
  1050. [int(part) for part in re.split(r"a|b|rc|\.", gevent.__version__)[:2]]
  1051. )
  1052. if is_object_patched("threading", "local"):
  1053. # Gevent 20.9.0 depends on Greenlet 0.4.17 which natively handles switching
  1054. # context vars when greenlets are switched, so, Gevent 20.9.0+ is all fine.
  1055. # Ref: https://github.com/gevent/gevent/blob/83c9e2ae5b0834b8f84233760aabe82c3ba065b4/src/gevent/monkey.py#L604-L609
  1056. # Gevent 20.5, that doesn't depend on Greenlet 0.4.17 with native support
  1057. # for contextvars, is able to patch both thread locals and contextvars, in
  1058. # that case, check if contextvars are effectively patched.
  1059. if (
  1060. # Gevent 20.9.0+
  1061. (sys.version_info >= (3, 7) and version_tuple >= (20, 9))
  1062. # Gevent 20.5.0+ or Python < 3.7
  1063. or (is_object_patched("contextvars", "ContextVar"))
  1064. ):
  1065. return False
  1066. return True
  1067. except ImportError:
  1068. pass
  1069. try:
  1070. import greenlet
  1071. from eventlet.patcher import is_monkey_patched # type: ignore
  1072. greenlet_version = parse_version(greenlet.__version__)
  1073. if greenlet_version is None:
  1074. logger.error(
  1075. "Internal error in Sentry SDK: Could not parse Greenlet version from greenlet.__version__."
  1076. )
  1077. return False
  1078. if is_monkey_patched("thread") and greenlet_version < (0, 5):
  1079. return True
  1080. except ImportError:
  1081. pass
  1082. return False
  1083. def _make_threadlocal_contextvars(local: type) -> type:
  1084. class ContextVar:
  1085. # Super-limited impl of ContextVar
  1086. def __init__(self, name: str, default: "Any" = None) -> None:
  1087. self._name = name
  1088. self._default = default
  1089. self._local = local()
  1090. self._original_local = local()
  1091. def get(self, default: "Any" = None) -> "Any":
  1092. return getattr(self._local, "value", default or self._default)
  1093. def set(self, value: "Any") -> "Any":
  1094. token = str(random.getrandbits(64))
  1095. original_value = self.get()
  1096. setattr(self._original_local, token, original_value)
  1097. self._local.value = value
  1098. return token
  1099. def reset(self, token: "Any") -> None:
  1100. self._local.value = getattr(self._original_local, token)
  1101. # delete the original value (this way it works in Python 3.6+)
  1102. del self._original_local.__dict__[token]
  1103. return ContextVar
  1104. def _get_contextvars() -> "Tuple[bool, type]":
  1105. """
  1106. Figure out the "right" contextvars installation to use. Returns a
  1107. `contextvars.ContextVar`-like class with a limited API.
  1108. See https://docs.sentry.io/platforms/python/contextvars/ for more information.
  1109. """
  1110. if not _is_contextvars_broken():
  1111. # aiocontextvars is a PyPI package that ensures that the contextvars
  1112. # backport (also a PyPI package) works with asyncio under Python 3.6
  1113. #
  1114. # Import it if available.
  1115. if sys.version_info < (3, 7):
  1116. # `aiocontextvars` is absolutely required for functional
  1117. # contextvars on Python 3.6.
  1118. try:
  1119. from aiocontextvars import ContextVar
  1120. return True, ContextVar
  1121. except ImportError:
  1122. pass
  1123. else:
  1124. # On Python 3.7 contextvars are functional.
  1125. try:
  1126. from contextvars import ContextVar
  1127. return True, ContextVar
  1128. except ImportError:
  1129. pass
  1130. # Fall back to basic thread-local usage.
  1131. from threading import local
  1132. return False, _make_threadlocal_contextvars(local)
  1133. HAS_REAL_CONTEXTVARS, ContextVar = _get_contextvars()
  1134. CONTEXTVARS_ERROR_MESSAGE = """
  1135. With asyncio/ASGI applications, the Sentry SDK requires a functional
  1136. installation of `contextvars` to avoid leaking scope/context data across
  1137. requests.
  1138. Please refer to https://docs.sentry.io/platforms/python/contextvars/ for more information.
  1139. """
  1140. def qualname_from_function(func: "Callable[..., Any]") -> "Optional[str]":
  1141. """Return the qualified name of func. Works with regular function, lambda, partial and partialmethod."""
  1142. func_qualname: "Optional[str]" = None
  1143. # Python 2
  1144. try:
  1145. return "%s.%s.%s" % (
  1146. func.im_class.__module__, # type: ignore
  1147. func.im_class.__name__, # type: ignore
  1148. func.__name__,
  1149. )
  1150. except Exception:
  1151. pass
  1152. prefix, suffix = "", ""
  1153. if isinstance(func, partial) and hasattr(func.func, "__name__"):
  1154. prefix, suffix = "partial(<function ", ">)"
  1155. func = func.func
  1156. else:
  1157. # The _partialmethod attribute of methods wrapped with partialmethod() was renamed to __partialmethod__ in CPython 3.13:
  1158. # https://github.com/python/cpython/pull/16600
  1159. partial_method = getattr(func, "_partialmethod", None) or getattr(
  1160. func, "__partialmethod__", None
  1161. )
  1162. if isinstance(partial_method, partialmethod):
  1163. prefix, suffix = "partialmethod(<function ", ">)"
  1164. func = partial_method.func
  1165. if hasattr(func, "__qualname__"):
  1166. func_qualname = func.__qualname__
  1167. elif hasattr(func, "__name__"): # Python 2.7 has no __qualname__
  1168. func_qualname = func.__name__
  1169. # Python 3: methods, functions, classes
  1170. if func_qualname is not None:
  1171. if hasattr(func, "__module__") and isinstance(func.__module__, str):
  1172. func_qualname = func.__module__ + "." + func_qualname
  1173. func_qualname = prefix + func_qualname + suffix
  1174. return func_qualname
  1175. def transaction_from_function(func: "Callable[..., Any]") -> "Optional[str]":
  1176. return qualname_from_function(func)
  1177. disable_capture_event = ContextVar("disable_capture_event")
  1178. class ServerlessTimeoutWarning(Exception): # noqa: N818
  1179. """Raised when a serverless method is about to reach its timeout."""
  1180. pass
  1181. class TimeoutThread(threading.Thread):
  1182. """Creates a Thread which runs (sleeps) for a time duration equal to
  1183. waiting_time and raises a custom ServerlessTimeout exception.
  1184. """
  1185. def __init__(
  1186. self,
  1187. waiting_time: float,
  1188. configured_timeout: int,
  1189. isolation_scope: "Optional[sentry_sdk.Scope]" = None,
  1190. current_scope: "Optional[sentry_sdk.Scope]" = None,
  1191. ) -> None:
  1192. threading.Thread.__init__(self)
  1193. self.waiting_time = waiting_time
  1194. self.configured_timeout = configured_timeout
  1195. self.isolation_scope = isolation_scope
  1196. self.current_scope = current_scope
  1197. self._stop_event = threading.Event()
  1198. def stop(self) -> None:
  1199. self._stop_event.set()
  1200. def _capture_exception(self) -> "ExcInfo":
  1201. exc_info = sys.exc_info()
  1202. client = sentry_sdk.get_client()
  1203. event, hint = event_from_exception(
  1204. exc_info,
  1205. client_options=client.options,
  1206. mechanism={"type": "threading", "handled": False},
  1207. )
  1208. sentry_sdk.capture_event(event, hint=hint)
  1209. return exc_info
  1210. def run(self) -> None:
  1211. self._stop_event.wait(self.waiting_time)
  1212. if self._stop_event.is_set():
  1213. return
  1214. integer_configured_timeout = int(self.configured_timeout)
  1215. # Setting up the exact integer value of configured time(in seconds)
  1216. if integer_configured_timeout < self.configured_timeout:
  1217. integer_configured_timeout = integer_configured_timeout + 1
  1218. # Raising Exception after timeout duration is reached
  1219. if self.isolation_scope is not None and self.current_scope is not None:
  1220. with sentry_sdk.scope.use_isolation_scope(self.isolation_scope):
  1221. with sentry_sdk.scope.use_scope(self.current_scope):
  1222. try:
  1223. raise ServerlessTimeoutWarning(
  1224. "WARNING : Function is expected to get timed out. Configured timeout duration = {} seconds.".format(
  1225. integer_configured_timeout
  1226. )
  1227. )
  1228. except Exception:
  1229. reraise(*self._capture_exception())
  1230. raise ServerlessTimeoutWarning(
  1231. "WARNING : Function is expected to get timed out. Configured timeout duration = {} seconds.".format(
  1232. integer_configured_timeout
  1233. )
  1234. )
  1235. def to_base64(original: str) -> "Optional[str]":
  1236. """
  1237. Convert a string to base64, via UTF-8. Returns None on invalid input.
  1238. """
  1239. base64_string = None
  1240. try:
  1241. utf8_bytes = original.encode("UTF-8")
  1242. base64_bytes = base64.b64encode(utf8_bytes)
  1243. base64_string = base64_bytes.decode("UTF-8")
  1244. except Exception as err:
  1245. logger.warning("Unable to encode {orig} to base64:".format(orig=original), err)
  1246. return base64_string
  1247. def from_base64(base64_string: str) -> "Optional[str]":
  1248. """
  1249. Convert a string from base64, via UTF-8. Returns None on invalid input.
  1250. """
  1251. utf8_string = None
  1252. try:
  1253. only_valid_chars = BASE64_ALPHABET.match(base64_string)
  1254. assert only_valid_chars
  1255. base64_bytes = base64_string.encode("UTF-8")
  1256. utf8_bytes = base64.b64decode(base64_bytes)
  1257. utf8_string = utf8_bytes.decode("UTF-8")
  1258. except Exception as err:
  1259. logger.warning(
  1260. "Unable to decode {b64} from base64:".format(b64=base64_string), err
  1261. )
  1262. return utf8_string
  1263. Components = namedtuple("Components", ["scheme", "netloc", "path", "query", "fragment"])
  1264. def sanitize_url(
  1265. url: str,
  1266. remove_authority: bool = True,
  1267. remove_query_values: bool = True,
  1268. split: bool = False,
  1269. ) -> "Union[str, Components]":
  1270. """
  1271. Removes the authority and query parameter values from a given URL.
  1272. """
  1273. parsed_url = urlsplit(url)
  1274. query_params = parse_qs(parsed_url.query, keep_blank_values=True)
  1275. # strip username:password (netloc can be usr:pwd@example.com)
  1276. if remove_authority:
  1277. netloc_parts = parsed_url.netloc.split("@")
  1278. if len(netloc_parts) > 1:
  1279. netloc = "%s:%s@%s" % (
  1280. SENSITIVE_DATA_SUBSTITUTE,
  1281. SENSITIVE_DATA_SUBSTITUTE,
  1282. netloc_parts[-1],
  1283. )
  1284. else:
  1285. netloc = parsed_url.netloc
  1286. else:
  1287. netloc = parsed_url.netloc
  1288. # strip values from query string
  1289. if remove_query_values:
  1290. query_string = unquote(
  1291. urlencode({key: SENSITIVE_DATA_SUBSTITUTE for key in query_params})
  1292. )
  1293. else:
  1294. query_string = parsed_url.query
  1295. components = Components(
  1296. scheme=parsed_url.scheme,
  1297. netloc=netloc,
  1298. query=query_string,
  1299. path=parsed_url.path,
  1300. fragment=parsed_url.fragment,
  1301. )
  1302. if split:
  1303. return components
  1304. else:
  1305. return urlunsplit(components)
  1306. ParsedUrl = namedtuple("ParsedUrl", ["url", "query", "fragment"])
  1307. def parse_url(url: str, sanitize: bool = True) -> "ParsedUrl":
  1308. """
  1309. Splits a URL into a url (including path), query and fragment. If sanitize is True, the query
  1310. parameters will be sanitized to remove sensitive data. The autority (username and password)
  1311. in the URL will always be removed.
  1312. """
  1313. parsed_url = sanitize_url(
  1314. url, remove_authority=True, remove_query_values=sanitize, split=True
  1315. )
  1316. base_url = urlunsplit(
  1317. Components(
  1318. scheme=parsed_url.scheme, # type: ignore
  1319. netloc=parsed_url.netloc, # type: ignore
  1320. query="",
  1321. path=parsed_url.path, # type: ignore
  1322. fragment="",
  1323. )
  1324. )
  1325. return ParsedUrl(
  1326. url=base_url,
  1327. query=parsed_url.query, # type: ignore
  1328. fragment=parsed_url.fragment, # type: ignore
  1329. )
  1330. def is_valid_sample_rate(rate: "Any", source: str) -> bool:
  1331. """
  1332. Checks the given sample rate to make sure it is valid type and value (a
  1333. boolean or a number between 0 and 1, inclusive).
  1334. """
  1335. # both booleans and NaN are instances of Real, so a) checking for Real
  1336. # checks for the possibility of a boolean also, and b) we have to check
  1337. # separately for NaN and Decimal does not derive from Real so need to check that too
  1338. if not isinstance(rate, (Real, Decimal)) or math.isnan(rate):
  1339. logger.warning(
  1340. "{source} Given sample rate is invalid. Sample rate must be a boolean or a number between 0 and 1. Got {rate} of type {type}.".format(
  1341. source=source, rate=rate, type=type(rate)
  1342. )
  1343. )
  1344. return False
  1345. # in case rate is a boolean, it will get cast to 1 if it's True and 0 if it's False
  1346. rate = float(rate)
  1347. if rate < 0 or rate > 1:
  1348. logger.warning(
  1349. "{source} Given sample rate is invalid. Sample rate must be between 0 and 1. Got {rate}.".format(
  1350. source=source, rate=rate
  1351. )
  1352. )
  1353. return False
  1354. return True
  1355. def match_regex_list(
  1356. item: str,
  1357. regex_list: "Optional[List[str]]" = None,
  1358. substring_matching: bool = False,
  1359. ) -> bool:
  1360. if regex_list is None:
  1361. return False
  1362. for item_matcher in regex_list:
  1363. if not substring_matching and item_matcher[-1] != "$":
  1364. item_matcher += "$"
  1365. matched = re.search(item_matcher, item)
  1366. if matched:
  1367. return True
  1368. return False
  1369. def is_sentry_url(client: "sentry_sdk.client.BaseClient", url: str) -> bool:
  1370. """
  1371. Determines whether the given URL matches the Sentry DSN.
  1372. """
  1373. return (
  1374. client is not None
  1375. and client.transport is not None
  1376. and client.transport.parsed_dsn is not None
  1377. and client.transport.parsed_dsn.netloc in url
  1378. )
  1379. def _generate_installed_modules() -> "Iterator[Tuple[str, str]]":
  1380. try:
  1381. from importlib import metadata
  1382. yielded = set()
  1383. for dist in metadata.distributions():
  1384. name = dist.metadata.get("Name", None) # type: ignore[attr-defined]
  1385. # `metadata` values may be `None`, see:
  1386. # https://github.com/python/cpython/issues/91216
  1387. # and
  1388. # https://github.com/python/importlib_metadata/issues/371
  1389. if name is not None:
  1390. normalized_name = _normalize_module_name(name)
  1391. if dist.version is not None and normalized_name not in yielded:
  1392. yield normalized_name, dist.version
  1393. yielded.add(normalized_name)
  1394. except ImportError:
  1395. # < py3.8
  1396. try:
  1397. import pkg_resources
  1398. except ImportError:
  1399. return
  1400. for info in pkg_resources.working_set:
  1401. yield _normalize_module_name(info.key), info.version
  1402. def _normalize_module_name(name: str) -> str:
  1403. return name.lower()
  1404. def _replace_hyphens_dots_and_underscores_with_dashes(name: str) -> str:
  1405. # https://peps.python.org/pep-0503/#normalized-names
  1406. return re.sub(r"[-_.]+", "-", name)
  1407. def _get_installed_modules() -> "Dict[str, str]":
  1408. global _installed_modules
  1409. if _installed_modules is None:
  1410. _installed_modules = dict(_generate_installed_modules())
  1411. return _installed_modules
  1412. def package_version(package: str) -> "Optional[Tuple[int, ...]]":
  1413. normalized_package = _normalize_module_name(
  1414. _replace_hyphens_dots_and_underscores_with_dashes(package)
  1415. )
  1416. installed_packages = {
  1417. _replace_hyphens_dots_and_underscores_with_dashes(module): v
  1418. for module, v in _get_installed_modules().items()
  1419. }
  1420. version = installed_packages.get(normalized_package)
  1421. if version is None:
  1422. return None
  1423. return parse_version(version)
  1424. def reraise(
  1425. tp: "Optional[Type[BaseException]]",
  1426. value: "Optional[BaseException]",
  1427. tb: "Optional[Any]" = None,
  1428. ) -> "NoReturn":
  1429. assert value is not None
  1430. if value.__traceback__ is not tb:
  1431. raise value.with_traceback(tb)
  1432. raise value
  1433. def _no_op(*_a: "Any", **_k: "Any") -> None:
  1434. """No-op function for ensure_integration_enabled."""
  1435. pass
  1436. if TYPE_CHECKING:
  1437. @overload
  1438. def ensure_integration_enabled(
  1439. integration: "type[sentry_sdk.integrations.Integration]",
  1440. original_function: "Callable[P, R]",
  1441. ) -> "Callable[[Callable[P, R]], Callable[P, R]]": ...
  1442. @overload
  1443. def ensure_integration_enabled(
  1444. integration: "type[sentry_sdk.integrations.Integration]",
  1445. ) -> "Callable[[Callable[P, None]], Callable[P, None]]": ...
  1446. def ensure_integration_enabled(
  1447. integration: "type[sentry_sdk.integrations.Integration]",
  1448. original_function: "Union[Callable[P, R], Callable[P, None]]" = _no_op,
  1449. ) -> "Callable[[Callable[P, R]], Callable[P, R]]":
  1450. """
  1451. Ensures a given integration is enabled prior to calling a Sentry-patched function.
  1452. The function takes as its parameters the integration that must be enabled and the original
  1453. function that the SDK is patching. The function returns a function that takes the
  1454. decorated (Sentry-patched) function as its parameter, and returns a function that, when
  1455. called, checks whether the given integration is enabled. If the integration is enabled, the
  1456. function calls the decorated, Sentry-patched function. If the integration is not enabled,
  1457. the original function is called.
  1458. The function also takes care of preserving the original function's signature and docstring.
  1459. Example usage:
  1460. ```python
  1461. @ensure_integration_enabled(MyIntegration, my_function)
  1462. def patch_my_function():
  1463. with sentry_sdk.start_transaction(...):
  1464. return my_function()
  1465. ```
  1466. """
  1467. if TYPE_CHECKING:
  1468. # Type hint to ensure the default function has the right typing. The overloads
  1469. # ensure the default _no_op function is only used when R is None.
  1470. original_function = cast(Callable[P, R], original_function)
  1471. def patcher(sentry_patched_function: "Callable[P, R]") -> "Callable[P, R]":
  1472. def runner(*args: "P.args", **kwargs: "P.kwargs") -> "R":
  1473. if sentry_sdk.get_client().get_integration(integration) is None:
  1474. return original_function(*args, **kwargs)
  1475. return sentry_patched_function(*args, **kwargs)
  1476. if original_function is _no_op:
  1477. return wraps(sentry_patched_function)(runner)
  1478. return wraps(original_function)(runner)
  1479. return patcher
  1480. if PY37:
  1481. def nanosecond_time() -> int:
  1482. return time.perf_counter_ns()
  1483. else:
  1484. def nanosecond_time() -> int:
  1485. return int(time.perf_counter() * 1e9)
  1486. def now() -> float:
  1487. return time.perf_counter()
  1488. try:
  1489. from gevent import get_hub as get_gevent_hub
  1490. from gevent.monkey import is_module_patched
  1491. except ImportError:
  1492. # it's not great that the signatures are different, get_hub can't return None
  1493. # consider adding an if TYPE_CHECKING to change the signature to Optional[Hub]
  1494. def get_gevent_hub() -> "Optional[Hub]": # type: ignore[misc]
  1495. return None
  1496. def is_module_patched(mod_name: str) -> bool:
  1497. # unable to import from gevent means no modules have been patched
  1498. return False
  1499. def is_gevent() -> bool:
  1500. return is_module_patched("threading") or is_module_patched("_thread")
  1501. def get_current_thread_meta(
  1502. thread: "Optional[threading.Thread]" = None,
  1503. ) -> "Tuple[Optional[int], Optional[str]]":
  1504. """
  1505. Try to get the id of the current thread, with various fall backs.
  1506. """
  1507. # if a thread is specified, that takes priority
  1508. if thread is not None:
  1509. try:
  1510. thread_id = thread.ident
  1511. thread_name = thread.name
  1512. if thread_id is not None:
  1513. return thread_id, thread_name
  1514. except AttributeError:
  1515. pass
  1516. # if the app is using gevent, we should look at the gevent hub first
  1517. # as the id there differs from what the threading module reports
  1518. if is_gevent():
  1519. gevent_hub = get_gevent_hub()
  1520. if gevent_hub is not None:
  1521. try:
  1522. # this is undocumented, so wrap it in try except to be safe
  1523. return gevent_hub.thread_ident, None
  1524. except AttributeError:
  1525. pass
  1526. # use the current thread's id if possible
  1527. try:
  1528. thread = threading.current_thread()
  1529. thread_id = thread.ident
  1530. thread_name = thread.name
  1531. if thread_id is not None:
  1532. return thread_id, thread_name
  1533. except AttributeError:
  1534. pass
  1535. # if we can't get the current thread id, fall back to the main thread id
  1536. try:
  1537. thread = threading.main_thread()
  1538. thread_id = thread.ident
  1539. thread_name = thread.name
  1540. if thread_id is not None:
  1541. return thread_id, thread_name
  1542. except AttributeError:
  1543. pass
  1544. # we've tried everything, time to give up
  1545. return None, None
  1546. def should_be_treated_as_error(ty: "Any", value: "Any") -> bool:
  1547. if ty == SystemExit and hasattr(value, "code") and value.code in (0, None):
  1548. # https://docs.python.org/3/library/exceptions.html#SystemExit
  1549. return False
  1550. return True
  1551. if TYPE_CHECKING:
  1552. T = TypeVar("T")
  1553. def try_convert(convert_func: "Callable[[Any], T]", value: "Any") -> "Optional[T]":
  1554. """
  1555. Attempt to convert from an unknown type to a specific type, using the
  1556. given function. Return None if the conversion fails, i.e. if the function
  1557. raises an exception.
  1558. """
  1559. try:
  1560. if isinstance(value, convert_func): # type: ignore
  1561. return value
  1562. except TypeError:
  1563. pass
  1564. try:
  1565. return convert_func(value)
  1566. except Exception:
  1567. return None
  1568. def safe_serialize(data: "Any") -> str:
  1569. """Safely serialize to a readable string."""
  1570. def serialize_item(
  1571. item: "Any",
  1572. ) -> "Union[str, dict[Any, Any], list[Any], tuple[Any, ...]]":
  1573. if callable(item):
  1574. try:
  1575. module = getattr(item, "__module__", None)
  1576. qualname = getattr(item, "__qualname__", None)
  1577. name = getattr(item, "__name__", "anonymous")
  1578. if module and qualname:
  1579. full_path = f"{module}.{qualname}"
  1580. elif module and name:
  1581. full_path = f"{module}.{name}"
  1582. else:
  1583. full_path = name
  1584. return f"<function {full_path}>"
  1585. except Exception:
  1586. return f"<callable {type(item).__name__}>"
  1587. elif isinstance(item, dict):
  1588. return {k: serialize_item(v) for k, v in item.items()}
  1589. elif isinstance(item, (list, tuple)):
  1590. return [serialize_item(x) for x in item]
  1591. elif hasattr(item, "__dict__"):
  1592. try:
  1593. attrs = {
  1594. k: serialize_item(v)
  1595. for k, v in vars(item).items()
  1596. if not k.startswith("_")
  1597. }
  1598. return f"<{type(item).__name__} {attrs}>"
  1599. except Exception:
  1600. return repr(item)
  1601. else:
  1602. return item
  1603. try:
  1604. serialized = serialize_item(data)
  1605. return json.dumps(serialized, default=str)
  1606. except Exception:
  1607. return str(data)
  1608. def has_logs_enabled(options: "Optional[dict[str, Any]]") -> bool:
  1609. if options is None:
  1610. return False
  1611. return bool(
  1612. options.get("enable_logs", False)
  1613. or options["_experiments"].get("enable_logs", False)
  1614. )
  1615. def get_before_send_log(
  1616. options: "Optional[dict[str, Any]]",
  1617. ) -> "Optional[Callable[[Log, Hint], Optional[Log]]]":
  1618. if options is None:
  1619. return None
  1620. return options.get("before_send_log") or options["_experiments"].get(
  1621. "before_send_log"
  1622. )
  1623. def has_metrics_enabled(options: "Optional[dict[str, Any]]") -> bool:
  1624. if options is None:
  1625. return False
  1626. return bool(options.get("enable_metrics", True))
  1627. def get_before_send_metric(
  1628. options: "Optional[dict[str, Any]]",
  1629. ) -> "Optional[Callable[[Metric, Hint], Optional[Metric]]]":
  1630. if options is None:
  1631. return None
  1632. return options.get("before_send_metric") or options["_experiments"].get(
  1633. "before_send_metric"
  1634. )
  1635. def format_attribute(val: "Any") -> "AttributeValue":
  1636. """
  1637. Turn unsupported attribute value types into an AttributeValue.
  1638. We do this as soon as a user-provided attribute is set, to prevent spans,
  1639. logs, metrics and similar from having live references to various objects.
  1640. Note: This is not the final attribute value format. Before they're sent,
  1641. they're serialized further into the actual format the protocol expects:
  1642. https://develop.sentry.dev/sdk/telemetry/attributes/
  1643. """
  1644. if isinstance(val, (bool, int, float, str)):
  1645. return val
  1646. # Only lists of elements of a single type are supported
  1647. list_types: 'dict[type, Literal["string[]", "integer[]", "double[]", "boolean[]"]]' = {
  1648. str: "string[]",
  1649. int: "integer[]",
  1650. float: "double[]",
  1651. bool: "boolean[]",
  1652. }
  1653. if isinstance(val, (list, tuple)) and not val:
  1654. return []
  1655. elif isinstance(val, list):
  1656. ty = type(val[0])
  1657. if ty in list_types and all(type(v) is ty for v in val):
  1658. return copy.deepcopy(val)
  1659. elif isinstance(val, tuple):
  1660. ty = type(val[0])
  1661. if ty in list_types and all(type(v) is ty for v in val):
  1662. return list(val)
  1663. return safe_repr(val)
  1664. def serialize_attribute(val: "AttributeValue") -> "SerializedAttributeValue":
  1665. """Serialize attribute value to the transport format."""
  1666. if isinstance(val, bool):
  1667. return {"value": val, "type": "boolean"}
  1668. if isinstance(val, int):
  1669. return {"value": val, "type": "integer"}
  1670. if isinstance(val, float):
  1671. return {"value": val, "type": "double"}
  1672. if isinstance(val, str):
  1673. return {"value": val, "type": "string"}
  1674. if isinstance(val, list):
  1675. if not val:
  1676. return {"value": [], "type": "string[]"}
  1677. # Only lists of elements of a single type are supported
  1678. list_types: 'dict[type, Literal["string[]", "integer[]", "double[]", "boolean[]"]]' = {
  1679. str: "string[]",
  1680. int: "integer[]",
  1681. float: "double[]",
  1682. bool: "boolean[]",
  1683. }
  1684. ty = type(val[0])
  1685. if ty in list_types and all(type(v) is ty for v in val):
  1686. return {"value": val, "type": list_types[ty]}
  1687. # Coerce to string if we don't know what to do with the value. This should
  1688. # never happen as we pre-format early in format_attribute, but let's be safe.
  1689. return {"value": safe_repr(val), "type": "string"}