// Copyright (c) Corporation for National Research Initiatives
package org.python.core;
import org.python.parser.ast.modType;
import java.lang.reflect.InvocationTargetException;
import java.io.*;
public final class Py
{
static boolean frozen;
static String frozenPackage=null;
private final static Object PRESENT=new Object();
static java.util.Hashtable frozenModules;
static boolean initialized;
/* Holds the singleton None and Ellipsis objects */
/** The singleton None Python object **/
public static PyObject None;
/** The singleton Ellipsis Python object - written as ... when indexing */
public static PyObject Ellipsis;
/** The singleton NotImplemented Python object. Used in rich comparison */
public static PyObject NotImplemented;
/** A zero-length array of Strings to pass to functions that
don't have any keyword arguments **/
public static String[] NoKeywords;
/** A zero-length array of PyObject's to pass to functions that
expect zero-arguments **/
public static PyObject[] EmptyObjects;
/** A tuple with zero elements **/
public static PyTuple EmptyTuple;
/** The Python integer 0 - also used as false **/
public static PyInteger Zero;
/** The Python integer 1 - also used as true **/
public static PyInteger One;
/** A zero-length Python string **/
public static PyString EmptyString;
/** A Python string containing '\n' **/
public static PyString Newline;
/** A Python string containing ' ' **/
public static PyString Space;
/** A unique object to indicate no conversion is possible
in __tojava__ methods **/
public static Object NoConversion;
public static PyObject OSError;
public static PyObject NotImplementedError;
public static PyObject EnvironmentError;
/* The standard Python exceptions */
public static PyObject OverflowError;
public static PyException OverflowError(String message) {
return new PyException(Py.OverflowError, message);
}
public static PyObject RuntimeError;
public static PyException RuntimeError(String message) {
return new PyException(Py.RuntimeError, message);
}
public static PyObject KeyboardInterrupt;
/*public static PyException KeyboardInterrupt(String message) {
return new PyException(Py.KeyboardInterrupt, message);
}*/
public static PyObject FloatingPointError;
public static PyException FloatingPointError(String message) {
return new PyException(Py.FloatingPointError, message);
}
public static PyObject SyntaxError;
public static PyException SyntaxError(String message) {
return new PyException(Py.SyntaxError, message);
}
public static PyObject IndentationError;
public static PyObject TabError;
public static PyObject AttributeError;
public static PyException AttributeError(String message) {
return new PyException(Py.AttributeError, message);
}
public static PyObject IOError;
public static PyException IOError(java.io.IOException ioe) {
//System.err.println("ioe: "+ioe);
//ioe.printStackTrace();
String message = ioe.getMessage();
if (ioe instanceof java.io.FileNotFoundException) {
message = "File not found - "+message;
}
return new PyException(Py.IOError, message);
}
public static PyException IOError(String message) {
//System.err.println("sioe: "+message);
return new PyException(Py.IOError, message);
}
public static PyObject KeyError;
public static PyException KeyError(String message) {
return new PyException(Py.KeyError, message);
}
public static PyObject AssertionError;
public static PyException AssertionError(String message) {
return new PyException(Py.AssertionError, message);
}
public static PyObject TypeError;
public static PyException TypeError(String message) {
return new PyException(Py.TypeError, message);
}
public static PyObject ReferenceError;
public static PyException ReferenceError(String message) {
return new PyException(Py.ReferenceError, message);
}
public static PyObject SystemError;
public static PyException SystemError(String message) {
return new PyException(Py.SystemError, message);
}
public static PyObject IndexError;
public static PyException IndexError(String message) {
return new PyException(Py.IndexError, message);
}
public static PyObject ZeroDivisionError;
public static PyException ZeroDivisionError(String message) {
return new PyException(Py.ZeroDivisionError, message);
}
public static PyObject NameError;
public static PyException NameError(String message) {
return new PyException(Py.NameError, message);
}
public static PyObject UnboundLocalError;
public static PyException UnboundLocalError(String message) {
return new PyException(Py.UnboundLocalError, message);
}
public static PyObject SystemExit;
/*public static PyException SystemExit(String message) {
return new PyException(Py.SystemExit, message);
}*/
static void maybeSystemExit(PyException exc) {
//System.err.println("maybeSystemExit: " + exc.type.toString());
if (Py.matchException(exc, Py.SystemExit)) {
PyObject value = exc.value;
//System.err.println("exiting: "+value.getClass().getName());
if (value instanceof PyInstance) {
PyObject tmp = value.__findattr__("code");
if (tmp != null)
value = tmp;
}
Py.getSystemState().callExitFunc();
if (value instanceof PyInteger) {
System.exit(((PyInteger)value).getValue());
} else {
if (value != Py.None) {
try {
Py.println(value);
System.exit(1);
}
catch (Throwable t0) { }
}
System.exit(0);
}
}
}
public static PyObject StopIteration;
public static PyException StopIteration(String message) {
return new PyException(Py.StopIteration, message);
}
public static PyObject ImportError;
public static PyException ImportError(String message) {
return new PyException(Py.ImportError, message);
}
public static PyObject ValueError;
public static PyException ValueError(String message) {
return new PyException(Py.ValueError, message);
}
public static PyObject UnicodeError;
public static PyException UnicodeError(String message) {
return new PyException(Py.UnicodeError, message);
}
public static PyObject EOFError;
public static PyException EOFError(String message) {
return new PyException(Py.EOFError, message);
}
public static PyObject MemoryError;
public static void memory_error(OutOfMemoryError t) {
if (Options.showJavaExceptions) {
t.printStackTrace();
}
// this logic would allow to re-enable the old behavior when it makes sense,
// or better offer a hook?
// try {
// byte[] alloc = new byte[(512*1024)];
// } catch(OutOfMemoryError oome) {
// System.err.println("Out Of Memory");
// System.err.println("You might want to try the -mx flag to increase heap size.");
// System.exit(-1);
// }
}
public static PyException MemoryError(String message) {
return new PyException(Py.MemoryError, message);
}
public static PyObject ArithmeticError;
public static PyObject LookupError;
public static PyObject StandardError;
public static PyObject Exception;
public static PyObject Warning;
public static void Warning(String message) {
warning(Warning, message);
}
public static PyObject UserWarning;
public static void UserWarning(String message) {
warning(UserWarning, message);
}
public static PyObject DeprecationWarning;
public static void DeprecationWarning(String message) {
warning(DeprecationWarning, message);
}
public static PyObject SyntaxWarning;
public static void SyntaxWarning(String message) {
warning(SyntaxWarning, message);
}
public static PyObject OverflowWarning;
public static void OverflowWarning(String message) {
warning(OverflowWarning, message);
}
public static PyObject RuntimeWarning;
public static void RuntimeWarning(String message) {
warning(RuntimeWarning, message);
}
private static PyObject warnings_mod;
private static PyObject importWarnings() {
if (warnings_mod != null) return warnings_mod;
PyObject mod;
try {
mod = __builtin__.__import__("warnings");
} catch(PyException e) {
if (matchException(e,ImportError)) {
return null;
}
throw e;
}
warnings_mod = mod;
return mod;
}
private static String warn_hcategory(PyObject category) {
PyObject name = category.__findattr__("__name__");
if (name != null) return "["+name+"]";
return "[warning]";
}
public static void warning(PyObject category, String message) {
PyObject func = null;
PyObject mod = importWarnings();
if (mod != null)
func = mod.__getattr__("warn");
if (func == null) {
System.err.println(warn_hcategory(category) + ": " + message);
return;
} else {
func.__call__(Py.newString(message), category);
}
}
public static void warning(PyObject category, String message,
String filename, int lineno, String module,
PyObject registry)
{
PyObject func = null;
PyObject mod = importWarnings();
if (mod != null)
func = mod.__getattr__("warn_explicit");
if (func == null) {
System.err.println(filename + ":" + lineno + ":" +
warn_hcategory(category) + ": " + message);
return;
} else {
func.__call__(new PyObject[] {
Py.newString(message), category,
Py.newString(filename), Py.newInteger(lineno),
(module == null) ? Py.None : Py.newString(module),
registry}, Py.NoKeywords);
}
}
public static PyObject JavaError;
public static PyException JavaError(Throwable t) {
// System.err.println("t: "+t);
if (t instanceof PyException) {
return (PyException)t;
}
else if (t instanceof InvocationTargetException) {
return JavaError(
((InvocationTargetException)t).getTargetException());
}
// Remove this automatic coercion, people want to see the real
// exceptions!
// else if (t instanceof java.io.IOException) {
// return IOError((java.io.IOException)t);
// }
// see corresponding logic in matchException
else if (t instanceof OutOfMemoryError) {
memory_error((OutOfMemoryError)t);
}
PyObject exc = Py.java2py(t);
return new PyException(exc.__class__, exc);
}
// Don't allow any constructors. Class only provides static methods.
private Py() { ; }
/** @deprecated **/
//public static InterpreterState interp;
/**
Convert a given PyObject
to an instance of a Java class.
Identical to o.__tojava__(c)
except that it will
raise a TypeError
if the conversion fails.
@param o the PyObject
to convert.
@param c the class to convert it to.
**/
public static Object tojava(PyObject o, Class c) {
Object obj = o.__tojava__(c);
if (obj == Py.NoConversion) {
throw Py.TypeError("can't convert "+o.__repr__()+" to "+
c.getName());
}
return obj;
}
// ??pending: was @deprecated but is actually used by proxie code.
// Can get rid of it?
public static Object tojava(PyObject o, String s) {
Class c = findClass(s);
if (c == null) throw Py.TypeError("can't convert to: "+s);
return tojava(o, c); // prev:Class.forName
}
/* Helper functions for PyProxy's */
/** @deprecated **/
public static PyObject jfindattr(PyProxy proxy, String name) {
PyInstance o = proxy._getPyInstance();
if (o == null) {
proxy.__initProxy__(new Object[0]);
o = proxy._getPyInstance();
}
PyObject ret = o.__jfindattr__(name);
if (ret == null)
return null;
// Set the current system state to match proxy -- usually
// this is a waste of time :-(
Py.setSystemState(proxy._getPySystemState());
return ret;
}
/** @deprecated **/
public static PyObject jgetattr(PyProxy proxy, String name) {
PyInstance o = proxy._getPyInstance();
PyObject ret = null;
if (o != null) {
ret = o.__jfindattr__(name);
}
if (ret == null)
throw Py.AttributeError("abstract method \""+name+
"\" not implemented");
// Set the current system state to match proxy -- usually this is a
// waste of time :-(
Py.setSystemState(proxy._getPySystemState());
return ret;
}
/* Convenience methods to create new constants without using "new" */
private static PyInteger[] integerCache = null;
public static final PyInteger newInteger(int i) {
if (integerCache == null) {
integerCache = new PyInteger[1000];
for(int j=-100; j<900; j++) {
integerCache[j+100] = new PyInteger(j);
}
}
if (i>=-100 && i < 900) {
return integerCache[i+100];
} else {
return new PyInteger(i);
}
}
public static PyObject newInteger(long i) {
if (i < Integer.MIN_VALUE || i > Integer.MAX_VALUE)
return new PyLong(i);
else
return newInteger((int)i);
}
public static PyLong newLong(String s) {
return new PyLong(s);
}
public static PyLong newLong(java.math.BigInteger i) {
return new PyLong(i);
}
public static PyComplex newImaginary(double v) {
return new PyComplex(0, v);
}
public static PyFloat newFloat(float v) {
return new PyFloat((double)v);
}
public static PyFloat newFloat(double v) {
return new PyFloat(v);
}
public static PyString newString(char c) {
return makeCharacter(c);
}
public static PyString newString(String s) {
return new PyString(s);
}
public static PyInteger newBoolean(boolean t) {
return t ? Py.One : Py.Zero;
}
// nested scopes:
// String[] cellvars,String[] freevars,int npurecell & int moreflags
public static PyCode newCode(int argcount, String varnames[],
String filename, String name,
boolean args, boolean keywords,
PyFunctionTable funcs, int func_id,
String[] cellvars,String[] freevars,
int npurecell, int moreflags)
{
return new PyTableCode(argcount, varnames,
filename, name, 0, args, keywords, funcs,
func_id, cellvars, freevars, npurecell,
moreflags);
}
public static PyCode newCode(int argcount, String varnames[],
String filename, String name,
int firstlineno,
boolean args, boolean keywords,
PyFunctionTable funcs, int func_id,
String[] cellvars,String[] freevars,
int npurecell, int moreflags)
{
return new PyTableCode(argcount, varnames,
filename, name, firstlineno, args, keywords,
funcs, func_id, cellvars, freevars, npurecell,
moreflags);
}
// --
public static PyCode newCode(int argcount, String varnames[],
String filename, String name,
boolean args, boolean keywords,
PyFunctionTable funcs, int func_id)
{
return new PyTableCode(argcount, varnames,
filename, name, 0, args, keywords, funcs,
func_id);
}
public static PyCode newCode(int argcount, String varnames[],
String filename, String name,
int firstlineno,
boolean args, boolean keywords,
PyFunctionTable funcs, int func_id)
{
return new PyTableCode(argcount, varnames,
filename, name, firstlineno, args, keywords,
funcs, func_id);
}
public static PyCode newJavaCode(Class cls, String name) {
return new JavaCode(newJavaFunc(cls, name));
}
public static PyObject newJavaFunc(Class cls, String name) {
try {
java.lang.reflect.Method m = cls.getMethod(name, new Class[] {
PyObject[].class, String[].class });
return new JavaFunc(m);
} catch (NoSuchMethodException e) {
throw Py.JavaError(e);
}
}
private static PyObject initExc(String name, PyObject exceptions,
PyObject dict) {
PyObject tmp = exceptions.__getattr__(name);
dict.__setitem__(name, tmp);
return tmp;
}
static void initClassExceptions(PyObject dict) {
PyObject exc = imp.load("exceptions");
Exception = initExc("Exception", exc, dict);
SystemExit = initExc("SystemExit", exc, dict);
StopIteration = initExc("StopIteration", exc, dict);
StandardError = initExc("StandardError", exc, dict);
KeyboardInterrupt = initExc("KeyboardInterrupt", exc, dict);
ImportError = initExc("ImportError", exc, dict);
EnvironmentError = initExc("EnvironmentError", exc, dict);
IOError = initExc("IOError", exc, dict);
OSError = initExc("OSError", exc, dict);
EOFError = initExc("EOFError", exc, dict);
RuntimeError = initExc("RuntimeError", exc, dict);
NotImplementedError = initExc("NotImplementedError", exc, dict);
NameError = initExc("NameError", exc, dict);
UnboundLocalError = initExc("UnboundLocalError", exc, dict);
AttributeError = initExc("AttributeError", exc, dict);
SyntaxError = initExc("SyntaxError", exc, dict);
IndentationError = initExc("IndentationError", exc, dict);
TabError = initExc("TabError", exc, dict);
TypeError = initExc("TypeError", exc, dict);
AssertionError = initExc("AssertionError", exc, dict);
LookupError = initExc("LookupError", exc, dict);
IndexError = initExc("IndexError", exc, dict);
KeyError = initExc("KeyError", exc, dict);
ArithmeticError = initExc("ArithmeticError", exc, dict);
OverflowError = initExc("OverflowError", exc, dict);
ZeroDivisionError = initExc("ZeroDivisionError", exc, dict);
FloatingPointError = initExc("FloatingPointError", exc, dict);
ValueError = initExc("ValueError", exc, dict);
UnicodeError = initExc("UnicodeError", exc, dict);
ReferenceError = initExc("ReferenceError", exc, dict);
SystemError = initExc("SystemError", exc, dict);
MemoryError = initExc("MemoryError", exc, dict);
Warning = initExc("Warning", exc, dict);
UserWarning = initExc("UserWarning", exc, dict);
DeprecationWarning = initExc("DeprecationWarning", exc, dict);
SyntaxWarning = initExc("SyntaxWarning", exc, dict);
OverflowWarning = initExc("OverflowWarning", exc, dict);
RuntimeWarning = initExc("RuntimeWarning", exc, dict);
}
public static PySystemState defaultSystemState;
// This is a hack to get initializations to work in proper order
public static synchronized boolean initPython() {
PySystemState.initialize();
return true;
}
public static Class relFindClass(Class home,String name) {
try {
ClassLoader loader = home.getClassLoader();
if (loader != null) return loader.loadClass(name);
else return Class.forName(name);
} catch (ClassNotFoundException exc) {
return null;
} catch (Throwable t) {
throw Py.JavaError(t);
}
}
private static boolean secEnv=false;
public static Class findClass(String name) {
try {
ClassLoader classLoader = Py.getSystemState().getClassLoader();
if (classLoader != null) return classLoader.loadClass(name);
if(!secEnv) {
try {
classLoader = imp.getSyspathJavaLoader();
}
catch(SecurityException e) {
secEnv=true;
}
if (classLoader != null) {
return classLoader.loadClass(name);
}
}
return Class.forName(name);
}
catch (ClassNotFoundException e) {
// e.printStackTrace();
return null;
}
catch (IllegalArgumentException e) {
// e.printStackTrace();
return null;
}
catch (NoClassDefFoundError e) {
// e.printStackTrace();
return null;
}
}
public static Class findClassEx(String name, String reason) {
try {
ClassLoader classLoader = Py.getSystemState().getClassLoader();
if (classLoader != null) {
writeDebug("import", "trying " + name + " as " + reason +
" in classLoader");
return classLoader.loadClass(name);
}
if(!secEnv) {
try {
classLoader = imp.getSyspathJavaLoader();
}
catch(SecurityException e) {
secEnv=true;
}
if (classLoader != null) {
writeDebug("import", "trying " + name + " as " + reason +
" in syspath loader");
return classLoader.loadClass(name);
}
}
writeDebug("import", "trying " + name + " as " + reason +
" in Class.forName");
return Class.forName(name);
}
catch (ClassNotFoundException e) {
return null;
}
catch (IllegalArgumentException e) {
throw JavaError(e);
}
catch (LinkageError e) {
throw JavaError(e);
}
}
private static void setArgv(String arg0, String[] args) {
PyObject argv[] = new PyObject[args.length+1];
argv[0] = new PyString(arg0);
for(int i=1; i 0)
Py.frozenPackage = frozenPackage;
}
java.util.Properties sprops;
try {
sprops = new java.util.Properties(System.getProperties());
} catch (Throwable t) {
sprops = new java.util.Properties();
}
if (props != null) {
for (int i=0; i= index0)
str = str.substring(index0+1,index+1);
return str;
}
/* Display a PyException and stack trace */
public static void printException(Throwable t) {
printException(t, null, null);
}
public static void printException(Throwable t, PyFrame f) {
printException(t, f, null);
}
public static synchronized void printException(Throwable t, PyFrame f,
PyObject file)
{
//System.err.println("printingException: "+t+", "+file);
StdoutWrapper stderr = Py.stderr;
if (file != null) {
stderr = new FixedFileWrapper(file);
}
if (Options.showJavaExceptions) {
stderr.println("Java Traceback:");
java.io.CharArrayWriter buf = new java.io.CharArrayWriter();
if (t instanceof PyException) {
((PyException)t).super__printStackTrace(
new java.io.PrintWriter(buf));
} else {
t.printStackTrace(new java.io.PrintWriter(buf));
}
stderr.print(buf.toString());
}
PyException exc = Py.JavaError(t);
maybeSystemExit(exc);
if (f != null && exc.traceback.tb_frame != f) {
exc.traceback = new PyTraceback(exc.traceback);
}
setException(exc, f);
ThreadState ts = getThreadState();
ts.systemState.last_value = exc.value;
ts.systemState.last_type = exc.type;
ts.systemState.last_traceback = exc.traceback;
PyObject exceptHook = ts.systemState.__findattr__("excepthook");
if (exceptHook != null) {
try {
exceptHook.__call__(exc.type, exc.value, exc.traceback);
} catch (PyException exc2) {
stderr.println("Error in sys.excepthook:");
displayException(exc2.type, exc2.value, exc2.traceback, file);
stderr.println();
stderr.println("Original exception was:");
displayException(exc.type, exc.value, exc.traceback, file);
}
} else {
stderr.println("sys.excepthook is missing");
displayException(exc.type, exc.value, exc.traceback, file);
}
ts.exception = null;
}
public static void displayException(PyObject type, PyObject value,
PyObject tb, PyObject file)
{
StdoutWrapper stderr = Py.stderr;
if (file != null) {
stderr = new FixedFileWrapper(file);
}
if (tb instanceof PyTraceback)
stderr.print(((PyTraceback) tb).dumpStack());
if (__builtin__.isinstance(value, (PyClass) Py.SyntaxError)) {
stderr.println(" File \""+value.__findattr__("filename")+
"\", line "+value.__findattr__("lineno"));
PyObject text = value.__findattr__("text");
if (text != Py.None && text.__len__() != 0) {
stderr.println("\t"+text);
String space = "\t";
int col = value.__findattr__("offset").__int__().getValue();
for(int j=1; j", "exec",
Py.getCompilerFlags());
}
Py.runCode(code, locals, globals);
}
private static ThreadStateMapping threadStateMapping = null;
public static final ThreadState getThreadState() {
return getThreadState(null);
}
public static final ThreadState
getThreadState(PySystemState newSystemState)
{
if (threadStateMapping == null) {
synchronized (Py.class) {
if (threadStateMapping == null)
threadStateMapping = ThreadStateMapping.makeMapping();
}
}
return threadStateMapping.getThreadState(newSystemState);
}
public static final PySystemState
setSystemState(PySystemState newSystemState)
{
ThreadState ts = getThreadState(newSystemState);
PySystemState oldSystemState = ts.systemState;
if (oldSystemState != newSystemState) {
//System.err.println("Warning: changing systemState "+
// "for same thread!");
ts.systemState = newSystemState;
}
return oldSystemState;
}
public static final PySystemState getSystemState() {
return getThreadState().systemState;
//defaultSystemState;
}
/* Get and set the current frame */
public static PyFrame getFrame() {
//System.out.println("getFrame");
ThreadState ts = getThreadState();
if (ts == null)
return null;
return ts.frame;
}
public static void setFrame(PyFrame f) {
//System.out.println("setFrame");
getThreadState().frame = f;
}
/* These are not used anymore. Uncomment them if there is a future
clamor to make this functionality more easily usable
public static void pushFrame(PyFrame f) {
ThreadState ts = getThreadState();
f.f_back = ts.frame;
if (f.f_builtins == null) f.f_builtins = f.f_back.f_builtins;
ts.frame = f;
}
public static PyFrame popFrame() {
ThreadState ts = getThreadState();
PyFrame f = ts.frame.f_back;
ts.frame = f;
return f;
}
*/
/* A collection of functions for implementing the print statement */
public static StdoutWrapper stderr;
static StdoutWrapper stdout;
//public static StdinWrapper stdin;
public static void print(PyObject file, PyObject o) {
if (file == None)
print(o);
else
new FixedFileWrapper(file).print(o);
}
public static void printComma(PyObject file, PyObject o) {
if (file == None)
printComma(o);
else
new FixedFileWrapper(file).printComma(o);
}
public static void println(PyObject file, PyObject o) {
if (file == None)
println(o);
else
new FixedFileWrapper(file).println(o);
}
public static void printlnv(PyObject file) {
if (file == None)
println();
else
new FixedFileWrapper(file).println();
}
public static void print(PyObject o) {
stdout.print(o);
}
public static void printComma(PyObject o) {
stdout.printComma(o);
}
public static void println(PyObject o) {
stdout.println(o);
}
public static void println() {
stdout.println();
}
/* A collection of convenience functions for converting PyObjects
to Java primitives */
public static boolean py2boolean(PyObject o) {
return o.__nonzero__();
}
public static byte py2byte(PyObject o) {
if (o instanceof PyInteger) return (byte)((PyInteger)o).getValue();
Object i = o.__tojava__(Byte.TYPE);
if (i == null || i == Py.NoConversion)
throw Py.TypeError("integer required");
return ((Byte) i).byteValue();
}
public static short py2short(PyObject o) {
if (o instanceof PyInteger)
return (short)((PyInteger)o).getValue();
Object i = o.__tojava__(Short.TYPE);
if (i == null || i == Py.NoConversion)
throw Py.TypeError("integer required");
return ((Short) i).shortValue();
}
public static int py2int(PyObject o) {
return py2int(o, "integer required");
}
public static int py2int(PyObject o, String msg) {
if (o instanceof PyInteger)
return (int)((PyInteger)o).getValue();
Object obj = o.__tojava__(Integer.TYPE);
if (obj == Py.NoConversion)
throw Py.TypeError(msg);
return ((Integer)obj).intValue();
}
public static long py2long(PyObject o) {
if (o instanceof PyInteger)
return (long)((PyInteger)o).getValue();
Object i = o.__tojava__(Long.TYPE);
if (i == null || i == Py.NoConversion)
throw Py.TypeError("integer required");
return ((Long) i).longValue();
}
public static float py2float(PyObject o) {
if (o instanceof PyFloat)
return (float)((PyFloat)o).getValue();
if (o instanceof PyInteger)
return (float)((PyInteger)o).getValue();
Object i = o.__tojava__(Float.TYPE);
if (i == null || i == Py.NoConversion)
throw Py.TypeError("float required");
return ((Float) i).floatValue();
}
public static double py2double(PyObject o) {
if (o instanceof PyFloat)
return (double)((PyFloat)o).getValue();
if (o instanceof PyInteger)
return (double)((PyInteger)o).getValue();
Object i = o.__tojava__(Double.TYPE);
if (i == null || i == Py.NoConversion)
throw Py.TypeError("float required");
return ((Double) i).doubleValue();
}
public static char py2char(PyObject o) {
return py2char(o, "char required");
}
public static char py2char(PyObject o, String msg) {
if (o instanceof PyString) {
PyString s = (PyString)o;
if (s.__len__() != 1)
throw Py.TypeError(msg);
return s.toString().charAt(0);
}
if (o instanceof PyInteger) {
return (char)((PyInteger)o).getValue();
}
Object i = o.__tojava__(Character.TYPE);
if (i == null || i == Py.NoConversion)
throw Py.TypeError(msg);
return ((Character) i).charValue();
}
public static void py2void(PyObject o) {
if (o != Py.None) {
throw Py.TypeError("None required for void return");
}
}
private static PyString[] letters=null;
static final PyString makeCharacter(Character o) {
return makeCharacter(o.charValue());
}
static final PyString makeCharacter(char c) {
if (c > 255) {
return new PyString(new Character(c).toString());
}
if (letters == null) {
letters = new PyString[256];
for(char j=0; j<256; j++) {
letters[j] = new PyString(new Character(j).toString());
}
}
return letters[c];
}
// Needs rewriting for efficiency and extensibility
public static PyObject java2py(Object o) {
if (o instanceof PyObject)
return (PyObject)o;
if (o instanceof PyProxy)
return ((PyProxy)o)._getPyInstance();
if (o instanceof Number) {
if (o instanceof Double || o instanceof Float) {
return new PyFloat(((Number)o).doubleValue());
}
else if (o instanceof Long) {
return new PyLong(((Number)o).longValue());
}
else if (o instanceof Integer ||
o instanceof Byte ||
o instanceof Short)
{
return new PyInteger(((Number)o).intValue());
}
}
if (o instanceof Boolean) {
return ((Boolean)o).booleanValue() ? Py.One : Py.Zero;
}
if (o == null) return Py.None;
if (o instanceof String) return new PyString((String)o);
if (o instanceof Character) return makeCharacter((Character)o);
if (o instanceof Class) return PyJavaClass.lookup((Class)o);
Class c = o.getClass();
if (c.isArray()) {
return new PyArray(c.getComponentType(), o);
}
return new PyJavaInstance(o);
}
public static PyObject makeClass(String name, PyObject[] bases,
PyCode code, PyObject doc)
{
return makeClass(name, bases, code, doc, null, null);
}
public static PyObject makeClass(String name, PyObject[] bases,
PyCode code, PyObject doc,
PyObject[] closure_cells)
{
return makeClass(name, bases, code, doc, null, closure_cells);
}
public static PyObject makeClass(String name, PyObject[] bases,
PyCode code, PyObject doc,
Class proxyClass) {
return makeClass(name, bases, code, doc, proxyClass, null);
}
private static Class[] pyClassCtrSignature = {
String.class, PyTuple.class, PyObject.class, Class.class
};
public static PyObject makeClass(String name, PyObject[] bases,
PyCode code, PyObject doc,
Class proxyClass,
PyObject[] closure_cells)
{
PyFrame frame = getFrame();
PyObject globals = frame.f_globals;
PyObject dict = code.call(Py.EmptyObjects, Py.NoKeywords,
globals, Py.EmptyObjects,
new PyTuple(closure_cells));
if (doc != null)
dict.__setitem__("__doc__", doc);
for (int i=0; i");
} else {
tmp = file.__tojava__(Writer.class);
if ((tmp != Py.NoConversion) && (tmp != null)) {
Writer w = (Writer)tmp;
this.file = new PyFile(w, "");
}
}
}
}
protected PyObject myFile() {
return file;
}
}
/**
* A code object wrapper for a python function.
*/
class JavaCode extends PyCode {
private PyObject func;
public JavaCode(PyObject func) {
this.func = func;
if (func instanceof PyReflectedFunction)
this.co_name = ((PyReflectedFunction) func).__name__;
}
public PyObject call(PyFrame frame, PyObject closure) {
System.out.println("call #1");
return Py.None;
}
public PyObject call(PyObject args[], String keywords[],
PyObject globals, PyObject[] defaults,
PyObject closure)
{
return func.__call__(args, keywords);
}
public PyObject call(PyObject self, PyObject args[], String keywords[],
PyObject globals, PyObject[] defaults,
PyObject closure)
{
return func.__call__(self, args, keywords);
}
public PyObject call(PyObject globals, PyObject[] defaults,
PyObject closure)
{
return func.__call__();
}
public PyObject call(PyObject arg1, PyObject globals,
PyObject[] defaults, PyObject closure)
{
return func.__call__(arg1);
}
public PyObject call(PyObject arg1, PyObject arg2, PyObject globals,
PyObject[] defaults, PyObject closure)
{
return func.__call__(arg1, arg2);
}
public PyObject call(PyObject arg1, PyObject arg2, PyObject arg3,
PyObject globals, PyObject[] defaults,
PyObject closure)
{
return func.__call__(arg1, arg2, arg3);
}
}
/**
* A function object wrapper for a java method which comply with the
* PyArgsKeywordsCall standard.
*/
class JavaFunc extends PyObject {
java.lang.reflect.Method method;
public JavaFunc(java.lang.reflect.Method method) {
this.method = method;
}
public PyObject __call__(PyObject[] args, String[] kws) {
Object[] margs = new Object[] { args, kws };
try {
return Py.java2py(method.invoke(null, margs));
} catch (Throwable t) {
throw Py.JavaError(t);
}
}
public PyObject _doget(PyObject container) {
return _doget(container, null);
}
public PyObject _doget(PyObject container, PyObject wherefound) {
if (container == null)
return this;
return new PyMethod(container, this, wherefound);
}
public boolean _doset(PyObject container) {
throw Py.TypeError("java function not settable: "+method.getName());
}
}