[Jython-checkins] jython: Added PyShadowString as replacement for os.name and sys.platform so we'll have
stefan.richthofer
jython-checkins at python.org
Fri Feb 24 10:10:45 EST 2017
https://hg.python.org/jython/rev/a01c366576b8
changeset: 8033:a01c366576b8
user: Stefan Richthofer <stefan.richthofer at gmx.de>
date: Fri Feb 24 16:10:28 2017 +0100
summary:
Added PyShadowString as replacement for os.name and sys.platform so we'll have a fine-grained platform detection control.
files:
CoreExposed.includes | 1 +
Lib/os.py | 4 +-
src/org/python/core/PyString.java | 23 +++++++----
src/org/python/core/PySystemState.java | 28 +++++++++++++-
4 files changed, 45 insertions(+), 11 deletions(-)
diff --git a/CoreExposed.includes b/CoreExposed.includes
--- a/CoreExposed.includes
+++ b/CoreExposed.includes
@@ -42,6 +42,7 @@
org/python/core/PyObject.class
org/python/core/PyProperty.class
org/python/core/PySet.class
+org/python/core/PyShadowString.class
org/python/core/PySlice.class
org/python/core/PySlot.class
org/python/core/PyStaticMethod.class
diff --git a/Lib/os.py b/Lib/os.py
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -24,6 +24,7 @@
#'
import sys, errno
+from org.python.core import PyShadowString
_names = sys.builtin_module_names
@@ -38,7 +39,6 @@
except AttributeError:
return [n for n in dir(module) if n[0] != '_']
-name = 'java'
if 'posix' in _names:
_name = 'posix'
linesep = '\n'
@@ -131,6 +131,8 @@
else:
raise ImportError, 'no os specific module found'
+name = PyShadowString('java', _name)
+
sys.modules['os.path'] = path
from os.path import (curdir, pardir, sep, pathsep, defpath, extsep, altsep,
devnull)
diff --git a/src/org/python/core/PyString.java b/src/org/python/core/PyString.java
--- a/src/org/python/core/PyString.java
+++ b/src/org/python/core/PyString.java
@@ -49,6 +49,16 @@
this("", true);
}
+ protected PyString(PyType subType, String string, boolean isBytes) {
+ super(subType);
+ if (string == null) {
+ throw new IllegalArgumentException("Cannot create PyString from null");
+ } else if (!isBytes && !isBytes(string)) {
+ throw new IllegalArgumentException("Cannot create PyString with non-byte value");
+ }
+ this.string = string;
+ }
+
/**
* Fundamental constructor for <code>PyString</code> objects when the client provides a Java
* <code>String</code>, necessitating that we range check the characters.
@@ -57,13 +67,7 @@
* @param string a Java String to be wrapped
*/
public PyString(PyType subType, String string) {
- super(subType);
- if (string == null) {
- throw new IllegalArgumentException("Cannot create PyString from null");
- } else if (!isBytes(string)) {
- throw new IllegalArgumentException("Cannot create PyString with non-byte value");
- }
- this.string = string;
+ this(subType, string, false);
}
public PyString(String string) {
@@ -597,6 +601,9 @@
@Override
public PyObject __eq__(PyObject other) {
+ if (other instanceof PyShadowString) {
+ return other.__eq__(this);
+ }
return str___eq__(other);
}
@@ -894,7 +901,7 @@
* @return coerced value
* @throws PyException if the coercion fails
*/
- private static String asUTF16StringOrError(PyObject obj) {
+ protected static String asUTF16StringOrError(PyObject obj) {
// PyUnicode accepted here. Care required in the client if obj is not basic plane.
String ret = asUTF16StringOrNull(obj);
if (ret != null) {
diff --git a/src/org/python/core/PySystemState.java b/src/org/python/core/PySystemState.java
--- a/src/org/python/core/PySystemState.java
+++ b/src/org/python/core/PySystemState.java
@@ -134,7 +134,7 @@
public PyList warnoptions = new PyList();
public PyObject builtins;
- private static PyObject defaultPlatform = new PyString("java");
+ private static PyObject defaultPlatform = new PyShadowString("java", getNativePlatform());
public PyObject platform = defaultPlatform;
public PyList meta_path;
@@ -787,7 +787,31 @@
if (version.equals("12")) {
version = "1.2";
}
- defaultPlatform = new PyString("java" + version);
+ defaultPlatform = new PyShadowString("java" + version, getNativePlatform());
+ }
+
+ /**
+ * Emulates CPython's way to name sys.platform.
+ */
+ public static String getNativePlatform() {
+ /* Works according to this table:
+ System Value
+ --------------------------
+ Linux (2.x and 3.x) linux2
+ Windows win32
+ Windows/Cygwin cygwin
+ Mac OS X darwin
+ OS/2 os2
+ OS/2 EMX os2emx
+ RiscOS riscos
+ AtheOS atheos
+ */
+ String osname = System.getProperty("os.name");
+ if (osname.equals("Linux")) return "linux2";
+ if (osname.equals("Mac OS X")) return "darwin";
+ if (osname.toLowerCase().contains("cygwin")) return "cygwin";
+ if (osname.startsWith("Windows")) return "win32";
+ return osname.replaceAll("[\\s/]", "").toLowerCase();
}
private static void initRegistry(Properties preProperties, Properties postProperties,
--
Repository URL: https://hg.python.org/jython
More information about the Jython-checkins
mailing list