Issue1709

classification
Title: jsr223 bug when dealing with datetime module
Type: behaviour Severity: critical
Components: Core Versions: Jython 2.5
Milestone:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: anamitrab, fwierzbicki, nriley, otmarhumbel, pjenvey
Priority: normal Keywords:

Created on 2011-02-22.20:53:46 by anamitrab, last changed 2013-02-19.23:46:05 by fwierzbicki.

Files
File name Uploaded Description Edit Remove
unnamed anamitrab, 2011-02-27.04:53:35
graycol.gif anamitrab, 2011-02-27.04:53:36
ecblank.gif anamitrab, 2011-02-27.04:53:36
Messages
msg6402 (view) Author: Anamitra Bhattacharyya (anamitrab) Date: 2011-02-22.21:00:02
The test case is simple and is shown below using a java test class

package com.ibm.tivoli.maximo.script;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import javax.script.*;

import org.python.util.PythonInterpreter;

import java.io.*;

public class TestJythonDateModule {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception
	{
		if(args.length>=1 && args[0].equals("usejsr"))
		{
			ScriptEngineManager factory = new ScriptEngineManager();
			ScriptEngine engine = factory.getEngineByName("jython");
			BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("c:\\harrier75\\datetimetest.py")));
			//engine.eval(reader);;
			CompiledScript cs = ((Compilable)engine).compile(reader);
			cs.eval();
		}
		else
		{
			 PythonInterpreter jython = new PythonInterpreter();
			 InputStream scriptStream = new FileInputStream("c:\\harrier75\\datetimetest.py");
	
			 try {
				 jython.execfile(scriptStream);
			 }
			 catch (Exception e) {
				 e.printStackTrace();
			 }
		}

	}

}

The datetimetest.py script is as below


import datetime

today = datetime.date.today()
print today
print 'ctime:', today.ctime()
print 'tuple:', today.timetuple()
print 'ordinal:', today.toordinal()
print 'Year:', today.year
print 'Mon :', today.month
print 'Day :', today.day

When we run the above test java class using the usejsr option - it gives an error as below. the non jsr 223 approach works perfect. This implies the jsr223 approach is broken and does not work for datetime module.

usejsr option
--------------
C:\harrier75\applications\maximo\businessobjects\classes>C:\harrier75\tools\java
\jre\bin\java -classpath .;C:\harrier75\jython\jython.jar;C:\harrier75\jython\Li
b com.ibm.tivoli.maximo.script.TestJythonDateModule usejsr
2011-02-20
ctime:Exception in thread "main" javax.script.ScriptException: AttributeError: '
java.sql.Date' object has no attribute 'ctime' in <script> at line number 5
        at org.python.jsr223.PyScriptEngine.scriptException(PyScriptEngine.java:
191)
        at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:42)
        at org.python.jsr223.PyScriptEngine.access$300(PyScriptEngine.java:20)
        at org.python.jsr223.PyScriptEngine$PyCompiledScript.eval(PyScriptEngine
.java:220)
        at javax.script.CompiledScript.eval(CompiledScript.java:101)
        at com.ibm.tivoli.maximo.script.TestJythonDateModule.main(TestJythonDate
Module.java:27)
Caused by:
Traceback (most recent call last):
  File "<script>", line 5, in <module>
AttributeError: 'java.sql.Date' object has no attribute 'ctime'

        at org.python.core.PyException.fillInStackTrace(PyException.java:70)
        at java.lang.Throwable.<init>(Throwable.java:56)
        at org.python.core.PyException.<init>(PyException.java:46)
        at org.python.core.PyException.<init>(PyException.java:43)
        at org.python.core.PyException.<init>(PyException.java:61)
        at org.python.core.Py.AttributeError(Py.java:166)
        at org.python.core.PyObject.noAttributeError(PyObject.java:930)
        at org.python.core.PyObject.object___getattribute__(PyObject.java:3692)
        at org.python.core.PyObject$object___getattribute___exposer.__call__(Unk
nown Source)
        at org.python.core.Deriveds.__findattr_ex__(Deriveds.java:59)
        at org.python.core.PyObjectDerived.__findattr_ex__(PyObjectDerived.java:
983)
        at org.python.core.PyObject.__getattr__(PyObject.java:923)
        at org.python.pycode._pyx0.f$0(<script>:10)
        at org.python.pycode._pyx0.call_function(<script>)
        at org.python.core.PyTableCode.call(PyTableCode.java:165)
        at org.python.core.PyCode.call(PyCode.java:18)
        at org.python.core.Py.runCode(Py.java:1261)
        at org.python.core.__builtin__.eval(__builtin__.java:484)
        at org.python.core.__builtin__.eval(__builtin__.java:488)
        at org.python.util.PythonInterpreter.eval(PythonInterpreter.java:198)
        at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:40)
        ... 4 more

The same thing if I run without the usejsr option I get this output


C:\harrier75\applications\maximo\businessobjects\classes>C:\harrier75\tools\java
\jre\bin\java -classpath .;C:\harrier75\jython\jython.jar;C:\harrier75\jython\Li
b com.ibm.tivoli.maximo.script.TestJythonDateModule
2011-02-20
ctime: Sun Feb 20 00:00:00 2011
tuple: (2011, 2, 20, 0, 0, 0, 6, 51, -1)
ordinal: 734188
Year: 2011
Mon : 2
Day : 20

C:\harrier75\applications\maximo\businessobjects\classes>
msg6403 (view) Author: Philip Jenvey (pjenvey) Date: 2011-02-23.05:02:16
Like I said on the ML ( http://old.nabble.com/Re%3A-jython-2.5.2-jsr-223-bug-p30988917.html ), the fact that the jsr223 ScriptEngineScope has always done this __tojava__/Py.java2py dance must cause this

datetime.date.__tojava__ returns a java.sql.Date. That doesn't necessarily mean Py.java2py(java.sql.Date) will give you a datetime.date back

Plus I think this "feature" could cause a significant slowdown of python code executing with the scope of the jsr223 context. Historically it's behavior adopted from the old jsr223 implementation. It needs to be rethought

Anamitra, thanks for the logging this, in your case you may be able to hack around the problem by disabling datetime.date's tojava conversion by calling this after importing datetime:

del datetime.date.__tojava__

this issue likely also affects datetime.time/datetime, Decimal and threading.Thread objects
msg6411 (view) Author: Anamitra Bhattacharyya (anamitrab) Date: 2011-02-27.04:53:36
thanks Philip - that workaround worked.
Anamitra

  >--------------------------------------------------------------------------------------------------------------------------------------------------|
  |Philip Jenvey <report@bugs.jython.org>                                                                                                            |
  >--------------------------------------------------------------------------------------------------------------------------------------------------|
|------------>
| To:        |
|------------>
  >--------------------------------------------------------------------------------------------------------------------------------------------------|
  |Anamitra Bhattacharyya/Bedford/IBM@IBMUS                                                                                                          |
  >--------------------------------------------------------------------------------------------------------------------------------------------------|
|------------>
| Date:      |
|------------>
  >--------------------------------------------------------------------------------------------------------------------------------------------------|
  |02/23/2011 12:05 AM                                                                                                                               |
  >--------------------------------------------------------------------------------------------------------------------------------------------------|
|------------>
| Subject:   |
|------------>
  >--------------------------------------------------------------------------------------------------------------------------------------------------|
  |[issue1709] jsr223 bug when dealing with datetime module                                                                                          |
  >--------------------------------------------------------------------------------------------------------------------------------------------------|

Philip Jenvey <pjenvey@underboss.org> added the comment:

Like I said on the ML (
http://old.nabble.com/Re%3A-jython-2.5.2-jsr-223-bug-p30988917.html ), the
fact that the jsr223 ScriptEngineScope has always done this
__tojava__/Py.java2py dance must cause this

datetime.date.__tojava__ returns a java.sql.Date. That doesn't necessarily
mean Py.java2py(java.sql.Date) will give you a datetime.date back

Plus I think this "feature" could cause a significant slowdown of python
code executing with the scope of the jsr223 context. Historically it's
behavior adopted from the old jsr223 implementation. It needs to be
rethought

Anamitra, thanks for the logging this, in your case you may be able to hack
around the problem by disabling datetime.date's tojava conversion by
calling this after importing datetime:

del datetime.date.__tojava__

this issue likely also affects datetime.time/datetime, Decimal and
threading.Thread objects

----------
nosy: +otmarhumbel, pjenvey

_______________________________________
Jython tracker <report@bugs.jython.org>
<http://bugs.jython.org/issue1709>
_______________________________________
History
Date User Action Args
2013-02-19 23:46:05fwierzbickisetpriority: normal
nosy: + fwierzbicki
versions: + Jython 2.5, - 2.5.2rc
2011-02-27 04:53:36anamitrabsetfiles: + unnamed, graycol.gif, ecblank.gif
messages: + msg6411
2011-02-23 05:05:41pjenveysetnosy: + nriley
2011-02-23 05:02:16pjenveysetnosy: + pjenvey, otmarhumbel
messages: + msg6403
2011-02-22 21:00:03anamitrabsetmessages: + msg6402
2011-02-22 20:53:46anamitrabcreate