class / function call behaviour (Jython vs. CPython)

View: New views
1 Messages — Rating Filter:   Alert me  

class / function call behaviour (Jython vs. CPython)

by Georgy Berdyshev :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

lately I have been trying to fix a problem of the different
behaviour of CPython and Jython.
At the moment Jython represents several calls to classes
as function calls.


I had to introduce some ugly changes to PyFrame.java,
PySystemState.java, Py.java and imp.java,
having to hook in between the call of the import statement and properly
setting f_locals and f_globals.
This worked so far to pass several unit tests for Zope, but is not the
right solution.

There is real need to fix this issue and I am looking for help
to get some hints on where to look at the Jython code (asm branch).
(TreeBuilder, CodeVisitor, ScopesCompiler, other compiler parts?
CodeReader?)


Here is some output of CPython 2.5 and Jython svn asm branch r4826:

gebe@venus:~/work/crash$ python test.py
class
********************
f_locals:  {'__module__': '__main__', 'classLevelFrameInfo': ('class',
<module '__main__' from 'test.py'>, {...}, {'kind': 'class',
'f_globals': {...}, 'frameInfo': <function frameInfo at 0xb7d2ad84>,
'__builtins__': <module '__builtin__' (built-in)>, 'ClassType': <type
'classobj'>, '__file__': 'test.py', 'module': <module '__main__' from
'test.py'>, 'f_locals': {...}, 'sys': <module 'sys' (built-in)>,
'ClassicClass': <class __main__.ClassicClass at 0xb7d3465c>, '__name__':
'__main__', '__doc__': None}), '__doc__': None}

f_globals:  {'kind': 'class', 'f_globals': {...}, 'frameInfo': <function
frameInfo at 0xb7d2ad84>, '__builtins__': <module
'__builtin__' (built-in)>, 'ClassType': <type 'classobj'>, '__file__':
'test.py', 'module': <module '__main__' from 'test.py'>, 'f_locals':
{'__module__': '__main__', 'classLevelFrameInfo': ('class', <module
'__main__' from 'test.py'>, {...}, {...}), '__doc__': None}, 'sys':
<module 'sys' (built-in)>, 'ClassicClass': <class __main__.ClassicClass
at 0xb7d3465c>, '__name__': '__main__', '__doc__': None}


gebe@venus:~/work/crash$ jython25 test.py
function call
********************
f_locals:  {'__doc__': None, '__module__': '__main__',
'classLevelFrameInfo': ('function call', <module '__main__' from
'test.py'>, {...}, {'ClassicClass': <class __main__.ClassicClass at 1>,
'sys': sys module, '__file__': 'test.py', '__doc__': None, 'f_globals':
{...}, 'frameInfo': <function frameInfo at 2>, 'kind': 'function call',
'__name__': '__main__', 'f_locals': {...}, 'ClassType': <type 'class'>,
'module': <module '__main__' from 'test.py'>})}

f_globals:  {'ClassicClass': <class __main__.ClassicClass at 1>, 'sys':
sys module, '__file__': 'test.py', '__doc__': None, 'f_globals': {...},
'frameInfo': <function frameInfo at 2>, 'kind': 'function call',
'__name__': '__main__', 'f_locals': {'__doc__': None, '__module__':
'__main__', 'classLevelFrameInfo': ('function call', <module '__main__'
from 'test.py'>, {...}, {...})}, 'ClassType': <type 'class'>, 'module':
<module '__main__' from 'test.py'>}


The code is a modified version of the parts for testing included in
zope.interface:

gebe@venus:~/work/crash$ cat test.py
from myimport import *

from types import ClassType

class ClassicClass:
    classLevelFrameInfo = frameInfo(sys._getframe())


kind, module, f_locals, f_globals = ClassicClass.classLevelFrameInfo

print kind
print "*" * 20
print "f_locals: ", f_locals
print
print "f_globals: ", f_globals

--------------------------------------------------

gebe@venus:~/work/crash$ cat myimport.py
import sys

def frameInfo(frame):
    f_locals = frame.f_locals
    f_globals = frame.f_globals

    sameNamespace = f_locals is f_globals
    hasModule = '__module__' in f_locals
    hasName = '__name__' in f_globals

    sameName = hasModule and hasName
    sameName =sameName and f_globals['__name__']==f_locals['__module__']

    module = hasName and sys.modules.get(f_globals['__name__']) or None

    namespaceIsModule = module and module.__dict__ is f_globals

    if not namespaceIsModule:
        kind = "exec"
    elif sameNamespace and not hasModule:
        kind = "module"
    elif sameName and not sameNamespace:
        kind = "class"
    elif not sameNamespace:
        kind = "function call"

    else:
        kind = "unknown"
   
    return kind, module, f_locals, f_globals



Thanks in advance!


Kind regards, Georgy Berdyshev

--
Georgy Berdyshev - Георгий Бердышев

GPG key: 830F68C5
Fingerprint: 0379 ED5A BEE5 65A8 7BD5 31E7 F5B4 1EC7 830F 68C5


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Jython-dev mailing list
Jython-dev@...
https://lists.sourceforge.net/lists/listinfo/jython-dev
LightInTheBox - Buy quality products at wholesale price