mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 12:54:11 +08:00
Add CPython int/float tests (#150795)
Tests: * test_int.py * test_int_literal.py * test_float.py Minor changes were made to each test to run them inside Dynamo One can reproduce the changes by downloading the tests from CPython and applying the diff: ```bash for f in "test_int" "test_int_literal" "test_float"; do wget -O "test/dynamo/cpython/3_13/${f}.py" "https://raw.githubusercontent.com/python/cpython/refs/heads/3.13/Lib/test/${f}.py" git apply "test/dynamo/cpython/3_13/${f}.diff" done ``` Pull Request resolved: https://github.com/pytorch/pytorch/pull/150795 Approved by: https://github.com/williamwen42
This commit is contained in:
committed by
PyTorch MergeBot
parent
d5f642211f
commit
21f45f7afb
279
test/dynamo/cpython/3_13/test_float.diff
Normal file
279
test/dynamo/cpython/3_13/test_float.diff
Normal file
@ -0,0 +1,279 @@
|
||||
diff --git a/test/dynamo/cpython/3_13/test_float.py b/test/dynamo/cpython/3_13/test_float.py
|
||||
index 97f951f1299..ce2c46777e0 100644
|
||||
--- a/test/dynamo/cpython/3_13/test_float.py
|
||||
+++ b/test/dynamo/cpython/3_13/test_float.py
|
||||
@@ -1,3 +1,54 @@
|
||||
+# ======= BEGIN Dynamo patch =======
|
||||
+# Owner(s): ["module: dynamo"]
|
||||
+
|
||||
+# ruff: noqa
|
||||
+# flake8: noqa
|
||||
+
|
||||
+import sys
|
||||
+import torch
|
||||
+import torch._dynamo.test_case
|
||||
+import unittest
|
||||
+from torch._dynamo.test_case import CPythonTestCase
|
||||
+from torch.testing._internal.common_utils import run_tests
|
||||
+
|
||||
+__TestCase = CPythonTestCase
|
||||
+
|
||||
+
|
||||
+# redirect import statements
|
||||
+import sys
|
||||
+import importlib.abc
|
||||
+
|
||||
+redirect_imports = (
|
||||
+ "test.mapping_tests",
|
||||
+ "test.typinganndata",
|
||||
+ "test.test_grammar",
|
||||
+ "test.test_math",
|
||||
+ "test.test_iter",
|
||||
+ "test.typinganndata.ann_module",
|
||||
+)
|
||||
+
|
||||
+class RedirectImportFinder(importlib.abc.MetaPathFinder):
|
||||
+ def find_spec(self, fullname, path, target=None):
|
||||
+ # Check if the import is the problematic one
|
||||
+ if fullname in redirect_imports:
|
||||
+ try:
|
||||
+ # Attempt to import the standalone module
|
||||
+ name = fullname.removeprefix("test.")
|
||||
+ r = importlib.import_module(name)
|
||||
+ # Redirect the module in sys.modules
|
||||
+ sys.modules[fullname] = r
|
||||
+ # Return a module spec from the found module
|
||||
+ return importlib.util.find_spec(name)
|
||||
+ except ImportError:
|
||||
+ return None
|
||||
+ return None
|
||||
+
|
||||
+# Add the custom finder to sys.meta_path
|
||||
+sys.meta_path.insert(0, RedirectImportFinder())
|
||||
+
|
||||
+
|
||||
+# ======= END DYNAMO PATCH =======
|
||||
+
|
||||
import fractions
|
||||
import operator
|
||||
import os
|
||||
@@ -8,11 +59,84 @@ import time
|
||||
import unittest
|
||||
|
||||
from test import support
|
||||
-from test.support.testcase import FloatsAreIdenticalMixin
|
||||
-from test.support.numbers import (
|
||||
- VALID_UNDERSCORE_LITERALS,
|
||||
- INVALID_UNDERSCORE_LITERALS,
|
||||
-)
|
||||
+
|
||||
+VALID_UNDERSCORE_LITERALS = [
|
||||
+ '0_0_0',
|
||||
+ '4_2',
|
||||
+ '1_0000_0000',
|
||||
+ '0b1001_0100',
|
||||
+ '0xffff_ffff',
|
||||
+ '0o5_7_7',
|
||||
+ '1_00_00.5',
|
||||
+ '1_00_00.5e5',
|
||||
+ '1_00_00e5_1',
|
||||
+ '1e1_0',
|
||||
+ '.1_4',
|
||||
+ '.1_4e1',
|
||||
+ '0b_0',
|
||||
+ '0x_f',
|
||||
+ '0o_5',
|
||||
+ '1_00_00j',
|
||||
+ '1_00_00.5j',
|
||||
+ '1_00_00e5_1j',
|
||||
+ '.1_4j',
|
||||
+ '(1_2.5+3_3j)',
|
||||
+ '(.5_6j)',
|
||||
+]
|
||||
+INVALID_UNDERSCORE_LITERALS = [
|
||||
+ # Trailing underscores:
|
||||
+ '0_',
|
||||
+ '42_',
|
||||
+ '1.4j_',
|
||||
+ '0x_',
|
||||
+ '0b1_',
|
||||
+ '0xf_',
|
||||
+ '0o5_',
|
||||
+ '0 if 1_Else 1',
|
||||
+ # Underscores in the base selector:
|
||||
+ '0_b0',
|
||||
+ '0_xf',
|
||||
+ '0_o5',
|
||||
+ # Old-style octal, still disallowed:
|
||||
+ '0_7',
|
||||
+ '09_99',
|
||||
+ # Multiple consecutive underscores:
|
||||
+ '4_______2',
|
||||
+ '0.1__4',
|
||||
+ '0.1__4j',
|
||||
+ '0b1001__0100',
|
||||
+ '0xffff__ffff',
|
||||
+ '0x___',
|
||||
+ '0o5__77',
|
||||
+ '1e1__0',
|
||||
+ '1e1__0j',
|
||||
+ # Underscore right before a dot:
|
||||
+ '1_.4',
|
||||
+ '1_.4j',
|
||||
+ # Underscore right after a dot:
|
||||
+ '1._4',
|
||||
+ '1._4j',
|
||||
+ '._5',
|
||||
+ '._5j',
|
||||
+ # Underscore right after a sign:
|
||||
+ '1.0e+_1',
|
||||
+ '1.0e+_1j',
|
||||
+ # Underscore right before j:
|
||||
+ '1.4_j',
|
||||
+ '1.4e5_j',
|
||||
+ # Underscore right before e:
|
||||
+ '1_e1',
|
||||
+ '1.4_e1',
|
||||
+ '1.4_e1j',
|
||||
+ # Underscore right after e:
|
||||
+ '1e_1',
|
||||
+ '1.4e_1',
|
||||
+ '1.4e_1j',
|
||||
+ # Complex cases with parens:
|
||||
+ '(1+1.5_j_)',
|
||||
+ '(1+1.5_j)',
|
||||
+]
|
||||
+
|
||||
from math import isinf, isnan, copysign, ldexp
|
||||
import math
|
||||
|
||||
@@ -35,7 +159,7 @@ class FloatSubclass(float):
|
||||
class OtherFloatSubclass(float):
|
||||
pass
|
||||
|
||||
-class GeneralFloatCases(unittest.TestCase):
|
||||
+class GeneralFloatCases(__TestCase):
|
||||
|
||||
def test_float(self):
|
||||
self.assertEqual(float(3.14), 3.14)
|
||||
@@ -620,7 +744,7 @@ class GeneralFloatCases(unittest.TestCase):
|
||||
|
||||
|
||||
@unittest.skipUnless(hasattr(float, "__getformat__"), "requires __getformat__")
|
||||
-class FormatFunctionsTestCase(unittest.TestCase):
|
||||
+class FormatFunctionsTestCase(__TestCase):
|
||||
def test_getformat(self):
|
||||
self.assertIn(float.__getformat__('double'),
|
||||
['unknown', 'IEEE, big-endian', 'IEEE, little-endian'])
|
||||
@@ -645,7 +769,7 @@ LE_FLOAT_NAN = bytes(reversed(BE_FLOAT_NAN))
|
||||
# is accident (today).
|
||||
# let's also try to guarantee that -0.0 and 0.0 don't get confused.
|
||||
|
||||
-class IEEEFormatTestCase(unittest.TestCase):
|
||||
+class IEEEFormatTestCase(__TestCase):
|
||||
|
||||
@support.requires_IEEE_754
|
||||
def test_double_specials_do_unpack(self):
|
||||
@@ -670,7 +794,7 @@ class IEEEFormatTestCase(unittest.TestCase):
|
||||
self.assertEqual(struct.pack("<f", 3.40282356e38), struct.pack("<f", FLT_MAX))
|
||||
self.assertEqual(struct.pack("<f", -3.40282356e38), struct.pack("<f", -FLT_MAX))
|
||||
|
||||
-class FormatTestCase(unittest.TestCase):
|
||||
+class FormatTestCase(__TestCase):
|
||||
|
||||
def test_format(self):
|
||||
# these should be rewritten to use both format(x, spec) and
|
||||
@@ -767,7 +891,7 @@ class FormatTestCase(unittest.TestCase):
|
||||
self.assertEqual(format(-123.34, '00.10e'), '-1.2334000000e+02')
|
||||
self.assertEqual(format(-123.34, '00.10g'), '-123.34')
|
||||
|
||||
-class ReprTestCase(unittest.TestCase):
|
||||
+class ReprTestCase(__TestCase):
|
||||
def test_repr(self):
|
||||
with open(os.path.join(os.path.split(__file__)[0],
|
||||
'mathdata',
|
||||
@@ -832,7 +956,29 @@ class ReprTestCase(unittest.TestCase):
|
||||
self.assertEqual(repr(float(negs)), str(float(negs)))
|
||||
|
||||
@support.requires_IEEE_754
|
||||
-class RoundTestCase(unittest.TestCase, FloatsAreIdenticalMixin):
|
||||
+class RoundTestCase(__TestCase):
|
||||
+ def assertFloatsAreIdentical(self, x, y):
|
||||
+ """assert that floats x and y are identical, in the sense that:
|
||||
+ (1) both x and y are nans, or
|
||||
+ (2) both x and y are infinities, with the same sign, or
|
||||
+ (3) both x and y are zeros, with the same sign, or
|
||||
+ (4) x and y are both finite and nonzero, and x == y
|
||||
+
|
||||
+ """
|
||||
+ msg = 'floats {!r} and {!r} are not identical'
|
||||
+
|
||||
+ if isnan(x) or isnan(y):
|
||||
+ if isnan(x) and isnan(y):
|
||||
+ return
|
||||
+ elif x == y:
|
||||
+ if x != 0.0:
|
||||
+ return
|
||||
+ # both zero; check that signs match
|
||||
+ elif copysign(1.0, x) == copysign(1.0, y):
|
||||
+ return
|
||||
+ else:
|
||||
+ msg += ': zeros have different signs'
|
||||
+ self.fail(msg.format(x, y))
|
||||
|
||||
def test_inf_nan(self):
|
||||
self.assertRaises(OverflowError, round, INF)
|
||||
@@ -955,7 +1101,7 @@ class RoundTestCase(unittest.TestCase, FloatsAreIdenticalMixin):
|
||||
|
||||
# Beginning with Python 2.6 float has cross platform compatible
|
||||
# ways to create and represent inf and nan
|
||||
-class InfNanTest(unittest.TestCase):
|
||||
+class InfNanTest(__TestCase):
|
||||
def test_inf_from_str(self):
|
||||
self.assertTrue(isinf(float("inf")))
|
||||
self.assertTrue(isinf(float("+inf")))
|
||||
@@ -1056,12 +1202,35 @@ class InfNanTest(unittest.TestCase):
|
||||
|
||||
fromHex = float.fromhex
|
||||
toHex = float.hex
|
||||
-class HexFloatTestCase(FloatsAreIdenticalMixin, unittest.TestCase):
|
||||
+class HexFloatTestCase(__TestCase):
|
||||
MAX = fromHex('0x.fffffffffffff8p+1024') # max normal
|
||||
MIN = fromHex('0x1p-1022') # min normal
|
||||
TINY = fromHex('0x0.0000000000001p-1022') # min subnormal
|
||||
EPS = fromHex('0x0.0000000000001p0') # diff between 1.0 and next float up
|
||||
|
||||
+ def assertFloatsAreIdentical(self, x, y):
|
||||
+ """assert that floats x and y are identical, in the sense that:
|
||||
+ (1) both x and y are nans, or
|
||||
+ (2) both x and y are infinities, with the same sign, or
|
||||
+ (3) both x and y are zeros, with the same sign, or
|
||||
+ (4) x and y are both finite and nonzero, and x == y
|
||||
+
|
||||
+ """
|
||||
+ msg = 'floats {!r} and {!r} are not identical'
|
||||
+
|
||||
+ if isnan(x) or isnan(y):
|
||||
+ if isnan(x) and isnan(y):
|
||||
+ return
|
||||
+ elif x == y:
|
||||
+ if x != 0.0:
|
||||
+ return
|
||||
+ # both zero; check that signs match
|
||||
+ elif copysign(1.0, x) == copysign(1.0, y):
|
||||
+ return
|
||||
+ else:
|
||||
+ msg += ': zeros have different signs'
|
||||
+ self.fail(msg.format(x, y))
|
||||
+
|
||||
def identical(self, x, y):
|
||||
self.assertFloatsAreIdentical(x, y)
|
||||
|
||||
@@ -1500,5 +1669,5 @@ class HexFloatTestCase(FloatsAreIdenticalMixin, unittest.TestCase):
|
||||
self.assertEqual(getattr(f, 'foo', 'none'), 'bar')
|
||||
|
||||
|
||||
-if __name__ == '__main__':
|
||||
- unittest.main()
|
||||
+if __name__ == "__main__":
|
||||
+ run_tests()
|
1673
test/dynamo/cpython/3_13/test_float.py
Normal file
1673
test/dynamo/cpython/3_13/test_float.py
Normal file
File diff suppressed because it is too large
Load Diff
179
test/dynamo/cpython/3_13/test_int.diff
Normal file
179
test/dynamo/cpython/3_13/test_int.diff
Normal file
@ -0,0 +1,179 @@
|
||||
diff --git a/test/dynamo/cpython/3_13/test_int.py b/test/dynamo/cpython/3_13/test_int.py
|
||||
index 48825f46911..072c591e073 100644
|
||||
--- a/test/dynamo/cpython/3_13/test_int.py
|
||||
+++ b/test/dynamo/cpython/3_13/test_int.py
|
||||
@@ -1,13 +1,137 @@
|
||||
+# ======= BEGIN Dynamo patch =======
|
||||
+# Owner(s): ["module: dynamo"]
|
||||
+
|
||||
+# ruff: noqa
|
||||
+# flake8: noqa
|
||||
+
|
||||
+import sys
|
||||
+import torch
|
||||
+import torch._dynamo.test_case
|
||||
+import unittest
|
||||
+from torch._dynamo.test_case import CPythonTestCase
|
||||
+from torch.testing._internal.common_utils import run_tests
|
||||
+
|
||||
+__TestCase = CPythonTestCase
|
||||
+
|
||||
+
|
||||
+# redirect import statements
|
||||
+import sys
|
||||
+import importlib.abc
|
||||
+
|
||||
+redirect_imports = (
|
||||
+ "test.mapping_tests",
|
||||
+ "test.typinganndata",
|
||||
+ "test.test_grammar",
|
||||
+ "test.test_math",
|
||||
+ "test.test_iter",
|
||||
+ "test.typinganndata.ann_module",
|
||||
+)
|
||||
+
|
||||
+class RedirectImportFinder(importlib.abc.MetaPathFinder):
|
||||
+ def find_spec(self, fullname, path, target=None):
|
||||
+ # Check if the import is the problematic one
|
||||
+ if fullname in redirect_imports:
|
||||
+ try:
|
||||
+ # Attempt to import the standalone module
|
||||
+ name = fullname.removeprefix("test.")
|
||||
+ r = importlib.import_module(name)
|
||||
+ # Redirect the module in sys.modules
|
||||
+ sys.modules[fullname] = r
|
||||
+ # Return a module spec from the found module
|
||||
+ return importlib.util.find_spec(name)
|
||||
+ except ImportError:
|
||||
+ return None
|
||||
+ return None
|
||||
+
|
||||
+# Add the custom finder to sys.meta_path
|
||||
+sys.meta_path.insert(0, RedirectImportFinder())
|
||||
+
|
||||
+
|
||||
+# ======= END DYNAMO PATCH =======
|
||||
+
|
||||
import sys
|
||||
import time
|
||||
|
||||
import unittest
|
||||
from unittest import mock
|
||||
from test import support
|
||||
-from test.support.numbers import (
|
||||
- VALID_UNDERSCORE_LITERALS,
|
||||
- INVALID_UNDERSCORE_LITERALS,
|
||||
-)
|
||||
+
|
||||
+VALID_UNDERSCORE_LITERALS = [
|
||||
+ '0_0_0',
|
||||
+ '4_2',
|
||||
+ '1_0000_0000',
|
||||
+ '0b1001_0100',
|
||||
+ '0xffff_ffff',
|
||||
+ '0o5_7_7',
|
||||
+ '1_00_00.5',
|
||||
+ '1_00_00.5e5',
|
||||
+ '1_00_00e5_1',
|
||||
+ '1e1_0',
|
||||
+ '.1_4',
|
||||
+ '.1_4e1',
|
||||
+ '0b_0',
|
||||
+ '0x_f',
|
||||
+ '0o_5',
|
||||
+ '1_00_00j',
|
||||
+ '1_00_00.5j',
|
||||
+ '1_00_00e5_1j',
|
||||
+ '.1_4j',
|
||||
+ '(1_2.5+3_3j)',
|
||||
+ '(.5_6j)',
|
||||
+]
|
||||
+INVALID_UNDERSCORE_LITERALS = [
|
||||
+ # Trailing underscores:
|
||||
+ '0_',
|
||||
+ '42_',
|
||||
+ '1.4j_',
|
||||
+ '0x_',
|
||||
+ '0b1_',
|
||||
+ '0xf_',
|
||||
+ '0o5_',
|
||||
+ '0 if 1_Else 1',
|
||||
+ # Underscores in the base selector:
|
||||
+ '0_b0',
|
||||
+ '0_xf',
|
||||
+ '0_o5',
|
||||
+ # Old-style octal, still disallowed:
|
||||
+ '0_7',
|
||||
+ '09_99',
|
||||
+ # Multiple consecutive underscores:
|
||||
+ '4_______2',
|
||||
+ '0.1__4',
|
||||
+ '0.1__4j',
|
||||
+ '0b1001__0100',
|
||||
+ '0xffff__ffff',
|
||||
+ '0x___',
|
||||
+ '0o5__77',
|
||||
+ '1e1__0',
|
||||
+ '1e1__0j',
|
||||
+ # Underscore right before a dot:
|
||||
+ '1_.4',
|
||||
+ '1_.4j',
|
||||
+ # Underscore right after a dot:
|
||||
+ '1._4',
|
||||
+ '1._4j',
|
||||
+ '._5',
|
||||
+ '._5j',
|
||||
+ # Underscore right after a sign:
|
||||
+ '1.0e+_1',
|
||||
+ '1.0e+_1j',
|
||||
+ # Underscore right before j:
|
||||
+ '1.4_j',
|
||||
+ '1.4e5_j',
|
||||
+ # Underscore right before e:
|
||||
+ '1_e1',
|
||||
+ '1.4_e1',
|
||||
+ '1.4_e1j',
|
||||
+ # Underscore right after e:
|
||||
+ '1e_1',
|
||||
+ '1.4e_1',
|
||||
+ '1.4e_1j',
|
||||
+ # Complex cases with parens:
|
||||
+ '(1+1.5_j_)',
|
||||
+ '(1+1.5_j)',
|
||||
+]
|
||||
|
||||
try:
|
||||
import _pylong
|
||||
@@ -38,7 +162,7 @@ L = [
|
||||
class IntSubclass(int):
|
||||
pass
|
||||
|
||||
-class IntTestCases(unittest.TestCase):
|
||||
+class IntTestCases(__TestCase):
|
||||
|
||||
def test_basic(self):
|
||||
self.assertEqual(int(314), 314)
|
||||
@@ -607,7 +731,7 @@ class IntTestCases(unittest.TestCase):
|
||||
self.assertEqual(int('1_2_3_4_5_6_7', 32), 1144132807)
|
||||
|
||||
|
||||
-class IntStrDigitLimitsTests(unittest.TestCase):
|
||||
+class IntStrDigitLimitsTests(__TestCase):
|
||||
|
||||
int_class = int # Override this in subclasses to reuse the suite.
|
||||
|
||||
@@ -818,7 +942,7 @@ class IntSubclassStrDigitLimitsTests(IntStrDigitLimitsTests):
|
||||
int_class = IntSubclass
|
||||
|
||||
|
||||
-class PyLongModuleTests(unittest.TestCase):
|
||||
+class PyLongModuleTests(__TestCase):
|
||||
# Tests of the functions in _pylong.py. Those get used when the
|
||||
# number of digits in the input values are large enough.
|
||||
|
||||
@@ -922,4 +1046,4 @@ class PyLongModuleTests(unittest.TestCase):
|
||||
bits <<= 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
- unittest.main()
|
||||
+ run_tests()
|
1049
test/dynamo/cpython/3_13/test_int.py
Normal file
1049
test/dynamo/cpython/3_13/test_int.py
Normal file
File diff suppressed because it is too large
Load Diff
74
test/dynamo/cpython/3_13/test_int_literal.diff
Normal file
74
test/dynamo/cpython/3_13/test_int_literal.diff
Normal file
@ -0,0 +1,74 @@
|
||||
diff --git a/test/dynamo/cpython/3_13/test_int_literal.py b/test/dynamo/cpython/3_13/test_int_literal.py
|
||||
index bf725710d55..831d03666fb 100644
|
||||
--- a/test/dynamo/cpython/3_13/test_int_literal.py
|
||||
+++ b/test/dynamo/cpython/3_13/test_int_literal.py
|
||||
@@ -1,3 +1,54 @@
|
||||
+# ======= BEGIN Dynamo patch =======
|
||||
+# Owner(s): ["module: dynamo"]
|
||||
+
|
||||
+# ruff: noqa
|
||||
+# flake8: noqa
|
||||
+
|
||||
+import sys
|
||||
+import torch
|
||||
+import torch._dynamo.test_case
|
||||
+import unittest
|
||||
+from torch._dynamo.test_case import CPythonTestCase
|
||||
+from torch.testing._internal.common_utils import run_tests
|
||||
+
|
||||
+__TestCase = CPythonTestCase
|
||||
+
|
||||
+
|
||||
+# redirect import statements
|
||||
+import sys
|
||||
+import importlib.abc
|
||||
+
|
||||
+redirect_imports = (
|
||||
+ "test.mapping_tests",
|
||||
+ "test.typinganndata",
|
||||
+ "test.test_grammar",
|
||||
+ "test.test_math",
|
||||
+ "test.test_iter",
|
||||
+ "test.typinganndata.ann_module",
|
||||
+)
|
||||
+
|
||||
+class RedirectImportFinder(importlib.abc.MetaPathFinder):
|
||||
+ def find_spec(self, fullname, path, target=None):
|
||||
+ # Check if the import is the problematic one
|
||||
+ if fullname in redirect_imports:
|
||||
+ try:
|
||||
+ # Attempt to import the standalone module
|
||||
+ name = fullname.removeprefix("test.")
|
||||
+ r = importlib.import_module(name)
|
||||
+ # Redirect the module in sys.modules
|
||||
+ sys.modules[fullname] = r
|
||||
+ # Return a module spec from the found module
|
||||
+ return importlib.util.find_spec(name)
|
||||
+ except ImportError:
|
||||
+ return None
|
||||
+ return None
|
||||
+
|
||||
+# Add the custom finder to sys.meta_path
|
||||
+sys.meta_path.insert(0, RedirectImportFinder())
|
||||
+
|
||||
+
|
||||
+# ======= END DYNAMO PATCH =======
|
||||
+
|
||||
"""Test correct treatment of hex/oct constants.
|
||||
|
||||
This is complex because of changes due to PEP 237.
|
||||
@@ -5,7 +56,7 @@ This is complex because of changes due to PEP 237.
|
||||
|
||||
import unittest
|
||||
|
||||
-class TestHexOctBin(unittest.TestCase):
|
||||
+class TestHexOctBin(__TestCase):
|
||||
|
||||
def test_hex_baseline(self):
|
||||
# A few upper/lowercase tests
|
||||
@@ -140,4 +191,4 @@ class TestHexOctBin(unittest.TestCase):
|
||||
self.assertEqual(-0b1111111111111111111111111111111111111111111111111111111111111111, -18446744073709551615)
|
||||
|
||||
if __name__ == "__main__":
|
||||
- unittest.main()
|
||||
+ run_tests()
|
194
test/dynamo/cpython/3_13/test_int_literal.py
Normal file
194
test/dynamo/cpython/3_13/test_int_literal.py
Normal file
@ -0,0 +1,194 @@
|
||||
# ======= BEGIN Dynamo patch =======
|
||||
# Owner(s): ["module: dynamo"]
|
||||
|
||||
# ruff: noqa
|
||||
# flake8: noqa
|
||||
|
||||
import sys
|
||||
import torch
|
||||
import torch._dynamo.test_case
|
||||
import unittest
|
||||
from torch._dynamo.test_case import CPythonTestCase
|
||||
from torch.testing._internal.common_utils import run_tests
|
||||
|
||||
__TestCase = CPythonTestCase
|
||||
|
||||
|
||||
# redirect import statements
|
||||
import sys
|
||||
import importlib.abc
|
||||
|
||||
redirect_imports = (
|
||||
"test.mapping_tests",
|
||||
"test.typinganndata",
|
||||
"test.test_grammar",
|
||||
"test.test_math",
|
||||
"test.test_iter",
|
||||
"test.typinganndata.ann_module",
|
||||
)
|
||||
|
||||
class RedirectImportFinder(importlib.abc.MetaPathFinder):
|
||||
def find_spec(self, fullname, path, target=None):
|
||||
# Check if the import is the problematic one
|
||||
if fullname in redirect_imports:
|
||||
try:
|
||||
# Attempt to import the standalone module
|
||||
name = fullname.removeprefix("test.")
|
||||
r = importlib.import_module(name)
|
||||
# Redirect the module in sys.modules
|
||||
sys.modules[fullname] = r
|
||||
# Return a module spec from the found module
|
||||
return importlib.util.find_spec(name)
|
||||
except ImportError:
|
||||
return None
|
||||
return None
|
||||
|
||||
# Add the custom finder to sys.meta_path
|
||||
sys.meta_path.insert(0, RedirectImportFinder())
|
||||
|
||||
|
||||
# ======= END DYNAMO PATCH =======
|
||||
|
||||
"""Test correct treatment of hex/oct constants.
|
||||
|
||||
This is complex because of changes due to PEP 237.
|
||||
"""
|
||||
|
||||
import unittest
|
||||
|
||||
class TestHexOctBin(__TestCase):
|
||||
|
||||
def test_hex_baseline(self):
|
||||
# A few upper/lowercase tests
|
||||
self.assertEqual(0x0, 0X0)
|
||||
self.assertEqual(0x1, 0X1)
|
||||
self.assertEqual(0x123456789abcdef, 0X123456789abcdef)
|
||||
# Baseline tests
|
||||
self.assertEqual(0x0, 0)
|
||||
self.assertEqual(0x10, 16)
|
||||
self.assertEqual(0x7fffffff, 2147483647)
|
||||
self.assertEqual(0x7fffffffffffffff, 9223372036854775807)
|
||||
# Ditto with a minus sign and parentheses
|
||||
self.assertEqual(-(0x0), 0)
|
||||
self.assertEqual(-(0x10), -16)
|
||||
self.assertEqual(-(0x7fffffff), -2147483647)
|
||||
self.assertEqual(-(0x7fffffffffffffff), -9223372036854775807)
|
||||
# Ditto with a minus sign and NO parentheses
|
||||
self.assertEqual(-0x0, 0)
|
||||
self.assertEqual(-0x10, -16)
|
||||
self.assertEqual(-0x7fffffff, -2147483647)
|
||||
self.assertEqual(-0x7fffffffffffffff, -9223372036854775807)
|
||||
|
||||
def test_hex_unsigned(self):
|
||||
# Positive constants
|
||||
self.assertEqual(0x80000000, 2147483648)
|
||||
self.assertEqual(0xffffffff, 4294967295)
|
||||
# Ditto with a minus sign and parentheses
|
||||
self.assertEqual(-(0x80000000), -2147483648)
|
||||
self.assertEqual(-(0xffffffff), -4294967295)
|
||||
# Ditto with a minus sign and NO parentheses
|
||||
# This failed in Python 2.2 through 2.2.2 and in 2.3a1
|
||||
self.assertEqual(-0x80000000, -2147483648)
|
||||
self.assertEqual(-0xffffffff, -4294967295)
|
||||
|
||||
# Positive constants
|
||||
self.assertEqual(0x8000000000000000, 9223372036854775808)
|
||||
self.assertEqual(0xffffffffffffffff, 18446744073709551615)
|
||||
# Ditto with a minus sign and parentheses
|
||||
self.assertEqual(-(0x8000000000000000), -9223372036854775808)
|
||||
self.assertEqual(-(0xffffffffffffffff), -18446744073709551615)
|
||||
# Ditto with a minus sign and NO parentheses
|
||||
# This failed in Python 2.2 through 2.2.2 and in 2.3a1
|
||||
self.assertEqual(-0x8000000000000000, -9223372036854775808)
|
||||
self.assertEqual(-0xffffffffffffffff, -18446744073709551615)
|
||||
|
||||
def test_oct_baseline(self):
|
||||
# A few upper/lowercase tests
|
||||
self.assertEqual(0o0, 0O0)
|
||||
self.assertEqual(0o1, 0O1)
|
||||
self.assertEqual(0o1234567, 0O1234567)
|
||||
# Baseline tests
|
||||
self.assertEqual(0o0, 0)
|
||||
self.assertEqual(0o20, 16)
|
||||
self.assertEqual(0o17777777777, 2147483647)
|
||||
self.assertEqual(0o777777777777777777777, 9223372036854775807)
|
||||
# Ditto with a minus sign and parentheses
|
||||
self.assertEqual(-(0o0), 0)
|
||||
self.assertEqual(-(0o20), -16)
|
||||
self.assertEqual(-(0o17777777777), -2147483647)
|
||||
self.assertEqual(-(0o777777777777777777777), -9223372036854775807)
|
||||
# Ditto with a minus sign and NO parentheses
|
||||
self.assertEqual(-0o0, 0)
|
||||
self.assertEqual(-0o20, -16)
|
||||
self.assertEqual(-0o17777777777, -2147483647)
|
||||
self.assertEqual(-0o777777777777777777777, -9223372036854775807)
|
||||
|
||||
def test_oct_unsigned(self):
|
||||
# Positive constants
|
||||
self.assertEqual(0o20000000000, 2147483648)
|
||||
self.assertEqual(0o37777777777, 4294967295)
|
||||
# Ditto with a minus sign and parentheses
|
||||
self.assertEqual(-(0o20000000000), -2147483648)
|
||||
self.assertEqual(-(0o37777777777), -4294967295)
|
||||
# Ditto with a minus sign and NO parentheses
|
||||
# This failed in Python 2.2 through 2.2.2 and in 2.3a1
|
||||
self.assertEqual(-0o20000000000, -2147483648)
|
||||
self.assertEqual(-0o37777777777, -4294967295)
|
||||
|
||||
# Positive constants
|
||||
self.assertEqual(0o1000000000000000000000, 9223372036854775808)
|
||||
self.assertEqual(0o1777777777777777777777, 18446744073709551615)
|
||||
# Ditto with a minus sign and parentheses
|
||||
self.assertEqual(-(0o1000000000000000000000), -9223372036854775808)
|
||||
self.assertEqual(-(0o1777777777777777777777), -18446744073709551615)
|
||||
# Ditto with a minus sign and NO parentheses
|
||||
# This failed in Python 2.2 through 2.2.2 and in 2.3a1
|
||||
self.assertEqual(-0o1000000000000000000000, -9223372036854775808)
|
||||
self.assertEqual(-0o1777777777777777777777, -18446744073709551615)
|
||||
|
||||
def test_bin_baseline(self):
|
||||
# A few upper/lowercase tests
|
||||
self.assertEqual(0b0, 0B0)
|
||||
self.assertEqual(0b1, 0B1)
|
||||
self.assertEqual(0b10101010101, 0B10101010101)
|
||||
# Baseline tests
|
||||
self.assertEqual(0b0, 0)
|
||||
self.assertEqual(0b10000, 16)
|
||||
self.assertEqual(0b1111111111111111111111111111111, 2147483647)
|
||||
self.assertEqual(0b111111111111111111111111111111111111111111111111111111111111111, 9223372036854775807)
|
||||
# Ditto with a minus sign and parentheses
|
||||
self.assertEqual(-(0b0), 0)
|
||||
self.assertEqual(-(0b10000), -16)
|
||||
self.assertEqual(-(0b1111111111111111111111111111111), -2147483647)
|
||||
self.assertEqual(-(0b111111111111111111111111111111111111111111111111111111111111111), -9223372036854775807)
|
||||
# Ditto with a minus sign and NO parentheses
|
||||
self.assertEqual(-0b0, 0)
|
||||
self.assertEqual(-0b10000, -16)
|
||||
self.assertEqual(-0b1111111111111111111111111111111, -2147483647)
|
||||
self.assertEqual(-0b111111111111111111111111111111111111111111111111111111111111111, -9223372036854775807)
|
||||
|
||||
def test_bin_unsigned(self):
|
||||
# Positive constants
|
||||
self.assertEqual(0b10000000000000000000000000000000, 2147483648)
|
||||
self.assertEqual(0b11111111111111111111111111111111, 4294967295)
|
||||
# Ditto with a minus sign and parentheses
|
||||
self.assertEqual(-(0b10000000000000000000000000000000), -2147483648)
|
||||
self.assertEqual(-(0b11111111111111111111111111111111), -4294967295)
|
||||
# Ditto with a minus sign and NO parentheses
|
||||
# This failed in Python 2.2 through 2.2.2 and in 2.3a1
|
||||
self.assertEqual(-0b10000000000000000000000000000000, -2147483648)
|
||||
self.assertEqual(-0b11111111111111111111111111111111, -4294967295)
|
||||
|
||||
# Positive constants
|
||||
self.assertEqual(0b1000000000000000000000000000000000000000000000000000000000000000, 9223372036854775808)
|
||||
self.assertEqual(0b1111111111111111111111111111111111111111111111111111111111111111, 18446744073709551615)
|
||||
# Ditto with a minus sign and parentheses
|
||||
self.assertEqual(-(0b1000000000000000000000000000000000000000000000000000000000000000), -9223372036854775808)
|
||||
self.assertEqual(-(0b1111111111111111111111111111111111111111111111111111111111111111), -18446744073709551615)
|
||||
# Ditto with a minus sign and NO parentheses
|
||||
# This failed in Python 2.2 through 2.2.2 and in 2.3a1
|
||||
self.assertEqual(-0b1000000000000000000000000000000000000000000000000000000000000000, -9223372036854775808)
|
||||
self.assertEqual(-0b1111111111111111111111111111111111111111111111111111111111111111, -18446744073709551615)
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_tests()
|
Reference in New Issue
Block a user