Jython V2.7 동작 변경
Jython V2.7을 사용하도록 Jython V2.1 스크립트를 이식하는 경우 알아야 하는 내용을 검토합니다.
다음은 V2.7에서 알려진 Jython 동작 변경사항입니다. 스크립트가 Jython V2.7에서 올바르게 작동하지 않으면 다음 제안을 사용하여 스크립트를 업데이트하거나 이전 Jython V2.1을 사용하십시오.
- Jython V2.7에서 더 이상 사용되지 않는 라이브러리 모듈
- 문자열 동작 변경
- sys.exit() 함수에 대한 변경
- 모듈 가져오기 변경사항
- 문자열 예외를 발생시키는 방법
- 숫자 유형 변경
Jython V2.7에서 더 이상 사용되지 않는 라이브러리 모듈
WASX7017E: Exception received while running file "c:/test.py"; exception information: com.ibm.bsf.BSFException: exception from Jython:
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named jreload
V2.7에서 사용 가능한 최신
Jython 라이브러리 기능을 사용하도록 Jython 스크립트를 다시 작성하거나
스크립트를 업데이트하지 않으려면 이전 버전의 Jython V2.1을 계속 사용해야 합니다. - dospath
- gopherlib
- javaos
- jreload
- reconvert
- tzparse
- whrandom
javaos 모듈은 os로 대체됩니다. 대부분의 jreload 함수는 V2.7에서 ihooks에 포함됩니다.
문자열 동작 변경
String 처리 동작은 V2.7에서 기본 문자열 유형인 유니코드를 사용합니다. 기존 WebSphere Application Server에는 영향을 주지 않지만, 리턴된 문자열을 문자열 리터럴 u가 앞에 추가된 Jython 유니코드 문자열로 표시합니다(예: u'string). 사용자는 print 명령을 지정하여 일반 문자열을 표시하거나 str() 명령을 호출하여 Jython 유니코드 문자열을 다음과 같은 일반 문자열로 변환할 수 있습니다.
AdminConfig.list('Node')
u'TestCellManager(cells/TestCell/nodes/TestCellManager|node.xml#Node_1)\r\nTestNode(cells/TestCell/nodes/TestNode|node.xml#Node_1)'
# use print command to display regular string
print AdminConfig.list('Node')
TestCellManager(cells/TestCell/nodes/TestCellManager|node.xml#Node_1)
TestNode(cells/TestCell/nodes/TestNode|node.xml#Node_1)
nodes = AdminConfig.list('Node')
u'TestCellManager(cells/TestCell/nodes/TestCellManager|node.xml#Node_1)\r\nTestNode(cells/TestCell/nodes/TestNode|node.xml#Node_1)'
print nodes
TestCellManager(cells/TestCell/nodes/TestCellManager|node.xml#Node_1)
TestNode(cells/TestCell/nodes/TestNode|node.xml#Node_1)
# Call str() command to convert unicode string to regular string
nodes = str(nodes)
nodes 'TestCellManager(cells/TestCell/nodes/TestCellManager|node.xml#Node_1)\r\nTestNode(cells/TestCell/nodes/TestNode|node.xml#Node_1)'
sys.exit() 함수에 대한 변경
sys.exit()는 SystemExit 예외를 발생시키므로, SystemExit 예외가 캡처되리라 예상됩니다. v2.1에서는 SystemExit 예외가 발생하지 않지만, v2.7에서 SystemExit 예외를 캡처하지 않으면 스크립트는 SystemExit 오류로 실패할 수 있습니다.
예제 코드 test.py를 참조하십시오. 여기에서 이 동작을 설명합니다.
test.py:
a = 5
b = 6
if (a > b):
print "a > b"
sys.exit(1)
else:
print " a < b"
sys.exit(0)
다음 예외가 발생합니다.
WASX7017E: Exception received while running file "c:/q.py"; exception information: com.ibm.bsf.BSFException: exception from Jython:
Traceback (most recent call last):
File "<string>", line 12, in <module>
SystemExit: 0
문제점은 다음 해결책 중 하나를 사용하여 피할 수 있습니다.
- SystemExit 예외를 발생시키지 않으려면 sys.exit()를 주석 처리하십시오.
- 예외를 트리거하지 않으려면 sys.exit() 대신에 os._exit()
함수를 사용하십시오.
test.py: import os a = 5 b = 6 if (a > b): print "a > b" os._exit(1) else: print "a < b" os._exit(0)
- SystemExit 오류를 감지하고 예외를 발생시키려면 SystemExit
예외를 캡처하십시오.
test.py: a = 5 b = 6 try: if (a > b): print "a > b" sys.exit(1) else: print "a < b" sys.exit(0) except SystemExit: print "sys.exit() worked as expected" except: print "Something went wrong"
모듈 가져오기 변경사항
Jython 스크립트가 모듈을 가져와야 하는 경우 모듈은 wsadmin 작업 디렉토리에 배치하거나 다른 위치에 배치할 수 있습니다.
예를 들어 다음과 같습니다.
test.py는 다음 행을 포함합니다.
import custom
custom.py는 wsadmin 작업 디렉토리(예: c:\WebSphere\AppServer\profiles\dmgr01\bin)에 배치할 수 있습니다. 여기서 wsadmin이 실행됩니다.
wsadmin -f c:/test.py
wsadmin -profile c:/customscripts/custom.py -f c:/test.py
- test1.py는 profile_root/profile_name 디렉토리에 있음
- test2.py는 profile_root/ 디렉토리에 있음
wsadmin -profile <profile_root/profile_name/test1.py> -profile <profile_root/test2.py> -f c:/test.py
wsadmin.sh -lang jython -javaoption "-Dpython.path=<profile_root/profile_name>;< profile_root>" -f c:/test.py
가져오는 모듈이 wsadmin Admin 명령을 호출하면 NameError: global name 'AdminConfig' is not defined 오류 메시지가 수신될 수 있습니다. 이는 다른 Admin 오브젝트(예: AdminControl, AdminApp, AdminTask 또는 Help)의 문제를 알릴 수도 있습니다. Jython 2.7에서 글로벌 네임스페이스와 Admin 오브젝트는 더 이상 글로벌 네임스페이스에 등록되지 않으므로 대신 시스템 로컬 네임스페이스에서 이를 검색해야 합니다.
def print1():
print "I am custom"
nodes = AdminConfig.list('Node')
print "Nodes: " + nodes
WASX7017E: Exception received while running file "c:/test.py"; exception information: com.ibm.bsf.BSFException: exception from Jython:
Traceback (most recent call last):
File "<string>", line 15, in <module>
File "<string>", line 9, in print2
File "c:\custom.py", line 21, in print1
cell = AdminConfig.list('Cell')
NameError: global name 'AdminConfig' is not defined
#custom.py:
import sys
# Retrieve scripting objects from system local name space
AdminConfig = sys._getframe(1).f_locals['AdminConfig']
AdminApp = sys._getframe(1).f_locals['AdminApp']
AdminControl = sys._getframe(1).f_locals['AdminControl']
AdminTask = sys._getframe(1).f_locals['AdminTask']
Help = sys._getframe(1).f_locals['Help']
def print1():
print "I am custom"
nodes = AdminConfig.list('Node')
print "Nodes: " + nodes
문자열 예외를 발생시키는 방법
Jython V2.7은 문자열 예외 발생을 허용하지 않으므로 ValueError, AttributeError, TypeError와 같은 예외를 발생시키거나 Jython 클래스에서 고유한 오류 유형을 작성해야 합니다.
예를 들어 다음과 같습니다.
test.py:
nodeName = "testNode1"
if (nodeName != "testNode2"):
raise "Could not find node name '%s'" % (nodeName)
Jython 2.1에서 작동하지만, Jython V2.7에서 다음 TypeError를 수신합니다.
WASX7017E: Exception received while running file "c:/p.py"; exception information: com.ibm.bsf.BSFException: exception from Jython:
Traceback (most recent call last):
File "<string>", line 5, in <module>
TypeError: exceptions must be old-style classes or derived from BaseException, not str
예외 또는 ValueError/TypeError/AttributeError로 발생시키려면 스크립트를 다시 작성해야 합니다.
- 예외 발생:
if (nodeName != "testNode2"): raise Exception("Could not find node name '%s'" % (nodeName))
- ValueError 발생:
if (nodeName != "testNode2"): raise ValueError("Could not find node name '%s'" % (nodeName))
- Jython 클래스에서 고유한 오류 유형 작성:
class MyError(Exception): '''raise this when there's an error from my command''' if nodeName != "testNode2": raise MyError("Could not find node name '%s'" % (nodeName))
숫자 유형 변경
Jython V2.1에서 numeric 유형은 Py 오브젝트(예: PyInteger 또는 PyFloat)였고, toString()과 같은 메소드를 포함합니다. Jython V2.7에서 numeric 유형은 native 유형과 더 유사하며, 사용 가능한 이러한 기본 Object 메소드는 포함하지 않습니다. 차이점은 정수가 Jython 함수로 전달되고 toString() 메소드를 사용하여 해당 값을 인쇄할 때 드러납니다.
Jython V2.7에서 다음 샘플 코드를 사용합니다.
foo = 3.1415
print "foo =", foo.toString()
foo =
다음 오류가 발생합니다.
WASX7015E: Exception running command: ""foo =", foo.toString()"; exception information:
com.ibm.bsf.BSFException: exception from Jython:
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'float' object has no attribute 'toString'
다음 명령을 사용하여 numeric 유형을 표시할 수 있습니다.
print "foo = %f" % foo
foo = 3.141500