mypy-dev 1.9.0.dev0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (1018) hide show
  1. mypy/__init__.py +1 -0
  2. mypy/__main__.py +37 -0
  3. mypy/api.py +94 -0
  4. mypy/applytype.py +156 -0
  5. mypy/argmap.py +268 -0
  6. mypy/binder.py +538 -0
  7. mypy/bogus_type.py +27 -0
  8. mypy/build.py +3562 -0
  9. mypy/checker.py +8372 -0
  10. mypy/checkexpr.py +6615 -0
  11. mypy/checkmember.py +1356 -0
  12. mypy/checkpattern.py +801 -0
  13. mypy/checkstrformat.py +1109 -0
  14. mypy/config_parser.py +670 -0
  15. mypy/constant_fold.py +187 -0
  16. mypy/constraints.py +1624 -0
  17. mypy/copytype.py +133 -0
  18. mypy/defaults.py +46 -0
  19. mypy/dmypy/__init__.py +0 -0
  20. mypy/dmypy/__main__.py +6 -0
  21. mypy/dmypy/client.py +749 -0
  22. mypy/dmypy_os.py +42 -0
  23. mypy/dmypy_server.py +1107 -0
  24. mypy/dmypy_util.py +117 -0
  25. mypy/erasetype.py +278 -0
  26. mypy/errorcodes.py +285 -0
  27. mypy/errors.py +1280 -0
  28. mypy/evalexpr.py +205 -0
  29. mypy/expandtype.py +510 -0
  30. mypy/exprtotype.py +206 -0
  31. mypy/fastparse.py +2147 -0
  32. mypy/find_sources.py +243 -0
  33. mypy/fixup.py +426 -0
  34. mypy/freetree.py +23 -0
  35. mypy/fscache.py +309 -0
  36. mypy/fswatcher.py +106 -0
  37. mypy/gclogger.py +47 -0
  38. mypy/git.py +34 -0
  39. mypy/graph_utils.py +112 -0
  40. mypy/indirection.py +121 -0
  41. mypy/infer.py +75 -0
  42. mypy/inspections.py +627 -0
  43. mypy/ipc.py +310 -0
  44. mypy/join.py +866 -0
  45. mypy/literals.py +306 -0
  46. mypy/lookup.py +61 -0
  47. mypy/main.py +1574 -0
  48. mypy/maptype.py +106 -0
  49. mypy/meet.py +1140 -0
  50. mypy/memprofile.py +121 -0
  51. mypy/message_registry.py +326 -0
  52. mypy/messages.py +3171 -0
  53. mypy/metastore.py +225 -0
  54. mypy/mixedtraverser.py +112 -0
  55. mypy/modulefinder.py +875 -0
  56. mypy/moduleinspect.py +184 -0
  57. mypy/mro.py +62 -0
  58. mypy/nodes.py +4112 -0
  59. mypy/operators.py +126 -0
  60. mypy/options.py +556 -0
  61. mypy/parse.py +30 -0
  62. mypy/partially_defined.py +675 -0
  63. mypy/patterns.py +150 -0
  64. mypy/plugin.py +901 -0
  65. mypy/plugins/__init__.py +0 -0
  66. mypy/plugins/attrs.py +1152 -0
  67. mypy/plugins/common.py +440 -0
  68. mypy/plugins/ctypes.py +245 -0
  69. mypy/plugins/dataclasses.py +1101 -0
  70. mypy/plugins/default.py +518 -0
  71. mypy/plugins/enums.py +259 -0
  72. mypy/plugins/functools.py +104 -0
  73. mypy/plugins/proper_plugin.py +175 -0
  74. mypy/plugins/singledispatch.py +224 -0
  75. mypy/py.typed +1 -0
  76. mypy/pyinfo.py +78 -0
  77. mypy/reachability.py +362 -0
  78. mypy/refinfo.py +92 -0
  79. mypy/renaming.py +568 -0
  80. mypy/report.py +924 -0
  81. mypy/scope.py +125 -0
  82. mypy/semanal.py +7023 -0
  83. mypy/semanal_classprop.py +187 -0
  84. mypy/semanal_enum.py +253 -0
  85. mypy/semanal_infer.py +128 -0
  86. mypy/semanal_main.py +511 -0
  87. mypy/semanal_namedtuple.py +670 -0
  88. mypy/semanal_newtype.py +273 -0
  89. mypy/semanal_pass1.py +156 -0
  90. mypy/semanal_shared.py +490 -0
  91. mypy/semanal_typeargs.py +265 -0
  92. mypy/semanal_typeddict.py +575 -0
  93. mypy/server/__init__.py +0 -0
  94. mypy/server/astdiff.py +518 -0
  95. mypy/server/astmerge.py +562 -0
  96. mypy/server/aststrip.py +281 -0
  97. mypy/server/deps.py +1137 -0
  98. mypy/server/mergecheck.py +83 -0
  99. mypy/server/objgraph.py +101 -0
  100. mypy/server/subexpr.py +198 -0
  101. mypy/server/target.py +11 -0
  102. mypy/server/trigger.py +26 -0
  103. mypy/server/update.py +1339 -0
  104. mypy/sharedparse.py +112 -0
  105. mypy/solve.py +562 -0
  106. mypy/split_namespace.py +35 -0
  107. mypy/state.py +28 -0
  108. mypy/stats.py +489 -0
  109. mypy/strconv.py +641 -0
  110. mypy/stubdoc.py +491 -0
  111. mypy/stubgen.py +1873 -0
  112. mypy/stubgenc.py +993 -0
  113. mypy/stubinfo.py +173 -0
  114. mypy/stubtest.py +2079 -0
  115. mypy/stubutil.py +807 -0
  116. mypy/subtypes.py +1967 -0
  117. mypy/suggestions.py +1046 -0
  118. mypy/test/__init__.py +0 -0
  119. mypy/test/config.py +28 -0
  120. mypy/test/data.py +823 -0
  121. mypy/test/helpers.py +476 -0
  122. mypy/test/meta/__init__.py +0 -0
  123. mypy/test/meta/_pytest.py +72 -0
  124. mypy/test/meta/test_diff_helper.py +47 -0
  125. mypy/test/meta/test_parse_data.py +73 -0
  126. mypy/test/meta/test_update_data.py +135 -0
  127. mypy/test/test_find_sources.py +376 -0
  128. mypy/test/test_ref_info.py +45 -0
  129. mypy/test/testapi.py +45 -0
  130. mypy/test/testargs.py +77 -0
  131. mypy/test/testcheck.py +322 -0
  132. mypy/test/testcmdline.py +152 -0
  133. mypy/test/testconstraints.py +134 -0
  134. mypy/test/testdaemon.py +132 -0
  135. mypy/test/testdeps.py +77 -0
  136. mypy/test/testdiff.py +67 -0
  137. mypy/test/testerrorstream.py +46 -0
  138. mypy/test/testfinegrained.py +438 -0
  139. mypy/test/testfinegrainedcache.py +18 -0
  140. mypy/test/testformatter.py +85 -0
  141. mypy/test/testfscache.py +101 -0
  142. mypy/test/testgraph.py +83 -0
  143. mypy/test/testinfer.py +373 -0
  144. mypy/test/testipc.py +119 -0
  145. mypy/test/testmerge.py +238 -0
  146. mypy/test/testmodulefinder.py +278 -0
  147. mypy/test/testmypyc.py +14 -0
  148. mypy/test/testparse.py +107 -0
  149. mypy/test/testpep561.py +211 -0
  150. mypy/test/testpythoneval.py +117 -0
  151. mypy/test/testreports.py +55 -0
  152. mypy/test/testsemanal.py +209 -0
  153. mypy/test/testsolve.py +285 -0
  154. mypy/test/teststubgen.py +1412 -0
  155. mypy/test/teststubinfo.py +12 -0
  156. mypy/test/teststubtest.py +2492 -0
  157. mypy/test/testsubtypes.py +303 -0
  158. mypy/test/testtransform.py +64 -0
  159. mypy/test/testtypegen.py +83 -0
  160. mypy/test/testtypes.py +1551 -0
  161. mypy/test/testutil.py +111 -0
  162. mypy/test/typefixture.py +415 -0
  163. mypy/test/update_data.py +87 -0
  164. mypy/test/visitors.py +63 -0
  165. mypy/traverser.py +961 -0
  166. mypy/treetransform.py +800 -0
  167. mypy/tvar_scope.py +147 -0
  168. mypy/type_visitor.py +564 -0
  169. mypy/typeanal.py +2510 -0
  170. mypy/typeops.py +1082 -0
  171. mypy/types.py +3686 -0
  172. mypy/types_utils.py +166 -0
  173. mypy/typeshed/LICENSE +237 -0
  174. mypy/typeshed/stdlib/VERSIONS +308 -0
  175. mypy/typeshed/stdlib/__future__.pyi +36 -0
  176. mypy/typeshed/stdlib/__main__.pyi +3 -0
  177. mypy/typeshed/stdlib/_ast.pyi +587 -0
  178. mypy/typeshed/stdlib/_bisect.pyi +84 -0
  179. mypy/typeshed/stdlib/_bootlocale.pyi +1 -0
  180. mypy/typeshed/stdlib/_codecs.pyi +133 -0
  181. mypy/typeshed/stdlib/_collections_abc.pyi +94 -0
  182. mypy/typeshed/stdlib/_compat_pickle.pyi +8 -0
  183. mypy/typeshed/stdlib/_compression.pyi +25 -0
  184. mypy/typeshed/stdlib/_csv.pyi +90 -0
  185. mypy/typeshed/stdlib/_ctypes.pyi +205 -0
  186. mypy/typeshed/stdlib/_curses.pyi +557 -0
  187. mypy/typeshed/stdlib/_decimal.pyi +281 -0
  188. mypy/typeshed/stdlib/_dummy_thread.pyi +27 -0
  189. mypy/typeshed/stdlib/_dummy_threading.pyi +164 -0
  190. mypy/typeshed/stdlib/_heapq.pyi +11 -0
  191. mypy/typeshed/stdlib/_imp.pyi +28 -0
  192. mypy/typeshed/stdlib/_json.pyi +49 -0
  193. mypy/typeshed/stdlib/_locale.pyi +100 -0
  194. mypy/typeshed/stdlib/_markupbase.pyi +16 -0
  195. mypy/typeshed/stdlib/_msi.pyi +91 -0
  196. mypy/typeshed/stdlib/_operator.pyi +147 -0
  197. mypy/typeshed/stdlib/_osx_support.pyi +34 -0
  198. mypy/typeshed/stdlib/_posixsubprocess.pyi +32 -0
  199. mypy/typeshed/stdlib/_py_abc.pyi +14 -0
  200. mypy/typeshed/stdlib/_pydecimal.pyi +43 -0
  201. mypy/typeshed/stdlib/_random.pyi +12 -0
  202. mypy/typeshed/stdlib/_sitebuiltins.pyi +16 -0
  203. mypy/typeshed/stdlib/_socket.pyi +802 -0
  204. mypy/typeshed/stdlib/_stat.pyi +103 -0
  205. mypy/typeshed/stdlib/_thread.pyi +52 -0
  206. mypy/typeshed/stdlib/_threading_local.pyi +17 -0
  207. mypy/typeshed/stdlib/_tkinter.pyi +120 -0
  208. mypy/typeshed/stdlib/_tracemalloc.pyi +17 -0
  209. mypy/typeshed/stdlib/_typeshed/__init__.pyi +347 -0
  210. mypy/typeshed/stdlib/_typeshed/dbapi.pyi +37 -0
  211. mypy/typeshed/stdlib/_typeshed/wsgi.pyi +44 -0
  212. mypy/typeshed/stdlib/_typeshed/xml.pyi +9 -0
  213. mypy/typeshed/stdlib/_warnings.pyi +55 -0
  214. mypy/typeshed/stdlib/_weakref.pyi +41 -0
  215. mypy/typeshed/stdlib/_weakrefset.pyi +51 -0
  216. mypy/typeshed/stdlib/_winapi.pyi +251 -0
  217. mypy/typeshed/stdlib/abc.pyi +49 -0
  218. mypy/typeshed/stdlib/aifc.pyi +91 -0
  219. mypy/typeshed/stdlib/antigravity.pyi +3 -0
  220. mypy/typeshed/stdlib/argparse.pyi +571 -0
  221. mypy/typeshed/stdlib/array.pyi +92 -0
  222. mypy/typeshed/stdlib/ast.pyi +277 -0
  223. mypy/typeshed/stdlib/asynchat.pyi +21 -0
  224. mypy/typeshed/stdlib/asyncio/__init__.pyi +41 -0
  225. mypy/typeshed/stdlib/asyncio/base_events.pyi +440 -0
  226. mypy/typeshed/stdlib/asyncio/base_futures.pyi +19 -0
  227. mypy/typeshed/stdlib/asyncio/base_subprocess.pyi +63 -0
  228. mypy/typeshed/stdlib/asyncio/base_tasks.pyi +9 -0
  229. mypy/typeshed/stdlib/asyncio/constants.pyi +20 -0
  230. mypy/typeshed/stdlib/asyncio/coroutines.pyi +26 -0
  231. mypy/typeshed/stdlib/asyncio/events.pyi +574 -0
  232. mypy/typeshed/stdlib/asyncio/exceptions.pyi +43 -0
  233. mypy/typeshed/stdlib/asyncio/format_helpers.pyi +20 -0
  234. mypy/typeshed/stdlib/asyncio/futures.pyi +57 -0
  235. mypy/typeshed/stdlib/asyncio/locks.pyi +121 -0
  236. mypy/typeshed/stdlib/asyncio/log.pyi +3 -0
  237. mypy/typeshed/stdlib/asyncio/mixins.pyi +9 -0
  238. mypy/typeshed/stdlib/asyncio/proactor_events.pyi +64 -0
  239. mypy/typeshed/stdlib/asyncio/protocols.pyi +34 -0
  240. mypy/typeshed/stdlib/asyncio/queues.pyi +47 -0
  241. mypy/typeshed/stdlib/asyncio/runners.pyi +32 -0
  242. mypy/typeshed/stdlib/asyncio/selector_events.pyi +8 -0
  243. mypy/typeshed/stdlib/asyncio/sslproto.pyi +163 -0
  244. mypy/typeshed/stdlib/asyncio/staggered.pyi +10 -0
  245. mypy/typeshed/stdlib/asyncio/streams.pyi +148 -0
  246. mypy/typeshed/stdlib/asyncio/subprocess.pyi +229 -0
  247. mypy/typeshed/stdlib/asyncio/taskgroups.pyi +25 -0
  248. mypy/typeshed/stdlib/asyncio/tasks.pyi +477 -0
  249. mypy/typeshed/stdlib/asyncio/threads.pyi +9 -0
  250. mypy/typeshed/stdlib/asyncio/timeouts.pyi +19 -0
  251. mypy/typeshed/stdlib/asyncio/transports.pyi +47 -0
  252. mypy/typeshed/stdlib/asyncio/trsock.pyi +98 -0
  253. mypy/typeshed/stdlib/asyncio/unix_events.pyi +173 -0
  254. mypy/typeshed/stdlib/asyncio/windows_events.pyi +83 -0
  255. mypy/typeshed/stdlib/asyncio/windows_utils.pyi +49 -0
  256. mypy/typeshed/stdlib/asyncore.pyi +90 -0
  257. mypy/typeshed/stdlib/atexit.pyi +12 -0
  258. mypy/typeshed/stdlib/audioop.pyi +42 -0
  259. mypy/typeshed/stdlib/base64.pyi +59 -0
  260. mypy/typeshed/stdlib/bdb.pyi +102 -0
  261. mypy/typeshed/stdlib/binascii.pyi +36 -0
  262. mypy/typeshed/stdlib/binhex.pyi +45 -0
  263. mypy/typeshed/stdlib/bisect.pyi +4 -0
  264. mypy/typeshed/stdlib/builtins.pyi +1937 -0
  265. mypy/typeshed/stdlib/bz2.pyi +146 -0
  266. mypy/typeshed/stdlib/cProfile.pyi +35 -0
  267. mypy/typeshed/stdlib/calendar.pyi +206 -0
  268. mypy/typeshed/stdlib/cgi.pyi +118 -0
  269. mypy/typeshed/stdlib/cgitb.pyi +32 -0
  270. mypy/typeshed/stdlib/chunk.pyi +20 -0
  271. mypy/typeshed/stdlib/cmath.pyi +36 -0
  272. mypy/typeshed/stdlib/cmd.pyi +45 -0
  273. mypy/typeshed/stdlib/code.pyi +33 -0
  274. mypy/typeshed/stdlib/codecs.pyi +285 -0
  275. mypy/typeshed/stdlib/codeop.pyi +13 -0
  276. mypy/typeshed/stdlib/collections/__init__.pyi +477 -0
  277. mypy/typeshed/stdlib/collections/abc.pyi +2 -0
  278. mypy/typeshed/stdlib/colorsys.pyi +13 -0
  279. mypy/typeshed/stdlib/compileall.pyi +111 -0
  280. mypy/typeshed/stdlib/concurrent/__init__.pyi +0 -0
  281. mypy/typeshed/stdlib/concurrent/futures/__init__.pyi +32 -0
  282. mypy/typeshed/stdlib/concurrent/futures/_base.pyi +120 -0
  283. mypy/typeshed/stdlib/concurrent/futures/process.pyi +233 -0
  284. mypy/typeshed/stdlib/concurrent/futures/thread.pyi +80 -0
  285. mypy/typeshed/stdlib/configparser.pyi +313 -0
  286. mypy/typeshed/stdlib/contextlib.pyi +207 -0
  287. mypy/typeshed/stdlib/contextvars.pyi +63 -0
  288. mypy/typeshed/stdlib/copy.pyi +16 -0
  289. mypy/typeshed/stdlib/copyreg.pyi +21 -0
  290. mypy/typeshed/stdlib/crypt.pyi +12 -0
  291. mypy/typeshed/stdlib/csv.pyi +147 -0
  292. mypy/typeshed/stdlib/ctypes/__init__.pyi +187 -0
  293. mypy/typeshed/stdlib/ctypes/_endian.pyi +19 -0
  294. mypy/typeshed/stdlib/ctypes/util.pyi +6 -0
  295. mypy/typeshed/stdlib/ctypes/wintypes.pyi +298 -0
  296. mypy/typeshed/stdlib/curses/__init__.pyi +21 -0
  297. mypy/typeshed/stdlib/curses/ascii.pyi +63 -0
  298. mypy/typeshed/stdlib/curses/has_key.pyi +4 -0
  299. mypy/typeshed/stdlib/curses/panel.pyi +25 -0
  300. mypy/typeshed/stdlib/curses/textpad.pyi +13 -0
  301. mypy/typeshed/stdlib/dataclasses.pyi +313 -0
  302. mypy/typeshed/stdlib/datetime.pyi +295 -0
  303. mypy/typeshed/stdlib/dbm/__init__.pyi +95 -0
  304. mypy/typeshed/stdlib/dbm/dumb.pyi +31 -0
  305. mypy/typeshed/stdlib/dbm/gnu.pyi +40 -0
  306. mypy/typeshed/stdlib/dbm/ndbm.pyi +36 -0
  307. mypy/typeshed/stdlib/decimal.pyi +2 -0
  308. mypy/typeshed/stdlib/difflib.pyi +140 -0
  309. mypy/typeshed/stdlib/dis.pyi +144 -0
  310. mypy/typeshed/stdlib/distutils/__init__.pyi +5 -0
  311. mypy/typeshed/stdlib/distutils/archive_util.pyi +20 -0
  312. mypy/typeshed/stdlib/distutils/bcppcompiler.pyi +3 -0
  313. mypy/typeshed/stdlib/distutils/ccompiler.pyi +152 -0
  314. mypy/typeshed/stdlib/distutils/cmd.pyi +66 -0
  315. mypy/typeshed/stdlib/distutils/command/__init__.pyi +0 -0
  316. mypy/typeshed/stdlib/distutils/command/bdist.pyi +25 -0
  317. mypy/typeshed/stdlib/distutils/command/bdist_dumb.pyi +21 -0
  318. mypy/typeshed/stdlib/distutils/command/bdist_msi.pyi +45 -0
  319. mypy/typeshed/stdlib/distutils/command/bdist_packager.pyi +0 -0
  320. mypy/typeshed/stdlib/distutils/command/bdist_rpm.pyi +52 -0
  321. mypy/typeshed/stdlib/distutils/command/bdist_wininst.pyi +16 -0
  322. mypy/typeshed/stdlib/distutils/command/build.pyi +31 -0
  323. mypy/typeshed/stdlib/distutils/command/build_clib.pyi +27 -0
  324. mypy/typeshed/stdlib/distutils/command/build_ext.pyi +50 -0
  325. mypy/typeshed/stdlib/distutils/command/build_py.pyi +44 -0
  326. mypy/typeshed/stdlib/distutils/command/build_scripts.pyi +24 -0
  327. mypy/typeshed/stdlib/distutils/command/check.pyi +39 -0
  328. mypy/typeshed/stdlib/distutils/command/clean.pyi +17 -0
  329. mypy/typeshed/stdlib/distutils/command/config.pyi +83 -0
  330. mypy/typeshed/stdlib/distutils/command/install.pyi +63 -0
  331. mypy/typeshed/stdlib/distutils/command/install_data.pyi +19 -0
  332. mypy/typeshed/stdlib/distutils/command/install_egg_info.pyi +18 -0
  333. mypy/typeshed/stdlib/distutils/command/install_headers.pyi +16 -0
  334. mypy/typeshed/stdlib/distutils/command/install_lib.pyi +25 -0
  335. mypy/typeshed/stdlib/distutils/command/install_scripts.pyi +18 -0
  336. mypy/typeshed/stdlib/distutils/command/register.pyi +18 -0
  337. mypy/typeshed/stdlib/distutils/command/sdist.pyi +42 -0
  338. mypy/typeshed/stdlib/distutils/command/upload.pyi +17 -0
  339. mypy/typeshed/stdlib/distutils/config.pyi +17 -0
  340. mypy/typeshed/stdlib/distutils/core.pyi +57 -0
  341. mypy/typeshed/stdlib/distutils/cygwinccompiler.pyi +20 -0
  342. mypy/typeshed/stdlib/distutils/debug.pyi +1 -0
  343. mypy/typeshed/stdlib/distutils/dep_util.pyi +3 -0
  344. mypy/typeshed/stdlib/distutils/dir_util.pyi +13 -0
  345. mypy/typeshed/stdlib/distutils/dist.pyi +146 -0
  346. mypy/typeshed/stdlib/distutils/errors.pyi +19 -0
  347. mypy/typeshed/stdlib/distutils/extension.pyi +36 -0
  348. mypy/typeshed/stdlib/distutils/fancy_getopt.pyi +34 -0
  349. mypy/typeshed/stdlib/distutils/file_util.pyi +14 -0
  350. mypy/typeshed/stdlib/distutils/filelist.pyi +50 -0
  351. mypy/typeshed/stdlib/distutils/log.pyi +25 -0
  352. mypy/typeshed/stdlib/distutils/msvccompiler.pyi +3 -0
  353. mypy/typeshed/stdlib/distutils/spawn.pyi +2 -0
  354. mypy/typeshed/stdlib/distutils/sysconfig.pyi +22 -0
  355. mypy/typeshed/stdlib/distutils/text_file.pyi +21 -0
  356. mypy/typeshed/stdlib/distutils/unixccompiler.pyi +3 -0
  357. mypy/typeshed/stdlib/distutils/util.pyi +46 -0
  358. mypy/typeshed/stdlib/distutils/version.pyi +36 -0
  359. mypy/typeshed/stdlib/doctest.pyi +248 -0
  360. mypy/typeshed/stdlib/dummy_threading.pyi +2 -0
  361. mypy/typeshed/stdlib/email/__init__.pyi +29 -0
  362. mypy/typeshed/stdlib/email/_header_value_parser.pyi +392 -0
  363. mypy/typeshed/stdlib/email/_policybase.pyi +51 -0
  364. mypy/typeshed/stdlib/email/base64mime.pyi +13 -0
  365. mypy/typeshed/stdlib/email/charset.pyi +34 -0
  366. mypy/typeshed/stdlib/email/contentmanager.pyi +11 -0
  367. mypy/typeshed/stdlib/email/encoders.pyi +8 -0
  368. mypy/typeshed/stdlib/email/errors.pyi +39 -0
  369. mypy/typeshed/stdlib/email/feedparser.pyi +23 -0
  370. mypy/typeshed/stdlib/email/generator.pyi +40 -0
  371. mypy/typeshed/stdlib/email/header.pyi +31 -0
  372. mypy/typeshed/stdlib/email/headerregistry.pyi +178 -0
  373. mypy/typeshed/stdlib/email/iterators.pyi +12 -0
  374. mypy/typeshed/stdlib/email/message.pyi +161 -0
  375. mypy/typeshed/stdlib/email/mime/__init__.pyi +0 -0
  376. mypy/typeshed/stdlib/email/mime/application.pyi +17 -0
  377. mypy/typeshed/stdlib/email/mime/audio.pyi +17 -0
  378. mypy/typeshed/stdlib/email/mime/base.pyi +8 -0
  379. mypy/typeshed/stdlib/email/mime/image.pyi +17 -0
  380. mypy/typeshed/stdlib/email/mime/message.pyi +8 -0
  381. mypy/typeshed/stdlib/email/mime/multipart.pyi +18 -0
  382. mypy/typeshed/stdlib/email/mime/nonmultipart.pyi +5 -0
  383. mypy/typeshed/stdlib/email/mime/text.pyi +9 -0
  384. mypy/typeshed/stdlib/email/parser.pyi +26 -0
  385. mypy/typeshed/stdlib/email/policy.pyi +38 -0
  386. mypy/typeshed/stdlib/email/quoprimime.pyi +28 -0
  387. mypy/typeshed/stdlib/email/utils.pyi +66 -0
  388. mypy/typeshed/stdlib/encodings/__init__.pyi +10 -0
  389. mypy/typeshed/stdlib/encodings/utf_8.pyi +21 -0
  390. mypy/typeshed/stdlib/encodings/utf_8_sig.pyi +22 -0
  391. mypy/typeshed/stdlib/ensurepip/__init__.pyi +12 -0
  392. mypy/typeshed/stdlib/enum.pyi +317 -0
  393. mypy/typeshed/stdlib/errno.pyi +222 -0
  394. mypy/typeshed/stdlib/faulthandler.pyi +13 -0
  395. mypy/typeshed/stdlib/fcntl.pyi +127 -0
  396. mypy/typeshed/stdlib/filecmp.pyi +57 -0
  397. mypy/typeshed/stdlib/fileinput.pyi +213 -0
  398. mypy/typeshed/stdlib/fnmatch.pyi +9 -0
  399. mypy/typeshed/stdlib/formatter.pyi +88 -0
  400. mypy/typeshed/stdlib/fractions.pyi +150 -0
  401. mypy/typeshed/stdlib/ftplib.pyi +178 -0
  402. mypy/typeshed/stdlib/functools.pyi +200 -0
  403. mypy/typeshed/stdlib/gc.pyi +37 -0
  404. mypy/typeshed/stdlib/genericpath.pyi +52 -0
  405. mypy/typeshed/stdlib/getopt.pyi +11 -0
  406. mypy/typeshed/stdlib/getpass.pyi +8 -0
  407. mypy/typeshed/stdlib/gettext.pyi +169 -0
  408. mypy/typeshed/stdlib/glob.pyi +42 -0
  409. mypy/typeshed/stdlib/graphlib.pyi +28 -0
  410. mypy/typeshed/stdlib/grp.pyi +21 -0
  411. mypy/typeshed/stdlib/gzip.pyi +160 -0
  412. mypy/typeshed/stdlib/hashlib.pyi +165 -0
  413. mypy/typeshed/stdlib/heapq.pyi +17 -0
  414. mypy/typeshed/stdlib/hmac.pyi +38 -0
  415. mypy/typeshed/stdlib/html/__init__.pyi +6 -0
  416. mypy/typeshed/stdlib/html/entities.pyi +6 -0
  417. mypy/typeshed/stdlib/html/parser.pyi +34 -0
  418. mypy/typeshed/stdlib/http/__init__.pyi +105 -0
  419. mypy/typeshed/stdlib/http/client.pyi +259 -0
  420. mypy/typeshed/stdlib/http/cookiejar.pyi +159 -0
  421. mypy/typeshed/stdlib/http/cookies.pyi +60 -0
  422. mypy/typeshed/stdlib/http/server.pyi +83 -0
  423. mypy/typeshed/stdlib/imaplib.pyi +168 -0
  424. mypy/typeshed/stdlib/imghdr.pyi +17 -0
  425. mypy/typeshed/stdlib/imp.pyi +62 -0
  426. mypy/typeshed/stdlib/importlib/__init__.pyi +24 -0
  427. mypy/typeshed/stdlib/importlib/_abc.pyi +15 -0
  428. mypy/typeshed/stdlib/importlib/abc.pyi +172 -0
  429. mypy/typeshed/stdlib/importlib/machinery.pyi +179 -0
  430. mypy/typeshed/stdlib/importlib/metadata/__init__.pyi +276 -0
  431. mypy/typeshed/stdlib/importlib/metadata/_meta.pyi +40 -0
  432. mypy/typeshed/stdlib/importlib/readers.pyi +68 -0
  433. mypy/typeshed/stdlib/importlib/resources/__init__.pyi +46 -0
  434. mypy/typeshed/stdlib/importlib/resources/abc.pyi +12 -0
  435. mypy/typeshed/stdlib/importlib/resources/readers.pyi +14 -0
  436. mypy/typeshed/stdlib/importlib/resources/simple.pyi +49 -0
  437. mypy/typeshed/stdlib/importlib/simple.pyi +11 -0
  438. mypy/typeshed/stdlib/importlib/util.pyi +43 -0
  439. mypy/typeshed/stdlib/inspect.pyi +631 -0
  440. mypy/typeshed/stdlib/io.pyi +204 -0
  441. mypy/typeshed/stdlib/ipaddress.pyi +208 -0
  442. mypy/typeshed/stdlib/itertools.pyi +293 -0
  443. mypy/typeshed/stdlib/json/__init__.pyi +61 -0
  444. mypy/typeshed/stdlib/json/decoder.pyi +32 -0
  445. mypy/typeshed/stdlib/json/encoder.pyi +38 -0
  446. mypy/typeshed/stdlib/json/tool.pyi +1 -0
  447. mypy/typeshed/stdlib/keyword.pyi +21 -0
  448. mypy/typeshed/stdlib/lib2to3/__init__.pyi +0 -0
  449. mypy/typeshed/stdlib/lib2to3/btm_matcher.pyi +28 -0
  450. mypy/typeshed/stdlib/lib2to3/fixer_base.pyi +42 -0
  451. mypy/typeshed/stdlib/lib2to3/fixes/__init__.pyi +0 -0
  452. mypy/typeshed/stdlib/lib2to3/fixes/fix_apply.pyi +8 -0
  453. mypy/typeshed/stdlib/lib2to3/fixes/fix_asserts.pyi +10 -0
  454. mypy/typeshed/stdlib/lib2to3/fixes/fix_basestring.pyi +8 -0
  455. mypy/typeshed/stdlib/lib2to3/fixes/fix_buffer.pyi +8 -0
  456. mypy/typeshed/stdlib/lib2to3/fixes/fix_dict.pyi +16 -0
  457. mypy/typeshed/stdlib/lib2to3/fixes/fix_except.pyi +14 -0
  458. mypy/typeshed/stdlib/lib2to3/fixes/fix_exec.pyi +8 -0
  459. mypy/typeshed/stdlib/lib2to3/fixes/fix_execfile.pyi +8 -0
  460. mypy/typeshed/stdlib/lib2to3/fixes/fix_exitfunc.pyi +13 -0
  461. mypy/typeshed/stdlib/lib2to3/fixes/fix_filter.pyi +9 -0
  462. mypy/typeshed/stdlib/lib2to3/fixes/fix_funcattrs.pyi +8 -0
  463. mypy/typeshed/stdlib/lib2to3/fixes/fix_future.pyi +8 -0
  464. mypy/typeshed/stdlib/lib2to3/fixes/fix_getcwdu.pyi +8 -0
  465. mypy/typeshed/stdlib/lib2to3/fixes/fix_has_key.pyi +8 -0
  466. mypy/typeshed/stdlib/lib2to3/fixes/fix_idioms.pyi +15 -0
  467. mypy/typeshed/stdlib/lib2to3/fixes/fix_import.pyi +16 -0
  468. mypy/typeshed/stdlib/lib2to3/fixes/fix_imports.pyi +21 -0
  469. mypy/typeshed/stdlib/lib2to3/fixes/fix_imports2.pyi +6 -0
  470. mypy/typeshed/stdlib/lib2to3/fixes/fix_input.pyi +11 -0
  471. mypy/typeshed/stdlib/lib2to3/fixes/fix_intern.pyi +9 -0
  472. mypy/typeshed/stdlib/lib2to3/fixes/fix_isinstance.pyi +8 -0
  473. mypy/typeshed/stdlib/lib2to3/fixes/fix_itertools.pyi +9 -0
  474. mypy/typeshed/stdlib/lib2to3/fixes/fix_itertools_imports.pyi +7 -0
  475. mypy/typeshed/stdlib/lib2to3/fixes/fix_long.pyi +7 -0
  476. mypy/typeshed/stdlib/lib2to3/fixes/fix_map.pyi +9 -0
  477. mypy/typeshed/stdlib/lib2to3/fixes/fix_metaclass.pyi +17 -0
  478. mypy/typeshed/stdlib/lib2to3/fixes/fix_methodattrs.pyi +10 -0
  479. mypy/typeshed/stdlib/lib2to3/fixes/fix_ne.pyi +8 -0
  480. mypy/typeshed/stdlib/lib2to3/fixes/fix_next.pyi +19 -0
  481. mypy/typeshed/stdlib/lib2to3/fixes/fix_nonzero.pyi +8 -0
  482. mypy/typeshed/stdlib/lib2to3/fixes/fix_numliterals.pyi +8 -0
  483. mypy/typeshed/stdlib/lib2to3/fixes/fix_operator.pyi +12 -0
  484. mypy/typeshed/stdlib/lib2to3/fixes/fix_paren.pyi +8 -0
  485. mypy/typeshed/stdlib/lib2to3/fixes/fix_print.pyi +12 -0
  486. mypy/typeshed/stdlib/lib2to3/fixes/fix_raise.pyi +8 -0
  487. mypy/typeshed/stdlib/lib2to3/fixes/fix_raw_input.pyi +8 -0
  488. mypy/typeshed/stdlib/lib2to3/fixes/fix_reduce.pyi +8 -0
  489. mypy/typeshed/stdlib/lib2to3/fixes/fix_reload.pyi +9 -0
  490. mypy/typeshed/stdlib/lib2to3/fixes/fix_renames.pyi +17 -0
  491. mypy/typeshed/stdlib/lib2to3/fixes/fix_repr.pyi +8 -0
  492. mypy/typeshed/stdlib/lib2to3/fixes/fix_set_literal.pyi +7 -0
  493. mypy/typeshed/stdlib/lib2to3/fixes/fix_standarderror.pyi +8 -0
  494. mypy/typeshed/stdlib/lib2to3/fixes/fix_sys_exc.pyi +9 -0
  495. mypy/typeshed/stdlib/lib2to3/fixes/fix_throw.pyi +8 -0
  496. mypy/typeshed/stdlib/lib2to3/fixes/fix_tuple_params.pyi +17 -0
  497. mypy/typeshed/stdlib/lib2to3/fixes/fix_types.pyi +8 -0
  498. mypy/typeshed/stdlib/lib2to3/fixes/fix_unicode.pyi +12 -0
  499. mypy/typeshed/stdlib/lib2to3/fixes/fix_urllib.pyi +15 -0
  500. mypy/typeshed/stdlib/lib2to3/fixes/fix_ws_comma.pyi +12 -0
  501. mypy/typeshed/stdlib/lib2to3/fixes/fix_xrange.pyi +20 -0
  502. mypy/typeshed/stdlib/lib2to3/fixes/fix_xreadlines.pyi +8 -0
  503. mypy/typeshed/stdlib/lib2to3/fixes/fix_zip.pyi +9 -0
  504. mypy/typeshed/stdlib/lib2to3/main.pyi +42 -0
  505. mypy/typeshed/stdlib/lib2to3/pgen2/__init__.pyi +9 -0
  506. mypy/typeshed/stdlib/lib2to3/pgen2/driver.pyi +27 -0
  507. mypy/typeshed/stdlib/lib2to3/pgen2/grammar.pyi +24 -0
  508. mypy/typeshed/stdlib/lib2to3/pgen2/literals.pyi +7 -0
  509. mypy/typeshed/stdlib/lib2to3/pgen2/parse.pyi +30 -0
  510. mypy/typeshed/stdlib/lib2to3/pgen2/pgen.pyi +50 -0
  511. mypy/typeshed/stdlib/lib2to3/pgen2/token.pyi +67 -0
  512. mypy/typeshed/stdlib/lib2to3/pgen2/tokenize.pyi +96 -0
  513. mypy/typeshed/stdlib/lib2to3/pygram.pyi +114 -0
  514. mypy/typeshed/stdlib/lib2to3/pytree.pyi +117 -0
  515. mypy/typeshed/stdlib/lib2to3/refactor.pyi +82 -0
  516. mypy/typeshed/stdlib/linecache.pyi +23 -0
  517. mypy/typeshed/stdlib/locale.pyi +152 -0
  518. mypy/typeshed/stdlib/logging/__init__.pyi +655 -0
  519. mypy/typeshed/stdlib/logging/config.pyi +134 -0
  520. mypy/typeshed/stdlib/logging/handlers.pyi +275 -0
  521. mypy/typeshed/stdlib/lzma.pyi +197 -0
  522. mypy/typeshed/stdlib/mailbox.pyi +256 -0
  523. mypy/typeshed/stdlib/mailcap.pyi +11 -0
  524. mypy/typeshed/stdlib/marshal.pyi +33 -0
  525. mypy/typeshed/stdlib/math.pyi +125 -0
  526. mypy/typeshed/stdlib/mimetypes.pyi +46 -0
  527. mypy/typeshed/stdlib/mmap.pyi +113 -0
  528. mypy/typeshed/stdlib/modulefinder.pyi +66 -0
  529. mypy/typeshed/stdlib/msilib/__init__.pyi +176 -0
  530. mypy/typeshed/stdlib/msilib/schema.pyi +94 -0
  531. mypy/typeshed/stdlib/msilib/sequence.pyi +13 -0
  532. mypy/typeshed/stdlib/msilib/text.pyi +7 -0
  533. mypy/typeshed/stdlib/msvcrt.pyi +32 -0
  534. mypy/typeshed/stdlib/multiprocessing/__init__.pyi +90 -0
  535. mypy/typeshed/stdlib/multiprocessing/connection.pyi +75 -0
  536. mypy/typeshed/stdlib/multiprocessing/context.pyi +187 -0
  537. mypy/typeshed/stdlib/multiprocessing/dummy/__init__.pyi +77 -0
  538. mypy/typeshed/stdlib/multiprocessing/dummy/connection.pyi +39 -0
  539. mypy/typeshed/stdlib/multiprocessing/forkserver.pyi +31 -0
  540. mypy/typeshed/stdlib/multiprocessing/heap.pyi +36 -0
  541. mypy/typeshed/stdlib/multiprocessing/managers.pyi +212 -0
  542. mypy/typeshed/stdlib/multiprocessing/pool.pyi +103 -0
  543. mypy/typeshed/stdlib/multiprocessing/popen_fork.pyi +23 -0
  544. mypy/typeshed/stdlib/multiprocessing/popen_forkserver.pyi +16 -0
  545. mypy/typeshed/stdlib/multiprocessing/popen_spawn_posix.pyi +20 -0
  546. mypy/typeshed/stdlib/multiprocessing/popen_spawn_win32.pyi +30 -0
  547. mypy/typeshed/stdlib/multiprocessing/process.pyi +39 -0
  548. mypy/typeshed/stdlib/multiprocessing/queues.pyi +41 -0
  549. mypy/typeshed/stdlib/multiprocessing/reduction.pyi +89 -0
  550. mypy/typeshed/stdlib/multiprocessing/resource_sharer.pyi +20 -0
  551. mypy/typeshed/stdlib/multiprocessing/resource_tracker.pyi +18 -0
  552. mypy/typeshed/stdlib/multiprocessing/shared_memory.pyi +40 -0
  553. mypy/typeshed/stdlib/multiprocessing/sharedctypes.pyi +101 -0
  554. mypy/typeshed/stdlib/multiprocessing/spawn.pyi +32 -0
  555. mypy/typeshed/stdlib/multiprocessing/synchronize.pyi +54 -0
  556. mypy/typeshed/stdlib/multiprocessing/util.pyi +79 -0
  557. mypy/typeshed/stdlib/netrc.pyi +23 -0
  558. mypy/typeshed/stdlib/nis.pyi +9 -0
  559. mypy/typeshed/stdlib/nntplib.pyi +125 -0
  560. mypy/typeshed/stdlib/nt.pyi +111 -0
  561. mypy/typeshed/stdlib/ntpath.pyi +119 -0
  562. mypy/typeshed/stdlib/nturl2path.pyi +2 -0
  563. mypy/typeshed/stdlib/numbers.pyi +154 -0
  564. mypy/typeshed/stdlib/opcode.pyi +59 -0
  565. mypy/typeshed/stdlib/operator.pyi +110 -0
  566. mypy/typeshed/stdlib/optparse.pyi +252 -0
  567. mypy/typeshed/stdlib/os/__init__.pyi +1139 -0
  568. mypy/typeshed/stdlib/os/path.pyi +8 -0
  569. mypy/typeshed/stdlib/ossaudiodev.pyi +131 -0
  570. mypy/typeshed/stdlib/parser.pyi +24 -0
  571. mypy/typeshed/stdlib/pathlib.pyi +232 -0
  572. mypy/typeshed/stdlib/pdb.pyi +181 -0
  573. mypy/typeshed/stdlib/pickle.pyi +270 -0
  574. mypy/typeshed/stdlib/pickletools.pyi +167 -0
  575. mypy/typeshed/stdlib/pipes.pyi +16 -0
  576. mypy/typeshed/stdlib/pkgutil.pyi +53 -0
  577. mypy/typeshed/stdlib/platform.pyi +42 -0
  578. mypy/typeshed/stdlib/plistlib.pyi +113 -0
  579. mypy/typeshed/stdlib/poplib.pyi +71 -0
  580. mypy/typeshed/stdlib/posix.pyi +359 -0
  581. mypy/typeshed/stdlib/posixpath.pyi +161 -0
  582. mypy/typeshed/stdlib/pprint.pyi +112 -0
  583. mypy/typeshed/stdlib/profile.pyi +31 -0
  584. mypy/typeshed/stdlib/pstats.pyi +78 -0
  585. mypy/typeshed/stdlib/pty.pyi +19 -0
  586. mypy/typeshed/stdlib/pwd.pyi +27 -0
  587. mypy/typeshed/stdlib/py_compile.pyi +34 -0
  588. mypy/typeshed/stdlib/pyclbr.pyi +74 -0
  589. mypy/typeshed/stdlib/pydoc.pyi +261 -0
  590. mypy/typeshed/stdlib/pydoc_data/__init__.pyi +0 -0
  591. mypy/typeshed/stdlib/pydoc_data/topics.pyi +1 -0
  592. mypy/typeshed/stdlib/pyexpat/__init__.pyi +80 -0
  593. mypy/typeshed/stdlib/pyexpat/errors.pyi +49 -0
  594. mypy/typeshed/stdlib/pyexpat/model.pyi +11 -0
  595. mypy/typeshed/stdlib/queue.pyi +58 -0
  596. mypy/typeshed/stdlib/quopri.pyi +11 -0
  597. mypy/typeshed/stdlib/random.pyi +138 -0
  598. mypy/typeshed/stdlib/re.pyi +290 -0
  599. mypy/typeshed/stdlib/readline.pyi +36 -0
  600. mypy/typeshed/stdlib/reprlib.pyi +65 -0
  601. mypy/typeshed/stdlib/resource.pyi +92 -0
  602. mypy/typeshed/stdlib/rlcompleter.pyi +9 -0
  603. mypy/typeshed/stdlib/runpy.pyi +24 -0
  604. mypy/typeshed/stdlib/sched.pyi +42 -0
  605. mypy/typeshed/stdlib/secrets.pyi +15 -0
  606. mypy/typeshed/stdlib/select.pyi +151 -0
  607. mypy/typeshed/stdlib/selectors.pyi +67 -0
  608. mypy/typeshed/stdlib/shelve.pyi +46 -0
  609. mypy/typeshed/stdlib/shlex.pyi +43 -0
  610. mypy/typeshed/stdlib/shutil.pyi +189 -0
  611. mypy/typeshed/stdlib/signal.pyi +189 -0
  612. mypy/typeshed/stdlib/site.pyi +27 -0
  613. mypy/typeshed/stdlib/smtpd.pyi +91 -0
  614. mypy/typeshed/stdlib/smtplib.pyi +204 -0
  615. mypy/typeshed/stdlib/sndhdr.pyi +14 -0
  616. mypy/typeshed/stdlib/socket.pyi +825 -0
  617. mypy/typeshed/stdlib/socketserver.pyi +168 -0
  618. mypy/typeshed/stdlib/spwd.pyi +40 -0
  619. mypy/typeshed/stdlib/sqlite3/__init__.pyi +1 -0
  620. mypy/typeshed/stdlib/sqlite3/dbapi2.pyi +469 -0
  621. mypy/typeshed/stdlib/sre_compile.pyi +11 -0
  622. mypy/typeshed/stdlib/sre_constants.pyi +130 -0
  623. mypy/typeshed/stdlib/sre_parse.pyi +104 -0
  624. mypy/typeshed/stdlib/ssl.pyi +529 -0
  625. mypy/typeshed/stdlib/stat.pyi +1 -0
  626. mypy/typeshed/stdlib/statistics.pyi +132 -0
  627. mypy/typeshed/stdlib/string.pyi +83 -0
  628. mypy/typeshed/stdlib/stringprep.pyi +27 -0
  629. mypy/typeshed/stdlib/struct.pyi +26 -0
  630. mypy/typeshed/stdlib/subprocess.pyi +2614 -0
  631. mypy/typeshed/stdlib/sunau.pyi +86 -0
  632. mypy/typeshed/stdlib/symbol.pyi +93 -0
  633. mypy/typeshed/stdlib/symtable.pyi +58 -0
  634. mypy/typeshed/stdlib/sys/__init__.pyi +373 -0
  635. mypy/typeshed/stdlib/sys/_monitoring.pyi +52 -0
  636. mypy/typeshed/stdlib/sysconfig.pyi +43 -0
  637. mypy/typeshed/stdlib/syslog.pyi +46 -0
  638. mypy/typeshed/stdlib/tabnanny.pyi +16 -0
  639. mypy/typeshed/stdlib/tarfile.pyi +438 -0
  640. mypy/typeshed/stdlib/telnetlib.pyi +122 -0
  641. mypy/typeshed/stdlib/tempfile.pyi +477 -0
  642. mypy/typeshed/stdlib/termios.pyi +267 -0
  643. mypy/typeshed/stdlib/textwrap.pyi +103 -0
  644. mypy/typeshed/stdlib/this.pyi +2 -0
  645. mypy/typeshed/stdlib/threading.pyi +187 -0
  646. mypy/typeshed/stdlib/time.pyi +107 -0
  647. mypy/typeshed/stdlib/timeit.pyi +32 -0
  648. mypy/typeshed/stdlib/tkinter/__init__.pyi +3641 -0
  649. mypy/typeshed/stdlib/tkinter/colorchooser.pyi +20 -0
  650. mypy/typeshed/stdlib/tkinter/commondialog.pyi +14 -0
  651. mypy/typeshed/stdlib/tkinter/constants.pyi +80 -0
  652. mypy/typeshed/stdlib/tkinter/dialog.pyi +16 -0
  653. mypy/typeshed/stdlib/tkinter/dnd.pyi +20 -0
  654. mypy/typeshed/stdlib/tkinter/filedialog.pyi +151 -0
  655. mypy/typeshed/stdlib/tkinter/font.pyi +116 -0
  656. mypy/typeshed/stdlib/tkinter/messagebox.pyi +44 -0
  657. mypy/typeshed/stdlib/tkinter/scrolledtext.pyi +10 -0
  658. mypy/typeshed/stdlib/tkinter/simpledialog.pyi +54 -0
  659. mypy/typeshed/stdlib/tkinter/tix.pyi +299 -0
  660. mypy/typeshed/stdlib/tkinter/ttk.pyi +1191 -0
  661. mypy/typeshed/stdlib/token.pyi +159 -0
  662. mypy/typeshed/stdlib/tokenize.pyi +177 -0
  663. mypy/typeshed/stdlib/tomllib.pyi +10 -0
  664. mypy/typeshed/stdlib/trace.pyi +79 -0
  665. mypy/typeshed/stdlib/traceback.pyi +260 -0
  666. mypy/typeshed/stdlib/tracemalloc.pyi +124 -0
  667. mypy/typeshed/stdlib/tty.pyi +30 -0
  668. mypy/typeshed/stdlib/turtle.pyi +713 -0
  669. mypy/typeshed/stdlib/types.pyi +605 -0
  670. mypy/typeshed/stdlib/typing.pyi +968 -0
  671. mypy/typeshed/stdlib/typing_extensions.pyi +503 -0
  672. mypy/typeshed/stdlib/unicodedata.pyi +71 -0
  673. mypy/typeshed/stdlib/unittest/__init__.pyi +67 -0
  674. mypy/typeshed/stdlib/unittest/_log.pyi +27 -0
  675. mypy/typeshed/stdlib/unittest/async_case.pyi +21 -0
  676. mypy/typeshed/stdlib/unittest/case.pyi +342 -0
  677. mypy/typeshed/stdlib/unittest/loader.pyi +51 -0
  678. mypy/typeshed/stdlib/unittest/main.pyi +69 -0
  679. mypy/typeshed/stdlib/unittest/mock.pyi +430 -0
  680. mypy/typeshed/stdlib/unittest/result.pyi +46 -0
  681. mypy/typeshed/stdlib/unittest/runner.pyi +72 -0
  682. mypy/typeshed/stdlib/unittest/signals.pyi +15 -0
  683. mypy/typeshed/stdlib/unittest/suite.pyi +22 -0
  684. mypy/typeshed/stdlib/unittest/util.pyi +23 -0
  685. mypy/typeshed/stdlib/urllib/__init__.pyi +0 -0
  686. mypy/typeshed/stdlib/urllib/error.pyi +23 -0
  687. mypy/typeshed/stdlib/urllib/parse.pyi +210 -0
  688. mypy/typeshed/stdlib/urllib/request.pyi +399 -0
  689. mypy/typeshed/stdlib/urllib/response.pyi +43 -0
  690. mypy/typeshed/stdlib/urllib/robotparser.pyi +20 -0
  691. mypy/typeshed/stdlib/uu.pyi +13 -0
  692. mypy/typeshed/stdlib/uuid.pyi +100 -0
  693. mypy/typeshed/stdlib/venv/__init__.pyi +79 -0
  694. mypy/typeshed/stdlib/warnings.pyi +112 -0
  695. mypy/typeshed/stdlib/wave.pyi +85 -0
  696. mypy/typeshed/stdlib/weakref.pyi +150 -0
  697. mypy/typeshed/stdlib/webbrowser.pyi +74 -0
  698. mypy/typeshed/stdlib/winreg.pyi +103 -0
  699. mypy/typeshed/stdlib/winsound.pyi +28 -0
  700. mypy/typeshed/stdlib/wsgiref/__init__.pyi +0 -0
  701. mypy/typeshed/stdlib/wsgiref/handlers.pyi +91 -0
  702. mypy/typeshed/stdlib/wsgiref/headers.pyi +26 -0
  703. mypy/typeshed/stdlib/wsgiref/simple_server.pyi +37 -0
  704. mypy/typeshed/stdlib/wsgiref/types.pyi +32 -0
  705. mypy/typeshed/stdlib/wsgiref/util.pyi +24 -0
  706. mypy/typeshed/stdlib/wsgiref/validate.pyi +50 -0
  707. mypy/typeshed/stdlib/xdrlib.pyi +57 -0
  708. mypy/typeshed/stdlib/xml/__init__.pyi +1 -0
  709. mypy/typeshed/stdlib/xml/dom/NodeFilter.pyi +19 -0
  710. mypy/typeshed/stdlib/xml/dom/__init__.pyi +69 -0
  711. mypy/typeshed/stdlib/xml/dom/domreg.pyi +8 -0
  712. mypy/typeshed/stdlib/xml/dom/expatbuilder.pyi +100 -0
  713. mypy/typeshed/stdlib/xml/dom/minicompat.pyi +22 -0
  714. mypy/typeshed/stdlib/xml/dom/minidom.pyi +404 -0
  715. mypy/typeshed/stdlib/xml/dom/pulldom.pyi +94 -0
  716. mypy/typeshed/stdlib/xml/dom/xmlbuilder.pyi +108 -0
  717. mypy/typeshed/stdlib/xml/etree/ElementInclude.pyi +28 -0
  718. mypy/typeshed/stdlib/xml/etree/ElementPath.pyi +34 -0
  719. mypy/typeshed/stdlib/xml/etree/ElementTree.pyi +321 -0
  720. mypy/typeshed/stdlib/xml/etree/__init__.pyi +0 -0
  721. mypy/typeshed/stdlib/xml/etree/cElementTree.pyi +1 -0
  722. mypy/typeshed/stdlib/xml/parsers/__init__.pyi +1 -0
  723. mypy/typeshed/stdlib/xml/parsers/expat/__init__.pyi +1 -0
  724. mypy/typeshed/stdlib/xml/parsers/expat/errors.pyi +1 -0
  725. mypy/typeshed/stdlib/xml/parsers/expat/model.pyi +1 -0
  726. mypy/typeshed/stdlib/xml/sax/__init__.pyi +25 -0
  727. mypy/typeshed/stdlib/xml/sax/_exceptions.pyi +19 -0
  728. mypy/typeshed/stdlib/xml/sax/handler.pyi +55 -0
  729. mypy/typeshed/stdlib/xml/sax/saxutils.pyi +60 -0
  730. mypy/typeshed/stdlib/xml/sax/xmlreader.pyi +87 -0
  731. mypy/typeshed/stdlib/xmlrpc/__init__.pyi +0 -0
  732. mypy/typeshed/stdlib/xmlrpc/client.pyi +296 -0
  733. mypy/typeshed/stdlib/xmlrpc/server.pyi +143 -0
  734. mypy/typeshed/stdlib/xxlimited.pyi +22 -0
  735. mypy/typeshed/stdlib/zipapp.pyi +20 -0
  736. mypy/typeshed/stdlib/zipfile/__init__.pyi +306 -0
  737. mypy/typeshed/stdlib/zipfile/_path.pyi +95 -0
  738. mypy/typeshed/stdlib/zipimport.pyi +32 -0
  739. mypy/typeshed/stdlib/zlib.pyi +56 -0
  740. mypy/typeshed/stdlib/zoneinfo/__init__.pyi +38 -0
  741. mypy/typeshed/stubs/mypy-extensions/mypy_extensions.pyi +218 -0
  742. mypy/typestate.py +323 -0
  743. mypy/typetraverser.py +142 -0
  744. mypy/typevars.py +93 -0
  745. mypy/typevartuples.py +32 -0
  746. mypy/util.py +869 -0
  747. mypy/version.py +1 -0
  748. mypy/visitor.py +621 -0
  749. mypy/xml/mypy-html.css +104 -0
  750. mypy/xml/mypy-html.xslt +81 -0
  751. mypy/xml/mypy-txt.xslt +100 -0
  752. mypy/xml/mypy.xsd +50 -0
  753. mypy_dev-1.9.0.dev0.dist-info/LICENSE +229 -0
  754. mypy_dev-1.9.0.dev0.dist-info/METADATA +39 -0
  755. mypy_dev-1.9.0.dev0.dist-info/RECORD +1018 -0
  756. mypy_dev-1.9.0.dev0.dist-info/WHEEL +5 -0
  757. mypy_dev-1.9.0.dev0.dist-info/entry_points.txt +6 -0
  758. mypy_dev-1.9.0.dev0.dist-info/top_level.txt +2 -0
  759. mypyc/README.md +133 -0
  760. mypyc/__init__.py +0 -0
  761. mypyc/__main__.py +57 -0
  762. mypyc/analysis/__init__.py +0 -0
  763. mypyc/analysis/attrdefined.py +436 -0
  764. mypyc/analysis/blockfreq.py +32 -0
  765. mypyc/analysis/dataflow.py +624 -0
  766. mypyc/analysis/ircheck.py +429 -0
  767. mypyc/analysis/selfleaks.py +207 -0
  768. mypyc/build.py +616 -0
  769. mypyc/codegen/__init__.py +0 -0
  770. mypyc/codegen/cstring.py +54 -0
  771. mypyc/codegen/emit.py +1193 -0
  772. mypyc/codegen/emitclass.py +1060 -0
  773. mypyc/codegen/emitfunc.py +846 -0
  774. mypyc/codegen/emitmodule.py +1131 -0
  775. mypyc/codegen/emitwrapper.py +979 -0
  776. mypyc/codegen/literals.py +302 -0
  777. mypyc/common.py +136 -0
  778. mypyc/crash.py +31 -0
  779. mypyc/doc/Makefile +20 -0
  780. mypyc/doc/bool_operations.rst +27 -0
  781. mypyc/doc/compilation_units.rst +20 -0
  782. mypyc/doc/conf.py +59 -0
  783. mypyc/doc/cpython-timings.md +25 -0
  784. mypyc/doc/dev-intro.md +548 -0
  785. mypyc/doc/dict_operations.rst +59 -0
  786. mypyc/doc/differences_from_python.rst +332 -0
  787. mypyc/doc/float_operations.rst +50 -0
  788. mypyc/doc/future.md +42 -0
  789. mypyc/doc/getting_started.rst +240 -0
  790. mypyc/doc/index.rst +61 -0
  791. mypyc/doc/int_operations.rst +162 -0
  792. mypyc/doc/introduction.rst +150 -0
  793. mypyc/doc/list_operations.rst +65 -0
  794. mypyc/doc/make.bat +35 -0
  795. mypyc/doc/native_classes.rst +206 -0
  796. mypyc/doc/native_operations.rst +55 -0
  797. mypyc/doc/performance_tips_and_tricks.rst +244 -0
  798. mypyc/doc/set_operations.rst +47 -0
  799. mypyc/doc/str_operations.rst +35 -0
  800. mypyc/doc/tuple_operations.rst +33 -0
  801. mypyc/doc/using_type_annotations.rst +398 -0
  802. mypyc/errors.py +29 -0
  803. mypyc/external/googletest/LICENSE +28 -0
  804. mypyc/external/googletest/README.md +280 -0
  805. mypyc/external/googletest/include/gtest/gtest-death-test.h +294 -0
  806. mypyc/external/googletest/include/gtest/gtest-message.h +250 -0
  807. mypyc/external/googletest/include/gtest/gtest-param-test.h +1444 -0
  808. mypyc/external/googletest/include/gtest/gtest-param-test.h.pump +510 -0
  809. mypyc/external/googletest/include/gtest/gtest-printers.h +993 -0
  810. mypyc/external/googletest/include/gtest/gtest-spi.h +232 -0
  811. mypyc/external/googletest/include/gtest/gtest-test-part.h +179 -0
  812. mypyc/external/googletest/include/gtest/gtest-typed-test.h +263 -0
  813. mypyc/external/googletest/include/gtest/gtest.h +2236 -0
  814. mypyc/external/googletest/include/gtest/gtest_pred_impl.h +358 -0
  815. mypyc/external/googletest/include/gtest/gtest_prod.h +58 -0
  816. mypyc/external/googletest/include/gtest/internal/custom/gtest-port.h +69 -0
  817. mypyc/external/googletest/include/gtest/internal/custom/gtest-printers.h +42 -0
  818. mypyc/external/googletest/include/gtest/internal/custom/gtest.h +41 -0
  819. mypyc/external/googletest/include/gtest/internal/gtest-death-test-internal.h +319 -0
  820. mypyc/external/googletest/include/gtest/internal/gtest-filepath.h +206 -0
  821. mypyc/external/googletest/include/gtest/internal/gtest-internal.h +1238 -0
  822. mypyc/external/googletest/include/gtest/internal/gtest-linked_ptr.h +243 -0
  823. mypyc/external/googletest/include/gtest/internal/gtest-param-util-generated.h +5146 -0
  824. mypyc/external/googletest/include/gtest/internal/gtest-param-util-generated.h.pump +286 -0
  825. mypyc/external/googletest/include/gtest/internal/gtest-param-util.h +731 -0
  826. mypyc/external/googletest/include/gtest/internal/gtest-port-arch.h +93 -0
  827. mypyc/external/googletest/include/gtest/internal/gtest-port.h +2560 -0
  828. mypyc/external/googletest/include/gtest/internal/gtest-string.h +167 -0
  829. mypyc/external/googletest/include/gtest/internal/gtest-tuple.h +1020 -0
  830. mypyc/external/googletest/include/gtest/internal/gtest-tuple.h.pump +347 -0
  831. mypyc/external/googletest/include/gtest/internal/gtest-type-util.h +3331 -0
  832. mypyc/external/googletest/include/gtest/internal/gtest-type-util.h.pump +297 -0
  833. mypyc/external/googletest/make/Makefile +61 -0
  834. mypyc/external/googletest/src/gtest-all.cc +48 -0
  835. mypyc/external/googletest/src/gtest-death-test.cc +1342 -0
  836. mypyc/external/googletest/src/gtest-filepath.cc +387 -0
  837. mypyc/external/googletest/src/gtest-internal-inl.h +1183 -0
  838. mypyc/external/googletest/src/gtest-port.cc +1259 -0
  839. mypyc/external/googletest/src/gtest-printers.cc +373 -0
  840. mypyc/external/googletest/src/gtest-test-part.cc +110 -0
  841. mypyc/external/googletest/src/gtest-typed-test.cc +118 -0
  842. mypyc/external/googletest/src/gtest.cc +5388 -0
  843. mypyc/external/googletest/src/gtest_main.cc +38 -0
  844. mypyc/ir/__init__.py +0 -0
  845. mypyc/ir/class_ir.py +499 -0
  846. mypyc/ir/func_ir.py +370 -0
  847. mypyc/ir/module_ir.py +88 -0
  848. mypyc/ir/ops.py +1642 -0
  849. mypyc/ir/pprint.py +496 -0
  850. mypyc/ir/rtypes.py +1038 -0
  851. mypyc/irbuild/__init__.py +0 -0
  852. mypyc/irbuild/ast_helpers.py +120 -0
  853. mypyc/irbuild/builder.py +1398 -0
  854. mypyc/irbuild/callable_class.py +173 -0
  855. mypyc/irbuild/classdef.py +850 -0
  856. mypyc/irbuild/constant_fold.py +95 -0
  857. mypyc/irbuild/context.py +186 -0
  858. mypyc/irbuild/env_class.py +223 -0
  859. mypyc/irbuild/expression.py +1067 -0
  860. mypyc/irbuild/for_helpers.py +1046 -0
  861. mypyc/irbuild/format_str_tokenizer.py +250 -0
  862. mypyc/irbuild/function.py +1089 -0
  863. mypyc/irbuild/generator.py +346 -0
  864. mypyc/irbuild/ll_builder.py +2407 -0
  865. mypyc/irbuild/main.py +153 -0
  866. mypyc/irbuild/mapper.py +221 -0
  867. mypyc/irbuild/match.py +355 -0
  868. mypyc/irbuild/nonlocalcontrol.py +197 -0
  869. mypyc/irbuild/prebuildvisitor.py +203 -0
  870. mypyc/irbuild/prepare.py +609 -0
  871. mypyc/irbuild/specialize.py +822 -0
  872. mypyc/irbuild/statement.py +1017 -0
  873. mypyc/irbuild/targets.py +57 -0
  874. mypyc/irbuild/util.py +189 -0
  875. mypyc/irbuild/visitor.py +401 -0
  876. mypyc/irbuild/vtable.py +82 -0
  877. mypyc/lib-rt/CPy.h +643 -0
  878. mypyc/lib-rt/bytes_ops.c +143 -0
  879. mypyc/lib-rt/dict_ops.c +446 -0
  880. mypyc/lib-rt/exc_ops.c +259 -0
  881. mypyc/lib-rt/float_ops.c +192 -0
  882. mypyc/lib-rt/generic_ops.c +64 -0
  883. mypyc/lib-rt/getargs.c +450 -0
  884. mypyc/lib-rt/getargsfast.c +569 -0
  885. mypyc/lib-rt/init.c +13 -0
  886. mypyc/lib-rt/int_ops.c +803 -0
  887. mypyc/lib-rt/list_ops.c +335 -0
  888. mypyc/lib-rt/misc_ops.c +942 -0
  889. mypyc/lib-rt/module_shim.tmpl +18 -0
  890. mypyc/lib-rt/mypyc_util.h +118 -0
  891. mypyc/lib-rt/pythoncapi_compat.h +497 -0
  892. mypyc/lib-rt/pythonsupport.h +548 -0
  893. mypyc/lib-rt/set_ops.c +17 -0
  894. mypyc/lib-rt/setup.py +70 -0
  895. mypyc/lib-rt/str_ops.c +241 -0
  896. mypyc/lib-rt/test_capi.cc +585 -0
  897. mypyc/lib-rt/tuple_ops.c +61 -0
  898. mypyc/namegen.py +115 -0
  899. mypyc/options.py +32 -0
  900. mypyc/primitives/__init__.py +0 -0
  901. mypyc/primitives/bytes_ops.py +101 -0
  902. mypyc/primitives/dict_ops.py +325 -0
  903. mypyc/primitives/exc_ops.py +101 -0
  904. mypyc/primitives/float_ops.py +168 -0
  905. mypyc/primitives/generic_ops.py +384 -0
  906. mypyc/primitives/int_ops.py +304 -0
  907. mypyc/primitives/list_ops.py +300 -0
  908. mypyc/primitives/misc_ops.py +241 -0
  909. mypyc/primitives/registry.py +323 -0
  910. mypyc/primitives/set_ops.py +121 -0
  911. mypyc/primitives/str_ops.py +229 -0
  912. mypyc/primitives/tuple_ops.py +83 -0
  913. mypyc/rt_subtype.py +77 -0
  914. mypyc/sametype.py +83 -0
  915. mypyc/subtype.py +88 -0
  916. mypyc/test/__init__.py +0 -0
  917. mypyc/test/config.py +13 -0
  918. mypyc/test/test_alwaysdefined.py +46 -0
  919. mypyc/test/test_analysis.py +77 -0
  920. mypyc/test/test_cheader.py +45 -0
  921. mypyc/test/test_commandline.py +82 -0
  922. mypyc/test/test_emit.py +69 -0
  923. mypyc/test/test_emitclass.py +35 -0
  924. mypyc/test/test_emitfunc.py +926 -0
  925. mypyc/test/test_emitwrapper.py +60 -0
  926. mypyc/test/test_exceptions.py +56 -0
  927. mypyc/test/test_external.py +49 -0
  928. mypyc/test/test_irbuild.py +87 -0
  929. mypyc/test/test_ircheck.py +199 -0
  930. mypyc/test/test_literals.py +90 -0
  931. mypyc/test/test_namegen.py +48 -0
  932. mypyc/test/test_pprint.py +42 -0
  933. mypyc/test/test_rarray.py +48 -0
  934. mypyc/test/test_refcount.py +59 -0
  935. mypyc/test/test_run.py +426 -0
  936. mypyc/test/test_serialization.py +108 -0
  937. mypyc/test/test_struct.py +112 -0
  938. mypyc/test/test_tuplename.py +33 -0
  939. mypyc/test/test_typeops.py +97 -0
  940. mypyc/test/testutil.py +283 -0
  941. mypyc/test-data/alwaysdefined.test +732 -0
  942. mypyc/test-data/analysis.test +603 -0
  943. mypyc/test-data/commandline.test +245 -0
  944. mypyc/test-data/driver/driver.py +48 -0
  945. mypyc/test-data/exceptions-freq.test +125 -0
  946. mypyc/test-data/exceptions.test +713 -0
  947. mypyc/test-data/fixtures/ir.py +373 -0
  948. mypyc/test-data/fixtures/testutil.py +103 -0
  949. mypyc/test-data/fixtures/typing-full.pyi +169 -0
  950. mypyc/test-data/irbuild-any.test +238 -0
  951. mypyc/test-data/irbuild-basic.test +3690 -0
  952. mypyc/test-data/irbuild-bool.test +463 -0
  953. mypyc/test-data/irbuild-bytes.test +184 -0
  954. mypyc/test-data/irbuild-classes.test +1303 -0
  955. mypyc/test-data/irbuild-constant-fold.test +480 -0
  956. mypyc/test-data/irbuild-dict.test +585 -0
  957. mypyc/test-data/irbuild-dunders.test +223 -0
  958. mypyc/test-data/irbuild-float.test +497 -0
  959. mypyc/test-data/irbuild-generics.test +151 -0
  960. mypyc/test-data/irbuild-glue-methods.test +437 -0
  961. mypyc/test-data/irbuild-i16.test +526 -0
  962. mypyc/test-data/irbuild-i32.test +598 -0
  963. mypyc/test-data/irbuild-i64.test +2164 -0
  964. mypyc/test-data/irbuild-int.test +235 -0
  965. mypyc/test-data/irbuild-isinstance.test +109 -0
  966. mypyc/test-data/irbuild-lists.test +547 -0
  967. mypyc/test-data/irbuild-match.test +1708 -0
  968. mypyc/test-data/irbuild-math.test +64 -0
  969. mypyc/test-data/irbuild-nested.test +807 -0
  970. mypyc/test-data/irbuild-optional.test +536 -0
  971. mypyc/test-data/irbuild-set.test +838 -0
  972. mypyc/test-data/irbuild-singledispatch.test +257 -0
  973. mypyc/test-data/irbuild-statements.test +1149 -0
  974. mypyc/test-data/irbuild-str.test +314 -0
  975. mypyc/test-data/irbuild-strip-asserts.test +12 -0
  976. mypyc/test-data/irbuild-try.test +523 -0
  977. mypyc/test-data/irbuild-tuple.test +462 -0
  978. mypyc/test-data/irbuild-u8.test +543 -0
  979. mypyc/test-data/irbuild-unreachable.test +241 -0
  980. mypyc/test-data/irbuild-vectorcall.test +153 -0
  981. mypyc/test-data/refcount.test +1588 -0
  982. mypyc/test-data/run-async.test +145 -0
  983. mypyc/test-data/run-attrs.test +318 -0
  984. mypyc/test-data/run-bench.test +196 -0
  985. mypyc/test-data/run-bools.test +229 -0
  986. mypyc/test-data/run-bytes.test +302 -0
  987. mypyc/test-data/run-classes.test +2505 -0
  988. mypyc/test-data/run-dicts.test +334 -0
  989. mypyc/test-data/run-dunders.test +945 -0
  990. mypyc/test-data/run-exceptions.test +448 -0
  991. mypyc/test-data/run-floats.test +516 -0
  992. mypyc/test-data/run-functions.test +1310 -0
  993. mypyc/test-data/run-generators.test +681 -0
  994. mypyc/test-data/run-i16.test +338 -0
  995. mypyc/test-data/run-i32.test +336 -0
  996. mypyc/test-data/run-i64.test +1519 -0
  997. mypyc/test-data/run-imports.test +265 -0
  998. mypyc/test-data/run-integers.test +540 -0
  999. mypyc/test-data/run-lists.test +411 -0
  1000. mypyc/test-data/run-loops.test +485 -0
  1001. mypyc/test-data/run-match.test +283 -0
  1002. mypyc/test-data/run-math.test +106 -0
  1003. mypyc/test-data/run-misc.test +1168 -0
  1004. mypyc/test-data/run-multimodule.test +887 -0
  1005. mypyc/test-data/run-mypy-sim.test +138 -0
  1006. mypyc/test-data/run-primitives.test +375 -0
  1007. mypyc/test-data/run-python37.test +159 -0
  1008. mypyc/test-data/run-python38.test +88 -0
  1009. mypyc/test-data/run-sets.test +150 -0
  1010. mypyc/test-data/run-singledispatch.test +698 -0
  1011. mypyc/test-data/run-strings.test +641 -0
  1012. mypyc/test-data/run-traits.test +411 -0
  1013. mypyc/test-data/run-tuples.test +258 -0
  1014. mypyc/test-data/run-u8.test +303 -0
  1015. mypyc/transform/__init__.py +0 -0
  1016. mypyc/transform/exceptions.py +182 -0
  1017. mypyc/transform/refcount.py +294 -0
  1018. mypyc/transform/uninit.py +190 -0
mypyc/codegen/emit.py ADDED
@@ -0,0 +1,1193 @@
1
+ """Utilities for emitting C code."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import pprint
6
+ import sys
7
+ import textwrap
8
+ from typing import Callable, Final
9
+
10
+ from mypyc.codegen.literals import Literals
11
+ from mypyc.common import (
12
+ ATTR_PREFIX,
13
+ BITMAP_BITS,
14
+ FAST_ISINSTANCE_MAX_SUBCLASSES,
15
+ NATIVE_PREFIX,
16
+ REG_PREFIX,
17
+ STATIC_PREFIX,
18
+ TYPE_PREFIX,
19
+ use_vectorcall,
20
+ )
21
+ from mypyc.ir.class_ir import ClassIR, all_concrete_classes
22
+ from mypyc.ir.func_ir import FuncDecl
23
+ from mypyc.ir.ops import BasicBlock, Value
24
+ from mypyc.ir.rtypes import (
25
+ RInstance,
26
+ RPrimitive,
27
+ RTuple,
28
+ RType,
29
+ RUnion,
30
+ int_rprimitive,
31
+ is_bit_rprimitive,
32
+ is_bool_rprimitive,
33
+ is_bytes_rprimitive,
34
+ is_dict_rprimitive,
35
+ is_fixed_width_rtype,
36
+ is_float_rprimitive,
37
+ is_int16_rprimitive,
38
+ is_int32_rprimitive,
39
+ is_int64_rprimitive,
40
+ is_int_rprimitive,
41
+ is_list_rprimitive,
42
+ is_none_rprimitive,
43
+ is_object_rprimitive,
44
+ is_optional_type,
45
+ is_range_rprimitive,
46
+ is_set_rprimitive,
47
+ is_short_int_rprimitive,
48
+ is_str_rprimitive,
49
+ is_tuple_rprimitive,
50
+ is_uint8_rprimitive,
51
+ object_rprimitive,
52
+ optional_value_type,
53
+ )
54
+ from mypyc.namegen import NameGenerator, exported_name
55
+ from mypyc.sametype import is_same_type
56
+
57
+ # Whether to insert debug asserts for all error handling, to quickly
58
+ # catch errors propagating without exceptions set.
59
+ DEBUG_ERRORS: Final = False
60
+
61
+
62
+ class HeaderDeclaration:
63
+ """A representation of a declaration in C.
64
+
65
+ This is used to generate declarations in header files and
66
+ (optionally) definitions in source files.
67
+
68
+ Attributes:
69
+ decl: C source code for the declaration.
70
+ defn: Optionally, C source code for a definition.
71
+ dependencies: The names of any objects that must be declared prior.
72
+ is_type: Whether the declaration is of a C type. (C types will be declared in
73
+ external header files and not marked 'extern'.)
74
+ needs_export: Whether the declared object needs to be exported to
75
+ other modules in the linking table.
76
+ """
77
+
78
+ def __init__(
79
+ self,
80
+ decl: str | list[str],
81
+ defn: list[str] | None = None,
82
+ *,
83
+ dependencies: set[str] | None = None,
84
+ is_type: bool = False,
85
+ needs_export: bool = False,
86
+ ) -> None:
87
+ self.decl = [decl] if isinstance(decl, str) else decl
88
+ self.defn = defn
89
+ self.dependencies = dependencies or set()
90
+ self.is_type = is_type
91
+ self.needs_export = needs_export
92
+
93
+
94
+ class EmitterContext:
95
+ """Shared emitter state for a compilation group."""
96
+
97
+ def __init__(
98
+ self,
99
+ names: NameGenerator,
100
+ group_name: str | None = None,
101
+ group_map: dict[str, str | None] | None = None,
102
+ ) -> None:
103
+ """Setup shared emitter state.
104
+
105
+ Args:
106
+ names: The name generator to use
107
+ group_map: Map from module names to group name
108
+ group_name: Current group name
109
+ """
110
+ self.temp_counter = 0
111
+ self.names = names
112
+ self.group_name = group_name
113
+ self.group_map = group_map or {}
114
+ # Groups that this group depends on
115
+ self.group_deps: set[str] = set()
116
+
117
+ # The map below is used for generating declarations and
118
+ # definitions at the top of the C file. The main idea is that they can
119
+ # be generated at any time during the emit phase.
120
+
121
+ # A map of a C identifier to whatever the C identifier declares. Currently this is
122
+ # used for declaring structs and the key corresponds to the name of the struct.
123
+ # The declaration contains the body of the struct.
124
+ self.declarations: dict[str, HeaderDeclaration] = {}
125
+
126
+ self.literals = Literals()
127
+
128
+
129
+ class ErrorHandler:
130
+ """Describes handling errors in unbox/cast operations."""
131
+
132
+
133
+ class AssignHandler(ErrorHandler):
134
+ """Assign an error value on error."""
135
+
136
+
137
+ class GotoHandler(ErrorHandler):
138
+ """Goto label on error."""
139
+
140
+ def __init__(self, label: str) -> None:
141
+ self.label = label
142
+
143
+
144
+ class TracebackAndGotoHandler(ErrorHandler):
145
+ """Add traceback item and goto label on error."""
146
+
147
+ def __init__(
148
+ self, label: str, source_path: str, module_name: str, traceback_entry: tuple[str, int]
149
+ ) -> None:
150
+ self.label = label
151
+ self.source_path = source_path
152
+ self.module_name = module_name
153
+ self.traceback_entry = traceback_entry
154
+
155
+
156
+ class ReturnHandler(ErrorHandler):
157
+ """Return a constant value on error."""
158
+
159
+ def __init__(self, value: str) -> None:
160
+ self.value = value
161
+
162
+
163
+ class Emitter:
164
+ """Helper for C code generation."""
165
+
166
+ def __init__(
167
+ self,
168
+ context: EmitterContext,
169
+ value_names: dict[Value, str] | None = None,
170
+ capi_version: tuple[int, int] | None = None,
171
+ ) -> None:
172
+ self.context = context
173
+ self.capi_version = capi_version or sys.version_info[:2]
174
+ self.names = context.names
175
+ self.value_names = value_names or {}
176
+ self.fragments: list[str] = []
177
+ self._indent = 0
178
+
179
+ # Low-level operations
180
+
181
+ def indent(self) -> None:
182
+ self._indent += 4
183
+
184
+ def dedent(self) -> None:
185
+ self._indent -= 4
186
+ assert self._indent >= 0
187
+
188
+ def label(self, label: BasicBlock) -> str:
189
+ return "CPyL%s" % label.label
190
+
191
+ def reg(self, reg: Value) -> str:
192
+ return REG_PREFIX + self.value_names[reg]
193
+
194
+ def attr(self, name: str) -> str:
195
+ return ATTR_PREFIX + name
196
+
197
+ def object_annotation(self, obj: object, line: str) -> str:
198
+ """Build a C comment with an object's string represention.
199
+
200
+ If the comment exceeds the line length limit, it's wrapped into a
201
+ multiline string (with the extra lines indented to be aligned with
202
+ the first line's comment).
203
+
204
+ If it contains illegal characters, an empty string is returned."""
205
+ line_width = self._indent + len(line)
206
+ formatted = pprint.pformat(obj, compact=True, width=max(90 - line_width, 20))
207
+ if any(x in formatted for x in ("/*", "*/", "\0")):
208
+ return ""
209
+
210
+ if "\n" in formatted:
211
+ first_line, rest = formatted.split("\n", maxsplit=1)
212
+ comment_continued = textwrap.indent(rest, (line_width + 3) * " ")
213
+ return f" /* {first_line}\n{comment_continued} */"
214
+ else:
215
+ return f" /* {formatted} */"
216
+
217
+ def emit_line(self, line: str = "", *, ann: object = None) -> None:
218
+ if line.startswith("}"):
219
+ self.dedent()
220
+ comment = self.object_annotation(ann, line) if ann is not None else ""
221
+ self.fragments.append(self._indent * " " + line + comment + "\n")
222
+ if line.endswith("{"):
223
+ self.indent()
224
+
225
+ def emit_lines(self, *lines: str) -> None:
226
+ for line in lines:
227
+ self.emit_line(line)
228
+
229
+ def emit_label(self, label: BasicBlock | str) -> None:
230
+ if isinstance(label, str):
231
+ text = label
232
+ else:
233
+ if label.label == 0 or not label.referenced:
234
+ return
235
+
236
+ text = self.label(label)
237
+ # Extra semicolon prevents an error when the next line declares a tempvar
238
+ self.fragments.append(f"{text}: ;\n")
239
+
240
+ def emit_from_emitter(self, emitter: Emitter) -> None:
241
+ self.fragments.extend(emitter.fragments)
242
+
243
+ def emit_printf(self, fmt: str, *args: str) -> None:
244
+ fmt = fmt.replace("\n", "\\n")
245
+ self.emit_line("printf(%s);" % ", ".join(['"%s"' % fmt] + list(args)))
246
+ self.emit_line("fflush(stdout);")
247
+
248
+ def temp_name(self) -> str:
249
+ self.context.temp_counter += 1
250
+ return "__tmp%d" % self.context.temp_counter
251
+
252
+ def new_label(self) -> str:
253
+ self.context.temp_counter += 1
254
+ return "__LL%d" % self.context.temp_counter
255
+
256
+ def get_module_group_prefix(self, module_name: str) -> str:
257
+ """Get the group prefix for a module (relative to the current group).
258
+
259
+ The prefix should be prepended to the object name whenever
260
+ accessing an object from this module.
261
+
262
+ If the module lives is in the current compilation group, there is
263
+ no prefix. But if it lives in a different group (and hence a separate
264
+ extension module), we need to access objects from it indirectly via an
265
+ export table.
266
+
267
+ For example, for code in group `a` to call a function `bar` in group `b`,
268
+ it would need to do `exports_b.CPyDef_bar(...)`, while code that is
269
+ also in group `b` can simply do `CPyDef_bar(...)`.
270
+
271
+ Thus the prefix for a module in group `b` is 'exports_b.' if the current
272
+ group is *not* b and just '' if it is.
273
+ """
274
+ groups = self.context.group_map
275
+ target_group_name = groups.get(module_name)
276
+ if target_group_name and target_group_name != self.context.group_name:
277
+ self.context.group_deps.add(target_group_name)
278
+ return f"exports_{exported_name(target_group_name)}."
279
+ else:
280
+ return ""
281
+
282
+ def get_group_prefix(self, obj: ClassIR | FuncDecl) -> str:
283
+ """Get the group prefix for an object."""
284
+ # See docs above
285
+ return self.get_module_group_prefix(obj.module_name)
286
+
287
+ def static_name(self, id: str, module: str | None, prefix: str = STATIC_PREFIX) -> str:
288
+ """Create name of a C static variable.
289
+
290
+ These are used for literals and imported modules, among other
291
+ things.
292
+
293
+ The caller should ensure that the (id, module) pair cannot
294
+ overlap with other calls to this method within a compilation
295
+ group.
296
+ """
297
+ lib_prefix = "" if not module else self.get_module_group_prefix(module)
298
+ # If we are accessing static via the export table, we need to dereference
299
+ # the pointer also.
300
+ star_maybe = "*" if lib_prefix else ""
301
+ suffix = self.names.private_name(module or "", id)
302
+ return f"{star_maybe}{lib_prefix}{prefix}{suffix}"
303
+
304
+ def type_struct_name(self, cl: ClassIR) -> str:
305
+ return self.static_name(cl.name, cl.module_name, prefix=TYPE_PREFIX)
306
+
307
+ def ctype(self, rtype: RType) -> str:
308
+ return rtype._ctype
309
+
310
+ def ctype_spaced(self, rtype: RType) -> str:
311
+ """Adds a space after ctype for non-pointers."""
312
+ ctype = self.ctype(rtype)
313
+ if ctype[-1] == "*":
314
+ return ctype
315
+ else:
316
+ return ctype + " "
317
+
318
+ def c_undefined_value(self, rtype: RType) -> str:
319
+ if not rtype.is_unboxed:
320
+ return "NULL"
321
+ elif isinstance(rtype, RPrimitive):
322
+ return rtype.c_undefined
323
+ elif isinstance(rtype, RTuple):
324
+ return self.tuple_undefined_value(rtype)
325
+ assert False, rtype
326
+
327
+ def c_error_value(self, rtype: RType) -> str:
328
+ return self.c_undefined_value(rtype)
329
+
330
+ def native_function_name(self, fn: FuncDecl) -> str:
331
+ return f"{NATIVE_PREFIX}{fn.cname(self.names)}"
332
+
333
+ def tuple_c_declaration(self, rtuple: RTuple) -> list[str]:
334
+ result = [
335
+ f"#ifndef MYPYC_DECLARED_{rtuple.struct_name}",
336
+ f"#define MYPYC_DECLARED_{rtuple.struct_name}",
337
+ f"typedef struct {rtuple.struct_name} {{",
338
+ ]
339
+ if len(rtuple.types) == 0: # empty tuple
340
+ # Empty tuples contain a flag so that they can still indicate
341
+ # error values.
342
+ result.append("int empty_struct_error_flag;")
343
+ else:
344
+ i = 0
345
+ for typ in rtuple.types:
346
+ result.append(f"{self.ctype_spaced(typ)}f{i};")
347
+ i += 1
348
+ result.append(f"}} {rtuple.struct_name};")
349
+ result.append("#endif")
350
+ result.append("")
351
+
352
+ return result
353
+
354
+ def bitmap_field(self, index: int) -> str:
355
+ """Return C field name used for attribute bitmap."""
356
+ n = index // BITMAP_BITS
357
+ if n == 0:
358
+ return "bitmap"
359
+ return f"bitmap{n + 1}"
360
+
361
+ def attr_bitmap_expr(self, obj: str, cl: ClassIR, index: int) -> str:
362
+ """Return reference to the attribute definedness bitmap."""
363
+ cast = f"({cl.struct_name(self.names)} *)"
364
+ attr = self.bitmap_field(index)
365
+ return f"({cast}{obj})->{attr}"
366
+
367
+ def emit_attr_bitmap_set(
368
+ self, value: str, obj: str, rtype: RType, cl: ClassIR, attr: str
369
+ ) -> None:
370
+ """Mark an attribute as defined in the attribute bitmap.
371
+
372
+ Assumes that the attribute is tracked in the bitmap (only some attributes
373
+ use the bitmap). If 'value' is not equal to the error value, do nothing.
374
+ """
375
+ self._emit_attr_bitmap_update(value, obj, rtype, cl, attr, clear=False)
376
+
377
+ def emit_attr_bitmap_clear(self, obj: str, rtype: RType, cl: ClassIR, attr: str) -> None:
378
+ """Mark an attribute as undefined in the attribute bitmap.
379
+
380
+ Unlike emit_attr_bitmap_set, clear unconditionally.
381
+ """
382
+ self._emit_attr_bitmap_update("", obj, rtype, cl, attr, clear=True)
383
+
384
+ def _emit_attr_bitmap_update(
385
+ self, value: str, obj: str, rtype: RType, cl: ClassIR, attr: str, clear: bool
386
+ ) -> None:
387
+ if value:
388
+ check = self.error_value_check(rtype, value, "==")
389
+ self.emit_line(f"if (unlikely({check})) {{")
390
+ index = cl.bitmap_attrs.index(attr)
391
+ mask = 1 << (index & (BITMAP_BITS - 1))
392
+ bitmap = self.attr_bitmap_expr(obj, cl, index)
393
+ if clear:
394
+ self.emit_line(f"{bitmap} &= ~{mask};")
395
+ else:
396
+ self.emit_line(f"{bitmap} |= {mask};")
397
+ if value:
398
+ self.emit_line("}")
399
+
400
+ def use_vectorcall(self) -> bool:
401
+ return use_vectorcall(self.capi_version)
402
+
403
+ def emit_undefined_attr_check(
404
+ self,
405
+ rtype: RType,
406
+ attr_expr: str,
407
+ compare: str,
408
+ obj: str,
409
+ attr: str,
410
+ cl: ClassIR,
411
+ *,
412
+ unlikely: bool = False,
413
+ ) -> None:
414
+ check = self.error_value_check(rtype, attr_expr, compare)
415
+ if unlikely:
416
+ check = f"unlikely({check})"
417
+ if rtype.error_overlap:
418
+ index = cl.bitmap_attrs.index(attr)
419
+ bit = 1 << (index & (BITMAP_BITS - 1))
420
+ attr = self.bitmap_field(index)
421
+ obj_expr = f"({cl.struct_name(self.names)} *){obj}"
422
+ check = f"{check} && !(({obj_expr})->{attr} & {bit})"
423
+ self.emit_line(f"if ({check}) {{")
424
+
425
+ def error_value_check(self, rtype: RType, value: str, compare: str) -> str:
426
+ if isinstance(rtype, RTuple):
427
+ return self.tuple_undefined_check_cond(
428
+ rtype, value, self.c_error_value, compare, check_exception=False
429
+ )
430
+ else:
431
+ return f"{value} {compare} {self.c_error_value(rtype)}"
432
+
433
+ def tuple_undefined_check_cond(
434
+ self,
435
+ rtuple: RTuple,
436
+ tuple_expr_in_c: str,
437
+ c_type_compare_val: Callable[[RType], str],
438
+ compare: str,
439
+ *,
440
+ check_exception: bool = True,
441
+ ) -> str:
442
+ if len(rtuple.types) == 0:
443
+ # empty tuple
444
+ return "{}.empty_struct_error_flag {} {}".format(
445
+ tuple_expr_in_c, compare, c_type_compare_val(int_rprimitive)
446
+ )
447
+ if rtuple.error_overlap:
448
+ i = 0
449
+ item_type = rtuple.types[0]
450
+ else:
451
+ for i, typ in enumerate(rtuple.types):
452
+ if not typ.error_overlap:
453
+ item_type = rtuple.types[i]
454
+ break
455
+ else:
456
+ assert False, "not expecting tuple with error overlap"
457
+ if isinstance(item_type, RTuple):
458
+ return self.tuple_undefined_check_cond(
459
+ item_type, tuple_expr_in_c + f".f{i}", c_type_compare_val, compare
460
+ )
461
+ else:
462
+ check = f"{tuple_expr_in_c}.f{i} {compare} {c_type_compare_val(item_type)}"
463
+ if rtuple.error_overlap and check_exception:
464
+ check += " && PyErr_Occurred()"
465
+ return check
466
+
467
+ def tuple_undefined_value(self, rtuple: RTuple) -> str:
468
+ """Undefined tuple value suitable in an expression."""
469
+ return f"({rtuple.struct_name}) {self.c_initializer_undefined_value(rtuple)}"
470
+
471
+ def c_initializer_undefined_value(self, rtype: RType) -> str:
472
+ """Undefined value represented in a form suitable for variable initialization."""
473
+ if isinstance(rtype, RTuple):
474
+ if not rtype.types:
475
+ # Empty tuples contain a flag so that they can still indicate
476
+ # error values.
477
+ return f"{{ {int_rprimitive.c_undefined} }}"
478
+ items = ", ".join([self.c_initializer_undefined_value(t) for t in rtype.types])
479
+ return f"{{ {items} }}"
480
+ else:
481
+ return self.c_undefined_value(rtype)
482
+
483
+ # Higher-level operations
484
+
485
+ def declare_tuple_struct(self, tuple_type: RTuple) -> None:
486
+ if tuple_type.struct_name not in self.context.declarations:
487
+ dependencies = set()
488
+ for typ in tuple_type.types:
489
+ # XXX other types might eventually need similar behavior
490
+ if isinstance(typ, RTuple):
491
+ dependencies.add(typ.struct_name)
492
+
493
+ self.context.declarations[tuple_type.struct_name] = HeaderDeclaration(
494
+ self.tuple_c_declaration(tuple_type), dependencies=dependencies, is_type=True
495
+ )
496
+
497
+ def emit_inc_ref(self, dest: str, rtype: RType, *, rare: bool = False) -> None:
498
+ """Increment reference count of C expression `dest`.
499
+
500
+ For composite unboxed structures (e.g. tuples) recursively
501
+ increment reference counts for each component.
502
+
503
+ If rare is True, optimize for code size and compilation speed.
504
+ """
505
+ if is_int_rprimitive(rtype):
506
+ if rare:
507
+ self.emit_line("CPyTagged_IncRef(%s);" % dest)
508
+ else:
509
+ self.emit_line("CPyTagged_INCREF(%s);" % dest)
510
+ elif isinstance(rtype, RTuple):
511
+ for i, item_type in enumerate(rtype.types):
512
+ self.emit_inc_ref(f"{dest}.f{i}", item_type)
513
+ elif not rtype.is_unboxed:
514
+ # Always inline, since this is a simple op
515
+ self.emit_line("CPy_INCREF(%s);" % dest)
516
+ # Otherwise assume it's an unboxed, pointerless value and do nothing.
517
+
518
+ def emit_dec_ref(
519
+ self, dest: str, rtype: RType, *, is_xdec: bool = False, rare: bool = False
520
+ ) -> None:
521
+ """Decrement reference count of C expression `dest`.
522
+
523
+ For composite unboxed structures (e.g. tuples) recursively
524
+ decrement reference counts for each component.
525
+
526
+ If rare is True, optimize for code size and compilation speed.
527
+ """
528
+ x = "X" if is_xdec else ""
529
+ if is_int_rprimitive(rtype):
530
+ if rare:
531
+ self.emit_line(f"CPyTagged_{x}DecRef({dest});")
532
+ else:
533
+ # Inlined
534
+ self.emit_line(f"CPyTagged_{x}DECREF({dest});")
535
+ elif isinstance(rtype, RTuple):
536
+ for i, item_type in enumerate(rtype.types):
537
+ self.emit_dec_ref(f"{dest}.f{i}", item_type, is_xdec=is_xdec, rare=rare)
538
+ elif not rtype.is_unboxed:
539
+ if rare:
540
+ self.emit_line(f"CPy_{x}DecRef({dest});")
541
+ else:
542
+ # Inlined
543
+ self.emit_line(f"CPy_{x}DECREF({dest});")
544
+ # Otherwise assume it's an unboxed, pointerless value and do nothing.
545
+
546
+ def pretty_name(self, typ: RType) -> str:
547
+ value_type = optional_value_type(typ)
548
+ if value_type is not None:
549
+ return "%s or None" % self.pretty_name(value_type)
550
+ return str(typ)
551
+
552
+ def emit_cast(
553
+ self,
554
+ src: str,
555
+ dest: str,
556
+ typ: RType,
557
+ *,
558
+ declare_dest: bool = False,
559
+ error: ErrorHandler | None = None,
560
+ raise_exception: bool = True,
561
+ optional: bool = False,
562
+ src_type: RType | None = None,
563
+ likely: bool = True,
564
+ ) -> None:
565
+ """Emit code for casting a value of given type.
566
+
567
+ Somewhat strangely, this supports unboxed types but only
568
+ operates on boxed versions. This is necessary to properly
569
+ handle types such as Optional[int] in compatibility glue.
570
+
571
+ By default, assign NULL (error value) to dest if the value has
572
+ an incompatible type and raise TypeError. These can be customized
573
+ using 'error' and 'raise_exception'.
574
+
575
+ Always copy/steal the reference in 'src'.
576
+
577
+ Args:
578
+ src: Name of source C variable
579
+ dest: Name of target C variable
580
+ typ: Type of value
581
+ declare_dest: If True, also declare the variable 'dest'
582
+ error: What happens on error
583
+ raise_exception: If True, also raise TypeError on failure
584
+ likely: If the cast is likely to succeed (can be False for unions)
585
+ """
586
+ error = error or AssignHandler()
587
+
588
+ # Special case casting *from* optional
589
+ if src_type and is_optional_type(src_type) and not is_object_rprimitive(typ):
590
+ value_type = optional_value_type(src_type)
591
+ assert value_type is not None
592
+ if is_same_type(value_type, typ):
593
+ if declare_dest:
594
+ self.emit_line(f"PyObject *{dest};")
595
+ check = "({} != Py_None)"
596
+ if likely:
597
+ check = f"(likely{check})"
598
+ self.emit_arg_check(src, dest, typ, check.format(src), optional)
599
+ self.emit_lines(f" {dest} = {src};", "else {")
600
+ self.emit_cast_error_handler(error, src, dest, typ, raise_exception)
601
+ self.emit_line("}")
602
+ return
603
+
604
+ # TODO: Verify refcount handling.
605
+ if (
606
+ is_list_rprimitive(typ)
607
+ or is_dict_rprimitive(typ)
608
+ or is_set_rprimitive(typ)
609
+ or is_str_rprimitive(typ)
610
+ or is_range_rprimitive(typ)
611
+ or is_float_rprimitive(typ)
612
+ or is_int_rprimitive(typ)
613
+ or is_bool_rprimitive(typ)
614
+ or is_bit_rprimitive(typ)
615
+ or is_fixed_width_rtype(typ)
616
+ ):
617
+ if declare_dest:
618
+ self.emit_line(f"PyObject *{dest};")
619
+ if is_list_rprimitive(typ):
620
+ prefix = "PyList"
621
+ elif is_dict_rprimitive(typ):
622
+ prefix = "PyDict"
623
+ elif is_set_rprimitive(typ):
624
+ prefix = "PySet"
625
+ elif is_str_rprimitive(typ):
626
+ prefix = "PyUnicode"
627
+ elif is_range_rprimitive(typ):
628
+ prefix = "PyRange"
629
+ elif is_float_rprimitive(typ):
630
+ prefix = "CPyFloat"
631
+ elif is_int_rprimitive(typ) or is_fixed_width_rtype(typ):
632
+ # TODO: Range check for fixed-width types?
633
+ prefix = "PyLong"
634
+ elif is_bool_rprimitive(typ) or is_bit_rprimitive(typ):
635
+ prefix = "PyBool"
636
+ else:
637
+ assert False, f"unexpected primitive type: {typ}"
638
+ check = "({}_Check({}))"
639
+ if likely:
640
+ check = f"(likely{check})"
641
+ self.emit_arg_check(src, dest, typ, check.format(prefix, src), optional)
642
+ self.emit_lines(f" {dest} = {src};", "else {")
643
+ self.emit_cast_error_handler(error, src, dest, typ, raise_exception)
644
+ self.emit_line("}")
645
+ elif is_bytes_rprimitive(typ):
646
+ if declare_dest:
647
+ self.emit_line(f"PyObject *{dest};")
648
+ check = "(PyBytes_Check({}) || PyByteArray_Check({}))"
649
+ if likely:
650
+ check = f"(likely{check})"
651
+ self.emit_arg_check(src, dest, typ, check.format(src, src), optional)
652
+ self.emit_lines(f" {dest} = {src};", "else {")
653
+ self.emit_cast_error_handler(error, src, dest, typ, raise_exception)
654
+ self.emit_line("}")
655
+ elif is_tuple_rprimitive(typ):
656
+ if declare_dest:
657
+ self.emit_line(f"{self.ctype(typ)} {dest};")
658
+ check = "(PyTuple_Check({}))"
659
+ if likely:
660
+ check = f"(likely{check})"
661
+ self.emit_arg_check(src, dest, typ, check.format(src), optional)
662
+ self.emit_lines(f" {dest} = {src};", "else {")
663
+ self.emit_cast_error_handler(error, src, dest, typ, raise_exception)
664
+ self.emit_line("}")
665
+ elif isinstance(typ, RInstance):
666
+ if declare_dest:
667
+ self.emit_line(f"PyObject *{dest};")
668
+ concrete = all_concrete_classes(typ.class_ir)
669
+ # If there are too many concrete subclasses or we can't find any
670
+ # (meaning the code ought to be dead or we aren't doing global opts),
671
+ # fall back to a normal typecheck.
672
+ # Otherwise check all the subclasses.
673
+ if not concrete or len(concrete) > FAST_ISINSTANCE_MAX_SUBCLASSES + 1:
674
+ check = "(PyObject_TypeCheck({}, {}))".format(
675
+ src, self.type_struct_name(typ.class_ir)
676
+ )
677
+ else:
678
+ full_str = "(Py_TYPE({src}) == {targets[0]})"
679
+ for i in range(1, len(concrete)):
680
+ full_str += " || (Py_TYPE({src}) == {targets[%d]})" % i
681
+ if len(concrete) > 1:
682
+ full_str = "(%s)" % full_str
683
+ check = full_str.format(
684
+ src=src, targets=[self.type_struct_name(ir) for ir in concrete]
685
+ )
686
+ if likely:
687
+ check = f"(likely{check})"
688
+ self.emit_arg_check(src, dest, typ, check, optional)
689
+ self.emit_lines(f" {dest} = {src};", "else {")
690
+ self.emit_cast_error_handler(error, src, dest, typ, raise_exception)
691
+ self.emit_line("}")
692
+ elif is_none_rprimitive(typ):
693
+ if declare_dest:
694
+ self.emit_line(f"PyObject *{dest};")
695
+ check = "({} == Py_None)"
696
+ if likely:
697
+ check = f"(likely{check})"
698
+ self.emit_arg_check(src, dest, typ, check.format(src), optional)
699
+ self.emit_lines(f" {dest} = {src};", "else {")
700
+ self.emit_cast_error_handler(error, src, dest, typ, raise_exception)
701
+ self.emit_line("}")
702
+ elif is_object_rprimitive(typ):
703
+ if declare_dest:
704
+ self.emit_line(f"PyObject *{dest};")
705
+ self.emit_arg_check(src, dest, typ, "", optional)
706
+ self.emit_line(f"{dest} = {src};")
707
+ if optional:
708
+ self.emit_line("}")
709
+ elif isinstance(typ, RUnion):
710
+ self.emit_union_cast(
711
+ src, dest, typ, declare_dest, error, optional, src_type, raise_exception
712
+ )
713
+ elif isinstance(typ, RTuple):
714
+ assert not optional
715
+ self.emit_tuple_cast(src, dest, typ, declare_dest, error, src_type)
716
+ else:
717
+ assert False, "Cast not implemented: %s" % typ
718
+
719
+ def emit_cast_error_handler(
720
+ self, error: ErrorHandler, src: str, dest: str, typ: RType, raise_exception: bool
721
+ ) -> None:
722
+ if raise_exception:
723
+ if isinstance(error, TracebackAndGotoHandler):
724
+ # Merge raising and emitting traceback entry into a single call.
725
+ self.emit_type_error_traceback(
726
+ error.source_path, error.module_name, error.traceback_entry, typ=typ, src=src
727
+ )
728
+ self.emit_line("goto %s;" % error.label)
729
+ return
730
+ self.emit_line(f'CPy_TypeError("{self.pretty_name(typ)}", {src}); ')
731
+ if isinstance(error, AssignHandler):
732
+ self.emit_line("%s = NULL;" % dest)
733
+ elif isinstance(error, GotoHandler):
734
+ self.emit_line("goto %s;" % error.label)
735
+ elif isinstance(error, TracebackAndGotoHandler):
736
+ self.emit_line("%s = NULL;" % dest)
737
+ self.emit_traceback(error.source_path, error.module_name, error.traceback_entry)
738
+ self.emit_line("goto %s;" % error.label)
739
+ else:
740
+ assert isinstance(error, ReturnHandler)
741
+ self.emit_line("return %s;" % error.value)
742
+
743
+ def emit_union_cast(
744
+ self,
745
+ src: str,
746
+ dest: str,
747
+ typ: RUnion,
748
+ declare_dest: bool,
749
+ error: ErrorHandler,
750
+ optional: bool,
751
+ src_type: RType | None,
752
+ raise_exception: bool,
753
+ ) -> None:
754
+ """Emit cast to a union type.
755
+
756
+ The arguments are similar to emit_cast.
757
+ """
758
+ if declare_dest:
759
+ self.emit_line(f"PyObject *{dest};")
760
+ good_label = self.new_label()
761
+ if optional:
762
+ self.emit_line(f"if ({src} == NULL) {{")
763
+ self.emit_line(f"{dest} = {self.c_error_value(typ)};")
764
+ self.emit_line(f"goto {good_label};")
765
+ self.emit_line("}")
766
+ for item in typ.items:
767
+ self.emit_cast(
768
+ src,
769
+ dest,
770
+ item,
771
+ declare_dest=False,
772
+ raise_exception=False,
773
+ optional=False,
774
+ likely=False,
775
+ )
776
+ self.emit_line(f"if ({dest} != NULL) goto {good_label};")
777
+ # Handle cast failure.
778
+ self.emit_cast_error_handler(error, src, dest, typ, raise_exception)
779
+ self.emit_label(good_label)
780
+
781
+ def emit_tuple_cast(
782
+ self,
783
+ src: str,
784
+ dest: str,
785
+ typ: RTuple,
786
+ declare_dest: bool,
787
+ error: ErrorHandler,
788
+ src_type: RType | None,
789
+ ) -> None:
790
+ """Emit cast to a tuple type.
791
+
792
+ The arguments are similar to emit_cast.
793
+ """
794
+ if declare_dest:
795
+ self.emit_line(f"PyObject *{dest};")
796
+ # This reuse of the variable is super dodgy. We don't even
797
+ # care about the values except to check whether they are
798
+ # invalid.
799
+ out_label = self.new_label()
800
+ self.emit_lines(
801
+ "if (unlikely(!(PyTuple_Check({r}) && PyTuple_GET_SIZE({r}) == {size}))) {{".format(
802
+ r=src, size=len(typ.types)
803
+ ),
804
+ f"{dest} = NULL;",
805
+ f"goto {out_label};",
806
+ "}",
807
+ )
808
+ for i, item in enumerate(typ.types):
809
+ # Since we did the checks above this should never fail
810
+ self.emit_cast(
811
+ f"PyTuple_GET_ITEM({src}, {i})",
812
+ dest,
813
+ item,
814
+ declare_dest=False,
815
+ raise_exception=False,
816
+ optional=False,
817
+ )
818
+ self.emit_line(f"if ({dest} == NULL) goto {out_label};")
819
+
820
+ self.emit_line(f"{dest} = {src};")
821
+ self.emit_label(out_label)
822
+
823
+ def emit_arg_check(self, src: str, dest: str, typ: RType, check: str, optional: bool) -> None:
824
+ if optional:
825
+ self.emit_line(f"if ({src} == NULL) {{")
826
+ self.emit_line(f"{dest} = {self.c_error_value(typ)};")
827
+ if check != "":
828
+ self.emit_line("{}if {}".format("} else " if optional else "", check))
829
+ elif optional:
830
+ self.emit_line("else {")
831
+
832
+ def emit_unbox(
833
+ self,
834
+ src: str,
835
+ dest: str,
836
+ typ: RType,
837
+ *,
838
+ declare_dest: bool = False,
839
+ error: ErrorHandler | None = None,
840
+ raise_exception: bool = True,
841
+ optional: bool = False,
842
+ borrow: bool = False,
843
+ ) -> None:
844
+ """Emit code for unboxing a value of given type (from PyObject *).
845
+
846
+ By default, assign error value to dest if the value has an
847
+ incompatible type and raise TypeError. These can be customized
848
+ using 'error' and 'raise_exception'.
849
+
850
+ Generate a new reference unless 'borrow' is True.
851
+
852
+ Args:
853
+ src: Name of source C variable
854
+ dest: Name of target C variable
855
+ typ: Type of value
856
+ declare_dest: If True, also declare the variable 'dest'
857
+ error: What happens on error
858
+ raise_exception: If True, also raise TypeError on failure
859
+ borrow: If True, create a borrowed reference
860
+
861
+ """
862
+ error = error or AssignHandler()
863
+ # TODO: Verify refcount handling.
864
+ if isinstance(error, AssignHandler):
865
+ failure = f"{dest} = {self.c_error_value(typ)};"
866
+ elif isinstance(error, GotoHandler):
867
+ failure = "goto %s;" % error.label
868
+ else:
869
+ assert isinstance(error, ReturnHandler)
870
+ failure = "return %s;" % error.value
871
+ if raise_exception:
872
+ raise_exc = f'CPy_TypeError("{self.pretty_name(typ)}", {src}); '
873
+ failure = raise_exc + failure
874
+ if is_int_rprimitive(typ) or is_short_int_rprimitive(typ):
875
+ if declare_dest:
876
+ self.emit_line(f"CPyTagged {dest};")
877
+ self.emit_arg_check(src, dest, typ, f"(likely(PyLong_Check({src})))", optional)
878
+ if borrow:
879
+ self.emit_line(f" {dest} = CPyTagged_BorrowFromObject({src});")
880
+ else:
881
+ self.emit_line(f" {dest} = CPyTagged_FromObject({src});")
882
+ self.emit_line("else {")
883
+ self.emit_line(failure)
884
+ self.emit_line("}")
885
+ elif is_bool_rprimitive(typ) or is_bit_rprimitive(typ):
886
+ # Whether we are borrowing or not makes no difference.
887
+ if declare_dest:
888
+ self.emit_line(f"char {dest};")
889
+ self.emit_arg_check(src, dest, typ, f"(unlikely(!PyBool_Check({src}))) {{", optional)
890
+ self.emit_line(failure)
891
+ self.emit_line("} else")
892
+ conversion = f"{src} == Py_True"
893
+ self.emit_line(f" {dest} = {conversion};")
894
+ elif is_none_rprimitive(typ):
895
+ # Whether we are borrowing or not makes no difference.
896
+ if declare_dest:
897
+ self.emit_line(f"char {dest};")
898
+ self.emit_arg_check(src, dest, typ, f"(unlikely({src} != Py_None)) {{", optional)
899
+ self.emit_line(failure)
900
+ self.emit_line("} else")
901
+ self.emit_line(f" {dest} = 1;")
902
+ elif is_int64_rprimitive(typ):
903
+ # Whether we are borrowing or not makes no difference.
904
+ assert not optional # Not supported for overlapping error values
905
+ if declare_dest:
906
+ self.emit_line(f"int64_t {dest};")
907
+ self.emit_line(f"{dest} = CPyLong_AsInt64({src});")
908
+ if not isinstance(error, AssignHandler):
909
+ self.emit_unbox_failure_with_overlapping_error_value(dest, typ, failure)
910
+ elif is_int32_rprimitive(typ):
911
+ # Whether we are borrowing or not makes no difference.
912
+ assert not optional # Not supported for overlapping error values
913
+ if declare_dest:
914
+ self.emit_line(f"int32_t {dest};")
915
+ self.emit_line(f"{dest} = CPyLong_AsInt32({src});")
916
+ if not isinstance(error, AssignHandler):
917
+ self.emit_unbox_failure_with_overlapping_error_value(dest, typ, failure)
918
+ elif is_int16_rprimitive(typ):
919
+ # Whether we are borrowing or not makes no difference.
920
+ assert not optional # Not supported for overlapping error values
921
+ if declare_dest:
922
+ self.emit_line(f"int16_t {dest};")
923
+ self.emit_line(f"{dest} = CPyLong_AsInt16({src});")
924
+ if not isinstance(error, AssignHandler):
925
+ self.emit_unbox_failure_with_overlapping_error_value(dest, typ, failure)
926
+ elif is_uint8_rprimitive(typ):
927
+ # Whether we are borrowing or not makes no difference.
928
+ assert not optional # Not supported for overlapping error values
929
+ if declare_dest:
930
+ self.emit_line(f"uint8_t {dest};")
931
+ self.emit_line(f"{dest} = CPyLong_AsUInt8({src});")
932
+ if not isinstance(error, AssignHandler):
933
+ self.emit_unbox_failure_with_overlapping_error_value(dest, typ, failure)
934
+ elif is_float_rprimitive(typ):
935
+ assert not optional # Not supported for overlapping error values
936
+ if declare_dest:
937
+ self.emit_line(f"double {dest};")
938
+ # TODO: Don't use __float__ and __index__
939
+ self.emit_line(f"{dest} = PyFloat_AsDouble({src});")
940
+ self.emit_lines(f"if ({dest} == -1.0 && PyErr_Occurred()) {{", failure, "}")
941
+ elif isinstance(typ, RTuple):
942
+ self.declare_tuple_struct(typ)
943
+ if declare_dest:
944
+ self.emit_line(f"{self.ctype(typ)} {dest};")
945
+ # HACK: The error handling for unboxing tuples is busted
946
+ # and instead of fixing it I am just wrapping it in the
947
+ # cast code which I think is right. This is not good.
948
+ if optional:
949
+ self.emit_line(f"if ({src} == NULL) {{")
950
+ self.emit_line(f"{dest} = {self.c_error_value(typ)};")
951
+ self.emit_line("} else {")
952
+
953
+ cast_temp = self.temp_name()
954
+ self.emit_tuple_cast(
955
+ src, cast_temp, typ, declare_dest=True, error=error, src_type=None
956
+ )
957
+ self.emit_line(f"if (unlikely({cast_temp} == NULL)) {{")
958
+
959
+ # self.emit_arg_check(src, dest, typ,
960
+ # '(!PyTuple_Check({}) || PyTuple_Size({}) != {}) {{'.format(
961
+ # src, src, len(typ.types)), optional)
962
+ self.emit_line(failure) # TODO: Decrease refcount?
963
+ self.emit_line("} else {")
964
+ if not typ.types:
965
+ self.emit_line(f"{dest}.empty_struct_error_flag = 0;")
966
+ for i, item_type in enumerate(typ.types):
967
+ temp = self.temp_name()
968
+ # emit_tuple_cast above checks the size, so this should not fail
969
+ self.emit_line(f"PyObject *{temp} = PyTuple_GET_ITEM({src}, {i});")
970
+ temp2 = self.temp_name()
971
+ # Unbox or check the item.
972
+ if item_type.is_unboxed:
973
+ self.emit_unbox(
974
+ temp,
975
+ temp2,
976
+ item_type,
977
+ raise_exception=raise_exception,
978
+ error=error,
979
+ declare_dest=True,
980
+ borrow=borrow,
981
+ )
982
+ else:
983
+ if not borrow:
984
+ self.emit_inc_ref(temp, object_rprimitive)
985
+ self.emit_cast(temp, temp2, item_type, declare_dest=True)
986
+ self.emit_line(f"{dest}.f{i} = {temp2};")
987
+ self.emit_line("}")
988
+ if optional:
989
+ self.emit_line("}")
990
+
991
+ else:
992
+ assert False, "Unboxing not implemented: %s" % typ
993
+
994
+ def emit_box(
995
+ self, src: str, dest: str, typ: RType, declare_dest: bool = False, can_borrow: bool = False
996
+ ) -> None:
997
+ """Emit code for boxing a value of given type.
998
+
999
+ Generate a simple assignment if no boxing is needed.
1000
+
1001
+ The source reference count is stolen for the result (no need to decref afterwards).
1002
+ """
1003
+ # TODO: Always generate a new reference (if a reference type)
1004
+ if declare_dest:
1005
+ declaration = "PyObject *"
1006
+ else:
1007
+ declaration = ""
1008
+ if is_int_rprimitive(typ) or is_short_int_rprimitive(typ):
1009
+ # Steal the existing reference if it exists.
1010
+ self.emit_line(f"{declaration}{dest} = CPyTagged_StealAsObject({src});")
1011
+ elif is_bool_rprimitive(typ) or is_bit_rprimitive(typ):
1012
+ # N.B: bool is special cased to produce a borrowed value
1013
+ # after boxing, so we don't need to increment the refcount
1014
+ # when this comes directly from a Box op.
1015
+ self.emit_lines(f"{declaration}{dest} = {src} ? Py_True : Py_False;")
1016
+ if not can_borrow:
1017
+ self.emit_inc_ref(dest, object_rprimitive)
1018
+ elif is_none_rprimitive(typ):
1019
+ # N.B: None is special cased to produce a borrowed value
1020
+ # after boxing, so we don't need to increment the refcount
1021
+ # when this comes directly from a Box op.
1022
+ self.emit_lines(f"{declaration}{dest} = Py_None;")
1023
+ if not can_borrow:
1024
+ self.emit_inc_ref(dest, object_rprimitive)
1025
+ elif is_int32_rprimitive(typ) or is_int16_rprimitive(typ) or is_uint8_rprimitive(typ):
1026
+ self.emit_line(f"{declaration}{dest} = PyLong_FromLong({src});")
1027
+ elif is_int64_rprimitive(typ):
1028
+ self.emit_line(f"{declaration}{dest} = PyLong_FromLongLong({src});")
1029
+ elif is_float_rprimitive(typ):
1030
+ self.emit_line(f"{declaration}{dest} = PyFloat_FromDouble({src});")
1031
+ elif isinstance(typ, RTuple):
1032
+ self.declare_tuple_struct(typ)
1033
+ self.emit_line(f"{declaration}{dest} = PyTuple_New({len(typ.types)});")
1034
+ self.emit_line(f"if (unlikely({dest} == NULL))")
1035
+ self.emit_line(" CPyError_OutOfMemory();")
1036
+ # TODO: Fail if dest is None
1037
+ for i in range(0, len(typ.types)):
1038
+ if not typ.is_unboxed:
1039
+ self.emit_line(f"PyTuple_SET_ITEM({dest}, {i}, {src}.f{i}")
1040
+ else:
1041
+ inner_name = self.temp_name()
1042
+ self.emit_box(f"{src}.f{i}", inner_name, typ.types[i], declare_dest=True)
1043
+ self.emit_line(f"PyTuple_SET_ITEM({dest}, {i}, {inner_name});")
1044
+ else:
1045
+ assert not typ.is_unboxed
1046
+ # Type is boxed -- trivially just assign.
1047
+ self.emit_line(f"{declaration}{dest} = {src};")
1048
+
1049
+ def emit_error_check(self, value: str, rtype: RType, failure: str) -> None:
1050
+ """Emit code for checking a native function return value for uncaught exception."""
1051
+ if isinstance(rtype, RTuple):
1052
+ if len(rtype.types) == 0:
1053
+ return # empty tuples can't fail.
1054
+ else:
1055
+ cond = self.tuple_undefined_check_cond(rtype, value, self.c_error_value, "==")
1056
+ self.emit_line(f"if ({cond}) {{")
1057
+ elif rtype.error_overlap:
1058
+ # The error value is also valid as a normal value, so we need to also check
1059
+ # for a raised exception.
1060
+ self.emit_line(f"if ({value} == {self.c_error_value(rtype)} && PyErr_Occurred()) {{")
1061
+ else:
1062
+ self.emit_line(f"if ({value} == {self.c_error_value(rtype)}) {{")
1063
+ self.emit_lines(failure, "}")
1064
+
1065
+ def emit_gc_visit(self, target: str, rtype: RType) -> None:
1066
+ """Emit code for GC visiting a C variable reference.
1067
+
1068
+ Assume that 'target' represents a C expression that refers to a
1069
+ struct member, such as 'self->x'.
1070
+ """
1071
+ if not rtype.is_refcounted:
1072
+ # Not refcounted -> no pointers -> no GC interaction.
1073
+ return
1074
+ elif isinstance(rtype, RPrimitive) and rtype.name == "builtins.int":
1075
+ self.emit_line(f"if (CPyTagged_CheckLong({target})) {{")
1076
+ self.emit_line(f"Py_VISIT(CPyTagged_LongAsObject({target}));")
1077
+ self.emit_line("}")
1078
+ elif isinstance(rtype, RTuple):
1079
+ for i, item_type in enumerate(rtype.types):
1080
+ self.emit_gc_visit(f"{target}.f{i}", item_type)
1081
+ elif self.ctype(rtype) == "PyObject *":
1082
+ # The simplest case.
1083
+ self.emit_line(f"Py_VISIT({target});")
1084
+ else:
1085
+ assert False, "emit_gc_visit() not implemented for %s" % repr(rtype)
1086
+
1087
+ def emit_gc_clear(self, target: str, rtype: RType) -> None:
1088
+ """Emit code for clearing a C attribute reference for GC.
1089
+
1090
+ Assume that 'target' represents a C expression that refers to a
1091
+ struct member, such as 'self->x'.
1092
+ """
1093
+ if not rtype.is_refcounted:
1094
+ # Not refcounted -> no pointers -> no GC interaction.
1095
+ return
1096
+ elif isinstance(rtype, RPrimitive) and rtype.name == "builtins.int":
1097
+ self.emit_line(f"if (CPyTagged_CheckLong({target})) {{")
1098
+ self.emit_line(f"CPyTagged __tmp = {target};")
1099
+ self.emit_line(f"{target} = {self.c_undefined_value(rtype)};")
1100
+ self.emit_line("Py_XDECREF(CPyTagged_LongAsObject(__tmp));")
1101
+ self.emit_line("}")
1102
+ elif isinstance(rtype, RTuple):
1103
+ for i, item_type in enumerate(rtype.types):
1104
+ self.emit_gc_clear(f"{target}.f{i}", item_type)
1105
+ elif self.ctype(rtype) == "PyObject *" and self.c_undefined_value(rtype) == "NULL":
1106
+ # The simplest case.
1107
+ self.emit_line(f"Py_CLEAR({target});")
1108
+ else:
1109
+ assert False, "emit_gc_clear() not implemented for %s" % repr(rtype)
1110
+
1111
+ def emit_traceback(
1112
+ self, source_path: str, module_name: str, traceback_entry: tuple[str, int]
1113
+ ) -> None:
1114
+ return self._emit_traceback("CPy_AddTraceback", source_path, module_name, traceback_entry)
1115
+
1116
+ def emit_type_error_traceback(
1117
+ self,
1118
+ source_path: str,
1119
+ module_name: str,
1120
+ traceback_entry: tuple[str, int],
1121
+ *,
1122
+ typ: RType,
1123
+ src: str,
1124
+ ) -> None:
1125
+ func = "CPy_TypeErrorTraceback"
1126
+ type_str = f'"{self.pretty_name(typ)}"'
1127
+ return self._emit_traceback(
1128
+ func, source_path, module_name, traceback_entry, type_str=type_str, src=src
1129
+ )
1130
+
1131
+ def _emit_traceback(
1132
+ self,
1133
+ func: str,
1134
+ source_path: str,
1135
+ module_name: str,
1136
+ traceback_entry: tuple[str, int],
1137
+ type_str: str = "",
1138
+ src: str = "",
1139
+ ) -> None:
1140
+ globals_static = self.static_name("globals", module_name)
1141
+ line = '%s("%s", "%s", %d, %s' % (
1142
+ func,
1143
+ source_path.replace("\\", "\\\\"),
1144
+ traceback_entry[0],
1145
+ traceback_entry[1],
1146
+ globals_static,
1147
+ )
1148
+ if type_str:
1149
+ assert src
1150
+ line += f", {type_str}, {src}"
1151
+ line += ");"
1152
+ self.emit_line(line)
1153
+ if DEBUG_ERRORS:
1154
+ self.emit_line('assert(PyErr_Occurred() != NULL && "failure w/o err!");')
1155
+
1156
+ def emit_unbox_failure_with_overlapping_error_value(
1157
+ self, dest: str, typ: RType, failure: str
1158
+ ) -> None:
1159
+ self.emit_line(f"if ({dest} == {self.c_error_value(typ)} && PyErr_Occurred()) {{")
1160
+ self.emit_line(failure)
1161
+ self.emit_line("}")
1162
+
1163
+
1164
+ def c_array_initializer(components: list[str], *, indented: bool = False) -> str:
1165
+ """Construct an initializer for a C array variable.
1166
+
1167
+ Components are C expressions valid in an initializer.
1168
+
1169
+ For example, if components are ["1", "2"], the result
1170
+ would be "{1, 2}", which can be used like this:
1171
+
1172
+ int a[] = {1, 2};
1173
+
1174
+ If the result is long, split it into multiple lines.
1175
+ """
1176
+ indent = " " * 4 if indented else ""
1177
+ res = []
1178
+ current: list[str] = []
1179
+ cur_len = 0
1180
+ for c in components:
1181
+ if not current or cur_len + 2 + len(indent) + len(c) < 70:
1182
+ current.append(c)
1183
+ cur_len += len(c) + 2
1184
+ else:
1185
+ res.append(indent + ", ".join(current))
1186
+ current = [c]
1187
+ cur_len = len(c)
1188
+ if not res:
1189
+ # Result fits on a single line
1190
+ return "{%s}" % ", ".join(current)
1191
+ # Multi-line result
1192
+ res.append(indent + ", ".join(current))
1193
+ return "{\n " + ",\n ".join(res) + "\n" + indent + "}"