From d8d257c18ca042563be4332ab75d10f5bf17fba5 Mon Sep 17 00:00:00 2001 From: Ethan Smith Date: Wed, 17 Aug 2022 20:49:21 -0700 Subject: [PATCH 1/8] Add support for building mypyc code on WASM --- mypyc/build.py | 4 +--- mypyc/common.py | 11 ++++++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/mypyc/build.py b/mypyc/build.py index a3a91fd97873..30234211ff9a 100644 --- a/mypyc/build.py +++ b/mypyc/build.py @@ -546,10 +546,8 @@ def mypycify( "-Wno-unused-variable", "-Wno-unused-command-line-argument", "-Wno-unknown-warning-option", + "-Wno-unused-but-set-variable", ] - if "gcc" in compiler.compiler[0] or "gnu-cc" in compiler.compiler[0]: - # This flag is needed for gcc but does not exist on clang. - cflags += ["-Wno-unused-but-set-variable"] elif compiler.compiler_type == "msvc": # msvc doesn't have levels, '/O2' is full and '/Od' is disable if opt_level == "0": diff --git a/mypyc/common.py b/mypyc/common.py index b631dff207ad..5b03e696f2c4 100644 --- a/mypyc/common.py +++ b/mypyc/common.py @@ -1,6 +1,8 @@ from __future__ import annotations +import math import sys +import sysconfig from typing import Any, Dict, Optional, Tuple from typing_extensions import Final @@ -30,7 +32,10 @@ # Maximal number of subclasses for a class to trigger fast path in isinstance() checks. FAST_ISINSTANCE_MAX_SUBCLASSES: Final = 2 -IS_32_BIT_PLATFORM: Final = sys.maxsize < (1 << 31) +# Size of size_t, if configured. +SIZEOF_SIZE_T: Final = sysconfig.get_config_var("SIZEOF_SIZE_T") or math.log2(sys.maxsize) + +IS_32_BIT_PLATFORM: Final = sys.maxsize < (1 << 31) if SIZEOF_SIZE_T is None else int(SIZEOF_SIZE_T) == 4 PLATFORM_SIZE = 4 if IS_32_BIT_PLATFORM else 8 @@ -42,13 +47,13 @@ IS_MIXED_32_64_BIT_BUILD: Final = sys.platform in ["darwin"] and sys.version_info < (3, 6) # Maximum value for a short tagged integer. -MAX_SHORT_INT: Final = sys.maxsize >> 1 +MAX_SHORT_INT: Final = sys.maxsize >> 1 if SIZEOF_SIZE_T is None else 2**int(SIZEOF_SIZE_T) # Maximum value for a short tagged integer represented as a C integer literal. # # Note: Assume that the compiled code uses the same bit width as mypyc, except for # Python 3.5 on macOS. -MAX_LITERAL_SHORT_INT: Final = sys.maxsize >> 1 if not IS_MIXED_32_64_BIT_BUILD else 2**30 - 1 +MAX_LITERAL_SHORT_INT: Final = MAX_SHORT_INT if not IS_MIXED_32_64_BIT_BUILD else 2**30 - 1 MIN_LITERAL_SHORT_INT: Final = -MAX_LITERAL_SHORT_INT - 1 # Runtime C library files From d750371eb5ae77ff56e7eda6391cee2cfa783275 Mon Sep 17 00:00:00 2001 From: Ethan Smith Date: Wed, 17 Aug 2022 21:21:27 -0700 Subject: [PATCH 2/8] Fix formatting for black --- mypyc/common.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mypyc/common.py b/mypyc/common.py index 5b03e696f2c4..444cdb79d871 100644 --- a/mypyc/common.py +++ b/mypyc/common.py @@ -35,7 +35,9 @@ # Size of size_t, if configured. SIZEOF_SIZE_T: Final = sysconfig.get_config_var("SIZEOF_SIZE_T") or math.log2(sys.maxsize) -IS_32_BIT_PLATFORM: Final = sys.maxsize < (1 << 31) if SIZEOF_SIZE_T is None else int(SIZEOF_SIZE_T) == 4 +IS_32_BIT_PLATFORM: Final = ( + sys.maxsize < (1 << 31) if SIZEOF_SIZE_T is None else int(SIZEOF_SIZE_T) == 4 +) PLATFORM_SIZE = 4 if IS_32_BIT_PLATFORM else 8 @@ -47,7 +49,7 @@ IS_MIXED_32_64_BIT_BUILD: Final = sys.platform in ["darwin"] and sys.version_info < (3, 6) # Maximum value for a short tagged integer. -MAX_SHORT_INT: Final = sys.maxsize >> 1 if SIZEOF_SIZE_T is None else 2**int(SIZEOF_SIZE_T) +MAX_SHORT_INT: Final = sys.maxsize >> 1 if SIZEOF_SIZE_T is None else 2 ** int(SIZEOF_SIZE_T) # Maximum value for a short tagged integer represented as a C integer literal. # From 6f342906713a035d381332b6069997f8ea955f3f Mon Sep 17 00:00:00 2001 From: Ethan Smith Date: Fri, 19 Aug 2022 16:43:38 -0700 Subject: [PATCH 3/8] Fix size of short --- mypyc/common.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mypyc/common.py b/mypyc/common.py index 444cdb79d871..6e103c04c04b 100644 --- a/mypyc/common.py +++ b/mypyc/common.py @@ -49,7 +49,9 @@ IS_MIXED_32_64_BIT_BUILD: Final = sys.platform in ["darwin"] and sys.version_info < (3, 6) # Maximum value for a short tagged integer. -MAX_SHORT_INT: Final = sys.maxsize >> 1 if SIZEOF_SIZE_T is None else 2 ** int(SIZEOF_SIZE_T) +MAX_SHORT_INT: Final = ( + sys.maxsize >> 1 if SIZEOF_SIZE_T is None else 2 ** (8 * int(SIZEOF_SIZE_T) - 2) - 1 +) # Maximum value for a short tagged integer represented as a C integer literal. # From dfba6034ffaf2cf1947d3f17fa3770d021fed9d2 Mon Sep 17 00:00:00 2001 From: Ethan Smith Date: Fri, 19 Aug 2022 22:11:05 -0700 Subject: [PATCH 4/8] More tweaks, broken --- mypyc/common.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/mypyc/common.py b/mypyc/common.py index 09a8233f6eb1..14bf4ff650a3 100644 --- a/mypyc/common.py +++ b/mypyc/common.py @@ -33,12 +33,16 @@ FAST_ISINSTANCE_MAX_SUBCLASSES: Final = 2 # Size of size_t, if configured. -SIZEOF_SIZE_T: Final = sysconfig.get_config_var("SIZEOF_SIZE_T") or math.log2(sys.maxsize) +SIZEOF_SIZE_T_SYSCONFIG: Final = sysconfig.get_config_var("SIZEOF_SIZE_T") -IS_32_BIT_PLATFORM: Final = ( - sys.maxsize < (1 << 31) if SIZEOF_SIZE_T is None else int(SIZEOF_SIZE_T) == 4 +SIZEOF_SIZE_T: Final = ( + int(SIZEOF_SIZE_T_SYSCONFIG) + if SIZEOF_SIZE_T_SYSCONFIG is not None + else (math.log2(sys.maxsize) + 1) // 8 ) +IS_32_BIT_PLATFORM: Final = int(SIZEOF_SIZE_T) == 4 + PLATFORM_SIZE = 4 if IS_32_BIT_PLATFORM else 8 # Python 3.5 on macOS uses a hybrid 32/64-bit build that requires some workarounds. @@ -49,9 +53,7 @@ IS_MIXED_32_64_BIT_BUILD: Final = sys.platform in ["darwin"] and sys.version_info < (3, 6) # Maximum value for a short tagged integer. -MAX_SHORT_INT: Final = ( - sys.maxsize >> 1 if SIZEOF_SIZE_T is None else 2 ** (8 * int(SIZEOF_SIZE_T) - 2) - 1 -) +MAX_SHORT_INT: Final = 2 ** (8 * int(SIZEOF_SIZE_T) - 2) - 1 # Maximum value for a short tagged integer represented as a C integer literal. # From 4ec39a835270ed8bec7f5d68022b5ea55ba22727 Mon Sep 17 00:00:00 2001 From: Ethan Smith Date: Thu, 15 Sep 2022 17:27:35 -0700 Subject: [PATCH 5/8] Add support for outputting vt100 colors with mypy running on emscripten --- mypy/util.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/mypy/util.py b/mypy/util.py index 686a71c4331b..4d4eff2be14a 100644 --- a/mypy/util.py +++ b/mypy/util.py @@ -525,7 +525,7 @@ class FancyFormatter: def __init__(self, f_out: IO[str], f_err: IO[str], show_error_codes: bool) -> None: self.show_error_codes = show_error_codes # Check if we are in a human-facing terminal on a supported platform. - if sys.platform not in ("linux", "darwin", "win32"): + if sys.platform not in ("linux", "darwin", "win32", "emscripten"): self.dummy_term = True return force_color = int(os.getenv("MYPY_FORCE_COLOR", "0")) @@ -534,6 +534,8 @@ def __init__(self, f_out: IO[str], f_err: IO[str], show_error_codes: bool) -> No return if sys.platform == "win32": self.dummy_term = not self.initialize_win_colors() + elif sys.platform == "emscripten": + self.dummy_term = not self.initialize_vt100_colors() else: self.dummy_term = not self.initialize_unix_colors() if not self.dummy_term: @@ -544,6 +546,19 @@ def __init__(self, f_out: IO[str], f_err: IO[str], show_error_codes: bool) -> No "yellow": self.YELLOW, "none": "", } + def initialize_vt100_colors(self) -> bool: + """Return True if initialization was successful and we can use colors, False otherwise""" + # Windows and Emscripten can both use ANSI/VT100 escape sequences for color + assert sys.platform in ("win32", "emscripten") + self.BOLD = "\033[1m" + self.UNDER = "\033[4m" + self.BLUE = "\033[94m" + self.GREEN = "\033[92m" + self.RED = "\033[91m" + self.YELLOW = "\033[93m" + self.NORMAL = "\033[0m" + self.DIM = "\033[2m" + return True def initialize_win_colors(self) -> bool: """Return True if initialization was successful and we can use colors, False otherwise""" @@ -571,14 +586,7 @@ def initialize_win_colors(self) -> bool: | ENABLE_WRAP_AT_EOL_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING, ) - self.BOLD = "\033[1m" - self.UNDER = "\033[4m" - self.BLUE = "\033[94m" - self.GREEN = "\033[92m" - self.RED = "\033[91m" - self.YELLOW = "\033[93m" - self.NORMAL = "\033[0m" - self.DIM = "\033[2m" + self.initialize_vt100_colors() return True return False From 9422bfcc055795b1b772a7414dfccebe453a2184 Mon Sep 17 00:00:00 2001 From: Ethan Smith Date: Fri, 16 Sep 2022 01:53:48 -0700 Subject: [PATCH 6/8] Run black --- mypy/util.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mypy/util.py b/mypy/util.py index 4d4eff2be14a..ad09395bc343 100644 --- a/mypy/util.py +++ b/mypy/util.py @@ -546,6 +546,7 @@ def __init__(self, f_out: IO[str], f_err: IO[str], show_error_codes: bool) -> No "yellow": self.YELLOW, "none": "", } + def initialize_vt100_colors(self) -> bool: """Return True if initialization was successful and we can use colors, False otherwise""" # Windows and Emscripten can both use ANSI/VT100 escape sequences for color From 7f84702eef4223e0102eef9520198ef5746ff391 Mon Sep 17 00:00:00 2001 From: Ethan Smith Date: Fri, 16 Sep 2022 15:48:17 -0700 Subject: [PATCH 7/8] Use bit_length instead of rounding Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com> --- mypyc/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypyc/common.py b/mypyc/common.py index 1619b68b9c42..c76a2a27a9bf 100644 --- a/mypyc/common.py +++ b/mypyc/common.py @@ -38,7 +38,7 @@ SIZEOF_SIZE_T: Final = ( int(SIZEOF_SIZE_T_SYSCONFIG) if SIZEOF_SIZE_T_SYSCONFIG is not None - else round((math.log2(sys.maxsize + 1)) / 8) + else (sys.maxsize + 1).bit_length() // 8 ) IS_32_BIT_PLATFORM: Final = int(SIZEOF_SIZE_T) == 4 From fdd78dba8825b12fd6d0d47b3e4c4b2bbf6d6648 Mon Sep 17 00:00:00 2001 From: Ethan Smith Date: Fri, 16 Sep 2022 15:54:19 -0700 Subject: [PATCH 8/8] Remove no longer needed math import --- mypyc/common.py | 1 - 1 file changed, 1 deletion(-) diff --git a/mypyc/common.py b/mypyc/common.py index c76a2a27a9bf..1ae564338609 100644 --- a/mypyc/common.py +++ b/mypyc/common.py @@ -1,6 +1,5 @@ from __future__ import annotations -import math import sys import sysconfig from typing import Any, Dict