Skip to content

Commit

Permalink
Basic inheritance support.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.dsource.org/projects/pyd/trunk@56 1df65b71-e716-0410-9316-ac55df2b1602
  • Loading branch information
KirkMcDonald authored and KirkMcDonald committed Dec 12, 2006
1 parent 0021192 commit bcee75c
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
40 changes: 40 additions & 0 deletions examples/inherit/inherit.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module inherit;

import pyd.pyd;
import std.stdio;

class Base {
void foo() {
writefln("Base.foo");
}
void bar() {
writefln("Base.bar");
}
}

class Derived : Base {
void foo() {
writefln("Derived.foo");
}
}

void call_poly(Base b) {
writef("call_poly: ");
b.foo();
}

extern(C)
export void initinherit() {
def!(call_poly);

module_init("inherit");

wrapped_class!(Base) b;
b.def!(Base.foo);
b.def!(Base.bar);
finalize_class(b);

wrapped_class!(Derived) d;
d.def!(Derived.foo);
finalize_class(d);
}
10 changes: 10 additions & 0 deletions examples/inherit/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
This example illustrates the basic automatic inheritance support provided by
Pyd.

Execute the conventional distutils command
python setup.py build
to build.

Then execute
python test.py
to exercise this example extension.
11 changes: 11 additions & 0 deletions examples/inherit/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from celerid.support import setup, Extension

projName = 'inherit'

setup(
name=projName,
version='0.1',
ext_modules=[
Extension(projName, ['inherit.d'])
],
)
34 changes: 34 additions & 0 deletions examples/inherit/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os.path, sys
import distutils.util

# Append the directory in which the binaries were placed to Python's sys.path,
# then import the D DLL.
libDir = os.path.join('build', 'lib.%s-%s' % (
distutils.util.get_platform(),
'.'.join(str(v) for v in sys.version_info[:2])
))
sys.path.append(os.path.abspath(libDir))

import inherit

b = inherit.Base()
d = inherit.Derived()

b.foo()
b.bar()
d.foo()
d.bar()

print "issubclass(inherit.Derived, inherit.Base)"
print issubclass(inherit.Derived, inherit.Base)

inherit.call_poly(b)
inherit.call_poly(d)

class PyClass(inherit.Derived):
def foo(self):
print 'PyClass.foo'

p = PyClass()
print "The basic inheritance support breaks down here:"
inherit.call_poly(p)
11 changes: 11 additions & 0 deletions infrastructure/pyd/class_wrap.d
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,17 @@ void finalize_class(CLS) (CLS cls, char[] modulename="") {
type.tp_methods = wrapped_method_list!(T).ptr;
type.tp_name = (module_name ~ "." ~ name ~ \0).ptr;

// Check for wrapped parent classes
static if (is(T B == super)) {
foreach (C; B) {
static if (is(C == class) && !is(C == Object)) {
if (is_wrapped!(C)) {
type.tp_base = &wrapped_class_type!(C);
}
}
}
}

// Numerical operator overloads
if (wrapped_class_as_number!(T) != PyNumberMethods.init) {
type.tp_as_number = &wrapped_class_as_number!(T);
Expand Down

0 comments on commit bcee75c

Please sign in to comment.