1
0
mirror of https://github.com/exaloop/codon.git synced 2025-06-03 15:03:52 +08:00
codon/test/python/pyext.py
A. R. Shajii 5613c1a84b
v0.16 (#335)
* Add Python extension lowering pass

* Add DocstringAttribute

* Add extension module codegen

* Handle different argument counts efficiently

* Add warnings to extension lowering

* Fix module name

* Fix extension codegen

* Fix argument check

* Auto-convert Codon exceptions to Python exceptions

* Fix #183

* Fix #162; Fix #135

* Fix #155

* Fix CPython interface in codegen

* Fix #191

* Fix #187

* Fix #189

* Generate object file in pyext mode

* Convert Codon exceptions to Python exceptions

* Fix vtable init; Fix failing tests on Linux

* Fix #190

* Fix #156

* Fix union routing

* Remove need for import python

* Automatic @export and wrapping for toplevel functions

* Reorganize API

* Add Python extension IR structs

* Add special calls for no-suspend yield-expr

* Add special calls for no-suspend yield-expr

* pyextension.h support [wip]

* pyextension.h support [wip]

* pyextension.h support

* pyextension.h support for toplevel functions

* clang-format

* Add PyFunction::nargs field

* Update pyextension codegen (WIP)

* SUpport nargs

* Add support for @pycapture

* PyType codegen (WIP)

* Py method codegen (WIP)

* Add type ptr hook

* Add getset codegen

* Add type alloc function

* Add type pointer hook codegen

* Re-organize codegen

* Add member codegen

* Update module init codegen

* Update module init codegen

* Add support for typePtrHook and new to/from_py hooks

* Fix extension codegen

* Fix init codegen

* Fix init codegen; add "tp_new" slot

* Fix type hook

* Add extra flags

* Specialized wrappers (PyType specs)

* Add static Python link option

* Fix C imports

* Add guards

* Remove unused field

* Python mode only when pyExt set

* Update python module

* Fix assert

* Update codegen/passes

* Fix tuple parsing in index expression

* Fix empty tuple unification

* Do not Cythonize underscore fns

* clang-format

* Fix switch

* Add Py support for cmp/setitem

* Add Py support for cmp/setitem

* Add type is support

* GetSet support

* clang-format

* GetSet support (fixes)

* Avoid useless vtable alloc

* Add iter support

* Fix size_t capture bug

* clang-format

* Fix POD type unification with tuples

* Add __try_from_py__ API

* Fix annotation

* Add static reflection methods (setattr; internal.static.*); refactor PyExt to python.codon; handle errors and kwargs in PyExt

* Python compat fixes

* Update Python object conversions

* Fix PyErrors

* clang-format; add copyright

* Add PyFunction::keywords field

* Fix JIT MRO handling; Refactor out Jupyter support

* Refactor out Jupyter support

* Add support for custom linking args (link=[]) to TOML plugins

* Fix tests

* Use g++ instead of gcc

* Fix Jupyter CMAKE

* Fix Jupyter CMAKE

* Add _PyArg_Parser definition

* Add complex64 type

* Add extra complex64 tests

* Fix Python calls; add staticenumerate

* Fix call

* Fix calls

* Update pyext wrappers

* Fix staticenumerate; Support static calls in tuple()

* Fix pyext routing

* Add add/mul for tuples

* clang-format

* Fix pyext codegen

* Fix wrap_multiple

* Add seq_alloc_atomic_uncollectable

* Fix default generics issue

* Add binary/ternary ops

* Fix missing generic issue

* Fix number slots

* Update pow

* Remove unnecessary pyobj

* Fix allocation

* Refactor errors

* Add test extension

* Fix formatting

* clang-format

* Fix getitem/setitem/delitem in pyext

* Fix pyext iterators

* Add builtin pow() (fix #294)

* Fix #244

* Fix #231

* Fix #229

* Fix #205

* Update docs

* Fix error message

* Add pyext tests

* Add pyext support for @property

* Add pyext support for toplevel fns and @tuple classes

* More pyext tests

* More pyext tests

* Fix file error checking

* More pyext tests

* Update pyext tests

* Update docs

* Add pyext test to CI

* Add pyext support for @tuple.__new__

* Add pyext support for @tuple.__new__

* Fix hetero-tuple issue with fn_overloads

* More pyext tests

* Bump versions

* Fix del magic in pyext

* Fix init magic for tuples in pyext

* Have test-pypi only run on develop branch

* Make exception type indices unnamed-addr

* Fix #316; Fix #317 (slash issue)

* Use uncollectible-alloc for vtable

* Fix #249

* Add pyext docs

* Fix #249; Fix clashing vtables; Fix super() and class_copy

* Add content-atomic type property instruction

* __contents_atomic__ support

* Update internal functions

* Use PIC when generating Python extension

* Cleanup

* Add Dockerfile & fix -fPIC

* Cleanup

* Fix setup.py

* Fix pyext fn iteration

* Fix CI

* clang-format

* Update long conversions in Py bridge

* Support wide-int to str conversions

* Fix test

* Add pow for arbitrary-width ints

* Fix Linux backtraces

* Cleanup

* Add more tests

* Fix docs; Remove tuple.__add__ for scalars

* Update docs

---------

Co-authored-by: Ibrahim Numanagić <ibrahimpasa@gmail.com>
2023-04-12 18:13:54 -04:00

418 lines
9.2 KiB
Python

import myext as m
import myext2 as m2
def equal(v, a, b, tag):
ok = (v.a == a and v.b == b and v.tag == tag)
if not ok:
print('GOT:', v.a, v.b, v.tag)
print('EXP:', a, b, tag)
return ok
saw_fun = False
saw_set = False
saw_foo = False
def test_codon_extensions(m):
m.reset()
# functions #
#############
global saw_fun
if hasattr(m, 'f1'):
assert m.f1(2.2, 3.3) == (2.2, 3.3)
assert m.f1(2.2, 3.3) == (2.2, 3.3)
assert m.f1(3.3) == (3.3, 2.22)
assert m.f1() == (1.11, 2.22)
assert m.f1(a=2.2, b=3.3) == (2.2, 3.3)
assert m.f1(2.2, b=3.3) == (2.2, 3.3)
assert m.f1(b=3.3, a=2.2) == (2.2, 3.3)
assert m.f1(a=2.2) == (2.2, 2.22)
assert m.f1(b=3.3) == (1.11, 3.3)
try:
m.f1(1.1, 2.2, 3.3)
except:
pass
else:
assert False
try:
m.f1(z=1)
except:
pass
else:
assert False
assert m.f2() == ({1: 'one'}, {2}, [3])
try:
m.f2(1)
except:
pass
else:
assert False
try:
m.f2(z=1, y=5)
except:
pass
else:
assert False
assert m.f3(42) == 84
assert m.f3(1.5) == 3.0
assert m.f3('x') == 'xx'
try:
m.f3(1, 2)
except:
pass
else:
assert False
try:
m.f3(a=1, b=2)
except:
pass
else:
assert False
assert m.f4() == ['f4()']
assert m.f4(2.2, 3.3) == (2.2, 3.3)
assert m.f4(3.3) == (3.3, 2.22)
assert m.f4(a=2.2, b=3.3) == (2.2, 3.3)
assert m.f4(2.2, b=3.3) == (2.2, 3.3)
assert m.f4(b=3.3, a=2.2) == (2.2, 3.3)
assert m.f4(a=2.2) == (2.2, 2.22)
assert m.f4(b=3.3) == (1.11, 3.3)
assert m.f4('foo') == ('foo', 'foo')
assert m.f4({1}) == {1}
assert m.f5() is None
assert equal(m.f6(1.9, 't'), 1.9, 1.9, 't')
saw_fun = True
# constructors #
################
x = m.Vec(3.14, 4.2, 'x')
y = m.Vec(100, 1000, tag='y')
z = m.Vec(b=2.2, a=1.1)
s = m.Vec(10)
t = m.Vec(b=11)
r = m.Vec(3, 4)
assert equal(x, 3.14, 4.2, 'x')
assert equal(y, 100, 1000, 'y')
assert equal(z, 1.1, 2.2, 'v0')
assert equal(s, 10, 0.0, 'v1')
assert equal(t, 0.0, 11, 'v2')
try:
m.Vec(tag=10, a=1, b=2)
except:
pass
else:
assert False
# to-str #
##########
assert str(x) == 'x: <3.14, 4.2>'
assert repr(x) == "Vec(3.14, 4.2, 'x')"
# methods #
###########
assert x.foo(2.2, 3.3) == (3.14, 2.2, 3.3)
assert y.foo(3.3) == (100, 3.3, 2.22)
assert z.foo() == (1.1, 1.11, 2.22)
assert x.foo(a=2.2, b=3.3) == (3.14, 2.2, 3.3)
assert x.foo(2.2, b=3.3) == (3.14, 2.2, 3.3)
assert x.foo(b=3.3, a=2.2) == (3.14, 2.2, 3.3)
assert x.foo(a=2.2) == (3.14, 2.2, 2.22)
assert x.foo(b=3.3) == (3.14, 1.11, 3.3)
try:
x.foo(1, a=1)
except:
pass
else:
assert False
try:
x.foo(1, 2, b=2)
except:
pass
else:
assert False
try:
x.foo(1, z=2)
except:
pass
else:
assert False
assert equal(x.bar(), 3.14, 4.2, 'x')
assert equal(y.bar(), 100, 1000, 'y')
assert equal(z.bar(), 1.1, 2.2, 'v0')
assert equal(s.bar(), 10, 0.0, 'v1')
assert equal(t.bar(), 0.0, 11, 'v2')
try:
x.bar(1)
except:
pass
else:
assert False
try:
x.bar(z=1)
except:
pass
else:
assert False
assert m.Vec.baz(2.2, 3.3) == (2.2, 3.3)
assert x.baz(2.2, 3.3) == (2.2, 3.3)
assert m.Vec.baz(3.3) == (3.3, 2.22)
assert m.Vec.baz() == (1.11, 2.22)
assert m.Vec.baz(a=2.2, b=3.3) == (2.2, 3.3)
assert m.Vec.baz(2.2, b=3.3) == (2.2, 3.3)
assert m.Vec.baz(b=3.3, a=2.2) == (2.2, 3.3)
assert m.Vec.baz(a=2.2) == (2.2, 2.22)
assert m.Vec.baz(b=3.3) == (1.11, 3.3)
try:
m.Vec.baz(1, a=1)
except:
pass
else:
assert False
try:
m.Vec.baz(1, 2, b=2)
except:
pass
else:
assert False
assert m.Vec.nop() == 'nop'
assert x.nop() == 'nop'
assert y.c == 1100
# fields #
##########
if hasattr(t, '__setitem__'):
t.a = 99
assert equal(t, 99, 11, 'v2')
t.tag = 't'
assert equal(t, 99, 11, 't')
# magics #
##########
assert equal(+y, 100, 1000, '(+y)')
assert equal(-y, -100, -1000, '(-y)')
assert equal(~y, -101, -1001, '(~y)')
assert abs(r) == 5.0
assert bool(y)
assert not bool(m.Vec())
assert len(x) == 1
assert len(x + y) == 5
assert hash(y) == 1100
assert equal(x + y, 103.14, 1004.2, '(x+y)')
try:
x + 'x'
except:
pass
else:
assert False
assert equal(x + y + y, 203.14, 2004.2, '((x+y)+y)')
assert equal(y + 50.5, 150.5, 1050.5, '(y+50.5)')
assert equal(y + 50, 150, 1050, '(y++50)')
# assert equal(50.5 + y, 150.5, 1050.5, '(y+50.5)') # support for r-magics?
assert equal(y - x, 96.86, 995.8, '(y-x)')
assert equal(y * 3.5, 350.0, 3500.0, '(y*3.5)')
assert equal(y // 3, 33, 333, '(y//3)')
assert equal(y / 2.5, 40.0, 400.0, '(y/2.5)')
try:
divmod(y, 1)
except ArithmeticError as e:
assert str(e) == 'no divmod'
else:
assert False
assert equal(y % 7, 2, 6, '(y%7)')
assert equal(y ** 2, 10000, 1000000, '(y**2)')
assert equal(y << 1, 200, 2000, '(y<<1)')
assert equal(y >> 2, 25, 250, '(y>>2)')
assert equal(y & 77, 68, 72, '(y&77)')
assert equal(y | 77, 109, 1005, '(y|77)')
assert equal(y ^ 77, 41, 933, '(y^77)')
assert y @ r == 4300
def dup(v):
return m.Vec(v.a, v.b, v.tag + '1')
y1 = dup(y)
y1 += x
assert equal(y1, 103.14, 1004.2, '(y1+=x)')
y1 = dup(y)
y1 += 1.5
assert equal(y1, 101.5, 1001.5, '(y1+=1.5)')
y1 = dup(y)
y1 -= x
assert equal(y1, 96.86, 995.8, '(y1-=x)')
y1 = dup(y)
y1 *= 3.5
assert equal(y1, 350.0, 3500.0, '(y1*=3.5)')
y1 = dup(y)
y1 //= 3
assert equal(y1, 33, 333, '(y1//=3)')
y1 = dup(y)
y1 /= 2.5
assert equal(y1, 40.0, 400.0, '(y1/=2.5)')
y1 = dup(y)
y1 %= 7
assert equal(y1, 2, 6, '(y1%=7)')
y1 = dup(y)
y1 **= 2
assert equal(y1, 10000, 1000000, '(y1**=2)')
y1 = dup(y)
y1 <<= 1
assert equal(y1, 200, 2000, '(y1<<=1)')
y1 = dup(y)
y1 >>= 2
assert equal(y1, 25, 250, '(y1>>=2)')
y1 = dup(y)
y1 &= 77
assert equal(y1, 68, 72, '(y1&=77)')
y1 = dup(y)
y1 |= 77
assert equal(y1, 109, 1005, '(y1|=77)')
y1 = dup(y)
y1 ^= 77
assert equal(y1, 41, 933, '(y1^=77)')
y1 = dup(y)
y1 @= 3.5
assert equal(y1, 350.0, 3500.0, '(y1@=3.5)')
assert equal(y(), 100, 1000, '(y())')
assert x(2.2, 3.3) == (3.14, 2.2, 3.3)
assert y(3.3) == (100, 3.3, 2.22)
assert x(a=2.2, b=3.3) == (3.14, 2.2, 3.3)
assert x(2.2, b=3.3) == (3.14, 2.2, 3.3)
assert x(b=3.3, a=2.2) == (3.14, 2.2, 3.3)
assert x(a=2.2) == (3.14, 2.2, 2.22)
assert x(b=3.3) == (3.14, 1.11, 3.3)
assert y('foo') == (100.0, 1000.0, 'foo')
assert x == x
assert x != y
assert r == m.Vec(3, 4, '?')
assert x < y
assert y > x
assert x <= y
assert y >= x
assert y <= y
assert x >= x
assert list(iter(x)) == ['x']
assert list(iter(x+y+y)) == list('((x+y)+y)')
assert 100 in y
assert 1000 in y
assert 100.5 not in y
assert 'y' in y
assert 'x' not in y
assert y[0] == 100
assert y[1] == 1000
assert y[11] == 1100
try:
y[-1]
except KeyError as e:
assert str(e) == "'bad vec key -1'"
else:
assert False
global saw_set
if hasattr(y, '__setitem__'):
y[0] = 99.9
assert equal(y, 99.9, 1000, 'y')
y[1] = -42.6
assert equal(y, 99.9, -42.6, 'y')
y[11] = 7.7
assert equal(y, 7.7, 7.7, 'y')
try:
y[2] = 1.2
except KeyError as e:
assert str(e) == "'bad vec key 2 with val 1.2'"
else:
assert False
del y[1]
assert equal(y, 7.7, 0.0, 'y')
saw_set = True
assert m.Vec.nd() > 0
# tuple classes #
#################
global saw_foo
if hasattr(m, 'Foo'):
x = m.Foo(list('hello'))
assert x.a == list('hello')
assert x.x == {s: i for i, s in enumerate('hello')}
assert x.hello() == 'x'
try:
x.a = ['bye']
except AttributeError:
pass
else:
assert False
assert int(x) == 42
assert float(x) == 3.14
assert x.__index__() == 99
saw_foo = True
# Codon-specific #
##################
def par_sum_check(n):
m = 0
for i in range(n):
m += 3*i + 7
return m
for n in (0, 1, 10, 33, 999, 1237):
assert m.par_sum(n) == par_sum_check(n)
for _ in range(3000):
test_codon_extensions(m)
test_codon_extensions(m2)
assert saw_fun
assert saw_set
assert saw_foo