From 8e4680ebfee6821fd3867903faecc666c5d690b5 Mon Sep 17 00:00:00 2001 From: Fabio Zadrozny Date: Sat, 12 Oct 2024 10:26:56 -0300 Subject: [PATCH] Using code generation for python version dependent constants --- .../org/python/pydev/core/IPythonNature.java | 103 ++++++++++++------ .../AbstractWorkbenchTestCase.java | 2 +- rootproject/dev.py | 1 + 3 files changed, 72 insertions(+), 34 deletions(-) diff --git a/plugins/org.python.pydev.core/src/org/python/pydev/core/IPythonNature.java b/plugins/org.python.pydev.core/src/org/python/pydev/core/IPythonNature.java index 129c8427fc..7b9e18cb09 100644 --- a/plugins/org.python.pydev.core/src/org/python/pydev/core/IPythonNature.java +++ b/plugins/org.python.pydev.core/src/org/python/pydev/core/IPythonNature.java @@ -40,9 +40,9 @@ public static class Versions { public static final HashSet ALL_VERSIONS_ANY_FLAVOR = new HashSet(); public static final List VERSION_NUMBERS = new ArrayList(); - public static final String JYTHON_VERSION_LATEST = JYTHON_VERSION_3_0; + public static final String JYTHON_VERSION_LATEST = JYTHON_VERSION_3_5; public static final String PYTHON_VERSION_LATEST = PYTHON_VERSION_3_12; - public static final String IRONPYTHON_VERSION_LATEST = IRONPYTHON_VERSION_3_0; + public static final String IRONPYTHON_VERSION_LATEST = IRONPYTHON_VERSION_3_5; public static final String INTERPRETER_VERSION = "interpreter"; private static final Map mappedVersions = new HashMap<>(); @@ -51,10 +51,60 @@ public static class Versions { public static final String JYTHON_PREFIX = "jython"; public static final String IRONYTHON_PREFIX = "ironpython"; + /*[[[cog + # Note: run + # python -m dev codegen + # to regenerate + from codegen_helper import python_versions_base + + cog.outl(f'public static final String LATEST_VERSION_NUMBER = "{python_versions_base[-1]}";') + ]]]*/ public static final String LATEST_VERSION_NUMBER = "3.12"; + /*[[[end]]]*/ static { - ALL_PYTHON_VERSIONS.add(PYTHON_VERSION_3_0); + // Anything below 3.5 is marked as 3.5 + mappedVersions.put("3.0", "3.5"); + mappedVersions.put("3.1", "3.5"); + mappedVersions.put("3.2", "3.5"); + mappedVersions.put("3.3", "3.5"); + mappedVersions.put("3.4", "3.5"); + mappedVersions.put("3.5", "3.5"); + VERSION_NUMBERS.add(INTERPRETER_VERSION); + + /*[[[cog + # Note: run + # python -m dev codegen + # to regenerate + from codegen_helper import python_versions_base, python_versions_underscore + + for version_under, version_base in zip(python_versions_underscore, python_versions_base): + constant_name = f'PYTHON_VERSION_{version_under}' + cog.outl(f'ALL_PYTHON_VERSIONS.add({constant_name});') + + cog.outl('') + + for version_under, version_base in zip(python_versions_underscore, python_versions_base): + constant_name = f'JYTHON_VERSION_{version_under}' + cog.outl(f'ALL_JYTHON_VERSIONS.add({constant_name});') + + cog.outl('') + + for version_under, version_base in zip(python_versions_underscore, python_versions_base): + constant_name = f'IRONPYTHON_VERSION_{version_under}' + cog.outl(f'ALL_IRONPYTHON_VERSIONS.add({constant_name});') + + cog.outl('') + + for version_under, version_base in zip(python_versions_underscore, python_versions_base): + cog.outl(f'VERSION_NUMBERS.add("{version_base}");') + + cog.outl('') + + for version_under, version_base in zip(python_versions_underscore, python_versions_base): + cog.outl(f'mappedVersions.put("{version_base}", "{version_base}");') + ]]]*/ + ALL_PYTHON_VERSIONS.add(PYTHON_VERSION_3_5); ALL_PYTHON_VERSIONS.add(PYTHON_VERSION_3_6); ALL_PYTHON_VERSIONS.add(PYTHON_VERSION_3_7); ALL_PYTHON_VERSIONS.add(PYTHON_VERSION_3_8); @@ -62,9 +112,8 @@ public static class Versions { ALL_PYTHON_VERSIONS.add(PYTHON_VERSION_3_10); ALL_PYTHON_VERSIONS.add(PYTHON_VERSION_3_11); ALL_PYTHON_VERSIONS.add(PYTHON_VERSION_3_12); - ALL_PYTHON_VERSIONS.add(PYTHON_VERSION_INTERPRETER); - ALL_JYTHON_VERSIONS.add(JYTHON_VERSION_3_0); + ALL_JYTHON_VERSIONS.add(JYTHON_VERSION_3_5); ALL_JYTHON_VERSIONS.add(JYTHON_VERSION_3_6); ALL_JYTHON_VERSIONS.add(JYTHON_VERSION_3_7); ALL_JYTHON_VERSIONS.add(JYTHON_VERSION_3_8); @@ -72,9 +121,8 @@ public static class Versions { ALL_JYTHON_VERSIONS.add(JYTHON_VERSION_3_10); ALL_JYTHON_VERSIONS.add(JYTHON_VERSION_3_11); ALL_JYTHON_VERSIONS.add(JYTHON_VERSION_3_12); - ALL_JYTHON_VERSIONS.add(JYTHON_VERSION_INTERPRETER); - ALL_IRONPYTHON_VERSIONS.add(IRONPYTHON_VERSION_3_0); + ALL_IRONPYTHON_VERSIONS.add(IRONPYTHON_VERSION_3_5); ALL_IRONPYTHON_VERSIONS.add(IRONPYTHON_VERSION_3_6); ALL_IRONPYTHON_VERSIONS.add(IRONPYTHON_VERSION_3_7); ALL_IRONPYTHON_VERSIONS.add(IRONPYTHON_VERSION_3_8); @@ -82,43 +130,32 @@ public static class Versions { ALL_IRONPYTHON_VERSIONS.add(IRONPYTHON_VERSION_3_10); ALL_IRONPYTHON_VERSIONS.add(IRONPYTHON_VERSION_3_11); ALL_IRONPYTHON_VERSIONS.add(IRONPYTHON_VERSION_3_12); - ALL_IRONPYTHON_VERSIONS.add(IRONPYTHON_VERSION_INTERPRETER); - - ALL_VERSIONS_ANY_FLAVOR.addAll(ALL_JYTHON_VERSIONS); - ALL_VERSIONS_ANY_FLAVOR.addAll(ALL_PYTHON_VERSIONS); - ALL_VERSIONS_ANY_FLAVOR.addAll(ALL_IRONPYTHON_VERSIONS); VERSION_NUMBERS.add("3.5"); VERSION_NUMBERS.add("3.6"); - VERSION_NUMBERS.add("3.7"); // actually the same as 3.6 + VERSION_NUMBERS.add("3.7"); VERSION_NUMBERS.add("3.8"); - VERSION_NUMBERS.add("3.9"); // actually the same as 3.8 + VERSION_NUMBERS.add("3.9"); VERSION_NUMBERS.add("3.10"); VERSION_NUMBERS.add("3.11"); VERSION_NUMBERS.add("3.12"); - VERSION_NUMBERS.add(INTERPRETER_VERSION); - // Anything below 3.5 is marked as 3.5 - mappedVersions.put("3.0", "3.5"); - mappedVersions.put("3.1", "3.5"); - mappedVersions.put("3.2", "3.5"); - mappedVersions.put("3.3", "3.5"); - mappedVersions.put("3.4", "3.5"); mappedVersions.put("3.5", "3.5"); - mappedVersions.put("3.6", "3.6"); - - mappedVersions.put("3.7", "3.7"); // actually the same as 3.6 - + mappedVersions.put("3.7", "3.7"); mappedVersions.put("3.8", "3.8"); - - mappedVersions.put("3.9", "3.9"); // actually the same as 3.8 - + mappedVersions.put("3.9", "3.9"); mappedVersions.put("3.10", "3.10"); - mappedVersions.put("3.11", "3.11"); - mappedVersions.put("3.12", "3.12"); + /*[[[end]]]*/ + + ALL_PYTHON_VERSIONS.add(PYTHON_VERSION_INTERPRETER); + ALL_JYTHON_VERSIONS.add(JYTHON_VERSION_INTERPRETER); + ALL_IRONPYTHON_VERSIONS.add(IRONPYTHON_VERSION_INTERPRETER); + ALL_VERSIONS_ANY_FLAVOR.addAll(ALL_JYTHON_VERSIONS); + ALL_VERSIONS_ANY_FLAVOR.addAll(ALL_PYTHON_VERSIONS); + ALL_VERSIONS_ANY_FLAVOR.addAll(ALL_IRONPYTHON_VERSIONS); } /** @@ -207,7 +244,7 @@ public static int getInternalVersion(String version) { * Constants persisted. Probably a better way would be disassociating whether it's python/jython and the * grammar version to be used (to avoid the explosion of constants below). */ - public static final String PYTHON_VERSION_3_0 = "python 3.5"; + public static final String PYTHON_VERSION_3_5 = "python 3.5"; public static final String PYTHON_VERSION_3_6 = "python 3.6"; public static final String PYTHON_VERSION_3_7 = "python 3.7"; public static final String PYTHON_VERSION_3_8 = "python 3.8"; @@ -217,7 +254,7 @@ public static int getInternalVersion(String version) { public static final String PYTHON_VERSION_3_12 = "python 3.12"; public static final String PYTHON_VERSION_INTERPRETER = "python interpreter"; - public static final String JYTHON_VERSION_3_0 = "jython 3.5"; + public static final String JYTHON_VERSION_3_5 = "jython 3.5"; public static final String JYTHON_VERSION_3_6 = "jython 3.6"; public static final String JYTHON_VERSION_3_7 = "jython 3.7"; public static final String JYTHON_VERSION_3_8 = "jython 3.8"; @@ -227,7 +264,7 @@ public static int getInternalVersion(String version) { public static final String JYTHON_VERSION_3_12 = "jython 3.12"; public static final String JYTHON_VERSION_INTERPRETER = "jython interpreter"; - public static final String IRONPYTHON_VERSION_3_0 = "ironpython 3.5"; + public static final String IRONPYTHON_VERSION_3_5 = "ironpython 3.5"; public static final String IRONPYTHON_VERSION_3_6 = "ironpython 3.6"; public static final String IRONPYTHON_VERSION_3_7 = "ironpython 3.7"; public static final String IRONPYTHON_VERSION_3_8 = "ironpython 3.8"; diff --git a/plugins/org.python.pydev/tests_completions/org/python/pydev/ast/codecompletion/revisited/javaintegration/AbstractWorkbenchTestCase.java b/plugins/org.python.pydev/tests_completions/org/python/pydev/ast/codecompletion/revisited/javaintegration/AbstractWorkbenchTestCase.java index bdc840448e..b9a95eaff6 100644 --- a/plugins/org.python.pydev/tests_completions/org/python/pydev/ast/codecompletion/revisited/javaintegration/AbstractWorkbenchTestCase.java +++ b/plugins/org.python.pydev/tests_completions/org/python/pydev/ast/codecompletion/revisited/javaintegration/AbstractWorkbenchTestCase.java @@ -531,7 +531,7 @@ protected IFolder createSourceFolder(IProgressMonitor monitor, IProject project, if (addNature) { String name = project.getName(); if (isJython) { - PythonNature.addNature(project, monitor, PythonNature.JYTHON_VERSION_3_0, "/" + name + + PythonNature.addNature(project, monitor, PythonNature.JYTHON_VERSION_3_5, "/" + name + "/src|/" + name + "/grinder.jar", null, null, null); diff --git a/rootproject/dev.py b/rootproject/dev.py index 0168c54f7e..58abb3fc7a 100644 --- a/rootproject/dev.py +++ b/rootproject/dev.py @@ -289,6 +289,7 @@ def codegen(): 'plugins/org.python.pydev.core/src/org/python/pydev/core/IGrammarVersionProvider.java', 'plugins/org.python.pydev.ast/src/org/python/pydev/plugin/nature/PythonNature.java', 'plugins/org.python.pydev.parser/src/org/python/pydev/parser/PyParser.java', + 'plugins/org.python.pydev.core/src/org/python/pydev/core/IPythonNature.java', ]: f1 = _pydev_root / f assert f1.exists(), f'{f1} does not exist'