Skip to content

Commit df9f2e9

Browse files
author
Ellery Newcomer
committed
begin impl for const awareness
for classes: store mutability in python/d mapping when calling member functions, be aware of mutability of function and check that mutability of object matches. todo: provide functionality for pyd to create immutable wrapped objects all of the above for struct pointers maybe store immutable data as immutable(void)* not void* ? check mutability of parameters/arguments for wrapped types on any function call fix issue 8 add license to top level. sure.
1 parent 8e52841 commit df9f2e9

15 files changed

+459
-78
lines changed

.hgignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
syntax: glob
22
*.pyc
33
*.o
4+
*.orig
45
*.a
56
*.so
67
*.x

LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2006 Kirk McDonald
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of
4+
this software and associated documentation files (the "Software"), to deal in
5+
the Software without restriction, including without limitation the rights to
6+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7+
of the Software, and to permit persons to whom the Software is furnished to do
8+
so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.
20+

examples/pyd_unittests/class_wrap.d

+5-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ shared static this() {
5656
ModuleName!"testing",
5757
Property!(Bizzy4.i),
5858
Repr!(Bizzy4.repr),
59+
Def!(Bizzy4.foo),
5960
Len!(),
6061
)();
6162
wrap_class!(Bizzy5,
@@ -215,6 +216,9 @@ class Bizzy4 {
215216
@property void i(int n) { _i = n; }
216217
@property size_t length() { return 5; }
217218
@property string repr() { return "cowabunga"; }
219+
220+
void foo(Bizzy4 other) {
221+
}
218222
}
219223

220224
class Bizzy5 {
@@ -231,14 +235,13 @@ class Bizzy5 {
231235
return format("<%s, %s, '%s'>", i,d,s);
232236
}
233237

234-
@property string b() const { return "abc"; }
238+
@property string b() { return "abc"; }
235239

236240
@property string c() { return "abc"; }
237241
@property void c(string _val) { }
238242

239243
@property void e(string _val) { }
240244

241-
242245
}
243246

244247
unittest {

examples/pyd_unittests/const.d

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import pyd.pyd, pyd.embedded;
2+
import std.exception;
3+
import std.stdio;
4+
5+
shared static this() {
6+
on_py_init({
7+
add_module!(ModuleName!"testing")();
8+
});
9+
py_init();
10+
on_py_init({
11+
wrap_class!(T1,
12+
ModuleName!"testing",
13+
Def!(T1.a),
14+
Def!(T1.b),
15+
Def!(T1.c),
16+
Def!(T1.d),
17+
Def!(T1.e),
18+
)();
19+
}, PyInitOrdering.After);
20+
21+
}
22+
23+
class T1 {
24+
int i;
25+
string s;
26+
27+
string a() immutable {
28+
return "abc";
29+
}
30+
31+
string b() const {
32+
return "def";
33+
}
34+
35+
void c(const(T1) t) {
36+
writeln(t.b());
37+
}
38+
void d(immutable(T1) t) {
39+
writeln(t.a());
40+
}
41+
void e(T1 t) {
42+
}
43+
}
44+
45+
unittest {
46+
InterpContext c = new InterpContext();
47+
c.py_stmts("from testing import *");
48+
c.py_stmts("boozy = T1()");
49+
assert(collectException!PythonException(c.py_eval!string("boozy.a()")));
50+
assert(c.py_eval!string("boozy.b()") == "def");
51+
}
52+
53+
unittest {
54+
immutable(T1) t = cast(immutable) new T1();
55+
56+
auto p = d_to_python(t);
57+
handle_exception();
58+
}
59+
60+
unittest {
61+
InterpContext c = new InterpContext();
62+
c.py_stmts("from testing import *");
63+
auto a = new T1();
64+
c.f = &a.e;
65+
c.a = a;
66+
c.py_stmts("a.e(a)");
67+
assert(false, "that should not have worked");
68+
}
69+
70+
void main() {}

examples/pyd_unittests/func_wrap.d

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ class Foo5{
4141
}
4242

4343
unittest {
44-
static assert(getparams!foo1 == "int i, int j");
45-
static assert(getparams!foo2.startsWith("int i, double j = 2"));
46-
static assert(getparams!foo3 == "...");
47-
static assert(getparams!foo4 == "int[] i...");
44+
static assert(getparams!(foo1,"A","B") == "A[0] i, A[1] j");
45+
static assert(getparams!(foo2, "A","B") == "A[0] i, A[1] j = B[1]");
46+
static assert(getparams!(foo3, "A","B") == "...");
47+
static assert(getparams!(foo4, "A","B") == "A[0] i...");
4848
auto fn = &call_ctor!(Foo2, Init!(int, double)).func;
4949
//pragma(msg, typeof(fn));
5050
//fn(1);

examples/pyd_unittests/makefile

+14-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,19 @@ PYTHON_3_2 = -version=Python_3_2_Or_Later \
2626
-version=Python_2_5_Or_Later \
2727
-version=Python_2_4_Or_Later \
2828
-L-lpython3.2mu
29+
PYTHON_3_3 = -version=Python_3_3_Or_Later \
30+
-version=Python_3_2_Or_Later \
31+
-version=Python_3_1_Or_Later \
32+
-version=Python_3_0_Or_Later \
33+
-version=Python_2_7_Or_Later \
34+
-version=Python_2_6_Or_Later \
35+
-version=Python_2_5_Or_Later \
36+
-version=Python_2_4_Or_Later \
37+
-L-lpython3.3m
2938
DC = dmd -unittest -property -debug -gc
3039
LDC = ldc2 -unittest -property -d-debug -gc -oq
3140

32-
all: pydobject.x pydobject3.x make_object.x make_object3.x embedded.x embedded3.x func_wrap.x func_wrap3.x class_wrap.x class_wrap3.x def.x def3.x struct_wrap.x threading.x
41+
all: pydobject.x pydobject3.x make_object.x make_object3.x embedded.x embedded3.x func_wrap.x func_wrap3.x class_wrap.x class_wrap3.x def.x def3.x struct_wrap.x threading.x const.x
3342
./pydobject.x
3443
./pydobject3.x
3544
./make_object.x
@@ -44,14 +53,15 @@ all: pydobject.x pydobject3.x make_object.x make_object3.x embedded.x embedded3.
4453
./def.x
4554
./def3.x
4655
./threading.x
56+
./const.x
4757

4858
clean:
4959
rm -f *.x
5060
rm -f *.o
5161

5262
%3.x: %3.d
53-
$(DC) $(PYTHON_3_2) $(PYD_FILES) $^ -of$@ -I$(PYD_DIR)
63+
$(DC) $(PYTHON_3_3) $(PYD_FILES) $^ -of$@ -I$(PYD_DIR)
5464
%.x: %.d
55-
#$(DC) $(PYTHON_2_7) $(PYD_FILES) $^ -of$@ -I$(PYD_DIR)
56-
$(LDC) $(LDC_PYTHON_2_7) $(PYD_FILES) $^ -of $@ -I$(PYD_DIR)
65+
$(DC) $(PYTHON_2_7) $(PYD_FILES) $^ -of$@ -I$(PYD_DIR)
66+
#$(LDC) $(LDC_PYTHON_2_7) $(PYD_FILES) $^ -of $@ -I$(PYD_DIR)
5767

examples/pyd_unittests/setup.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
projName = 'pyd_unittests'
1111
exts = [
12-
'pydobject','make_object','embedded','func_wrap','class_wrap','def','struct_wrap'
12+
'pydobject','make_object','embedded','func_wrap','class_wrap','def','struct_wrap', 'const'
1313
];
1414
string_imports = {
1515
'func_wrap': ["important_message.txt"]
@@ -29,6 +29,7 @@ def ext(e):
2929
Extension(ext(e), [ext(e)+".d"],
3030
d_unittest=True,
3131
build_deimos=True,
32+
d_lump=True,
3233
string_imports = string_imports.get(e, [])
3334
)
3435
for e in exts

examples/pyind/setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
version='1.0',
2424
ext_modules=[
2525
Extension(projName, srcs,
26-
build_deimos=True
26+
build_deimos=True, d_lump=True
2727
)
2828
],
2929
)

0 commit comments

Comments
 (0)