Skip to content

Commit c8ff161

Browse files
alexmalyshevmeta-codesync[bot]
authored andcommitted
Fix Native Python incompatibilities in tests
Summary: Need to actually import it in some of these tests that use subprocesses. Rewrite the one native test that tries to dlopen _cinderx.so, that doesn't work with Native Python as there is no _cinderx.so. Reviewed By: DinoV Differential Revision: D86524535 fbshipit-source-id: 7f3c18e4fd4dcc652358ad55f2224aacdebf8ae3
1 parent 83dfa57 commit c8ff161

4 files changed

Lines changed: 23 additions & 44 deletions

File tree

cinderx/PythonLib/test_cinderx/test_cinderjit.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,8 +1636,7 @@ def junk{i}(j):
16361636
def run_test(
16371637
asserts_func: Callable[[list[str]], None], params: list[str]
16381638
) -> None:
1639-
# Disable the import of the site module with `-S` so Imports Monitor isn't enabled
1640-
args = [sys.executable, "-S"]
1639+
args = [sys.executable]
16411640
args.extend(params)
16421641
args.append("mod.py")
16431642
proc = subprocess.run(
@@ -1665,8 +1664,8 @@ def onek_asserts(actual_stdout: list[str]) -> None:
16651664
used_size = int(actual_stdout[1].split(" ")[1])
16661665
self.assertGreater(used_size, 1024)
16671666
# This is a bit fragile because it depends on what the initial 'zeroth'
1668-
# allocation is; we assume < 200K.
1669-
self.assertLess(used_size, 1024 * 200)
1667+
# allocation is; we assume < 600K.
1668+
self.assertLess(used_size, 1024 * 600)
16701669

16711670
# Run the zero-assert tests with JitAuto=1000 to test "normal" behavior
16721671
# where we compile some code but don't have any limits to trip.

cinderx/PythonLib/test_cinderx/test_compiler/test_static/native.py

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# Copyright (c) Meta Platforms, Inc. and affiliates.
2-
from .common import StaticTestBase
32

4-
import xxclassloader # usort: skip
5-
import _cinderx
3+
from .common import StaticTestBase
64

75

86
class NativeDecoratorTests(StaticTestBase):
@@ -256,48 +254,26 @@ def invoke_abs(i: int) -> int:
256254

257255
def test_invoke_native_fn_multiple_args(self) -> None:
258256
codestr = f"""
259-
from __static__ import native, int64, box
260-
from typing import Final
261-
262-
LIB_NAME: Final[str] = "{_cinderx.__file__}"
263-
264-
@native(LIB_NAME)
265-
def native_add(a: int64, b: int64) -> int64:
266-
pass
267-
268-
def invoke_add(i: int, j: int) -> int:
269-
k: int64 = int64(i)
270-
l: int64 = int64(j)
271-
return box(native_add(k, l))
272-
"""
273-
274-
with self.in_strict_module(codestr) as mod:
275-
self.assertEqual(mod.invoke_add(6, 5), 11)
276-
self.assertEqual(mod.invoke_add(-1, 5), 4)
277-
self.assertEqual(mod.invoke_add(-1, -5), -6)
278-
279-
def test_invoke_native_fn_heterogenous_args(self) -> None:
280-
codestr = f"""
281-
from __static__ import native, int64, uint8, box
257+
from __static__ import native, int32, box
282258
from typing import Final
283259
284-
LIB_NAME: Final[str] = "{_cinderx.__file__}"
260+
LIB_NAME: Final[str] = "libc.so.6"
285261
286262
@native(LIB_NAME)
287-
def native_sub(a: int64, b: uint8) -> int64:
263+
def div(a: int32, b: int32) -> int32:
264+
# This returns a div_t but that's okay as the ABI will put the
265+
# numerator in RAX.
266+
#
267+
# There are no other C stdlib functions that take multiple arguments
268+
# that are only integers. Everything else takes pointers or
269+
# doubles, and doubles aren't supported yet, TODO(T130985738).
288270
pass
289271
290-
def invoke_sub(i: int, j: int) -> int:
291-
k: int64 = int64(i)
292-
l: uint8 = uint8(j)
293-
return box(native_sub(k, l))
272+
def invoke_div(i: int32, j: int32) -> int:
273+
k: int32 = int32(i)
274+
l: int32 = int32(j)
275+
return box(div(k, l))
294276
"""
295277

296278
with self.in_strict_module(codestr) as mod:
297-
self.assertEqual(mod.invoke_sub(6, 5), 1)
298-
self.assertEqual(mod.invoke_sub(-1, 5), -6)
299-
self.assertEqual(mod.invoke_sub(-1, 0), -1)
300-
301-
with self.assertRaisesRegex(OverflowError, "int overflow"):
302-
# -1 can't be represented in uint8
303-
mod.invoke_sub(0, -1)
279+
self.assertEqual(mod.invoke_div(15, 3), 5)

cinderx/PythonLib/test_cinderx/test_enabling_parallel_gc.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
class TestEnablingParallelGc(unittest.TestCase):
1919
def test_gc_settings(self) -> None:
2020
codestr = textwrap.dedent("""
21+
import cinderx
22+
2123
import gc
2224
def g():
2325
return gc.get_threshold()

cinderx/PythonLib/test_cinderx/test_jitlist.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,9 @@ def test_precompile_all_bad_args(self) -> None:
253253
cinderx.jit.precompile_all(workers=200000)
254254

255255
def test_fail_on_parse_error_startup(self) -> None:
256-
code = 'print("Hello world!")'
256+
code = """if 1:
257+
import cinderx.jit
258+
print("Hello world!")"""
257259
jitlist = "OH NO"
258260
with tempfile.TemporaryDirectory() as tmp:
259261
dirpath = Path(tmp)

0 commit comments

Comments
 (0)