[Jython-checkins] jython (merge default -> default): Merge misc improvements to import
jeff.allen
jython-checkins at python.org
Mon May 4 23:24:14 CEST 2015
https://hg.python.org/jython/rev/6855fa289d48
changeset: 7704:6855fa289d48
parent: 7697:412a8f9445f7
parent: 7703:cb5bc04f17d1
user: Jeff Allen <ja.py at farowl.co.uk>
date: Mon May 04 22:23:58 2015 +0100
summary:
Merge misc improvements to import
files:
Lib/test/symlink_support.py | 101 +
Lib/test/test_import_pep328.py | 281 +++-
NEWS | 5 +
src/org/python/compiler/CodeCompiler.java | 595 ++++-----
src/org/python/core/imp.java | 536 ++++----
src/org/python/modules/posix/PosixModule.java | 8 +-
6 files changed, 847 insertions(+), 679 deletions(-)
diff --git a/Lib/test/symlink_support.py b/Lib/test/symlink_support.py
new file mode 100644
--- /dev/null
+++ b/Lib/test/symlink_support.py
@@ -0,0 +1,101 @@
+import os
+import unittest
+import platform
+
+from test.test_support import TESTFN
+
+def can_symlink():
+ # cache the result in can_symlink.prev_val
+ prev_val = getattr(can_symlink, 'prev_val', None)
+ if prev_val is not None:
+ return prev_val
+ symlink_path = TESTFN + "can_symlink"
+ try:
+ symlink(TESTFN, symlink_path)
+ can = True
+ except (OSError, NotImplementedError, AttributeError, TypeError):
+ # Not allowed, not implemented, or symlink is None.
+ can = False
+ else:
+ os.remove(symlink_path)
+ can_symlink.prev_val = can
+ return can
+
+def skip_unless_symlink(test):
+ """Skip decorator for tests that require functional symlink"""
+ ok = can_symlink()
+ msg = "Requires functional symlink implementation"
+ return test if ok else unittest.skip(msg)(test)
+
+def _symlink_win32(target, link, target_is_directory=False):
+ """
+ Ctypes symlink implementation since Python doesn't support
+ symlinks in windows yet. Borrowed from jaraco.windows project.
+ """
+ import ctypes.wintypes
+ CreateSymbolicLink = ctypes.windll.kernel32.CreateSymbolicLinkW
+ CreateSymbolicLink.argtypes = (
+ ctypes.wintypes.LPWSTR,
+ ctypes.wintypes.LPWSTR,
+ ctypes.wintypes.DWORD,
+ )
+ CreateSymbolicLink.restype = ctypes.wintypes.BOOLEAN
+
+ def format_system_message(errno):
+ """
+ Call FormatMessage with a system error number to retrieve
+ the descriptive error message.
+ """
+ # first some flags used by FormatMessageW
+ ALLOCATE_BUFFER = 0x100
+ ARGUMENT_ARRAY = 0x2000
+ FROM_HMODULE = 0x800
+ FROM_STRING = 0x400
+ FROM_SYSTEM = 0x1000
+ IGNORE_INSERTS = 0x200
+
+ # Let FormatMessageW allocate the buffer (we'll free it below)
+ # Also, let it know we want a system error message.
+ flags = ALLOCATE_BUFFER | FROM_SYSTEM
+ source = None
+ message_id = errno
+ language_id = 0
+ result_buffer = ctypes.wintypes.LPWSTR()
+ buffer_size = 0
+ arguments = None
+ bytes = ctypes.windll.kernel32.FormatMessageW(
+ flags,
+ source,
+ message_id,
+ language_id,
+ ctypes.byref(result_buffer),
+ buffer_size,
+ arguments,
+ )
+ # note the following will cause an infinite loop if GetLastError
+ # repeatedly returns an error that cannot be formatted, although
+ # this should not happen.
+ handle_nonzero_success(bytes)
+ message = result_buffer.value
+ ctypes.windll.kernel32.LocalFree(result_buffer)
+ return message
+
+ def handle_nonzero_success(result):
+ if result == 0:
+ value = ctypes.windll.kernel32.GetLastError()
+ strerror = format_system_message(value)
+ raise WindowsError(value, strerror)
+
+ target_is_directory = target_is_directory or os.path.isdir(target)
+ handle_nonzero_success(CreateSymbolicLink(link, target, target_is_directory))
+
+symlink = os.symlink if hasattr(os, 'symlink') else (
+ _symlink_win32 if platform.system() == 'Windows' else None
+)
+
+def remove_symlink(name):
+ # On Windows, to remove a directory symlink, one must use rmdir
+ try:
+ os.rmdir(name)
+ except OSError:
+ os.remove(name)
diff --git a/Lib/test/test_import_pep328.py b/Lib/test/test_import_pep328.py
--- a/Lib/test/test_import_pep328.py
+++ b/Lib/test/test_import_pep328.py
@@ -33,33 +33,50 @@
import sys
import types
+EXPLAIN = False # If True, produce commentary in TestImportFunction
+MAXDOTS = 10 # adjust enthusiasm of dotted tests
+
+def dump_module(m):
+ "Print values of attributes relevant to import mechanism"
+ if isinstance(m, types.ModuleType):
+ m = m.__dict__
+ print " Module name: {}".format(m.get('__name__', ''))
+ for n in ['__package__', '__path__']:
+ print " {:12s} = {}".format(n, m.get(n,''))
+
origImport = __import__
-class TestImportStatementError(exceptions.ImportError):
+class TestImportStatementTell(exceptions.ImportError):
+ # Raised by TestImportStatement.importFunction to tell us how it was called
def __init__(self, args):
+ # Smuggle the arguments of importFunction() through the call stack
+ if EXPLAIN: print "\nimport:"
names = ['name', 'globals', 'locals', 'fromlist', 'level']
self.len = len(args)
for a in args:
n = names.pop(0)
setattr(self, n, a)
+ if EXPLAIN:
+ too_long = not isinstance(a, (int, tuple, str, unicode))
+ print " {:12s}= {}".format(n, a if not too_long else type(a))
for n in names:
setattr(self, n, None)
class TestImportStatement(unittest.TestCase):
"""Test the 'import' and 'from ... import' statements
-
+
This class tests, how the compiler calls the
'__import__'-function for various forms of the
'import' and 'from ... import' statements.
"""
-
+
AI = "from __future__ import absolute_import ;"
def importFunction(*args):
if args[0] == '__future__':
return origImport(*args)
- raise TestImportStatementError(args)
+ raise TestImportStatementTell(args)
importFunction = staticmethod(importFunction)
def setUp(self):
@@ -73,73 +90,79 @@
g = {}
try:
exec statement in g, l
- except TestImportStatementError,e:
+ except TestImportStatementTell, e:
self.assert_(e.globals is g, "globals is changed")
self.assert_(e.locals is l, "locals is changed")
return e
- self.fail("Expected a TestImportStatementError")
-
+ self.fail("Expected a TestImportStatementTell")
+
+ @staticmethod
+ def dotrange(n=MAXDOTS):
+ "Return a longer sequence of dots in each iteration"
+ for i in range(1, n):
+ yield '.'*i
+
def testFromDotsOnly(self):
- dots = ''
- for i in range(1,10):
- dots += '.'
+ for dots in self.dotrange():
a = self.runImport("from %s import (A,B)" % (dots,))
self.assertEqual(a.len, 5)
self.assertEqual(a.name, "")
- self.assertEqual(a.level, i)
+ self.assertEqual(a.level, len(dots))
self.assertEqual(a.fromlist, ('A', 'B'))
def testFromDotsOnlyAs(self):
- dots = ''
- for i in range(1,10):
- dots += '.'
+ for dots in self.dotrange():
a = self.runImport("from %s import A as B" % (dots,))
self.assertEqual(a.len, 5)
self.assertEqual(a.name, "")
self.assertEqual(a.fromlist, ('A',))
- self.assertEqual(a.level, i)
+ self.assertEqual(a.level, len(dots))
def testFromDotsAndName(self):
- dots = ''
- for i in range(1,10):
- dots += '.'
+ for dots in self.dotrange():
a = self.runImport("from %sX import A" % (dots,))
self.assertEqual(a.len, 5)
self.assertEqual(a.name, "X")
self.assertEqual(a.fromlist, ('A',))
- self.assertEqual(a.level, i)
+ self.assertEqual(a.level, len(dots))
- def testFromDotsAndDotedName(self):
- dots = ''
- for i in range(1,10):
- dots += '.'
+ def testFromDotsAndDottedName(self):
+ for dots in self.dotrange():
a = self.runImport("from %sX.Y import A" % (dots,))
self.assertEqual(a.len, 5)
self.assertEqual(a.name, "X.Y")
self.assertEqual(a.fromlist, ('A',))
- self.assertEqual(a.level, i)
-
- def testAbsoluteFromDotedNameAs(self):
+ self.assertEqual(a.level, len(dots))
+
+ def testFromDotsAndDottedNameAll(self):
+ for dots in self.dotrange():
+ a = self.runImport("from %sX.Y import *" % (dots,))
+ self.assertEqual(a.len, 5, "level argument elided") # Issue 2158
+ self.assertEqual(a.name, "X.Y")
+ self.assertEqual(a.fromlist, ('*',))
+ self.assertEqual(a.level, len(dots))
+
+ def testAbsoluteFromDottedNameAs(self):
a = self.runImport(self.AI + "from X.Y import A as B")
self.assertEqual(a.len, 5)
self.assertEqual(a.name, "X.Y")
self.assertEqual(a.fromlist, ('A',))
self.assertEqual(a.level, 0)
- def testRelativeOrAbsoluteFromDotedNameAs(self):
+ def testRelativeOrAbsoluteFromDottedNameAs(self):
a = self.runImport("from X.Y import A as B")
self.assertEqual(a.name, "X.Y")
self.assertEqual(a.fromlist, ('A',))
self.assertEqual(a.len, 4)
- def testAbsoluteFromDotedNameAll(self):
+ def testAbsoluteFromDottedNameAll(self):
a = self.runImport(self.AI + "from X.Y import *")
self.assertEqual(a.len, 5)
self.assertEqual(a.name, "X.Y")
self.assertEqual(a.fromlist, ('*',))
self.assertEqual(a.level, 0)
- def testRelativeOrAbsoluteFromDotedNameAll(self):
+ def testRelativeOrAbsoluteFromDottedNameAll(self):
a = self.runImport("from X.Y import *")
self.assertEqual(a.name, "X.Y")
self.assertEqual(a.fromlist, ('*',))
@@ -152,26 +175,26 @@
self.assertEqual(a.fromlist, None)
self.assertEqual(a.level, 0)
- def testAbsoluteImportDotedName(self):
+ def testAbsoluteImportDottedName(self):
a = self.runImport(self.AI + "import X.Y")
self.assertEqual(a.len, 5)
self.assertEqual(a.name, "X.Y")
self.assertEqual(a.fromlist, None)
self.assertEqual(a.level, 0)
-
+
def testRelativeOrAbsoluteImportName(self):
a = self.runImport("import X")
self.assertEqual(a.name, "X")
self.assertEqual(a.fromlist, None)
self.assertEqual(a.len, 4)
- def testRelativeOrAbsoluteImportDotedName(self):
+ def testRelativeOrAbsoluteImportDottedName(self):
a = self.runImport("import X.Y")
self.assertEqual(a.name, "X.Y")
self.assertEqual(a.fromlist, None)
self.assertEqual(a.len, 4)
- def testAbsoluteImportDotedNameAs(self):
+ def testAbsoluteImportDottedNameAs(self):
a = self.runImport(self.AI + "import X.Y as Z")
self.assertEqual(a.len, 5)
self.assertEqual(a.name, "X.Y")
@@ -179,46 +202,53 @@
self.assertEqual(a.level, 0)
-class TestImportFunctionError(exceptions.ImportError):
+class TestImportFunctionSuccess(exceptions.ImportError):
pass
class TestImportFunction(unittest.TestCase):
"""Test the '__import__' function
-
- This class tests, how the '__import__'-function
- resolves module names. It uses the 'meta_path' hook,
- to intercept the actual module loading.
-
- Module Structure:
-
+
+ This class tests, how the '__import__'-function resolves module names.
+ It uses the 'meta_path' hook, to intercept the actual module loading.
+ When consulted through find_module, it claims to have access to the
+ following module structure:
+
Top
- \---- X package
- | \-- Y package
- | | \-- Z1 module
- | | \-- Z2 module
- | \-- Y2 package
- \---- X2 module
-
+ +---- X package
+ | +-- Y package
+ | | +-- Z1 module
+ | | +-- Z2 module
+ | +-- Y2 package
+ +---- X2 module
+
"""
nameX = "TestImportFunctionX"
-
def setUp(self):
- self.modX = imp.new_module(self.nameX)
- self.modX.__path__ = ['X']
-
- self.modX2 = imp.new_module(self.nameX+"2")
- self.modY = imp.new_module(self.nameX+".Y")
- self.modY.__path__ = ['X/Y']
- self.modY2 = imp.new_module(self.nameX+".Y2")
- self.modY2.__path__ = ['X/Y']
- self.modZ1 = imp.new_module(self.nameX+".Y.Z1")
- self.modZ2 = imp.new_module(self.nameX+".Y.Z2")
-
+ self.modX = self._new_module(None, self.nameX, True)
+ self.modY = self._new_module(self.modX, "Y", True)
+ self.modZ1 = self._new_module(self.modY, "Z1")
+ self.modZ2 = self._new_module(self.modY, "Z2")
+ self.modY2 = self._new_module(self.modX, "Y2", True)
+ self.modX2 = self._new_module(None, self.nameX + "2")
self.expected = "something_completely_different"
sys.meta_path.insert(0, self)
-
+
+ @staticmethod
+ def _new_module(in_package, name, is_package=False):
+ if not in_package:
+ m = imp.new_module(name)
+ else:
+ m = imp.new_module(in_package.__name__ + '.' + name)
+ if is_package:
+ m.__package__ = m.__name__ # surprisingly not the parent name
+ else:
+ m.__package__ = in_package.__name__
+ if is_package:
+ m.__path__ = [m.__name__.replace('.', '/')]
+ return m
+
def tearDown(self):
try:
sys.meta_path.remove(self)
@@ -227,184 +257,239 @@
for k in sys.modules.keys():
if k.startswith(self.nameX):
del sys.modules[k]
-
+
def importX(self):
sys.modules[self.modX.__name__] = self.modX
+
def importX2(self):
sys.modules[self.modX2.__name__] = self.modX2
+
def importY(self):
self.importX()
sys.modules[self.modY.__name__] = self.modY
self.modX.Y = self.modY
+
def importY2(self):
self.importX()
sys.modules[self.modY2.__name__] = self.modY2
self.modX.Y2 = self.modY2
+
def importZ1(self):
self.importY()
sys.modules[self.modZ1.__name__] = self.modZ1
self.modY.Z1 = self.modZ1
- def top(self):
- if sys.modules.has_key("__main__"):
- return sys.modules["__main__"].__dict__
- return globals()
+ @staticmethod
+ def top():
+ "Return the __dict__ of a non-package, top-level module"
+ # When this program runs as python -m test.test_import_pep328, it is
+ # called __main__, but is inside package test, so we must fake it.
+ myName = TestImportFunction.nameX[:-1] + "Top"
+ return {'__name__': myName, '__package__': None, '__file__': None}
def find_module(self, fullname, path=None):
+ # Simulate the operation of a module finder object on the sys.meta_path
+ if EXPLAIN:
+ print "find_module:"
+ print " fullname =", fullname
+ print " path =", path
if self.expected and self.expected != fullname:
+ # Equivalent of "import name" was called and the import mechanism is
+ # trying something other than the expected full name. For example, X
+ # called "import X2", and something other than X2 is tried (first).
return None
self.fullname = fullname
self.path = path
return self
-
+
def load_module(self, fullname):
+ # Masquerade as the loader matching fullname
self.assertEqual(fullname, self.fullname)
- raise TestImportFunctionError()
-
+ # Signal success, disguised as an ImportError
+ raise TestImportFunctionSuccess()
+
def runImport(self, expected, name, globals, fromlist=None, level=None):
self.expected = expected
if isinstance(globals, types.ModuleType):
globals = globals.__dict__
+ if EXPLAIN:
+ print "\nrunImport:"
+ dotname = ('.'*level if level>0 else '') + name
+ callername = globals['__name__']
+ callerpkg = globals.get('__package__', None)
+ if fromlist:
+ print " from {} import {} # in {} in package {}".format(
+ dotname, fromlist, callername, callerpkg)
+ else:
+ print " import {} # in {} in package {}".format(
+ dotname, callername, callerpkg)
try:
if level is not None:
__import__(name, globals, None, fromlist, level)
else:
- __import__(name, globals, None, fromlist)
- except TestImportFunctionError:
+ __import__(name, globals, None, fromlist)
+ except TestImportFunctionSuccess:
return
- self.fail("Expected a TestImportFunctionError")
-
+ self.fail("Expected a TestImportFunctionSuccess")
+
def testRelativeOrAbsolute_top_X2_1(self):
+ # In context of a non-package, top-level module, find X2.
+ # The finder should only be consulted with the absolute name.
self.runImport(None, self.modX2.__name__, self.top())
self.assertEqual(self.fullname, self.modX2.__name__)
self.assertEqual(self.path, None)
def testRelativeOrAbsolute_top_X2_2(self):
+ # In context of a non-package, top-level module, find X2.
+ # The finder should only be consulted with the absolute name.
self.runImport(None, self.modX2.__name__, self.top(), None, -1)
self.assertEqual(self.fullname, self.modX2.__name__)
self.assertEqual(self.path, None)
-
def testRelativeOrAbsolute_top_Y_1(self):
+ # In context of a non-package, top-level module, find X.Y.
+ # The finder should only be consulted with the absolute name.
self.importX()
self.runImport(None, self.modY.__name__, self.top())
self.assertEqual(self.fullname, self.modY.__name__)
- self.assertEqual(self.path, ['X'])
+ self.assertEqual(self.path, [self.nameX])
def testRelativeOrAbsolute_top_Y_2(self):
+ # In context of a non-package, top-level module, find X.Y.
+ # The finder should only be consulted with the absolute name.
self.importX()
self.runImport(None, self.modY.__name__, self.top(), None, -1)
self.assertEqual(self.fullname, self.modY.__name__)
- self.assertEqual(self.path, ['X'])
-
+ self.assertEqual(self.path, [self.nameX])
def testAbsolute_top_X2(self):
+ # In context of a non-package, top-level module, find X2 absolutely.
self.runImport(None, self.modX2.__name__, globals(), None, 0)
self.assertEqual(self.fullname, self.modX2.__name__)
self.assertEqual(self.path, None)
def testAbsolute_top_Y(self):
+ # In context of a non-package, top-level module, find X.Y absolutely.
self.importX()
self.runImport(None, self.modY.__name__, globals(), None, 0)
self.assertEqual(self.fullname, self.modY.__name__)
- self.assertEqual(self.path, ['X'])
+ self.assertEqual(self.path, [self.nameX])
# Relative case
def testRelativeOrAbsolute_X_X2_rel1(self):
+ # In context of package X, look for X2 at X.X2 (where actually it isn't).
self.importX()
self.runImport(None, self.modX2.__name__, self.modX)
self.assertEqual(self.fullname, self.nameX + "." + self.modX2.__name__)
- self.assertEqual(self.path, ['X'])
+ self.assertEqual(self.path, [self.nameX])
def testRelativeOrAbsolute_X_X2_rel2(self):
+ # In context of package X, look for X2 at X.X2 (where actually it isn't).
self.importX()
self.runImport(None, self.modX2.__name__, self.modX, None, -1)
- self.assertEqual(self.path, ['X'])
+ self.assertEqual(self.path, [self.nameX])
self.assertEqual(self.fullname, self.nameX + "." + self.modX2.__name__)
# Absolute case
def testRelativeOrAbsolute_X_X2_abs1(self):
+ # In context of package X, find X2 at absolute X2 (on second attempt).
self.importX()
self.runImport(self.modX2.__name__, self.modX2.__name__, self.modX)
self.assertEqual(self.fullname, self.modX2.__name__)
self.assertEqual(self.path, None)
def testRelativeOrAbsolute_X_X2_abs2(self):
+ # In context of package X, find X2 at absolute X2 (on second attempt).
self.importX()
self.runImport(self.modX2.__name__, self.modX2.__name__, self.modX, None, -1)
self.assertEqual(self.path, None)
self.assertEqual(self.fullname, self.modX2.__name__)
def testAbsolute_X_X2(self):
+ # In context of package X, find X2 at explicitly absolute X2.
self.importX()
self.runImport(None, self.modX2.__name__, self.modX, None, 0)
self.assertEqual(self.fullname, self.modX2.__name__)
self.assertEqual(self.path, None)
def testAbsolute_X_Y(self):
+ # In context of package X, find Y at explicitly absolute X.Y.
self.importX()
self.runImport(None, self.modY.__name__, self.modX, None, 0)
self.assertEqual(self.fullname, self.modY.__name__)
- self.assertEqual(self.path, ['X'])
+ self.assertEqual(self.path, [self.nameX])
def testRelative_Z1_Z2(self):
+ # In context of module Z1, from . import Z2.
self.importZ1()
self.runImport(None, "", self.modZ1, ['Z2'], 1)
self.assertEqual(self.fullname, self.modZ2.__name__)
- self.assertEqual(self.path, ['X/Y'])
+ self.assertEqual(self.path, [self.nameX + '/Y'])
def testRelative_Z1_Y2(self):
+ # In context of module Z1, from .. import Y2.
self.importZ1()
self.runImport(None, "", self.modZ1, ["Y2"], 2)
- self.assertEqual(self.fullname, self.modX.__name__+".Y2")
- self.assertEqual(self.path, ['X'])
+ self.assertEqual(self.fullname, self.modX.__name__ + ".Y2")
+ self.assertEqual(self.path, [self.nameX])
def testRelative_Z1_X2(self):
- # """beyond top level"""
+ # In context of module Z1, from ... import X2 (incorrectly beyond top level).
self.importZ1()
- self.assertRaises(ValueError, self.runImport, None, "", self.modZ1, [self.modX2.__name__], 3)
+ with self.assertRaises(ValueError):
+ self.runImport(None, "", self.modZ1, [self.modX2.__name__], 3)
def testRelative_X2_X(self):
- # """not a package"""
+ # In context of module X2, from . import X (incorrectly)
+ # This is incorrect as X2 is not in a package (is a top-level module).
self.importX2()
- self.assertRaises(ValueError, self.runImport, None, "", self.modX2, [self.modX.__name__], 1)
+ with self.assertRaises(ValueError):
+ self.runImport(None, "", self.modX2, [self.modX.__name__], 1)
def testRelative_X2_Y(self):
- # """not a package"""
+ # In context of module X2, from .X import Y (incorrectly).
+ # This is incorrect as X2 is not in a package (is a top-level module).
self.importX2()
self.importX()
- self.assertRaises(ValueError, self.runImport, None, self.modX.__name__, self.modX2, ["Y"], 1)
+ with self.assertRaises(ValueError):
+ self.runImport(None, self.modX.__name__, self.modX2, ["Y"], 1)
def testRelative_X_Z1_1(self):
+ # In context of package X, from .Y import Z1.
self.importX()
self.runImport(None, "Y", self.modX, ['Z1'], 1)
self.assertEqual(self.fullname, self.modY.__name__)
- self.assertEqual(self.path, ['X'])
+ self.assertEqual(self.path, [self.nameX])
def testRelative_X_Z1_2(self):
+ # In context of package X, from .Y import Z1.
self.importY()
self.runImport(None, "Y", self.modX, ['Z1'], 1)
self.assertEqual(self.fullname, self.modZ1.__name__)
- self.assertEqual(self.path, ['X/Y'])
+ self.assertEqual(self.path, [self.nameX + '/Y'])
def testRelative_Y_Z1(self):
+ # In context of package Y: from .Z1 import A, B.
self.importY()
self.runImport(None, "Z1", self.modY, ['A', 'B'], 1)
self.assertEqual(self.fullname, self.modZ1.__name__)
- self.assertEqual(self.path, ['X/Y'])
+ self.assertEqual(self.path, [self.nameX + '/Y'])
def testRelative_Y2_Z1_1(self):
+ # In context of package Y2, from ..Y import Z1.
self.importY2()
self.runImport(None, "Y", self.modY2, ['Z1'], 2)
self.assertEqual(self.fullname, self.modY.__name__)
- self.assertEqual(self.path, ['X'])
+ self.assertEqual(self.path, [self.nameX])
def testRelative_Y2_Z1_2(self):
+ # In context of package Y2, from ..Y import Z1.
self.importY2()
self.importY()
self.runImport(None, "Y", self.modY2, ['Z1'], 2)
self.assertEqual(self.fullname, self.modZ1.__name__)
- self.assertEqual(self.path, ['X/Y'])
+ self.assertEqual(self.path, [self.nameX + '/Y'])
+
try:
from test import test_support
@@ -412,8 +497,10 @@
test_main = unittest.main
else:
def test_main():
- test_support.run_unittest(TestImportStatement,
- TestImportFunction)
+ test_support.run_unittest(
+ TestImportStatement,
+ TestImportFunction,
+ )
if __name__ == '__main__':
test_main()
diff --git a/NEWS b/NEWS
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,11 @@
For more details, please see https://hg.python.org/jython
+ Bugs fixed
+ - [ 2310 ] test_import runs on Windows (and passes with 4 skips).
+ - [ 2347 ] failures in test_import_pep328 when run with -m
+ - [ 2158, 2259 ] Fixed behaviour of relative from ... import *
+
Jython 2.7rc3
Bugs fixed
- [ 2311, 2319 ] Many compatibility fixes for launcher (bin/jython, bin/jython.exe)
diff --git a/src/org/python/compiler/CodeCompiler.java b/src/org/python/compiler/CodeCompiler.java
--- a/src/org/python/compiler/CodeCompiler.java
+++ b/src/org/python/compiler/CodeCompiler.java
@@ -126,12 +126,11 @@
private Vector<Label> yields = new Vector<Label>();
/*
- * break/continue finally's level. This is the lowest level in the
- * exceptionHandlers which should be executed at break or continue. It is
- * saved/updated/restored when compiling loops. A similar level for
- * returns is not needed because a new CodeCompiler is used for each
- * PyCode, in other words: each 'function'. When returning through
- * finally's all the exceptionHandlers are executed.
+ * break/continue finally's level. This is the lowest level in the exceptionHandlers which
+ * should be executed at break or continue. It is saved/updated/restored when compiling loops. A
+ * similar level for returns is not needed because a new CodeCompiler is used for each PyCode,
+ * in other words: each 'function'. When returning through finally's all the exceptionHandlers
+ * are executed.
*/
private int bcfLevel = 0;
private int yield_count = 0;
@@ -253,7 +252,7 @@
}
void parse(mod node, Code code, boolean fast_locals, String className, Str classDoc,
- boolean classBody, ScopeInfo scope, CompilerFlags cflags) throws Exception {
+ boolean classBody, ScopeInfo scope, CompilerFlags cflags) throws Exception {
this.fast_locals = fast_locals;
this.className = className;
this.code = code;
@@ -261,7 +260,7 @@
this.my_scope = scope;
this.tbl = scope.tbl;
- //BEGIN preparse
+ // BEGIN preparse
if (classBody) {
// Set the class's __module__ to __name__. fails when there's no __name__
loadFrame();
@@ -270,15 +269,15 @@
loadFrame();
code.ldc("__name__");
code.invokevirtual(p(PyFrame.class), "getname", sig(PyObject.class, String.class));
- code.invokevirtual(p(PyFrame.class), "setlocal", sig(Void.TYPE, String.class,
- PyObject.class));
+ code.invokevirtual(p(PyFrame.class), "setlocal",
+ sig(Void.TYPE, String.class, PyObject.class));
if (classDoc != null) {
loadFrame();
code.ldc("__doc__");
visit(classDoc);
- code.invokevirtual(p(PyFrame.class), "setlocal", sig(Void.TYPE, String.class,
- PyObject.class));
+ code.invokevirtual(p(PyFrame.class), "setlocal",
+ sig(Void.TYPE, String.class, PyObject.class));
}
}
@@ -297,11 +296,11 @@
SymInfo syminf = tbl.get(paramcells.get(i));
code.iconst(syminf.locals_index);
code.iconst(syminf.env_index);
- code.invokevirtual(p(PyFrame.class), "to_cell", sig(Void.TYPE, Integer.TYPE,
- Integer.TYPE));
+ code.invokevirtual(p(PyFrame.class), "to_cell",
+ sig(Void.TYPE, Integer.TYPE, Integer.TYPE));
}
}
- //END preparse
+ // END preparse
optimizeGlobals = checkOptimizeGlobals(fast_locals, my_scope);
@@ -329,7 +328,7 @@
}
}
- //BEGIN postparse
+ // BEGIN postparse
// similar to visitResume code in pyasm.py
if (my_scope.generator) {
@@ -345,7 +344,7 @@
}
code.tableswitch(0, y.length - 1, start, y);
}
- //END postparse
+ // END postparse
}
@Override
@@ -361,8 +360,8 @@
loadFrame();
code.ldc("__doc__");
visit(docStr);
- code.invokevirtual(p(PyFrame.class), "setglobal", sig(Void.TYPE, String.class,
- PyObject.class));
+ code.invokevirtual(p(PyFrame.class), "setglobal",
+ sig(Void.TYPE, String.class, PyObject.class));
}
traverse(suite);
return null;
@@ -371,8 +370,7 @@
@Override
public Object visitExpression(Expression node) throws Exception {
if (my_scope.generator && node.getInternalBody() != null) {
- module.error("'return' with argument inside generator",
- true, node);
+ module.error("'return' with argument inside generator", true, node);
}
return visitReturn(new Return(node, node.getInternalBody()), true);
}
@@ -449,8 +447,8 @@
public Str getDocStr(java.util.List<stmt> suite) {
if (suite.size() > 0) {
stmt stmt = suite.get(0);
- if (stmt instanceof Expr && ((Expr) stmt).getInternalValue() instanceof Str) {
- return (Str) ((Expr) stmt).getInternalValue();
+ if (stmt instanceof Expr && ((Expr)stmt).getInternalValue() instanceof Str) {
+ return (Str)((Expr)stmt).getInternalValue();
}
}
return null;
@@ -510,9 +508,8 @@
scope.setup_closure();
scope.dump();
- module.codeConstant(new Suite(node, node.getInternalBody()), name, true,
- className, false, false,
- node.getLine(), scope, cflags).get(code);
+ module.codeConstant(new Suite(node, node.getInternalBody()), name, true, className, false,
+ false, node.getLine(), scope, cflags).get(code);
Str docStr = getDocStr(node.getInternalBody());
if (docStr != null) {
@@ -522,11 +519,14 @@
}
if (!makeClosure(scope)) {
- code.invokespecial(p(PyFunction.class), "<init>", sig(Void.TYPE, PyObject.class,
- PyObject[].class, PyCode.class, PyObject.class));
+ code.invokespecial(p(PyFunction.class), "<init>",
+ sig(Void.TYPE, PyObject.class, PyObject[].class, PyCode.class, PyObject.class));
} else {
- code.invokespecial(p(PyFunction.class), "<init>", sig(Void.TYPE, PyObject.class,
- PyObject[].class, PyCode.class, PyObject.class, PyObject[].class));
+ code.invokespecial(
+ p(PyFunction.class),
+ "<init>",
+ sig(Void.TYPE, PyObject.class, PyObject[].class, PyCode.class, PyObject.class,
+ PyObject[].class));
}
applyDecorators(node.getInternalDecorator_list());
@@ -546,8 +546,8 @@
stackConsume();
loadThreadState();
code.aload(res);
- code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class,
- ThreadState.class, PyObject.class));
+ code.invokevirtual(p(PyObject.class), "__call__",
+ sig(PyObject.class, ThreadState.class, PyObject.class));
code.astore(res);
}
code.aload(res);
@@ -606,19 +606,18 @@
code.aload(tmp);
visit(node.getInternalValues().get(i));
if (node.getInternalNl() && i == node.getInternalValues().size() - 1) {
- code.invokestatic(p(Py.class), "println", sig(Void.TYPE, PyObject.class,
- PyObject.class));
+ code.invokestatic(p(Py.class), "println",
+ sig(Void.TYPE, PyObject.class, PyObject.class));
} else {
- code.invokestatic(p(Py.class), "printComma", sig(Void.TYPE, PyObject.class,
- PyObject.class));
+ code.invokestatic(p(Py.class), "printComma",
+ sig(Void.TYPE, PyObject.class, PyObject.class));
}
} else {
visit(node.getInternalValues().get(i));
if (node.getInternalNl() && i == node.getInternalValues().size() - 1) {
code.invokestatic(p(Py.class), "println", sig(Void.TYPE, PyObject.class));
} else {
- code.invokestatic(p(Py.class), "printComma", sig(Void.TYPE,
- PyObject.class));
+ code.invokestatic(p(Py.class), "printComma", sig(Void.TYPE, PyObject.class));
}
}
@@ -645,7 +644,7 @@
@Override
public Object visitBreak(Break node) throws Exception {
- //setline(node); Not needed here...
+ // setline(node); Not needed here...
if (breakLabels.empty()) {
throw new ParseException("'break' outside loop", node);
}
@@ -658,7 +657,7 @@
@Override
public Object visitContinue(Continue node) throws Exception {
- //setline(node); Not needed here...
+ // setline(node); Not needed here...
if (continueLabels.empty()) {
throw new ParseException("'continue' not properly in loop", node);
}
@@ -804,11 +803,10 @@
}
/**
- * Close all the open exception handler ranges. This should be paired
- * with restartExceptionHandlers to delimit internal code that
- * shouldn't be handled by user handlers. This allows us to set
- * variables without the verifier thinking we might jump out of our
- * handling with an exception.
+ * Close all the open exception handler ranges. This should be paired with
+ * restartExceptionHandlers to delimit internal code that shouldn't be handled by user handlers.
+ * This allows us to set variables without the verifier thinking we might jump out of our
+ * handling with an exception.
*/
private void endExceptionHandlers() {
Label end = new Label();
@@ -842,7 +840,7 @@
}
code.aload(locals);
code.iconst(i);
- //code.checkcast(code.pool.Class(p(Object.class)));
+ // code.checkcast(code.pool.Class(p(Object.class)));
if (i == 2222) {
code.aconst_null();
} else {
@@ -870,8 +868,7 @@
int tmp = 0;
if (node.getInternalValue() != null) {
if (my_scope.generator && !(node instanceof LambdaSyntheticReturn)) {
- throw new ParseException("'return' with argument " +
- "inside generator", node);
+ throw new ParseException("'return' with argument " + "inside generator", node);
}
visit(node.getInternalValue());
tmp = code.getReturnLocal();
@@ -912,29 +909,31 @@
code.invokestatic(p(Py.class), "makeException", sig(PyException.class, PyObject.class));
} else if (node.getInternalTback() == null) {
stackConsume(2);
- code.invokestatic(p(Py.class), "makeException", sig(PyException.class, PyObject.class,
- PyObject.class));
+ code.invokestatic(p(Py.class), "makeException",
+ sig(PyException.class, PyObject.class, PyObject.class));
} else {
stackConsume(3);
- code.invokestatic(p(Py.class), "makeException", sig(PyException.class, PyObject.class,
- PyObject.class, PyObject.class));
+ code.invokestatic(p(Py.class), "makeException",
+ sig(PyException.class, PyObject.class, PyObject.class, PyObject.class));
}
code.athrow();
return Exit;
}
/**
- * Push the import level <code>0</code> or <code>-1</code>.
+ * Return the implied import level, which is different from the argument only if the argument is
+ * zero (no leading dots) meaning try relative then absolute (in Python 2), signified by
+ * returning level <code>-1</code>.
*/
- private void defaultImportLevel() {
- // already prepared for a future change of DEFAULT_LEVEL
- if (module.getFutures().isAbsoluteImportOn() || imp.DEFAULT_LEVEL == 0) {
- code.iconst_0();
+ private int impliedImportLevel(int level) {
+ // already prepared for a future change of DEFAULT_LEVEL
+ if (imp.DEFAULT_LEVEL == 0 || level != 0 || module.getFutures().isAbsoluteImportOn()) {
+ return level;
} else {
- code.iconst_m1();
- }
+ return imp.DEFAULT_LEVEL;
+ }
}
-
+
@Override
public Object visitImport(Import node) throws Exception {
setline(node);
@@ -945,9 +944,9 @@
asname = a.getInternalAsname();
code.ldc(name);
loadFrame();
- defaultImportLevel();
- code.invokestatic(p(imp.class), "importOneAs", sig(PyObject.class, String.class,
- PyFrame.class, Integer.TYPE));
+ code.iconst(impliedImportLevel(0));
+ code.invokestatic(p(imp.class), "importOneAs",
+ sig(PyObject.class, String.class, PyFrame.class, Integer.TYPE));
} else {
String name = a.getInternalName();
asname = name;
@@ -956,9 +955,9 @@
}
code.ldc(name);
loadFrame();
- defaultImportLevel();
- code.invokestatic(p(imp.class), "importOne", sig(PyObject.class, String.class,
- PyFrame.class, Integer.TYPE));
+ code.iconst(impliedImportLevel(0));
+ code.invokestatic(p(imp.class), "importOne",
+ sig(PyObject.class, String.class, PyFrame.class, Integer.TYPE));
}
set(new Name(a, asname, expr_contextType.Store));
}
@@ -978,26 +977,24 @@
module.error("import * only allowed at module level", false, node);
if (my_scope.contains_ns_free_vars) {
- module.error("import * is not allowed in function '" +
- my_scope.scope_name +
- "' because it contains a nested function with free variables",
- true, node);
+ module.error("import * is not allowed in function '" + my_scope.scope_name
+ + "' because it contains a nested function with free variables", true,
+ node);
}
}
if (my_scope.func_level > 1) {
- module.error("import * is not allowed in function '" +
- my_scope.scope_name +
- "' because it is a nested function",
- true, node);
+ module.error("import * is not allowed in function '" + my_scope.scope_name
+ + "' because it is a nested function", true, node);
}
loadFrame();
- defaultImportLevel();
- code.invokestatic(p(imp.class), "importAll", sig(Void.TYPE, String.class,
- PyFrame.class, Integer.TYPE));
+ code.iconst(impliedImportLevel(node.getInternalLevel()));
+ code.invokestatic(p(imp.class), "importAll",
+ sig(Void.TYPE, String.class, PyFrame.class, Integer.TYPE));
+
} else {
- java.util.List<String> fromNames = new ArrayList<String>();//[names.size()];
- java.util.List<String> asnames = new ArrayList<String>();//[names.size()];
+ java.util.List<String> fromNames = new ArrayList<String>(); // [names.size()];
+ java.util.List<String> asnames = new ArrayList<String>(); // [names.size()];
for (int i = 0; i < aliases.size(); i++) {
fromNames.add(aliases.get(i).getInternalName());
asnames.add(aliases.get(i).getInternalAsname());
@@ -1010,14 +1007,11 @@
code.freeLocal(strArray);
loadFrame();
-
- if (node.getInternalLevel() == 0) {
- defaultImportLevel();
- } else {
- code.iconst(node.getInternalLevel());
- }
- code.invokestatic(p(imp.class), "importFrom", sig(PyObject[].class, String.class,
- String[].class, PyFrame.class, Integer.TYPE));
+ code.iconst(impliedImportLevel(node.getInternalLevel()));
+ code.invokestatic(
+ p(imp.class),
+ "importFrom",
+ sig(PyObject[].class, String.class, String[].class, PyFrame.class, Integer.TYPE));
int tmp = storeTop();
for (int i = 0; i < aliases.size(); i++) {
code.aload(tmp);
@@ -1055,10 +1049,10 @@
}
stackProduce();
- //do the real work here
+ // do the real work here
stackConsume(3);
- code.invokestatic(p(Py.class), "exec", sig(Void.TYPE, PyObject.class, PyObject.class,
- PyObject.class));
+ code.invokestatic(p(Py.class), "exec",
+ sig(Void.TYPE, PyObject.class, PyObject.class, PyObject.class));
return null;
}
@@ -1075,9 +1069,11 @@
code.ifeq(end_of_assert);
- /* Now do the body of the assert. If PyObject.__nonzero__ is true,
- then the assertion succeeded, the message portion should not be
- processed. Otherwise, the message will be processed. */
+ /*
+ * Now do the body of the assert. If PyObject.__nonzero__ is true, then the assertion
+ * succeeded, the message portion should not be processed. Otherwise, the message will be
+ * processed.
+ */
visit(node.getInternalTest());
code.invokevirtual(p(PyObject.class), "__nonzero__", sig(Boolean.TYPE));
@@ -1097,8 +1093,8 @@
code.swap(); // The type is the first argument, but the message could be a yield
- code.invokestatic(p(Py.class), "makeException", sig(PyException.class, PyObject.class,
- PyObject.class));
+ code.invokestatic(p(Py.class), "makeException",
+ sig(PyException.class, PyObject.class, PyObject.class));
/* Raise assertion error. Only executes this logic if assertion failed */
code.athrow();
@@ -1108,8 +1104,7 @@
return null;
}
- public Object doTest(Label end_of_if, If node, int index)
- throws Exception {
+ public Object doTest(Label end_of_if, If node, int index) throws Exception {
Label end_of_suite = new Label();
setline(node.getInternalTest());
@@ -1193,13 +1188,13 @@
code.goto_(continue_loop);
code.label(start_loop);
- //Do suite
+ // Do suite
suite(node.getInternalBody());
code.label(continue_loop);
setline(node);
- //Do test
+ // Do test
visit(node.getInternalTest());
code.invokevirtual(p(PyObject.class), "__nonzero__", sig(Boolean.TYPE));
code.ifne(start_loop);
@@ -1207,7 +1202,7 @@
finishLoop(savebcf);
if (node.getInternalOrelse() != null) {
- //Do else
+ // Do else
suite(node.getInternalOrelse());
}
code.label(break_loop);
@@ -1226,43 +1221,43 @@
setline(node);
- //parse the list
+ // parse the list
visit(node.getInternalIter());
int iter_tmp = code.getLocal(p(PyObject.class));
int expr_tmp = code.getLocal(p(PyObject.class));
- //set up the loop iterator
+ // set up the loop iterator
code.invokevirtual(p(PyObject.class), "__iter__", sig(PyObject.class));
code.astore(iter_tmp);
- //do check at end of loop. Saves one opcode ;-)
+ // do check at end of loop. Saves one opcode ;-)
code.goto_(next_loop);
code.label(start_loop);
- //set iter variable to current entry in list
+ // set iter variable to current entry in list
set(node.getInternalTarget(), expr_tmp);
- //evaluate for body
+ // evaluate for body
suite(node.getInternalBody());
code.label(continue_loop);
code.label(next_loop);
setline(node);
- //get the next element from the list
+ // get the next element from the list
code.aload(iter_tmp);
code.invokevirtual(p(PyObject.class), "__iternext__", sig(PyObject.class));
code.astore(expr_tmp);
code.aload(expr_tmp);
- //if no more elements then fall through
+ // if no more elements then fall through
code.ifnonnull(start_loop);
finishLoop(savebcf);
if (node.getInternalOrelse() != null) {
- //Do else clause if provided
+ // Do else clause if provided
suite(node.getInternalOrelse());
}
@@ -1275,26 +1270,23 @@
return null;
}
- public void exceptionTest(int exc, Label end_of_exceptions,
- TryExcept node, int index)
+ public void exceptionTest(int exc, Label end_of_exceptions, TryExcept node, int index)
throws Exception {
for (int i = 0; i < node.getInternalHandlers().size(); i++) {
- ExceptHandler handler = (ExceptHandler) node.getInternalHandlers().get(i);
-
- //setline(name);
+ ExceptHandler handler = (ExceptHandler)node.getInternalHandlers().get(i);
+
+ // setline(name);
Label end_of_self = new Label();
if (handler.getInternalType() != null) {
code.aload(exc);
- //get specific exception
+ // get specific exception
visit(handler.getInternalType());
- code.invokevirtual(p(PyException.class), "match", sig(Boolean.TYPE,
- PyObject.class));
+ code.invokevirtual(p(PyException.class), "match", sig(Boolean.TYPE, PyObject.class));
code.ifeq(end_of_self);
} else {
if (i != node.getInternalHandlers().size() - 1) {
- throw new ParseException(
- "default 'except:' must be last", handler);
+ throw new ParseException("default 'except:' must be last", handler);
}
}
@@ -1304,7 +1296,7 @@
set(handler.getInternalName());
}
- //do exception body
+ // do exception body
suite(handler.getInternalBody());
code.goto_(end_of_exceptions);
code.label(end_of_self);
@@ -1393,10 +1385,9 @@
}
/**
- * Inline the finally handling code for levels down to the levelth parent
- * (0 means all). This takes care to avoid having more nested finallys
- * catch exceptions throw by the parent finally code. This also pops off
- * all the handlers above level temporarily.
+ * Inline the finally handling code for levels down to the levelth parent (0 means all). This
+ * takes care to avoid having more nested finallys catch exceptions throw by the parent finally
+ * code. This also pops off all the handlers above level temporarily.
*/
private void doFinallysDownTo(int level) throws Exception {
Stack<ExceptionHandler> poppedHandlers = new Stack<ExceptionHandler>();
@@ -1423,7 +1414,7 @@
code.label(start);
handler.exceptionStarts.addElement(start);
exceptionHandlers.push(handler);
- //Do suite
+ // Do suite
Object exit = suite(node.getInternalBody());
exceptionHandlers.pop();
code.label(end);
@@ -1437,23 +1428,23 @@
loadFrame();
- code.invokestatic(p(Py.class), "setException", sig(PyException.class, Throwable.class,
- PyFrame.class));
+ code.invokestatic(p(Py.class), "setException",
+ sig(PyException.class, Throwable.class, PyFrame.class));
int exc = code.getFinallyLocal(p(Throwable.class));
code.astore(exc);
if (node.getInternalOrelse() == null) {
- //No else clause to worry about
+ // No else clause to worry about
exceptionTest(exc, handler_end, node, 1);
code.label(handler_end);
} else {
- //Have else clause
+ // Have else clause
Label else_end = new Label();
exceptionTest(exc, else_end, node, 1);
code.label(handler_end);
- //do else clause
+ // do else clause
suite(node.getInternalOrelse());
code.label(else_end);
}
@@ -1718,8 +1709,7 @@
return null;
}
- static int makeStrings(Code c, Collection<String> names)
- throws IOException {
+ static int makeStrings(Code c, Collection<String> names) throws IOException {
if (names != null) {
c.iconst(names.size());
} else {
@@ -1741,8 +1731,7 @@
return strings;
}
- public Object invokeNoKeywords(Attribute node, java.util.List<expr> values)
- throws Exception {
+ public Object invokeNoKeywords(Attribute node, java.util.List<expr> values) throws Exception {
String name = getName(node.getInternalAttr());
visit(node.getInternalValue());
stackProduce();
@@ -1754,22 +1743,22 @@
switch (values.size()) {
case 0:
stackConsume(2); // target + ts
- code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class,
- ThreadState.class));
+ code.invokevirtual(p(PyObject.class), "__call__",
+ sig(PyObject.class, ThreadState.class));
break;
case 1:
visit(values.get(0));
stackConsume(2); // target + ts
- code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class,
- ThreadState.class, PyObject.class));
+ code.invokevirtual(p(PyObject.class), "__call__",
+ sig(PyObject.class, ThreadState.class, PyObject.class));
break;
case 2:
visit(values.get(0));
stackProduce();
visit(values.get(1));
stackConsume(3); // target + ts + arguments
- code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class,
- ThreadState.class, PyObject.class, PyObject.class));
+ code.invokevirtual(p(PyObject.class), "__call__",
+ sig(PyObject.class, ThreadState.class, PyObject.class, PyObject.class));
break;
case 3:
visit(values.get(0));
@@ -1778,8 +1767,11 @@
stackProduce();
visit(values.get(2));
stackConsume(4); // target + ts + arguments
- code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class,
- ThreadState.class, PyObject.class, PyObject.class, PyObject.class));
+ code.invokevirtual(
+ p(PyObject.class),
+ "__call__",
+ sig(PyObject.class, ThreadState.class, PyObject.class, PyObject.class,
+ PyObject.class));
break;
case 4:
visit(values.get(0));
@@ -1790,17 +1782,19 @@
stackProduce();
visit(values.get(3));
stackConsume(5); // target + ts + arguments
- code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class,
- ThreadState.class, PyObject.class, PyObject.class, PyObject.class,
- PyObject.class));
+ code.invokevirtual(
+ p(PyObject.class),
+ "__call__",
+ sig(PyObject.class, ThreadState.class, PyObject.class, PyObject.class,
+ PyObject.class, PyObject.class));
break;
default:
int argArray = makeArray(values);
code.aload(argArray);
code.freeLocal(argArray);
stackConsume(2); // target + ts
- code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class,
- ThreadState.class, PyObject[].class));
+ code.invokevirtual(p(PyObject.class), "__call__",
+ sig(PyObject.class, ThreadState.class, PyObject[].class));
break;
}
return null;
@@ -1818,10 +1812,10 @@
values.add(node.getInternalKeywords().get(i).getInternalValue());
}
- if ((node.getInternalKeywords() == null || node.getInternalKeywords().size() == 0) &&
- node.getInternalStarargs() == null && node.getInternalKwargs() == null &&
- node.getInternalFunc() instanceof Attribute) {
- return invokeNoKeywords((Attribute) node.getInternalFunc(), values);
+ if ((node.getInternalKeywords() == null || node.getInternalKeywords().size() == 0)
+ && node.getInternalStarargs() == null && node.getInternalKwargs() == null
+ && node.getInternalFunc() instanceof Attribute) {
+ return invokeNoKeywords((Attribute)node.getInternalFunc(), values);
}
visit(node.getInternalFunc());
@@ -1850,8 +1844,11 @@
code.pop2();
stackConsume(3); // target + starargs + kwargs
- code.invokevirtual(p(PyObject.class), "_callextra", sig(PyObject.class,
- PyObject[].class, String[].class, PyObject.class, PyObject.class));
+ code.invokevirtual(
+ p(PyObject.class),
+ "_callextra",
+ sig(PyObject.class, PyObject[].class, String[].class, PyObject.class,
+ PyObject.class));
freeArrayRef(argArray);
} else if (keys.size() > 0) {
loadThreadState();
@@ -1862,8 +1859,8 @@
code.aload(strArray);
code.freeLocal(strArray);
stackConsume(2); // target + ts
- code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class, ThreadState.class,
- PyObject[].class, String[].class));
+ code.invokevirtual(p(PyObject.class), "__call__",
+ sig(PyObject.class, ThreadState.class, PyObject[].class, String[].class));
freeArrayRef(argArray);
} else {
loadThreadState();
@@ -1871,22 +1868,22 @@
switch (values.size()) {
case 0:
stackConsume(2); // target + ts
- code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class,
- ThreadState.class));
+ code.invokevirtual(p(PyObject.class), "__call__",
+ sig(PyObject.class, ThreadState.class));
break;
case 1:
visit(values.get(0));
stackConsume(2); // target + ts
- code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class,
- ThreadState.class, PyObject.class));
+ code.invokevirtual(p(PyObject.class), "__call__",
+ sig(PyObject.class, ThreadState.class, PyObject.class));
break;
case 2:
visit(values.get(0));
stackProduce();
visit(values.get(1));
stackConsume(3); // target + ts + arguments
- code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class,
- ThreadState.class, PyObject.class, PyObject.class));
+ code.invokevirtual(p(PyObject.class), "__call__",
+ sig(PyObject.class, ThreadState.class, PyObject.class, PyObject.class));
break;
case 3:
visit(values.get(0));
@@ -1895,8 +1892,11 @@
stackProduce();
visit(values.get(2));
stackConsume(4); // target + ts + arguments
- code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class,
- ThreadState.class, PyObject.class, PyObject.class, PyObject.class));
+ code.invokevirtual(
+ p(PyObject.class),
+ "__call__",
+ sig(PyObject.class, ThreadState.class, PyObject.class, PyObject.class,
+ PyObject.class));
break;
case 4:
visit(values.get(0));
@@ -1907,17 +1907,19 @@
stackProduce();
visit(values.get(3));
stackConsume(5); // target + ts + arguments
- code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class,
- ThreadState.class, PyObject.class, PyObject.class, PyObject.class,
- PyObject.class));
+ code.invokevirtual(
+ p(PyObject.class),
+ "__call__",
+ sig(PyObject.class, ThreadState.class, PyObject.class, PyObject.class,
+ PyObject.class, PyObject.class));
break;
default:
int argArray = makeArray(values);
code.aload(argArray);
code.freeLocal(argArray);
stackConsume(2); // target + ts
- code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class,
- ThreadState.class, PyObject[].class));
+ code.invokevirtual(p(PyObject.class), "__call__",
+ sig(PyObject.class, ThreadState.class, PyObject[].class));
break;
}
}
@@ -1951,8 +1953,8 @@
}
stackProduce();
- if (node.getInternalCtx() == expr_contextType.AugStore &&
- augmode == expr_contextType.Load) {
+ if (node.getInternalCtx() == expr_contextType.AugStore
+ && augmode == expr_contextType.Load) {
saveAugTmps(node, 4);
ctx = expr_contextType.Load;
}
@@ -1961,18 +1963,21 @@
switch (ctx) {
case Del:
- code.invokevirtual(p(PyObject.class), "__delslice__", sig(Void.TYPE, PyObject.class,
- PyObject.class, PyObject.class));
+ code.invokevirtual(p(PyObject.class), "__delslice__",
+ sig(Void.TYPE, PyObject.class, PyObject.class, PyObject.class));
return null;
case Load:
- code.invokevirtual(p(PyObject.class), "__getslice__", sig(PyObject.class,
- PyObject.class, PyObject.class, PyObject.class));
+ code.invokevirtual(p(PyObject.class), "__getslice__",
+ sig(PyObject.class, PyObject.class, PyObject.class, PyObject.class));
return null;
case Param:
case Store:
code.aload(temporary);
- code.invokevirtual(p(PyObject.class), "__setslice__", sig(Void.TYPE, PyObject.class,
- PyObject.class, PyObject.class, PyObject.class));
+ code.invokevirtual(
+ p(PyObject.class),
+ "__setslice__",
+ sig(Void.TYPE, PyObject.class, PyObject.class, PyObject.class,
+ PyObject.class));
return null;
}
return null;
@@ -1982,13 +1987,12 @@
@Override
public Object visitSubscript(Subscript node) throws Exception {
if (node.getInternalSlice() instanceof Slice) {
- return Slice(node, (Slice) node.getInternalSlice());
+ return Slice(node, (Slice)node.getInternalSlice());
}
int value = temporary;
expr_contextType ctx = node.getInternalCtx();
- if (node.getInternalCtx() == expr_contextType.AugStore &&
- augmode == expr_contextType.Store) {
+ if (node.getInternalCtx() == expr_contextType.AugStore && augmode == expr_contextType.Store) {
restoreAugTmps(node, 2);
ctx = expr_contextType.Store;
} else {
@@ -1997,8 +2001,8 @@
visit(node.getInternalSlice());
stackConsume();
- if (node.getInternalCtx() == expr_contextType.AugStore &&
- augmode == expr_contextType.Load) {
+ if (node.getInternalCtx() == expr_contextType.AugStore
+ && augmode == expr_contextType.Load) {
saveAugTmps(node, 2);
ctx = expr_contextType.Load;
}
@@ -2006,8 +2010,7 @@
switch (ctx) {
case Del:
- code.invokevirtual(p(PyObject.class), "__delitem__",
- sig(Void.TYPE, PyObject.class));
+ code.invokevirtual(p(PyObject.class), "__delitem__", sig(Void.TYPE, PyObject.class));
return null;
case Load:
code.invokevirtual(p(PyObject.class), "__getitem__",
@@ -2044,16 +2047,15 @@
public Object visitAttribute(Attribute node) throws Exception {
expr_contextType ctx = node.getInternalCtx();
- if (node.getInternalCtx() == expr_contextType.AugStore &&
- augmode == expr_contextType.Store) {
+ if (node.getInternalCtx() == expr_contextType.AugStore && augmode == expr_contextType.Store) {
restoreAugTmps(node, 2);
ctx = expr_contextType.Store;
} else {
visit(node.getInternalValue());
code.ldc(getName(node.getInternalAttr()));
- if (node.getInternalCtx() == expr_contextType.AugStore &&
- augmode == expr_contextType.Load) {
+ if (node.getInternalCtx() == expr_contextType.AugStore
+ && augmode == expr_contextType.Load) {
saveAugTmps(node, 2);
ctx = expr_contextType.Load;
}
@@ -2192,19 +2194,17 @@
code.invokespecial(p(PyDictionary.class), "<init>", sig(Void.TYPE));
code.dup();
java.util.List<expr> kv = Arrays.asList(node.getInternalKey(), node.getInternalValue());
- visitInternalGenerators(
- node,
- new Tuple(node, kv, expr_contextType.UNDEFINED),
+ visitInternalGenerators(node, new Tuple(node, kv, expr_contextType.UNDEFINED),
node.getInternalGenerators());
code.invokevirtual(p(PyDictionary.class), "update", sig(Void.TYPE, PyObject.class));
return null;
}
- private void finishComp(expr node, java.util.List<expr> args, java.util.List<comprehension> generators,
- String tmp_append) throws Exception {
+ private void finishComp(expr node, java.util.List<expr> args,
+ java.util.List<comprehension> generators, String tmp_append) throws Exception {
set(new Name(node, tmp_append, expr_contextType.Store));
- stmt n = new Expr(node, new Call(node, new Name(node, tmp_append, expr_contextType.Load),
+ stmt n = new Expr(node, new Call(node, new Name(node, tmp_append, expr_contextType.Load), //
args, new ArrayList<keyword>(), null, null));
for (int i = generators.size() - 1; i >= 0; i--) {
@@ -2212,12 +2212,12 @@
for (int j = lc.getInternalIfs().size() - 1; j >= 0; j--) {
java.util.List<stmt> body = new ArrayList<stmt>();
body.add(n);
- n = new If(lc.getInternalIfs().get(j), lc.getInternalIfs().get(j), body,
+ n = new If(lc.getInternalIfs().get(j), lc.getInternalIfs().get(j), body, //
new ArrayList<stmt>());
}
java.util.List<stmt> body = new ArrayList<stmt>();
body.add(n);
- n = new For(lc, lc.getInternalTarget(), lc.getInternalIter(), body,
+ n = new For(lc, lc.getInternalTarget(), lc.getInternalIter(), body, //
new ArrayList<stmt>());
}
visit(n);
@@ -2268,7 +2268,6 @@
return null;
}
-
@Override
public Object visitRepr(Repr node) throws Exception {
visit(node.getInternalValue());
@@ -2276,20 +2275,20 @@
return null;
}
-
// a marker class to distinguish this usage; future generator rewriting may likely
// want to remove this support
private class LambdaSyntheticReturn extends Return {
+
private LambdaSyntheticReturn(PythonTree tree, expr value) {
super(tree, value);
}
}
-
+
@Override
public Object visitLambda(Lambda node) throws Exception {
String name = "<lambda>";
- //Add a synthetic return node onto the outside of suite;
+ // Add a synthetic return node onto the outside of suite;
java.util.List<stmt> bod = new ArrayList<stmt>();
bod.add(new LambdaSyntheticReturn(node, node.getInternalBody()));
mod retSuite = new Suite(node, bod);
@@ -2313,15 +2312,17 @@
scope.setup_closure();
scope.dump();
- module.codeConstant(retSuite, name, true, className,
- false, false, node.getLine(), scope, cflags).get(code);
+ module.codeConstant(retSuite, name, true, className, false, false, node.getLine(), scope,
+ cflags).get(code);
if (!makeClosure(scope)) {
- code.invokespecial(p(PyFunction.class), "<init>", sig(Void.TYPE, PyObject.class,
- PyObject[].class, PyCode.class));
+ code.invokespecial(p(PyFunction.class), "<init>",
+ sig(Void.TYPE, PyObject.class, PyObject[].class, PyCode.class));
} else {
- code.invokespecial(p(PyFunction.class), "<init>", sig(Void.TYPE, PyObject.class,
- PyObject[].class, PyCode.class, PyObject[].class));
+ code.invokespecial(
+ p(PyFunction.class),
+ "<init>",
+ sig(Void.TYPE, PyObject.class, PyObject[].class, PyCode.class, PyObject[].class));
}
return null;
}
@@ -2362,8 +2363,8 @@
code.aload(step);
code.freeLocal(step);
- code.invokespecial(p(PySlice.class), "<init>", sig(Void.TYPE, PyObject.class,
- PyObject.class, PyObject.class));
+ code.invokespecial(p(PySlice.class), "<init>",
+ sig(Void.TYPE, PyObject.class, PyObject.class, PyObject.class));
return null;
}
@@ -2373,7 +2374,7 @@
int baseArray = makeArray(node.getInternalBases());
- //Get class name
+ // Get class name
String name = getName(node.getInternalName());
code.ldc(name);
@@ -2383,24 +2384,27 @@
scope.setup_closure();
scope.dump();
- //Make code object out of suite
+ // Make code object out of suite
module.codeConstant(new Suite(node, node.getInternalBody()), name, false, name,
- getDocStr(node.getInternalBody()), true, false, node.getLine(), scope,
- cflags).get(code);
-
- //Make class out of name, bases, and code
+ getDocStr(node.getInternalBody()), true, false, node.getLine(), scope, cflags).get(
+ code);
+
+ // Make class out of name, bases, and code
if (!makeClosure(scope)) {
- code.invokestatic(p(Py.class), "makeClass", sig(PyObject.class, String.class,
- PyObject[].class, PyCode.class));
+ code.invokestatic(p(Py.class), "makeClass",
+ sig(PyObject.class, String.class, PyObject[].class, PyCode.class));
} else {
- code.invokestatic(p(Py.class), "makeClass", sig(PyObject.class, String.class,
- PyObject[].class, PyCode.class, PyObject[].class));
+ code.invokestatic(
+ p(Py.class),
+ "makeClass",
+ sig(PyObject.class, String.class, PyObject[].class, PyCode.class,
+ PyObject[].class));
}
applyDecorators(node.getInternalDecorator_list());
- //Assign this new class to the given name
+ // Assign this new class to the given name
set(new Name(node, node.getInternalName(), expr_contextType.Store));
freeArray(baseArray);
return null;
@@ -2409,21 +2413,20 @@
@Override
public Object visitNum(Num node) throws Exception {
if (node.getInternalN() instanceof PyInteger) {
- module.integerConstant(((PyInteger) node.getInternalN()).getValue()).get(code);
+ module.integerConstant(((PyInteger)node.getInternalN()).getValue()).get(code);
} else if (node.getInternalN() instanceof PyLong) {
- module.longConstant(((PyObject) node.getInternalN()).__str__().toString()).get(code);
+ module.longConstant(((PyObject)node.getInternalN()).__str__().toString()).get(code);
} else if (node.getInternalN() instanceof PyFloat) {
- module.floatConstant(((PyFloat) node.getInternalN()).getValue()).get(code);
+ module.floatConstant(((PyFloat)node.getInternalN()).getValue()).get(code);
} else if (node.getInternalN() instanceof PyComplex) {
- module.complexConstant(((PyComplex) node.getInternalN()).imag).get(code);
+ module.complexConstant(((PyComplex)node.getInternalN()).imag).get(code);
}
return null;
}
private String getName(String name) {
- if (className != null && name.startsWith("__") &&
- !name.endsWith("__")) {
- //remove leading '_' from classname
+ if (className != null && name.startsWith("__") && !name.endsWith("__")) {
+ // remove leading '_' from classname
int i = 0;
while (className.charAt(i) == '_') {
i++;
@@ -2459,31 +2462,29 @@
loadFrame();
if (syminf != null) {
int flags = syminf.flags;
- if ((flags & ScopeInfo.GLOBAL) != 0 || optimizeGlobals &&
- (flags & (ScopeInfo.BOUND | ScopeInfo.CELL |
- ScopeInfo.FREE)) == 0) {
+ if ((flags & ScopeInfo.GLOBAL) != 0 || optimizeGlobals
+ && (flags & (ScopeInfo.BOUND | ScopeInfo.CELL | ScopeInfo.FREE)) == 0) {
emitGetGlobal(name);
return null;
}
if (fast_locals) {
if ((flags & ScopeInfo.CELL) != 0) {
code.iconst(syminf.env_index);
- code.invokevirtual(p(PyFrame.class), "getderef", sig(PyObject.class,
- Integer.TYPE));
+ code.invokevirtual(p(PyFrame.class), "getderef",
+ sig(PyObject.class, Integer.TYPE));
return null;
}
if ((flags & ScopeInfo.BOUND) != 0) {
code.iconst(syminf.locals_index);
- code.invokevirtual(p(PyFrame.class), "getlocal", sig(PyObject.class,
- Integer.TYPE));
+ code.invokevirtual(p(PyFrame.class), "getlocal",
+ sig(PyObject.class, Integer.TYPE));
return null;
}
}
- if ((flags & ScopeInfo.FREE) != 0 &&
- (flags & ScopeInfo.BOUND) == 0) {
+ if ((flags & ScopeInfo.FREE) != 0 && (flags & ScopeInfo.BOUND) == 0) {
code.iconst(syminf.env_index);
- code.invokevirtual(p(PyFrame.class), "getderef", sig(PyObject.class,
- Integer.TYPE));
+ code.invokevirtual(p(PyFrame.class), "getderef",
+ sig(PyObject.class, Integer.TYPE));
return null;
}
}
@@ -2497,8 +2498,8 @@
if (syminf != null && (syminf.flags & ScopeInfo.GLOBAL) != 0) {
code.ldc(name);
code.aload(temporary);
- code.invokevirtual(p(PyFrame.class), "setglobal", sig(Void.TYPE, String.class,
- PyObject.class));
+ code.invokevirtual(p(PyFrame.class), "setglobal",
+ sig(Void.TYPE, String.class, PyObject.class));
} else {
if (!fast_locals) {
code.ldc(name);
@@ -2512,13 +2513,13 @@
if ((syminf.flags & ScopeInfo.CELL) != 0) {
code.iconst(syminf.env_index);
code.aload(temporary);
- code.invokevirtual(p(PyFrame.class), "setderef", sig(Void.TYPE,
- Integer.TYPE, PyObject.class));
+ code.invokevirtual(p(PyFrame.class), "setderef",
+ sig(Void.TYPE, Integer.TYPE, PyObject.class));
} else {
code.iconst(syminf.locals_index);
code.aload(temporary);
- code.invokevirtual(p(PyFrame.class), "setlocal", sig(Void.TYPE,
- Integer.TYPE, PyObject.class));
+ code.invokevirtual(p(PyFrame.class), "setlocal",
+ sig(Void.TYPE, Integer.TYPE, PyObject.class));
}
}
}
@@ -2538,8 +2539,8 @@
throw new ParseException("internal compiler error", node);
}
if ((syminf.flags & ScopeInfo.CELL) != 0) {
- module.error("can not delete variable '" + name +
- "' referenced in nested scope", true, node);
+ module.error("can not delete variable '" + name
+ + "' referenced in nested scope", true, node);
}
code.iconst(syminf.locals_index);
code.invokevirtual(p(PyFrame.class), "dellocal",
@@ -2554,7 +2555,7 @@
@Override
public Object visitStr(Str node) throws Exception {
- PyString s = (PyString) node.getInternalS();
+ PyString s = (PyString)node.getInternalS();
if (s instanceof PyUnicode) {
module.unicodeConstant(s.asString()).get(code);
} else {
@@ -2563,8 +2564,8 @@
return null;
}
- private Object visitInternalGenerators(expr node, expr elt, java.util.List<comprehension> generators)
- throws Exception {
+ private Object visitInternalGenerators(expr node, expr elt,
+ java.util.List<comprehension> generators) throws Exception {
String bound_exp = "_(x)";
setline(node);
@@ -2589,16 +2590,16 @@
for (int j = comp.getInternalIfs().size() - 1; j >= 0; j--) {
java.util.List<stmt> bod = new ArrayList<stmt>();
bod.add(n);
- n = new If(comp.getInternalIfs().get(j), comp.getInternalIfs().get(j), bod,
+ n = new If(comp.getInternalIfs().get(j), comp.getInternalIfs().get(j), bod, //
new ArrayList<stmt>());
}
java.util.List<stmt> bod = new ArrayList<stmt>();
bod.add(n);
if (i != 0) {
- n = new For(comp, comp.getInternalTarget(), comp.getInternalIter(), bod,
+ n = new For(comp, comp.getInternalTarget(), comp.getInternalIter(), bod, //
new ArrayList<stmt>());
} else {
- n = new For(comp, comp.getInternalTarget(), new Name(node, bound_exp,
+ n = new For(comp, comp.getInternalTarget(), new Name(node, bound_exp, //
expr_contextType.Load), bod, new ArrayList<stmt>());
iter = comp.getInternalIter();
}
@@ -2606,17 +2607,19 @@
java.util.List<stmt> bod = new ArrayList<stmt>();
bod.add(n);
- module.codeConstant(new Suite(node, bod), "<genexpr>", true,
- className, false, false,
+ module.codeConstant(new Suite(node, bod), "<genexpr>", true, className, false, false,
node.getLine(), scope, cflags).get(code);
code.aconst_null();
if (!makeClosure(scope)) {
- code.invokespecial(p(PyFunction.class), "<init>", sig(Void.TYPE, PyObject.class,
- PyObject[].class, PyCode.class, PyObject.class));
+ code.invokespecial(p(PyFunction.class), "<init>",
+ sig(Void.TYPE, PyObject.class, PyObject[].class, PyCode.class, PyObject.class));
} else {
- code.invokespecial(p(PyFunction.class), "<init>", sig(Void.TYPE, PyObject.class,
- PyObject[].class, PyCode.class, PyObject.class, PyObject[].class));
+ code.invokespecial(
+ p(PyFunction.class),
+ "<init>",
+ sig(Void.TYPE, PyObject.class, PyObject[].class, PyCode.class, PyObject.class,
+ PyObject[].class));
}
int genExp = storeTop();
@@ -2660,16 +2663,16 @@
for (int j = comp.getInternalIfs().size() - 1; j >= 0; j--) {
java.util.List<stmt> bod = new ArrayList<stmt>();
bod.add(n);
- n = new If(comp.getInternalIfs().get(j), comp.getInternalIfs().get(j), bod,
+ n = new If(comp.getInternalIfs().get(j), comp.getInternalIfs().get(j), bod, //
new ArrayList<stmt>());
}
java.util.List<stmt> bod = new ArrayList<stmt>();
bod.add(n);
if (i != 0) {
- n = new For(comp, comp.getInternalTarget(), comp.getInternalIter(), bod,
+ n = new For(comp, comp.getInternalTarget(), comp.getInternalIter(), bod, //
new ArrayList<stmt>());
} else {
- n = new For(comp, comp.getInternalTarget(), new Name(node, bound_exp,
+ n = new For(comp, comp.getInternalTarget(), new Name(node, bound_exp, //
expr_contextType.Load), bod, new ArrayList<stmt>());
iter = comp.getInternalIter();
}
@@ -2677,17 +2680,19 @@
java.util.List<stmt> bod = new ArrayList<stmt>();
bod.add(n);
- module.codeConstant(new Suite(node, bod), "<genexpr>", true,
- className, false, false,
+ module.codeConstant(new Suite(node, bod), "<genexpr>", true, className, false, false,
node.getLine(), scope, cflags).get(code);
code.aconst_null();
if (!makeClosure(scope)) {
- code.invokespecial(p(PyFunction.class), "<init>", sig(Void.TYPE, PyObject.class,
- PyObject[].class, PyCode.class, PyObject.class));
+ code.invokespecial(p(PyFunction.class), "<init>",
+ sig(Void.TYPE, PyObject.class, PyObject[].class, PyCode.class, PyObject.class));
} else {
- code.invokespecial(p(PyFunction.class), "<init>", sig(Void.TYPE, PyObject.class,
- PyObject[].class, PyCode.class, PyObject.class, PyObject[].class));
+ code.invokespecial(
+ p(PyFunction.class),
+ "<init>",
+ sig(Void.TYPE, PyObject.class, PyObject[].class, PyCode.class, PyObject.class,
+ PyObject[].class));
}
int genExp = storeTop();
@@ -2716,12 +2721,12 @@
final Label label_catch = new Label();
final Label label_end = new Label();
- final Method contextGuard_getManager = Method.getMethod(
- "org.python.core.ContextManager getManager (org.python.core.PyObject)");
- final Method __enter__ = Method.getMethod(
- "org.python.core.PyObject __enter__ (org.python.core.ThreadState)");
- final Method __exit__ = Method.getMethod(
- "boolean __exit__ (org.python.core.ThreadState,org.python.core.PyException)");
+ final Method contextGuard_getManager =
+ Method.getMethod("org.python.core.ContextManager getManager (org.python.core.PyObject)");
+ final Method __enter__ =
+ Method.getMethod("org.python.core.PyObject __enter__ (org.python.core.ThreadState)");
+ final Method __exit__ =
+ Method.getMethod("boolean __exit__ (org.python.core.ThreadState,org.python.core.PyException)");
// mgr = (EXPR)
visit(node.getInternalContext_expr());
@@ -2732,7 +2737,6 @@
contextGuard_getManager.getName(), contextGuard_getManager.getDescriptor());
code.dup();
-
final int mgr_tmp = code.getLocal(Type.getType(ContextManager.class).getInternalName());
code.astore(mgr_tmp);
@@ -2750,7 +2754,7 @@
// here
// # The normal and non-local-goto cases are handled here
// if exc: # implicit
- // exit(None, None, None)
+ // exit(None, None, None)
ExceptionHandler normalExit = new ExceptionHandler() {
@Override
@@ -2775,7 +2779,7 @@
exceptionHandlers.push(handler);
handler.exceptionStarts.addElement(label_body_start);
- // VAR = value # Only if "as VAR" is present
+ // VAR = value # Only if "as VAR" is present
code.label(label_body_start);
if (node.getInternalOptional_vars() != null) {
set(node.getInternalOptional_vars(), value_tmp);
@@ -2802,8 +2806,8 @@
code.label(label_catch);
loadFrame();
- code.invokestatic(p(Py.class), "setException", sig(PyException.class, Throwable.class,
- PyFrame.class));
+ code.invokestatic(p(Py.class), "setException",
+ sig(PyException.class, Throwable.class, PyFrame.class));
code.aload(mgr_tmp);
code.swap();
loadThreadState();
@@ -2815,7 +2819,7 @@
// exc = False # implicit
// if not exit(*sys.exc_info()):
code.ifne(label_end);
- // raise
+ // raise
// # The exception is swallowed if exit() returns true
code.invokestatic(p(Py.class), "makeException", sig(PyException.class));
code.checkcast(p(Throwable.class));
@@ -2834,33 +2838,29 @@
}
/**
- * Data about a given exception range whether a try:finally: or a
- * try:except:. The finally needs to inline the finally block for
- * each exit of the try: section, so we carry around that data for it.
- *
- * Both of these need to stop exception coverage of an area that is either
- * the inlined fin ally of a parent try:finally: or the reentry block after
- * a yield. Thus we keep around a set of exception ranges that the
- * catch block will eventually handle.
+ * Data about a given exception range whether a try:finally: or a try:except:. The finally needs
+ * to inline the finally block for each exit of the try: section, so we carry around that data
+ * for it.
+ *
+ * Both of these need to stop exception coverage of an area that is either the inlined fin ally
+ * of a parent try:finally: or the reentry block after a yield. Thus we keep around a set of
+ * exception ranges that the catch block will eventually handle.
*/
class ExceptionHandler {
/**
- * Each handler gets several exception ranges, this is because inlined
- * finally exit code shouldn't be covered by the exception handler of
- * that finally block. Thus each time we inline the finally code, we
- * stop one range and then enter a new one.
+ * Each handler gets several exception ranges, this is because inlined finally exit code
+ * shouldn't be covered by the exception handler of that finally block. Thus each time we
+ * inline the finally code, we stop one range and then enter a new one.
*
- * We also need to stop coverage for the recovery of the locals after
- * a yield.
+ * We also need to stop coverage for the recovery of the locals after a yield.
*/
public Vector<Label> exceptionStarts = new Vector<Label>();
public Vector<Label> exceptionEnds = new Vector<Label>();
public boolean bodyDone = false;
public PythonTree node = null;
- public ExceptionHandler() {
- }
+ public ExceptionHandler() {}
public ExceptionHandler(PythonTree n) {
node = n;
@@ -2874,20 +2874,17 @@
for (int i = 0; i < exceptionStarts.size(); ++i) {
Label start = exceptionStarts.elementAt(i);
Label end = exceptionEnds.elementAt(i);
- //FIXME: not at all sure that getOffset() test is correct or necessary.
+ // FIXME: not at all sure that getOffset() test is correct or necessary.
if (start.getOffset() != end.getOffset()) {
- code.trycatch(
- exceptionStarts.elementAt(i),
- exceptionEnds.elementAt(i),
- handlerStart,
- p(Throwable.class));
+ code.trycatch(exceptionStarts.elementAt(i), exceptionEnds.elementAt(i),
+ handlerStart, p(Throwable.class));
}
}
}
public void finalBody(CodeCompiler compiler) throws Exception {
if (node instanceof TryFinally) {
- suite(((TryFinally) node).getInternalFinalbody());
+ suite(((TryFinally)node).getInternalFinalbody());
}
}
}
diff --git a/src/org/python/core/imp.java b/src/org/python/core/imp.java
--- a/src/org/python/core/imp.java
+++ b/src/org/python/core/imp.java
@@ -17,17 +17,18 @@
/**
* Utility functions for "import" support.
*
- * Note that this class tries to match the names of the corresponding functions
- * from CPython's Python/import.c. In these cases we use CPython's function
- * naming style (underscores and all lowercase) instead of Java's typical
- * camelCase style so that it's easier to compare with import.c.
+ * Note that this class tries to match the names of the corresponding functions from CPython's
+ * Python/import.c. In these cases we use CPython's function naming style (underscores and all
+ * lowercase) instead of Java's typical camelCase style so that it's easier to compare with
+ * import.c.
*/
public class imp {
+
private static final String IMPORT_LOG = "import";
private static final String UNKNOWN_SOURCEFILE = "<unknown>";
- private static final int APIVersion = 36;
+ private static final int APIVersion = 37;
public static final int NO_MTIME = -1;
@@ -36,6 +37,7 @@
public static final int DEFAULT_LEVEL = -1;
public static class CodeData {
+
private final byte[] bytes;
private final long mtime;
private final String filename;
@@ -69,78 +71,73 @@
public static ClassLoader getSyspathJavaLoader() {
return Py.getSystemState().getSyspathJavaLoader();
}
-
+
/**
- * Selects the parent class loader for Jython, to be used for
- * dynamically loaded classes and resources. Chooses between the
- * current and context classloader based on the following
+ * Selects the parent class loader for Jython, to be used for dynamically loaded classes and
+ * resources. Chooses between the current and context classloader based on the following
* criteria:
*
* <ul>
* <li>If both are the same classloader, return that classloader.
* <li>If either is null, then the non-null one is selected.
- * <li>If both are not null, and a parent/child relationship can
- * be determined, then the child is selected.
- * <li>If both are not null and not on a parent/child
- * relationship, then the current class loader is returned (since
- * it is likely for the context class loader to <b>not</b> see the
+ * <li>If both are not null, and a parent/child relationship can be determined, then the child
+ * is selected.
+ * <li>If both are not null and not on a parent/child relationship, then the current class
+ * loader is returned (since it is likely for the context class loader to <b>not</b> see the
* Jython classes)
* </ul>
- *
- * @return the parent class loader for Jython or null if both the
- * current and context classloaders are null.
+ *
+ * @return the parent class loader for Jython or null if both the current and context
+ * classloaders are null.
*/
public static ClassLoader getParentClassLoader() {
- ClassLoader current = imp.class.getClassLoader();
- ClassLoader context = Thread.currentThread().getContextClassLoader();
- if (context == current) {
- return current;
- }
- if (context == null) {
- return current;
- }
- if (current == null) {
- return context;
- }
- if (isParentClassLoader(context, current)) {
- return current;
- }
- if (isParentClassLoader(current, context)) {
- return context;
- }
- return current;
- }
-
- private static boolean isParentClassLoader(
- ClassLoader suspectedParent, ClassLoader child) {
- try {
- ClassLoader parent = child.getParent();
- if (suspectedParent == parent) {
- return true;
- }
- if (parent == null || parent == child) {
- // We reached the boot class loader
- return false;
- }
- return isParentClassLoader(suspectedParent, parent);
-
- } catch (SecurityException e) {
- return false;
- }
- }
-
- private imp() {
+ ClassLoader current = imp.class.getClassLoader();
+ ClassLoader context = Thread.currentThread().getContextClassLoader();
+ if (context == current) {
+ return current;
+ }
+ if (context == null) {
+ return current;
+ }
+ if (current == null) {
+ return context;
+ }
+ if (isParentClassLoader(context, current)) {
+ return current;
+ }
+ if (isParentClassLoader(current, context)) {
+ return context;
+ }
+ return current;
}
+ private static boolean isParentClassLoader(ClassLoader suspectedParent, ClassLoader child) {
+ try {
+ ClassLoader parent = child.getParent();
+ if (suspectedParent == parent) {
+ return true;
+ }
+ if (parent == null || parent == child) {
+ // We reached the boot class loader
+ return false;
+ }
+ return isParentClassLoader(suspectedParent, parent);
+
+ } catch (SecurityException e) {
+ return false;
+ }
+ }
+
+ private imp() {}
+
/**
- * If the given name is found in sys.modules, the entry from there is
- * returned. Otherwise a new PyModule is created for the name and added to
- * sys.modules
+ * If the given name is found in sys.modules, the entry from there is returned. Otherwise a new
+ * PyModule is created for the name and added to sys.modules
*/
public static PyModule addModule(String name) {
name = name.intern();
PyObject modules = Py.getSystemState().modules;
- PyModule module = (PyModule) modules.__finditem__(name);
+ PyModule module = (PyModule)modules.__finditem__(name);
if (module != null) {
return module;
}
@@ -176,12 +173,12 @@
private static byte[] readBytes(InputStream fp) {
try {
return FileUtil.readBytes(fp);
- } catch(IOException ioe) {
+ } catch (IOException ioe) {
throw Py.IOError(ioe);
} finally {
try {
fp.close();
- } catch(IOException e) {
+ } catch (IOException e) {
throw Py.IOError(e);
}
}
@@ -194,22 +191,24 @@
throw Py.IOError(ioe);
}
}
+
static PyObject createFromPyClass(String name, InputStream fp, boolean testing,
- String sourceName, String compiledName) {
+ String sourceName, String compiledName) {
return createFromPyClass(name, fp, testing, sourceName, compiledName, NO_MTIME);
}
static PyObject createFromPyClass(String name, InputStream fp, boolean testing,
- String sourceName, String compiledName, long mtime) {
- return createFromPyClass(name, fp, testing, sourceName, compiledName, mtime, CodeImport.source);
+ String sourceName, String compiledName, long mtime) {
+ return createFromPyClass(name, fp, testing, sourceName, compiledName, mtime,
+ CodeImport.source);
}
static PyObject createFromPyClass(String name, InputStream fp, boolean testing,
- String sourceName, String compiledName, long mtime, CodeImport source) {
+ String sourceName, String compiledName, long mtime, CodeImport source) {
CodeData data = null;
try {
- data = readCodeData(name, fp, testing, mtime);
+ data = readCodeData(compiledName, fp, testing, mtime);
} catch (IOException ioe) {
if (!testing) {
throw Py.ImportError(ioe.getMessage() + "[name=" + name + ", source=" + sourceName
@@ -221,7 +220,7 @@
}
PyCode code;
try {
- code = BytecodeLoader.makeCode(name + "$py", data.getBytes(),
+ code = BytecodeLoader.makeCode(name + "$py", data.getBytes(), //
source == CodeImport.compiled_only ? data.getFilename() : sourceName);
} catch (Throwable t) {
if (testing) {
@@ -231,8 +230,8 @@
}
}
- Py.writeComment(IMPORT_LOG, String.format("import %s # precompiled from %s", name,
- compiledName));
+ Py.writeComment(IMPORT_LOG,
+ String.format("import %s # precompiled from %s", name, compiledName));
return createFromCode(name, code, compiledName);
}
@@ -240,7 +239,8 @@
return readCode(name, fp, testing, NO_MTIME);
}
- public static byte[] readCode(String name, InputStream fp, boolean testing, long mtime) throws IOException {
+ public static byte[] readCode(String name, InputStream fp, boolean testing, long mtime)
+ throws IOException {
CodeData data = readCodeData(name, fp, testing, mtime);
if (data == null) {
return null;
@@ -249,11 +249,13 @@
}
}
- public static CodeData readCodeData(String name, InputStream fp, boolean testing) throws IOException {
+ public static CodeData readCodeData(String name, InputStream fp, boolean testing)
+ throws IOException {
return readCodeData(name, fp, testing, NO_MTIME);
}
- public static CodeData readCodeData(String name, InputStream fp, boolean testing, long mtime) throws IOException {
+ public static CodeData readCodeData(String name, InputStream fp, boolean testing, long mtime)
+ throws IOException {
byte[] data = readBytes(fp);
int api;
AnnotationReader ar = new AnnotationReader(data);
@@ -262,8 +264,8 @@
if (testing) {
return null;
} else {
- throw Py.ImportError("invalid api version(" + api + " != "
- + APIVersion + ") in: " + name);
+ String fmt = "compiled unit contains version %d code (%d required): %.200s";
+ throw Py.ImportError(String.format(fmt, api, APIVersion, name));
}
}
if (testing && mtime != NO_MTIME) {
@@ -299,22 +301,18 @@
/**
* Stores the bytes in compiledSource in compiledFilename.
*
- * If compiledFilename is null, it's set to the results of
- * makeCompiledFilename(sourcefileName).
+ * If compiledFilename is null, it's set to the results of makeCompiledFilename(sourcefileName).
*
- * If sourceFilename is null or set to UNKNOWN_SOURCEFILE, then
- * null is returned.
+ * If sourceFilename is null or set to UNKNOWN_SOURCEFILE, then null is returned.
*
- * @return the compiledFilename eventually used; or null if a
- * compiledFilename couldn't be determined or if an error
- * was thrown while writing to the cache file.
+ * @return the compiledFilename eventually used; or null if a compiledFilename couldn't be
+ * determined or if an error was thrown while writing to the cache file.
*/
- public static String cacheCompiledSource(String sourceFilename,
- String compiledFilename,
- byte[] compiledSource) {
- if(compiledFilename == null){
- if(sourceFilename == null || sourceFilename.equals(UNKNOWN_SOURCEFILE)){
- return null;
+ public static String cacheCompiledSource(String sourceFilename, String compiledFilename,
+ byte[] compiledSource) {
+ if (compiledFilename == null) {
+ if (sourceFilename == null || sourceFilename.equals(UNKNOWN_SOURCEFILE)) {
+ return null;
}
compiledFilename = makeCompiledFilename(sourceFilename);
}
@@ -328,24 +326,23 @@
fop.write(compiledSource);
fop.close();
return compiledFilename;
- } catch(IOException exc) {
+ } catch (IOException exc) {
// If we can't write the cache file, just log and continue
- Py.writeDebug(IMPORT_LOG, "Unable to write to source cache file '"
- + compiledFilename + "' due to " + exc);
+ Py.writeDebug(IMPORT_LOG, "Unable to write to source cache file '" + compiledFilename
+ + "' due to " + exc);
return null;
- } catch(SecurityException exc) {
+ } catch (SecurityException exc) {
// If we can't write the cache file, just log and continue
- Py.writeDebug(IMPORT_LOG, "Unable to write to source cache file '"
- + compiledFilename + "' due to " + exc);
+ Py.writeDebug(IMPORT_LOG, "Unable to write to source cache file '" + compiledFilename
+ + "' due to " + exc);
return null;
} finally {
- if(fop != null) {
+ if (fop != null) {
try {
fop.close();
- } catch(IOException e) {
- Py.writeDebug(IMPORT_LOG,
- "Unable to close source cache file '"
- + compiledFilename + "' due to " + e);
+ } catch (IOException e) {
+ Py.writeDebug(IMPORT_LOG, "Unable to close source cache file '"
+ + compiledFilename + "' due to " + e);
}
}
}
@@ -358,7 +355,7 @@
public static byte[] compileSource(String name, InputStream fp, String filename, long mtime) {
ByteArrayOutputStream ofp = new ByteArrayOutputStream();
try {
- if(filename == null) {
+ if (filename == null) {
filename = UNKNOWN_SOURCEFILE;
}
org.python.antlr.base.mod node;
@@ -369,7 +366,7 @@
}
Module.compile(node, ofp, name + "$py", filename, true, false, null, mtime);
return ofp.toByteArray();
- } catch(Throwable t) {
+ } catch (Throwable t) {
throw ParserFacade.fixParseError(null, t, filename);
}
}
@@ -378,13 +375,13 @@
return createFromSource(name, fp, filename, null, NO_MTIME);
}
- public static PyObject createFromSource(String name, InputStream fp,
- String filename, String outFilename) {
+ public static PyObject createFromSource(String name, InputStream fp, String filename,
+ String outFilename) {
return createFromSource(name, fp, filename, outFilename, NO_MTIME);
}
- public static PyObject createFromSource(String name, InputStream fp,
- String filename, String outFilename, long mtime) {
+ public static PyObject createFromSource(String name, InputStream fp, String filename,
+ String outFilename, long mtime) {
byte[] bytes = compileSource(name, fp, filename, mtime);
if (!Py.getSystemState().dont_write_bytecode) {
outFilename = cacheCompiledSource(filename, outFilename, bytes);
@@ -397,20 +394,19 @@
}
/**
- * Returns a module with the given name whose contents are the results of
- * running c. __file__ is set to whatever is in c.
+ * Returns a module with the given name whose contents are the results of running c. __file__ is
+ * set to whatever is in c.
*/
- public static PyObject createFromCode(String name, PyCode c){
+ public static PyObject createFromCode(String name, PyCode c) {
return createFromCode(name, c, null);
}
/**
- * Returns a module with the given name whose contents are the results of
- * running c. Sets __file__ on the module to be moduleLocation unless
- * moduleLocation is null. If c comes from a local .py file or compiled
- * $py.class class moduleLocation should be the result of running new
- * File(moduleLocation).getAbsolutePath(). If c comes from a remote file or
- * is a jar moduleLocation should be the full uri for c.
+ * Returns a module with the given name whose contents are the results of running c. Sets
+ * __file__ on the module to be moduleLocation unless moduleLocation is null. If c comes from a
+ * local .py file or compiled $py.class class moduleLocation should be the result of running new
+ * File(moduleLocation).getAbsolutePath(). If c comes from a remote file or is a jar
+ * moduleLocation should be the full uri for c.
*/
public static PyObject createFromCode(String name, PyCode c, String moduleLocation) {
PyUnicode.checkEncoding(name);
@@ -418,7 +414,7 @@
PyTableCode code = null;
if (c instanceof PyTableCode) {
- code = (PyTableCode) c;
+ code = (PyTableCode)c;
}
if (moduleLocation != null) {
@@ -512,8 +508,8 @@
for (PyObject importer : metaPath.asIterable()) {
PyObject findModule = importer.__getattr__("find_module");
- loader = findModule.__call__(new PyObject[] {
- new PyString(moduleName), path == null ? Py.None : path });
+ loader = findModule.__call__(new PyObject[] { //
+ new PyString(moduleName), path == null ? Py.None : path});
if (loader != Py.None) {
return loadFromLoader(loader, moduleName);
}
@@ -527,12 +523,10 @@
path = path == null ? sys.path : path;
for (int i = 0; i < path.__len__(); i++) {
PyObject p = path.__getitem__(i);
- PyObject importer = getPathImporter(sys.path_importer_cache,
- sys.path_hooks, p);
+ PyObject importer = getPathImporter(sys.path_importer_cache, sys.path_hooks, p);
if (importer != Py.None) {
PyObject findModule = importer.__getattr__("find_module");
- loader = findModule.__call__(new PyObject[] { new PyString(
- moduleName) });
+ loader = findModule.__call__(new PyObject[] {new PyString(moduleName)});
if (loader != Py.None) {
return loadFromLoader(loader, moduleName);
}
@@ -562,16 +556,14 @@
if (mod != null) {
Class c = Py.findClassEx(mod, "builtin modules");
if (c != null) {
- Py.writeComment(IMPORT_LOG, "'" + name + "' as " + mod
- + " in builtin modules");
+ Py.writeComment(IMPORT_LOG, "'" + name + "' as " + mod + " in builtin modules");
try {
if (PyObject.class.isAssignableFrom(c)) { // xxx ok?
return PyType.fromClass(c);
}
return createFromClass(name, c);
} catch (NoClassDefFoundError e) {
- throw Py.ImportError("Cannot import " + name
- + ", missing class " + c.getName());
+ throw Py.ImportError("Cannot import " + name + ", missing class " + c.getName());
}
}
}
@@ -584,14 +576,14 @@
ReentrantLock importLock = Py.getSystemState().getImportLock();
importLock.lock();
try {
- return load_module.__call__(new PyObject[]{new PyString(name)});
+ return load_module.__call__(new PyObject[] {new PyString(name)});
} finally {
importLock.unlock();
}
}
public static PyObject loadFromCompiled(String name, InputStream stream, String sourceName,
- String compiledName) {
+ String compiledName) {
return createFromPyClass(name, stream, false, sourceName, compiledName);
}
@@ -604,8 +596,8 @@
// /tmp/foo/bar.py)
String displayDirName = entry.equals("") ? null : entry.toString();
String displaySourceName = new File(new File(displayDirName, name), sourceName).getPath();
- String displayCompiledName = new File(new File(displayDirName, name),
- compiledName).getPath();
+ String displayCompiledName =
+ new File(new File(displayDirName, name), compiledName).getPath();
// First check for packages
File dir = new File(dirName, name);
@@ -619,8 +611,7 @@
pkg = true;
} else {
Py.warning(Py.ImportWarning, String.format(
- "Not importing directory '%s': missing __init__.py",
- dirName));
+ "Not importing directory '%s': missing __init__.py", dirName));
}
}
} catch (SecurityException e) {
@@ -648,26 +639,26 @@
Py.writeDebug(IMPORT_LOG, "trying precompiled " + compiledFile.getPath());
long classTime = compiledFile.lastModified();
if (classTime >= pyTime) {
- PyObject ret = createFromPyClass(
- modName, makeStream(compiledFile), true,
+ PyObject ret = createFromPyClass(modName, makeStream(compiledFile), //
+ true, // OK to fail here as we have the source
displaySourceName, displayCompiledName, pyTime);
if (ret != null) {
return ret;
}
}
return createFromSource(modName, makeStream(sourceFile), displaySourceName,
- compiledFile.getPath(), pyTime);
+ compiledFile.getPath(), pyTime);
}
return createFromSource(modName, makeStream(sourceFile), displaySourceName,
- compiledFile.getPath(), pyTime);
+ compiledFile.getPath(), pyTime);
}
// If no source, try loading precompiled
Py.writeDebug(IMPORT_LOG, "trying precompiled with no source " + compiledFile.getPath());
if (compiledFile.isFile() && caseok(compiledFile, compiledName)) {
- return createFromPyClass(
- modName, makeStream(compiledFile), true, displaySourceName,
- displayCompiledName, NO_MTIME, CodeImport.compiled_only);
+ return createFromPyClass(modName, makeStream(compiledFile), //
+ false, // throw ImportError here if this fails
+ displaySourceName, displayCompiledName, NO_MTIME, CodeImport.compiled_only);
}
} catch (SecurityException e) {
// ok
@@ -683,13 +674,13 @@
File canFile = new File(file.getCanonicalPath());
boolean match = filename.regionMatches(0, canFile.getName(), 0, filename.length());
if (!match) {
- //possibly a symlink. Get parent and look for exact match in listdir()
- //This is what CPython does in the case of Mac OS X and Cygwin.
- //XXX: This will be a performance hit, maybe jdk7 nio2 can give us a better
- // method?
+ // possibly a symlink. Get parent and look for exact match in listdir()
+ // This is what CPython does in the case of Mac OS X and Cygwin.
+ // XXX: This will be a performance hit, maybe jdk7 nio2 can give us a better
+ // method?
File parent = file.getParentFile();
String[] children = parent.list();
- for (String c: children) {
+ for (String c : children) {
if (c.equals(filename)) {
return true;
}
@@ -702,8 +693,7 @@
}
/**
- * Load the module by name. Upon loading the module it will be added to
- * sys.modules.
+ * Load the module by name. Upon loading the module it will be added to sys.modules.
*
* @param name the name of the module to load
* @return the loaded module
@@ -719,36 +709,32 @@
}
}
- /**
- * Find the parent package name for a module.
- *
- * If __name__ does not exist in the module or if level is <code>0</code>,
- * then the parent is <code>null</code>. If __name__ does exist and is not a
- * package name, the containing package is located. If no such package
- * exists and level is <code>-1</code>, the parent is <code>null</code>. If
- * level is <code>-1</code>, the parent is the current name. Otherwise,
- * <code>level-1</code> doted parts are stripped from the current name. For
- * example, the __name__ <code>"a.b.c"</code> and level <code>2</code> would
- * return <code>"a.b"</code>, if <code>c</code> is a package and would
- * return <code>"a"</code>, if <code>c</code> is not a package.
- *
- * @param dict
- * the __dict__ of a loaded module
- * @param level
- * used for relative and absolute imports. -1 means try both, 0
- * means absolute only, positive ints represent the level to look
- * upward for a relative path (1 means current package, 2 means
- * one level up). See PEP 328 at
- * http://www.python.org/dev/peps/pep-0328/
- *
- * @return the parent name for a module
- */
+ /**
+ * Find the parent package name for a module.
+ *
+ * If __name__ does not exist in the module or if level is <code>0</code>, then the parent is
+ * <code>null</code>. If __name__ does exist and is not a package name, the containing package
+ * is located. If no such package exists and level is <code>-1</code>, the parent is
+ * <code>null</code>. If level is <code>-1</code>, the parent is the current name. Otherwise,
+ * <code>level-1</code> doted parts are stripped from the current name. For example, the
+ * __name__ <code>"a.b.c"</code> and level <code>2</code> would return <code>"a.b"</code>, if
+ * <code>c</code> is a package and would return <code>"a"</code>, if <code>c</code> is not a
+ * package.
+ *
+ * @param dict the __dict__ of a loaded module
+ * @param level used for relative and absolute imports. -1 means try both, 0 means absolute
+ * only, positive ints represent the level to look upward for a relative path (1
+ * means current package, 2 means one level up). See PEP 328 at
+ * http://www.python.org/dev/peps/pep-0328/
+ *
+ * @return the parent name for a module
+ */
private static String get_parent(PyObject dict, int level) {
String modname;
int orig_level = level;
if ((dict == null && level == -1) || level == 0) {
- // try an absolute import
+ // try an absolute import
return null;
}
@@ -759,7 +745,7 @@
}
modname = ((PyString)tmp).getString();
} else {
- //__package__ not set, so figure it out and set it.
+ // __package__ not set, so figure it out and set it.
tmp = dict.__finditem__("__name__");
if (tmp == null) {
@@ -770,7 +756,7 @@
// locate the current package
tmp = dict.__finditem__("__path__");
if (tmp instanceof PyList) {
- //__path__ is set, so modname is already the package name.
+ // __path__ is set, so modname is already the package name.
dict.__setitem__("__package__", new PyString(modname));
} else {
// __name__ is not a package name, try one level upwards.
@@ -801,15 +787,12 @@
if (Py.getSystemState().modules.__finditem__(modname) == null) {
if (orig_level < 1) {
if (modname.length() > 0) {
- Py.warning(Py.RuntimeWarning,
- String.format(
- "Parent module '%.200s' not found " +
- "while handling absolute import", modname));
+ Py.warning(Py.RuntimeWarning, String.format("Parent module '%.200s' not found "
+ + "while handling absolute import", modname));
}
} else {
- throw Py.SystemError(String.format(
- "Parent module '%.200s' not loaded, " +
- "cannot perform relative import", modname));
+ throw Py.SystemError(String.format("Parent module '%.200s' not loaded, "
+ + "cannot perform relative import", modname));
}
}
return modname.intern();
@@ -822,8 +805,8 @@
* @param name the name of the module to load
* @return null or None
*/
- private static PyObject import_next(PyObject mod,
- StringBuilder parentNameBuffer, String name, String outerFullName, PyObject fromlist) {
+ private static PyObject import_next(PyObject mod, StringBuilder parentNameBuffer, String name,
+ String outerFullName, PyObject fromlist) {
if (parentNameBuffer.length() > 0 && name != null && name.length() > 0) {
parentNameBuffer.append('.');
}
@@ -856,8 +839,7 @@
}
// never returns null or None
- private static PyObject import_first(String name,
- StringBuilder parentNameBuffer) {
+ private static PyObject import_first(String name, StringBuilder parentNameBuffer) {
PyObject ret = import_next(null, parentNameBuffer, name, null, null);
if (ret == null || ret == Py.None) {
throw Py.ImportError("No module named " + name);
@@ -865,8 +847,8 @@
return ret;
}
-
- private static PyObject import_first(String name, StringBuilder parentNameBuffer, String fullName, PyObject fromlist) {
+ private static PyObject import_first(String name, StringBuilder parentNameBuffer,
+ String fullName, PyObject fromlist) {
PyObject ret = import_next(null, parentNameBuffer, name, fullName, fromlist);
if (ret == null || ret == Py.None) {
if (JavaImportHelper.tryAddPackage(fullName, fromlist)) {
@@ -879,12 +861,11 @@
return ret;
}
-
// Hierarchy-recursively search for dotted name in mod;
// never returns null or None
// ??pending: check if result is really a module/jpkg/jclass?
- private static PyObject import_logic(PyObject mod,
- StringBuilder parentNameBuffer, String dottedName, String fullName, PyObject fromlist) {
+ private static PyObject import_logic(PyObject mod, StringBuilder parentNameBuffer,
+ String dottedName, String fullName, PyObject fromlist) {
int dot = 0;
int last_dot = 0;
@@ -905,7 +886,7 @@
if (jpkg != null && (mod == null || mod == Py.None)) {
// try again -- under certain circumstances a PyJavaPackage may
// have been added as a side effect of the last import_next
- // attempt. see Lib/test_classpathimport.py#test_bug1126
+ // attempt. see Lib/test_classpathimport.py#test_bug1126
mod = import_next(jpkg, parentNameBuffer, name, fullName, fromlist);
}
if (mod == null || mod == Py.None) {
@@ -923,8 +904,8 @@
* @param modDict
* @return a module
*/
- private static PyObject import_module_level(String name, boolean top,
- PyObject modDict, PyObject fromlist, int level) {
+ private static PyObject import_module_level(String name, boolean top, PyObject modDict,
+ PyObject fromlist, int level) {
if (name.length() == 0) {
if (level == 0 || modDict == null) {
throw Py.ValueError("Empty module name");
@@ -959,7 +940,7 @@
PyObject topMod = import_next(pkgMod, parentNameBuffer, firstName, name, fromlist);
if (topMod == Py.None || topMod == null) {
// Add None to sys.modules for submodule or subpackage names that aren't found, but
- // leave top-level entries out. This allows them to be tried again if another
+ // leave top-level entries out. This allows them to be tried again if another
// import attempt is made after they've been added to sys.path.
if (topMod == null && pkgMod != null) {
modules.__setitem__(parentNameBuffer.toString().intern(), Py.None);
@@ -975,8 +956,7 @@
PyObject mod = topMod;
if (dot != -1) {
// could throw ImportError
- mod = import_logic(topMod, parentNameBuffer, name
- .substring(dot + 1), name, fromlist);
+ mod = import_logic(topMod, parentNameBuffer, name.substring(dot + 1), name, fromlist);
}
if (top) {
return topMod;
@@ -993,37 +973,37 @@
}
private static void ensureFromList(PyObject mod, PyObject fromlist, String name,
- boolean recursive) {
- if (mod.__findattr__("__path__") == null) {
- return;
+ boolean recursive) {
+ if (mod.__findattr__("__path__") == null) {
+ return;
+ }
+
+ // This can happen with imports like "from . import foo"
+ if (name.length() == 0) {
+ name = mod.__findattr__("__name__").toString();
+ }
+
+ StringBuilder modNameBuffer = new StringBuilder(name);
+ for (PyObject item : fromlist.asIterable()) {
+ if (!Py.isInstance(item, PyBaseString.TYPE)) {
+ throw Py.TypeError("Item in ``from list'' not a string");
+ }
+ if (item.toString().equals("*")) {
+ if (recursive) {
+ // Avoid endless recursion
+ continue;
+ }
+ PyObject all;
+ if ((all = mod.__findattr__("__all__")) != null) {
+ ensureFromList(mod, all, name, true);
+ }
}
- //This can happen with imports like "from . import foo"
- if (name.length() == 0) {
- name = mod.__findattr__("__name__").toString();
+ if (mod.__findattr__((PyString)item) == null) {
+ String fullName = modNameBuffer.toString() + "." + item.toString();
+ import_next(mod, modNameBuffer, item.toString(), fullName, null);
}
-
- StringBuilder modNameBuffer = new StringBuilder(name);
- for (PyObject item : fromlist.asIterable()) {
- if (!Py.isInstance(item, PyBaseString.TYPE)) {
- throw Py.TypeError("Item in ``from list'' not a string");
- }
- if (item.toString().equals("*")) {
- if (recursive) {
- // Avoid endless recursion
- continue;
- }
- PyObject all;
- if ((all = mod.__findattr__("__all__")) != null) {
- ensureFromList(mod, all, name, true);
- }
- }
-
- if (mod.__findattr__((PyString) item) == null) {
- String fullName = modNameBuffer.toString() + "." + item.toString();
- import_next(mod, modNameBuffer, item.toString(), fullName, null);
- }
- }
+ }
}
/**
@@ -1045,16 +1025,15 @@
}
/**
- * Import a module by name. This is the default call for
- * __builtin__.__import__.
+ * Import a module by name. This is the default call for __builtin__.__import__.
*
* @param name the name of the package to import
* @param top if true, return the top module in the name, otherwise the last
* @param modDict the __dict__ of an already imported module
* @return an imported module (Java or Python)
*/
- public static PyObject importName(String name, boolean top,
- PyObject modDict, PyObject fromlist, int level) {
+ public static PyObject importName(String name, boolean top, PyObject modDict,
+ PyObject fromlist, int level) {
PyUnicode.checkEncoding(name);
ReentrantLock importLock = Py.getSystemState().getImportLock();
importLock.lock();
@@ -1066,38 +1045,36 @@
}
/**
- * Called from jython generated code when a statement like "import spam" is
- * executed.
+ * Called from jython generated code when a statement like "import spam" is executed.
*/
@Deprecated
public static PyObject importOne(String mod, PyFrame frame) {
- return importOne(mod, frame, imp.DEFAULT_LEVEL);
+ return importOne(mod, frame, imp.DEFAULT_LEVEL);
}
+
/**
- * Called from jython generated code when a statement like "import spam" is
- * executed.
+ * Called from jython generated code when a statement like "import spam" is executed.
*/
public static PyObject importOne(String mod, PyFrame frame, int level) {
- PyObject module = __builtin__.__import__(mod, frame.f_globals, frame
- .getLocals(), Py.None, level);
+ PyObject module =
+ __builtin__.__import__(mod, frame.f_globals, frame.getLocals(), Py.None, level);
return module;
}
/**
- * Called from jython generated code when a statement like "import spam as
- * foo" is executed.
+ * Called from jython generated code when a statement like "import spam as foo" is executed.
*/
@Deprecated
public static PyObject importOneAs(String mod, PyFrame frame) {
- return importOneAs(mod, frame, imp.DEFAULT_LEVEL);
+ return importOneAs(mod, frame, imp.DEFAULT_LEVEL);
}
+
/**
- * Called from jython generated code when a statement like "import spam as
- * foo" is executed.
+ * Called from jython generated code when a statement like "import spam as foo" is executed.
*/
public static PyObject importOneAs(String mod, PyFrame frame, int level) {
- PyObject module = __builtin__.__import__(mod, frame.f_globals, frame
- .getLocals(), Py.None, level);
+ PyObject module =
+ __builtin__.__import__(mod, frame.f_globals, frame.getLocals(), Py.None, level);
int dot = mod.indexOf('.');
while (dot != -1) {
int dot2 = mod.indexOf('.', dot + 1);
@@ -1114,55 +1091,55 @@
}
/**
- * replaced by importFrom with level param. Kept for backwards compatibility.
+ * replaced by importFrom with level param. Kept for backwards compatibility.
+ *
* @deprecated use importFrom with level param.
*/
@Deprecated
- public static PyObject[] importFrom(String mod, String[] names,
- PyFrame frame) {
+ public static PyObject[] importFrom(String mod, String[] names, PyFrame frame) {
return importFromAs(mod, names, null, frame, DEFAULT_LEVEL);
}
/**
- * Called from jython generated code when a statement like "from spam.eggs
- * import foo, bar" is executed.
+ * Called from jython generated code when a statement like "from spam.eggs import foo, bar" is
+ * executed.
*/
- public static PyObject[] importFrom(String mod, String[] names,
- PyFrame frame, int level) {
+ public static PyObject[] importFrom(String mod, String[] names, PyFrame frame, int level) {
return importFromAs(mod, names, null, frame, level);
}
/**
- * replaced by importFromAs with level param. Kept for backwards compatibility.
+ * replaced by importFromAs with level param. Kept for backwards compatibility.
+ *
* @deprecated use importFromAs with level param.
*/
@Deprecated
- public static PyObject[] importFromAs(String mod, String[] names,
- PyFrame frame) {
+ public static PyObject[] importFromAs(String mod, String[] names, PyFrame frame) {
return importFromAs(mod, names, null, frame, DEFAULT_LEVEL);
}
/**
- * Called from jython generated code when a statement like "from spam.eggs
- * import foo as spam" is executed.
+ * Called from jython generated code when a statement like "from spam.eggs import foo as spam"
+ * is executed.
*/
- public static PyObject[] importFromAs(String mod, String[] names,
- String[] asnames, PyFrame frame, int level) {
+ public static PyObject[] importFromAs(String mod, String[] names, String[] asnames,
+ PyFrame frame, int level) {
PyObject[] pyNames = new PyObject[names.length];
for (int i = 0; i < names.length; i++) {
pyNames[i] = Py.newString(names[i]);
}
- PyObject module = __builtin__.__import__(mod, frame.f_globals, frame.getLocals(),
- new PyTuple(pyNames), level);
+ PyObject module =
+ __builtin__.__import__(mod, frame.f_globals, frame.getLocals(),
+ new PyTuple(pyNames), level);
PyObject[] submods = new PyObject[names.length];
for (int i = 0; i < names.length; i++) {
PyObject submod = module.__findattr__(names[i]);
- //XXX: Temporary fix for http://bugs.jython.org/issue1900
+ // XXX: Temporary fix for http://bugs.jython.org/issue1900
if (submod == null) {
submod = module.impAttr(names[i]);
}
- //end temporary fix.
+ // end temporary fix.
if (submod == null) {
throw Py.ImportError("cannot import name " + names[i]);
@@ -1175,25 +1152,25 @@
private final static PyTuple all = new PyTuple(Py.newString('*'));
/**
- * Called from jython generated code when a statement like "from spam.eggs
- * import *" is executed.
+ * Called from jython generated code when a statement like "from spam.eggs import *" is
+ * executed.
*/
public static void importAll(String mod, PyFrame frame, int level) {
- PyObject module = __builtin__.__import__(mod, frame.f_globals, frame
- .getLocals(), all, level);
+ PyObject module =
+ __builtin__.__import__(mod, frame.f_globals, frame.getLocals(), all, level);
importAll(module, frame);
}
+
@Deprecated
public static void importAll(String mod, PyFrame frame) {
importAll(mod, frame, DEFAULT_LEVEL);
}
-
public static void importAll(PyObject module, PyFrame frame) {
PyObject names;
boolean filter = true;
if (module instanceof PyJavaPackage) {
- names = ((PyJavaPackage) module).fillDir();
+ names = ((PyJavaPackage)module).fillDir();
} else {
PyObject __all__ = module.__findattr__("__all__");
if (__all__ != null) {
@@ -1207,23 +1184,17 @@
loadNames(names, module, frame.getLocals(), filter);
}
-
-
-
/**
- * From a module, load the attributes found in <code>names</code> into
- * locals.
+ * From a module, load the attributes found in <code>names</code> into locals.
*
- * @param filter if true, if the name starts with an underscore '_' do not
- * add it to locals
+ * @param filter if true, if the name starts with an underscore '_' do not add it to locals
* @param locals the namespace into which names will be loaded
* @param names the names to load from the module
* @param module the fully imported module
*/
- private static void loadNames(PyObject names, PyObject module,
- PyObject locals, boolean filter) {
+ private static void loadNames(PyObject names, PyObject module, PyObject locals, boolean filter) {
for (PyObject name : names.asIterable()) {
- String sname = ((PyString) name).internedString();
+ String sname = ((PyString)name).internedString();
if (filter && sname.startsWith("_")) {
continue;
} else {
@@ -1233,8 +1204,9 @@
PyObject nameObj = module.__findattr__("__name__");
if (nameObj != null) {
String submodName = nameObj.__str__().toString() + '.' + sname;
- value = __builtin__.__import__(submodName, null, null,
- nonEmptyFromlist);
+ value =
+ __builtin__
+ .__import__(submodName, null, null, nonEmptyFromlist);
}
}
locals.__setitem__(sname, value);
@@ -1259,16 +1231,16 @@
}
}
- private static PyObject _reload(PyModule m, PyObject modules, Map<String, PyModule> modules_reloading) {
+ private static PyObject _reload(PyModule m, PyObject modules,
+ Map<String, PyModule> modules_reloading) {
String name = m.__getattr__("__name__").toString().intern();
- PyModule nm = (PyModule) modules.__finditem__(name);
+ PyModule nm = (PyModule)modules.__finditem__(name);
if (nm == null || !nm.__getattr__("__name__").toString().equals(name)) {
- throw Py.ImportError("reload(): module " + name
- + " not in sys.modules");
+ throw Py.ImportError("reload(): module " + name + " not in sys.modules");
}
PyModule existing_module = modules_reloading.get(name);
if (existing_module != null) {
- // Due to a recursive reload, this module is already being reloaded.
+ // Due to a recursive reload, this module is already being reloaded.
return existing_module;
}
// Since we are already in a re-entrant lock,
@@ -1284,7 +1256,7 @@
if (pkg == null) {
throw Py.ImportError("reload(): parent not in sys.modules");
}
- path = (PyList) pkg.__getattr__("__path__");
+ path = (PyList)pkg.__getattr__("__path__");
name = name.substring(dot + 1, name.length()).intern();
}
diff --git a/src/org/python/modules/posix/PosixModule.java b/src/org/python/modules/posix/PosixModule.java
--- a/src/org/python/modules/posix/PosixModule.java
+++ b/src/org/python/modules/posix/PosixModule.java
@@ -307,10 +307,16 @@
public static PyString __doc__chmod = new PyString(
"chmod(path, mode)\n\n" +
"Change the access permissions of a file.");
+
public static void chmod(PyObject path, int mode) {
if (os == OS.NT) {
try {
- if (!absolutePath(path).toFile().setWritable((mode & FileStat.S_IWUSR) != 0)) {
+ // We can only allow/deny write access (not read & execute)
+ boolean writable = (mode & FileStat.S_IWUSR) != 0;
+ File f = absolutePath(path).toFile();
+ if (!f.exists()) {
+ throw Py.OSError(Errno.ENOENT, path);
+ } else if (!f.setWritable(writable)) {
throw Py.OSError(Errno.EPERM, path);
}
} catch (SecurityException ex) {
--
Repository URL: https://hg.python.org/jython
More information about the Jython-checkins
mailing list