Skip to content

Commit 88d1285

Browse files
committed
added isinstance & issubclass with tests
1 parent baa0abc commit 88d1285

6 files changed

Lines changed: 284 additions & 2 deletions

File tree

pythonscript.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,50 @@ value = args[2];
412412
return set_attribute(object, attribute, value);
413413
}
414414

415+
var issubclass = function(args, kwargs) {
416+
var C, B, base;
417+
C = args[0];
418+
B = args[1];
419+
if(C === B) {
420+
return true;
421+
}
422+
else {
423+
424+
}
425+
426+
var iter = jsrange(C.bases.length);
427+
for (var index=0; index < iter.length; index++) {
428+
var backup = index;
429+
index = iter[index];
430+
base = C.bases[index];
431+
if(issubclass([base, B], {})) {
432+
return true;
433+
}
434+
else {
435+
436+
}
437+
438+
index = backup;
439+
}
440+
441+
return false;
442+
}
443+
444+
var isinstance = function(args, kwargs) {
445+
var object_class, object, klass;
446+
object = args[0];
447+
klass = args[1];
448+
object_class = object.__class__;
449+
if(object_class === undefined) {
450+
return false;
451+
}
452+
else {
453+
454+
}
455+
456+
return issubclass(create_array(object_class, klass));
457+
}
458+
415459
var range = function(args, kwargs) {
416460
var signature = {"kwargs": {}, "args": create_array("num")};
417461
var arguments = get_arguments(signature, args, kwargs);

pythonscript/pythonjs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,9 @@ def visit_For(self, node):
208208
# iter is the python iterator
209209
init_iter = 'var iter = %s;\n' % iter
210210
# backup iterator and affect value of the next element to the target
211-
pre = 'var backup = %s;\ni = iter[%s];\n' % (target, target)
211+
pre = 'var backup = %s;\n%s = iter[%s];\n' % (target, target, target)
212212
# replace the replace target with the javascript iterator
213-
post = 'i = backup;\n'
213+
post = '%s = backup;\n' % target
214214
body = '\n'.join(map(self.visit, node.body)) + '\n'
215215
body = pre + body + post
216216
for_block = init_iter + 'for (var %s=0; %s < iter.length; %s++) {\n%s}\n' % (target, target, target, body)

pythonscript/pythonpythonjs.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,26 @@ def setattr(args, kwargs):
225225
attribute = JS('args[1]')
226226
value = JS('args[2]')
227227
return set_attribute(object, attribute, value)
228+
229+
230+
def issubclass(args, kwargs):
231+
var(C, B, base)
232+
C = JS('args[0]')
233+
B = JS('args[1]')
234+
if C is B:
235+
return True
236+
for index in jsrange(C.bases.length):
237+
base = JS('C.bases[index]')
238+
if issubclass(JS('[base, B]'), JS('{}')):
239+
return True
240+
return False
241+
242+
243+
def isinstance(args, kwargs):
244+
var(object_class, object, klass)
245+
object = JS('args[0]')
246+
klass = JS('args[1]')
247+
object_class = object.__class__
248+
if object_class is None:
249+
return False
250+
return issubclass(create_array(object_class, klass))

tests.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<h1>PythonScript Tests</h1>
2+
<p>Please hit <code>Ctrl+Shift+J</code></p>
3+
<script src="pythonscript.js" type="text/javascript" charset="utf-8"></script>
4+
<script src="tests.py.js" type="text/javascript" charset="utf-8"></script>

tests.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
def test_equal(a, b, message):
2+
if a == b:
3+
print message
4+
else:
5+
print message, 'failed'
6+
7+
8+
def test_true(a, message):
9+
if a:
10+
print message
11+
else:
12+
print message, 'failed'
13+
14+
15+
def test_false(a, message):
16+
if not a:
17+
print message
18+
else:
19+
print message, 'failed'
20+
21+
22+
tests = list()
23+
24+
25+
def test_issubclass():
26+
class A:
27+
pass
28+
29+
class B(A):
30+
pass
31+
32+
class C(B):
33+
pass
34+
35+
class D:
36+
pass
37+
38+
class E(C, D):
39+
pass
40+
41+
test_true(issubclass(C, C), 'C is a subclass of C')
42+
test_true(issubclass(C, B), 'C is a subclass of B')
43+
test_true(issubclass(C, A), 'C is a subclass of A')
44+
test_true(issubclass(B, B), 'B is a subclass of B')
45+
test_true(issubclass(B, A), 'B is a subclass of A')
46+
test_true(issubclass(A, A), 'A is a subclass of A')
47+
48+
test_false(issubclass(A, B), 'A is not a subclass of B')
49+
test_false(issubclass(B, C), 'B is not a subclass of C')
50+
51+
test_false(issubclass(D, A), 'D is not a subclass of A')
52+
test_false(issubclass(D, C), 'D is not a subclass of C')
53+
54+
test_true(issubclass(E, E), 'E is subclass of E')
55+
test_true(issubclass(E, D), 'E is subclass of D')
56+
test_true(issubclass(E, C), 'E is subclass of C')
57+
test_true(issubclass(E, B), 'E is subclass of B')
58+
test_true(issubclass(E, A), 'E is subclass of A')
59+
60+
tests.append(test_issubclass)
61+
62+
63+
def test_isinstance():
64+
class A:
65+
pass
66+
67+
class B(A):
68+
pass
69+
70+
class X:
71+
pass
72+
73+
class Y(X):
74+
pass
75+
76+
test_true(isinstance(A(), A), 'A() is an instance of A')
77+
test_true(isinstance(B(), A), 'B() is an instance of A')
78+
test_true(isinstance(B(), A), 'B() is an instance of B')
79+
test_false(isinstance(B, B), 'B is not an instance of B')
80+
test_false(isinstance(B, A), 'B is not an instance of A')
81+
test_false(isinstance(B(), X), 'B() is not an instance of X')
82+
test_false(isinstance(B(), Y), 'B() is not an instance of Y')
83+
84+
85+
tests.append(test_isinstance)
86+
87+
88+
for test in tests:
89+
test()

tests.py.js

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
var test_equal = function(args, kwargs) {
2+
var signature = {"kwargs": {}, "args": create_array("a", "b", "message")};
3+
var arguments = get_arguments(signature, args, kwargs);
4+
var a = arguments["a"];
5+
var b = arguments["b"];
6+
var message = arguments["message"];
7+
if(a == b) {
8+
console.log(message);
9+
}
10+
else {
11+
console.log(message, "failed");
12+
}
13+
14+
}
15+
16+
var test_true = function(args, kwargs) {
17+
var signature = {"kwargs": {}, "args": create_array("a", "message")};
18+
var arguments = get_arguments(signature, args, kwargs);
19+
var a = arguments["a"];
20+
var message = arguments["message"];
21+
if(a) {
22+
console.log(message);
23+
}
24+
else {
25+
console.log(message, "failed");
26+
}
27+
28+
}
29+
30+
var test_false = function(args, kwargs) {
31+
var signature = {"kwargs": {}, "args": create_array("a", "message")};
32+
var arguments = get_arguments(signature, args, kwargs);
33+
var a = arguments["a"];
34+
var message = arguments["message"];
35+
if(!a) {
36+
console.log(message);
37+
}
38+
else {
39+
console.log(message, "failed");
40+
}
41+
42+
}
43+
44+
tests = get_attribute(list, "__call__")(create_array(), {});
45+
var test_issubclass = function(args, kwargs) {
46+
var signature = {"kwargs": {}, "args": create_array()};
47+
var arguments = get_arguments(signature, args, kwargs);
48+
A = {};
49+
parents = create_array();
50+
A = create_class("A", parents, A);
51+
B = {};
52+
parents = create_array();
53+
parents.push(A);
54+
B = create_class("B", parents, B);
55+
C = {};
56+
parents = create_array();
57+
parents.push(B);
58+
C = create_class("C", parents, C);
59+
D = {};
60+
parents = create_array();
61+
D = create_class("D", parents, D);
62+
E = {};
63+
parents = create_array();
64+
parents.push(C, D);
65+
E = create_class("E", parents, E);
66+
get_attribute(test_true, "__call__")(create_array(get_attribute(issubclass, "__call__")(create_array(C, C), {}), "C is a subclass of C"), {});
67+
get_attribute(test_true, "__call__")(create_array(get_attribute(issubclass, "__call__")(create_array(C, B), {}), "C is a subclass of B"), {});
68+
get_attribute(test_true, "__call__")(create_array(get_attribute(issubclass, "__call__")(create_array(C, A), {}), "C is a subclass of A"), {});
69+
get_attribute(test_true, "__call__")(create_array(get_attribute(issubclass, "__call__")(create_array(B, B), {}), "B is a subclass of B"), {});
70+
get_attribute(test_true, "__call__")(create_array(get_attribute(issubclass, "__call__")(create_array(B, A), {}), "B is a subclass of A"), {});
71+
get_attribute(test_true, "__call__")(create_array(get_attribute(issubclass, "__call__")(create_array(A, A), {}), "A is a subclass of A"), {});
72+
get_attribute(test_false, "__call__")(create_array(get_attribute(issubclass, "__call__")(create_array(A, B), {}), "A is not a subclass of B"), {});
73+
get_attribute(test_false, "__call__")(create_array(get_attribute(issubclass, "__call__")(create_array(B, C), {}), "B is not a subclass of C"), {});
74+
get_attribute(test_false, "__call__")(create_array(get_attribute(issubclass, "__call__")(create_array(D, A), {}), "D is not a subclass of A"), {});
75+
get_attribute(test_false, "__call__")(create_array(get_attribute(issubclass, "__call__")(create_array(D, C), {}), "D is not a subclass of C"), {});
76+
get_attribute(test_true, "__call__")(create_array(get_attribute(issubclass, "__call__")(create_array(E, E), {}), "E is subclass of E"), {});
77+
get_attribute(test_true, "__call__")(create_array(get_attribute(issubclass, "__call__")(create_array(E, D), {}), "E is subclass of D"), {});
78+
get_attribute(test_true, "__call__")(create_array(get_attribute(issubclass, "__call__")(create_array(E, C), {}), "E is subclass of C"), {});
79+
get_attribute(test_true, "__call__")(create_array(get_attribute(issubclass, "__call__")(create_array(E, B), {}), "E is subclass of B"), {});
80+
get_attribute(test_true, "__call__")(create_array(get_attribute(issubclass, "__call__")(create_array(E, A), {}), "E is subclass of A"), {});
81+
}
82+
83+
get_attribute(get_attribute(tests, "append"), "__call__")(create_array(test_issubclass), {});
84+
var test_isinstance = function(args, kwargs) {
85+
var signature = {"kwargs": {}, "args": create_array()};
86+
var arguments = get_arguments(signature, args, kwargs);
87+
A = {};
88+
parents = create_array();
89+
A = create_class("A", parents, A);
90+
B = {};
91+
parents = create_array();
92+
parents.push(A);
93+
B = create_class("B", parents, B);
94+
X = {};
95+
parents = create_array();
96+
X = create_class("X", parents, X);
97+
Y = {};
98+
parents = create_array();
99+
parents.push(X);
100+
Y = create_class("Y", parents, Y);
101+
get_attribute(test_true, "__call__")(create_array(get_attribute(isinstance, "__call__")(create_array(get_attribute(A, "__call__")(create_array(), {}), A), {}), "A() is an instance of A"), {});
102+
get_attribute(test_true, "__call__")(create_array(get_attribute(isinstance, "__call__")(create_array(get_attribute(B, "__call__")(create_array(), {}), A), {}), "B() is an instance of A"), {});
103+
get_attribute(test_true, "__call__")(create_array(get_attribute(isinstance, "__call__")(create_array(get_attribute(B, "__call__")(create_array(), {}), A), {}), "B() is an instance of B"), {});
104+
get_attribute(test_false, "__call__")(create_array(get_attribute(isinstance, "__call__")(create_array(B, B), {}), "B is not an instance of B"), {});
105+
get_attribute(test_false, "__call__")(create_array(get_attribute(isinstance, "__call__")(create_array(B, A), {}), "B is not an instance of A"), {});
106+
get_attribute(test_false, "__call__")(create_array(get_attribute(isinstance, "__call__")(create_array(get_attribute(B, "__call__")(create_array(), {}), X), {}), "B() is not an instance of X"), {});
107+
get_attribute(test_false, "__call__")(create_array(get_attribute(isinstance, "__call__")(create_array(get_attribute(B, "__call__")(create_array(), {}), Y), {}), "B() is not an instance of Y"), {});
108+
}
109+
110+
get_attribute(get_attribute(tests, "append"), "__call__")(create_array(test_isinstance), {});
111+
var __iterator__ = get_attribute(tests, "__iter__")(create_array(), {});
112+
try {
113+
var test = get_attribute(__iterator__, "next")(create_array(), {});
114+
while(true) {
115+
get_attribute(test, "__call__")(create_array(), {});
116+
var test = get_attribute(__iterator__, "next")(create_array(), {});
117+
}
118+
}
119+
catch(__exception__) {
120+
121+
}
122+

0 commit comments

Comments
 (0)