[java2python] r172 committed - makefile and test dir cleanup.

4 views
Skip to first unread message

codesite...@google.com

unread,
Jul 28, 2010, 4:50:43 AM7/28/10
to java2pyth...@googlegroups.com
Revision: 172
Author: troy.melhase
Date: Wed Jul 28 01:49:35 2010
Log: makefile and test dir cleanup.
http://code.google.com/p/java2python/source/detail?r=172

Added:
/branches/0.5/test/Package1
/branches/0.5/test/Package1/Class1.java
/branches/0.5/test/Package1/Class1.py
/branches/0.5/test/PackageUse1.java
/branches/0.5/test/configs
/branches/0.5/test/configs/Class0.py
/branches/0.5/test/configs/__init__.py
/branches/0.5/test/configs/overloading.py
Deleted:
/branches/0.5/test/PackageUse.java
/branches/0.5/test/compare_outputs.sh
/branches/0.5/test/overloading.py
/branches/0.5/test/testconfig.py
Modified:
/branches/0.5/test
/branches/0.5/test/Makefile

=======================================
--- /dev/null
+++ /branches/0.5/test/Package1/Class1.java Wed Jul 28 01:49:35 2010
@@ -0,0 +1,5 @@
+class Class1 {
+ public int m() {
+ return 42;
+ }
+}
=======================================
--- /dev/null
+++ /branches/0.5/test/Package1/Class1.py Wed Jul 28 01:49:35 2010
@@ -0,0 +1,14 @@
+#!/usr/bin/env python
+""" generated source for module Class1
+
+"""
+class Class1(object):
+ """ generated source for class Class1
+
+ """
+ def m(self):
+ """ generated source for method m
+
+ """
+ return 42
+
=======================================
--- /dev/null
+++ /branches/0.5/test/PackageUse1.java Wed Jul 28 01:49:35 2010
@@ -0,0 +1,8 @@
+import Package1.*;
+
+class PackageUse1 {
+ public static void main(String[] args) {
+ Package1.Class1 c = new Package1.Class1();
+ System.out.println( c.m() );
+ }
+}
=======================================
--- /dev/null
+++ /branches/0.5/test/configs/Class0.py Wed Jul 28 01:49:35 2010
@@ -0,0 +1,2 @@
+modulePrologueHandlers = []
+moduleEpilogueHandlers = []
=======================================
--- /dev/null
+++ /branches/0.5/test/configs/__init__.py Wed Jul 28 01:49:35 2010
@@ -0,0 +1,29 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+from java2python.config import default
+
+
+modulePrologueHandlers = default.modulePrologueHandlers + [
+ 'from configs.overloading import overloaded',
+]
+
+
+outputSubs = [
+ (r'(.*?)\.getMessage\(\)', r'\1.message'),
+ (r'(.*?)(\w+?)\.length(.*)', r'\1len(\2)\3'),
+ (r'assertEquals', 'self.assertEquals'),
+ (r'## import junit.framework', 'from unittest import *'),
+ (r'(.*?)\.getClass\(\)', r'\1.__class__'),
+ (r'(.*?)\.getName\(\)', r'\1.__name__'),
+ (r'(.*?)\.getInterfaces\(\)', r'\1.__bases__'),
+
+ (r'(.*?)\.fooProp\(\)', r'\1.fooProp'),
+ (r'(.*?)\.fooProp\((.+)\)', r'\1.fooProp = \2'),
+
+ (r'(.*?)lock(.*?) = object\(\)', r'\1lock\2 = Lock()'),
+
+ ## these two fudge something that should be handled in parsing
+ (r'(.*?)StaticInner\(\)', r'\1cls.StaticInner()'),
+ (r'(.*?)outer\.cls', r'\1outer'),
+ (r'from java\.util import \*', ''),
+ ]
=======================================
--- /dev/null
+++ /branches/0.5/test/configs/overloading.py Wed Jul 28 01:49:35 2010
@@ -0,0 +1,146 @@
+#!/usr/bin/env python2.5
+
+##
+# Dynamically overloaded functions.
+#
+# This is an implementation of (dynamically, or run-time) overloaded
+# functions; also known as generic functions or multi-methods.
+#
+# This module is from Python SVN,
+# http://svn.python.org/view/sandbox/trunk/overload/overloading.py
+##
+
+"""Dynamically overloaded functions.
+
+This is an implementation of (dynamically, or run-time) overloaded
+functions; also known as generic functions or multi-methods.
+
+The dispatch algorithm uses the types of all argument for dispatch,
+similar to (compile-time) overloaded functions or methods in C++ and
+Java.
+
+Most of the complexity in the algorithm comes from the need to support
+subclasses in call signatures. For example, if an function is
+registered for a signature (T1, T2), then a call with a signature (S1,
+S2) is acceptable, assuming that S1 is a subclass of T1, S2 a subclass
+of T2, and there are no other more specific matches (see below).
+
+If there are multiple matches and one of those doesn't *dominate* all
+others, the match is deemed ambiguous and an exception is raised. A
+subtlety here: if, after removing the dominated matches, there are
+still multiple matches left, but they all map to the same function,
+then the match is not deemed ambiguous and that function is used.
+Read the method find_func() below for details.
+
+Python 2.5 is required due to the use of predicates any() and all().
+
+"""
+
+import new
+
+# Make the environment more like Python 3.0
+__metaclass__ = type
+from itertools import izip as zip
+
+
+class overloaded:
+ """An implementation of overloaded functions."""
+
+ def __init__(self, default_func):
+ # Decorator to declare new overloaded function.
+ self.registry = {}
+ self.cache = {}
+ self.default_func = default_func
+
+ def __get__(self, obj, type=None):
+ if obj is None:
+ return self
+ return new.instancemethod(self, obj)
+
+ def register(self, *types):
+ """Decorator to register an implementation for a specific set of
types.
+
+ .register(t1, t2)(f) is equivalent to .register_func((t1, t2), f).
+
+ """
+ def helper(func):
+ self.register_func(types, func)
+ return func
+ return helper
+
+ def register_func(self, types, func):
+ """Helper to register an implementation."""
+ self.registry[tuple(types)] = func
+ self.cache = {} # Clear the cache (later we can optimize this).
+
+ def __call__(self, *args):
+ """Call the overloaded function."""
+ types = tuple(map(type, args))
+ func = self.cache.get(types)
+ if func is None:
+ self.cache[types] = func = self.find_func(types)
+ return func(*args)
+
+ def find_func(self, types):
+ """Find the appropriate overloaded function; don't call it.
+
+ This won't work for old-style classes or classes without __mro__.
+
+ """
+ func = self.registry.get(types)
+ if func is not None:
+ # Easy case -- direct hit in registry.
+ return func
+
+ # XXX Phillip Eby suggests to use issubclass() instead of __mro__.
+ # There are advantages and disadvantages.
+
+ # I can't help myself -- this is going to be intense functional
code.
+ # Find all possible candidate signatures.
+ mros = tuple(t.__mro__ for t in types)
+ n = len(mros)
+ candidates = [sig for sig in self.registry
+ if len(sig) == n and
+ all(t in mro for t, mro in zip(sig, mros))]
+ if not candidates:
+ # No match at all -- use the default function.
+ return self.default_func
+ if len(candidates) == 1:
+ # Unique match -- that's an easy case.
+ return self.registry[candidates[0]]
+
+ # More than one match -- weed out the subordinate ones.
+
+ def dominates(dom, sub,
+ orders=tuple(dict((t, i) for i, t in enumerate(mro))
+ for mro in mros)):
+ # Predicate to decide whether dom strictly dominates sub.
+ # Strict domination is defined as domination without equality.
+ # The arguments dom and sub are type tuples of equal length.
+ # The orders argument is a precomputed auxiliary data structure
+ # giving dicts of ordering information corresponding to the
+ # positions in the type tuples.
+ # A type d dominates a type s iff order[d] <= order[s].
+ # A type tuple (d1, d2, ...) dominates a type tuple of equal
length
+ # (s1, s2, ...) iff d1 dominates s1, d2 dominates s2, etc.
+ if dom is sub:
+ return False
+ return all(order[d] <= order[s]
+ for d, s, order in zip(dom, sub, orders))
+
+ # I suppose I could inline dominates() but it wouldn't get any
clearer.
+ candidates = [cand
+ for cand in candidates
+ if not any(dominates(dom, cand) for dom in
candidates)]
+ if len(candidates) == 1:
+ # There's exactly one candidate left.
+ return self.registry[candidates[0]]
+
+ # Perhaps these multiple candidates all have the same
implementation?
+ funcs = set(self.registry[cand] for cand in candidates)
+ if len(funcs) == 1:
+ return funcs.pop()
+
+ # No, the situation is irreducibly ambiguous.
+ raise TypeError("ambigous call; types=%r; candidates=%r" %
+ (types, candidates))
=======================================
--- /branches/0.5/test/PackageUse.java Thu Jul 1 15:12:49 2010
+++ /dev/null
@@ -1,8 +0,0 @@
-import Package1.*;
-
-class PackageUse {
- public static void main(String[] args) {
- Package1.Class1 c = new Package1.Class1();
- System.out.println( c.m() );
- }
-}
=======================================
--- /branches/0.5/test/compare_outputs.sh Thu Jul 1 15:12:49 2010
+++ /dev/null
@@ -1,18 +0,0 @@
-#!/bin/bash
-## small script to compare the output of two commands. should move this
to the makefile.
-o1=`$1`
-r1=$?
-
-o2=`$2`
-r2=$?
-
-let "e = $r2 | $r1"
-
-if [[ "$o1" == "$o2" && $e == 0 ]]
-then
- echo "[OKAY]" $3
- exit 0
-else
- echo "[FAIL]" $3
- exit 1
-fi
=======================================
--- /branches/0.5/test/overloading.py Thu Jul 1 15:12:49 2010
+++ /dev/null
@@ -1,146 +0,0 @@
-#!/usr/bin/env python2.5
-
-##
-# Dynamically overloaded functions.
-#
-# This is an implementation of (dynamically, or run-time) overloaded
-# functions; also known as generic functions or multi-methods.
-#
-# This module is from Python SVN,
-# http://svn.python.org/view/sandbox/trunk/overload/overloading.py
-##
-
-"""Dynamically overloaded functions.
-
-This is an implementation of (dynamically, or run-time) overloaded
-functions; also known as generic functions or multi-methods.
-
-The dispatch algorithm uses the types of all argument for dispatch,
-similar to (compile-time) overloaded functions or methods in C++ and
-Java.
-
-Most of the complexity in the algorithm comes from the need to support
-subclasses in call signatures. For example, if an function is
-registered for a signature (T1, T2), then a call with a signature (S1,
-S2) is acceptable, assuming that S1 is a subclass of T1, S2 a subclass
-of T2, and there are no other more specific matches (see below).
-
-If there are multiple matches and one of those doesn't *dominate* all
-others, the match is deemed ambiguous and an exception is raised. A
-subtlety here: if, after removing the dominated matches, there are
-still multiple matches left, but they all map to the same function,
-then the match is not deemed ambiguous and that function is used.
-Read the method find_func() below for details.
-
-Python 2.5 is required due to the use of predicates any() and all().
-
-"""
-
-import new
-
-# Make the environment more like Python 3.0
-__metaclass__ = type
-from itertools import izip as zip
-
-
-class overloaded:
- """An implementation of overloaded functions."""
-
- def __init__(self, default_func):
- # Decorator to declare new overloaded function.
- self.registry = {}
- self.cache = {}
- self.default_func = default_func
-
- def __get__(self, obj, type=None):
- if obj is None:
- return self
- return new.instancemethod(self, obj)
-
- def register(self, *types):
- """Decorator to register an implementation for a specific set of
types.
-
- .register(t1, t2)(f) is equivalent to .register_func((t1, t2), f).
-
- """
- def helper(func):
- self.register_func(types, func)
- return func
- return helper
-
- def register_func(self, types, func):
- """Helper to register an implementation."""
- self.registry[tuple(types)] = func
- self.cache = {} # Clear the cache (later we can optimize this).
-
- def __call__(self, *args):
- """Call the overloaded function."""
- types = tuple(map(type, args))
- func = self.cache.get(types)
- if func is None:
- self.cache[types] = func = self.find_func(types)
- return func(*args)
-
- def find_func(self, types):
- """Find the appropriate overloaded function; don't call it.
-
- This won't work for old-style classes or classes without __mro__.
-
- """
- func = self.registry.get(types)
- if func is not None:
- # Easy case -- direct hit in registry.
- return func
-
- # XXX Phillip Eby suggests to use issubclass() instead of __mro__.
- # There are advantages and disadvantages.
-
- # I can't help myself -- this is going to be intense functional
code.
- # Find all possible candidate signatures.
- mros = tuple(t.__mro__ for t in types)
- n = len(mros)
- candidates = [sig for sig in self.registry
- if len(sig) == n and
- all(t in mro for t, mro in zip(sig, mros))]
- if not candidates:
- # No match at all -- use the default function.
- return self.default_func
- if len(candidates) == 1:
- # Unique match -- that's an easy case.
- return self.registry[candidates[0]]
-
- # More than one match -- weed out the subordinate ones.
-
- def dominates(dom, sub,
- orders=tuple(dict((t, i) for i, t in enumerate(mro))
- for mro in mros)):
- # Predicate to decide whether dom strictly dominates sub.
- # Strict domination is defined as domination without equality.
- # The arguments dom and sub are type tuples of equal length.
- # The orders argument is a precomputed auxiliary data structure
- # giving dicts of ordering information corresponding to the
- # positions in the type tuples.
- # A type d dominates a type s iff order[d] <= order[s].
- # A type tuple (d1, d2, ...) dominates a type tuple of equal
length
- # (s1, s2, ...) iff d1 dominates s1, d2 dominates s2, etc.
- if dom is sub:
- return False
- return all(order[d] <= order[s]
- for d, s, order in zip(dom, sub, orders))
-
- # I suppose I could inline dominates() but it wouldn't get any
clearer.
- candidates = [cand
- for cand in candidates
- if not any(dominates(dom, cand) for dom in
candidates)]
- if len(candidates) == 1:
- # There's exactly one candidate left.
- return self.registry[candidates[0]]
-
- # Perhaps these multiple candidates all have the same
implementation?
- funcs = set(self.registry[cand] for cand in candidates)
- if len(funcs) == 1:
- return funcs.pop()
-
- # No, the situation is irreducibly ambiguous.
- raise TypeError("ambigous call; types=%r; candidates=%r" %
- (types, candidates))
=======================================
--- /branches/0.5/test/testconfig.py Mon Jul 26 18:14:27 2010
+++ /dev/null
@@ -1,29 +0,0 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-from java2python.config import default
-
-
-modulePrologueHandlers = default.modulePrologueHandlers + [
- 'from overloading import overloaded',
-]
-
-
-outputSubs = [
- (r'(.*?)\.getMessage\(\)', r'\1.message'),
- (r'(.*?)(\w+?)\.length(.*)', r'\1len(\2)\3'),
- (r'assertEquals', 'self.assertEquals'),
- (r'## import junit.framework', 'from unittest import *'),
- (r'(.*?)\.getClass\(\)', r'\1.__class__'),
- (r'(.*?)\.getName\(\)', r'\1.__name__'),
- (r'(.*?)\.getInterfaces\(\)', r'\1.__bases__'),
-
- (r'(.*?)\.fooProp\(\)', r'\1.fooProp'),
- (r'(.*?)\.fooProp\((.+)\)', r'\1.fooProp = \2'),
-
- (r'(.*?)lock(.*?) = object\(\)', r'\1lock\2 = Lock()'),
-
- ## these two fudge something that should be handled in parsing
- (r'(.*?)StaticInner\(\)', r'\1cls.StaticInner()'),
- (r'(.*?)outer\.cls', r'\1outer'),
- (r'from java\.util import \*', ''),
- ]
=======================================
--- /branches/0.5/test/Makefile Mon Jul 26 18:14:27 2010
+++ /branches/0.5/test/Makefile Wed Jul 28 01:49:35 2010
@@ -1,34 +1,43 @@
-PYTHONPATH=$PYTHONPATH:..:.
-## CLASSPATH=.
-
-java_sources := $(wildcard *.java)
-py_sources := $(addsuffix .py, $(notdir $(basename $(wildcard *.java))))
-
-
-.SILENT: clean
-.PHONY: all $(java_sources) $(python_sources) clean X
-
-all: package_1 $(java_sources)
-
-package_1:
- #@echo cd Package1 && ../../bin/j2py -i Class1.java -o Class1.py
-
-$(java_sources):
- @javac $@ && ../bin/j2py -i $@ -o $(basename $@).py -c ./testconfig.py
- @chmod u+x $(basename $@).py
- @bash ./compare_outputs.sh "java -ea $(basename $@)" "python $(basename
$@).py" "$@" && rm "$(basename $@).py" "$(basename $@).class"
+javac := javac
+java := java
+python := python -B
+j2py = ../bin/j2py
+
+
+java_files := $(wildcard *.java)
+class_files := $(wildcard *.class)
+python_files := $(addsuffix .py, $(notdir $(basename $(wildcard *.java))))
+
+
+.PHONY: all clean
+.SILENT: %:
+
+
+all:
+ @echo run something else for now


-%.py: %.java testconfig.py
- ../bin/j2py -i $< -o $@ -c testconfig
- python $@
-
-%: %.java
- javac $<
- java $@
-
-clean:
- rm -f *.class *.pyc $(py_sources)
-
-parser:
- cd ../java2python/lang && make
+clean:
+ @rm -f *.class *.pyc $(python_files)
+
+
+packages:
+ @cd Package1 && javac Class1.java
+ @cd Package1 && ../$(j2py) -i Class1.java -o Class1.py
+
+
+parsers:
+ @cd ../java2python/lang && make -s
+
+
+%.class:
+ @$(javac) $(addsuffix .java, $(basename $@))
+
+
+%.py: %.class
+ @$(j2py) -i $(addsuffix .java, $(basename $@)) -o $@
-c ./configs/__init__.py
+
+
+%: %.py
+ @bash -c "diff <($(python) $(addsuffix .py, $@)) <(java -ea $@)" &&
echo "[OKAY] $@"
+

Reply all
Reply to author
Forward
0 new messages