Skip to content

Commit

Permalink
gh-99482: remove jython compatibility parts from stdlib and tests (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn committed Dec 23, 2022
1 parent c5726b7 commit 745545b
Show file tree
Hide file tree
Showing 20 changed files with 136 additions and 234 deletions.
3 changes: 3 additions & 0 deletions Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,9 @@ Removed
``SSTATE_INTERNED_IMMORTAL`` macro.
(Contributed by Victor Stinner in :gh:`85858`.)

* Remove ``Jython`` compatibility hacks from several stdlib modules and tests.
(Contributed by Nikita Sobolev in :gh:`99482`.)

* Remove ``_use_broken_old_ctypes_structure_semantics_`` flag
from :mod:`ctypes` module.
(Contributed by Nikita Sobolev in :gh:`99285`.)
12 changes: 1 addition & 11 deletions Lib/copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ class Error(Exception):
pass
error = Error # backward compatibility

try:
from org.python.core import PyStringMap
except ImportError:
PyStringMap = None

__all__ = ["Error", "copy", "deepcopy"]

def copy(x):
Expand Down Expand Up @@ -120,9 +115,6 @@ def _copy_immutable(x):
d[set] = set.copy
d[bytearray] = bytearray.copy

if PyStringMap is not None:
d[PyStringMap] = PyStringMap.copy

del d, t

def deepcopy(x, memo=None, _nil=[]):
Expand Down Expand Up @@ -231,8 +223,6 @@ def _deepcopy_dict(x, memo, deepcopy=deepcopy):
y[deepcopy(key, memo)] = deepcopy(value, memo)
return y
d[dict] = _deepcopy_dict
if PyStringMap is not None:
d[PyStringMap] = _deepcopy_dict

def _deepcopy_method(x, memo): # Copy instance methods
return type(x)(x.__func__, deepcopy(x.__self__, memo))
Expand Down Expand Up @@ -301,4 +291,4 @@ def _reconstruct(x, memo, func, args,
y[key] = value
return y

del types, weakref, PyStringMap
del types, weakref
8 changes: 0 additions & 8 deletions Lib/pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,6 @@ class _Stop(Exception):
def __init__(self, value):
self.value = value

# Jython has PyStringMap; it's a dict subclass with string keys
try:
from org.python.core import PyStringMap
except ImportError:
PyStringMap = None

# Pickle opcodes. See pickletools.py for extensive docs. The listing
# here is in kind-of alphabetical order of 1-character pickle code.
# pickletools groups them by purpose.
Expand Down Expand Up @@ -972,8 +966,6 @@ def save_dict(self, obj):
self._batch_setitems(obj.items())

dispatch[dict] = save_dict
if PyStringMap is not None:
dispatch[PyStringMap] = save_dict

def _batch_setitems(self, items):
# Helper to batch up SETITEMS sequences; proto >= 1 only
Expand Down
2 changes: 1 addition & 1 deletion Lib/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,7 @@ def platform(aliased=0, terse=0):
else:
platform = _platform(system, release, version, csd)

elif system in ('Linux',):
elif system == 'Linux':
# check for libc vs. glibc
libcname, libcversion = libc_ver()
platform = _platform(system, release, machine, processor,
Expand Down
7 changes: 1 addition & 6 deletions Lib/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,12 +404,7 @@ def setquit():
def setcopyright():
"""Set 'copyright' and 'credits' in builtins"""
builtins.copyright = _sitebuiltins._Printer("copyright", sys.copyright)
if sys.platform[:4] == 'java':
builtins.credits = _sitebuiltins._Printer(
"credits",
"Jython is maintained by the Jython developers (www.jython.org).")
else:
builtins.credits = _sitebuiltins._Printer("credits", """\
builtins.credits = _sitebuiltins._Printer("credits", """\
Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands
for supporting Python development. See www.python.org for more information.""")
files, dirs = [], []
Expand Down
3 changes: 1 addition & 2 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ def requires_debug_ranges(reason='requires co_positions / debug_ranges'):
requires_legacy_unicode_capi = unittest.skipUnless(unicode_legacy_string,
'requires legacy Unicode C API')

# Is not actually used in tests, but is kept for compatibility.
is_jython = sys.platform.startswith('java')

is_android = hasattr(sys, 'getandroidapilevel')
Expand Down Expand Up @@ -736,8 +737,6 @@ def gc_collect():
"""
import gc
gc.collect()
if is_jython:
time.sleep(0.1)
gc.collect()
gc.collect()

Expand Down
6 changes: 1 addition & 5 deletions Lib/test/support/os_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@


# Filename used for testing
if os.name == 'java':
# Jython disallows @ in module names
TESTFN_ASCII = '$test'
else:
TESTFN_ASCII = '@test'
TESTFN_ASCII = '@test'

# Disambiguate TESTFN for parallel testing, while letting it remain a valid
# module name.
Expand Down
7 changes: 3 additions & 4 deletions Lib/test/test___all__.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,9 @@ def test_all(self):
'__future__',
])

if not sys.platform.startswith('java'):
# In case _socket fails to build, make this test fail more gracefully
# than an AttributeError somewhere deep in CGIHTTPServer.
import _socket
# In case _socket fails to build, make this test fail more gracefully
# than an AttributeError somewhere deep in CGIHTTPServer.
import _socket

ignored = []
failed_imports = []
Expand Down
49 changes: 8 additions & 41 deletions Lib/test/test_codeop.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,18 @@
Test cases for codeop.py
Nick Mathewson
"""
import sys
import unittest
import warnings
from test import support
from test.support import warnings_helper

from codeop import compile_command, PyCF_DONT_IMPLY_DEDENT
import io

if support.is_jython:

def unify_callables(d):
for n,v in d.items():
if hasattr(v, '__call__'):
d[n] = True
return d

class CodeopTests(unittest.TestCase):

def assertValid(self, str, symbol='single'):
'''succeed iff str is a valid piece of code'''
if support.is_jython:
code = compile_command(str, "<input>", symbol)
self.assertTrue(code)
if symbol == "single":
d,r = {},{}
saved_stdout = sys.stdout
sys.stdout = io.StringIO()
try:
exec(code, d)
exec(compile(str,"<input>","single"), r)
finally:
sys.stdout = saved_stdout
elif symbol == 'eval':
ctx = {'a': 2}
d = { 'value': eval(code,ctx) }
r = { 'value': eval(str,ctx) }
self.assertEqual(unify_callables(r),unify_callables(d))
else:
expected = compile(str, "<input>", symbol, PyCF_DONT_IMPLY_DEDENT)
self.assertEqual(compile_command(str, "<input>", symbol), expected)
expected = compile(str, "<input>", symbol, PyCF_DONT_IMPLY_DEDENT)
self.assertEqual(compile_command(str, "<input>", symbol), expected)

def assertIncomplete(self, str, symbol='single'):
'''succeed iff str is the start of a valid piece of code'''
Expand All @@ -62,16 +33,12 @@ def test_valid(self):
av = self.assertValid

# special case
if not support.is_jython:
self.assertEqual(compile_command(""),
compile("pass", "<input>", 'single',
PyCF_DONT_IMPLY_DEDENT))
self.assertEqual(compile_command("\n"),
compile("pass", "<input>", 'single',
PyCF_DONT_IMPLY_DEDENT))
else:
av("")
av("\n")
self.assertEqual(compile_command(""),
compile("pass", "<input>", 'single',
PyCF_DONT_IMPLY_DEDENT))
self.assertEqual(compile_command("\n"),
compile("pass", "<input>", 'single',
PyCF_DONT_IMPLY_DEDENT))

av("a = 1")
av("\na = 1")
Expand Down
7 changes: 3 additions & 4 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,9 @@ def test_capi3():
self.assertRaises(SystemError, _testcapi.raise_exception,
InvalidException, 1)

if not sys.platform.startswith('java'):
test_capi1()
test_capi2()
test_capi3()
test_capi1()
test_capi2()
test_capi3()

def test_WindowsError(self):
try:
Expand Down
7 changes: 2 additions & 5 deletions Lib/test/test_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from test.support import os_helper
from test.support import (
STDLIB_DIR, is_jython, swap_attr, swap_item, cpython_only, is_emscripten,
STDLIB_DIR, swap_attr, swap_item, cpython_only, is_emscripten,
is_wasi)
from test.support.import_helper import (
forget, make_legacy_pyc, unlink, unload, DirsOnSysPath, CleanImport)
Expand Down Expand Up @@ -163,10 +163,7 @@ def test_import(self):
def test_with_extension(ext):
# The extension is normally ".py", perhaps ".pyw".
source = TESTFN + ext
if is_jython:
pyc = TESTFN + "$py.class"
else:
pyc = TESTFN + ".pyc"
pyc = TESTFN + ".pyc"

with open(source, "w", encoding='utf-8') as f:
print("# This tests Python's ability to import a",
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def raises_oserror(*a):

def test_java_ver(self):
res = platform.java_ver()
if sys.platform == 'java':
if sys.platform == 'java': # Is never actually checked in CI
self.assertTrue(all(res))

def test_win32_ver(self):
Expand Down
12 changes: 4 additions & 8 deletions Lib/test/test_strftime.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,10 @@ def _update_variables(self, now):
self.now = now

def setUp(self):
try:
import java
java.util.Locale.setDefault(java.util.Locale.US)
except ImportError:
from locale import setlocale, LC_TIME
saved_locale = setlocale(LC_TIME)
setlocale(LC_TIME, 'C')
self.addCleanup(setlocale, LC_TIME, saved_locale)
from locale import setlocale, LC_TIME
saved_locale = setlocale(LC_TIME)
setlocale(LC_TIME, 'C')
self.addCleanup(setlocale, LC_TIME, saved_locale)

def test_strftime(self):
now = time.time()
Expand Down
Loading

0 comments on commit 745545b

Please sign in to comment.