Class PyShadowString

All Implemented Interfaces:
Serializable, CharSequence, BufferProtocol
Direct Known Subclasses:
PyShadowStringDerived

public class PyShadowString extends PyString
This class provides a string that sometimes seems to change value, as far as equality tests and startswith are concerned. This solves a problem that Jython users sometimes experience with libraries that are sensitive to platform.

A library may test for a particular platform in order to adjust to local file name conventions or to decide which operating system commands are available. In Jython, os.name and sys.platform indicate that Java is the platform, which is necessary information in some parts of the standard library, but other libraries assuming CPython then draw incorrect conclusions.

With hindsight, a better choice could be made, where sys.platform etc. indicated Windows or Posix, and something else indicates the implementation. A change in Jython 2 would cause more problems than it solved, but we expect Jython 3 to work that way. In the Python Standard Library, the Jython project can make all necessary changes. It can't do anything about third-party libraries. But it would be a big help if users could cause sys.platform or os.name to appear to have the OS-dependent value as far as those troublesome libraries were concerned.

This is what this class achieves. os.name and sys.platform regular strings for most purposes, but each has a "shadow" value that is used in contexts the user may specify.

See Also:
  • Field Details

    • TYPE

      public static final PyType TYPE
  • Constructor Details

    • PyShadowString

      public PyShadowString()
      Empty string (not very useful but needed for technical reasons).
    • PyShadowString

      public PyShadowString(String primary, String shadow)
      Construct an instance specifying primary and shadow values.
    • PyShadowString

      @Deprecated public PyShadowString(PyObject primary, String shadow)
      Deprecated.
      use the constructor with strings instead.
      Construct an instance specifying primary and shadow values (bytes object expected for primary). This somewhat uncanonical constructor was removed in Jython 2.7.2. The deprecated version is kept for compatibility with JyNI 2.7-alpha5.
    • PyShadowString

      public PyShadowString(PyType subtype, PyObject primary, PyObject shadow)
  • Method Details

    • getshadow

      public PyString getshadow()
      Get the shadow value.
    • shadowstr_getshadow

      public final PyString shadowstr_getshadow()
    • addTarget

      public void addTarget(String className, String methodName)
      Specify a context (class, method) in which the shadow string is allowed to match.
      Parameters:
      className - class name to match or null to match anything.
      methodName - method name to match or null to match anything.
    • shadowstr_addtarget

      public final void shadowstr_addtarget(PyObject classname, PyObject methodname)
    • getTargets

      public PyList getTargets()
      Return a list of the tuples specifying the contexts in which the shadow value will be consulted during matching.
    • shadowstr_gettargets

      public final PyObject shadowstr_gettargets()
    • __eq__

      public PyObject __eq__(PyObject other)
      Compare this PyShadowString with another PyObject for equality. A PyShadowString is equal to the other object if its primary value is equal to it, or if its shadow value is equal to the other object and the test is made in one of its target contexts. (Two PyShadowString are equal if the primary values are equal, the primary of one matches the shadow of the other in the shadow's context, or their shadows match and both are in context.
      Overrides:
      __eq__ in class PyString
      Parameters:
      other - to compare
      Returns:
      PyBoolean result (or null if not implemented)
    • __getslice__

      public PyObject __getslice__(PyObject start, PyObject stop, PyObject step)
      Overrides:
      __getslice__ in class PySequence
    • startswith

      public boolean startswith(PyObject prefix)
      Description copied from class: PyString
      Equivalent to the Python str.startswith method testing whether a string starts with a specified prefix. prefix can also be a tuple of prefixes to look for.
      Overrides:
      startswith in class PyString
      Parameters:
      prefix - string to check for (or a PyTuple of them).
      Returns:
      true if this string slice starts with a specified prefix, otherwise false.
    • startswith

      public boolean startswith(PyObject prefix, PyObject start)
      Description copied from class: PyString
      Equivalent to the Python str.startswith method, testing whether a string starts with a specified prefix, where a sub-range is specified by [start:]. start is interpreted as in slice notation, with null or Py.None representing "missing". prefix can also be a tuple of prefixes to look for.
      Overrides:
      startswith in class PyString
      Parameters:
      prefix - string to check for (or a PyTuple of them).
      start - start of slice.
      Returns:
      true if this string slice starts with a specified prefix, otherwise false.
    • startswith

      public boolean startswith(PyObject prefix, PyObject start, PyObject end)
      Description copied from class: PyString
      Equivalent to the Python str.startswith method, testing whether a string starts with a specified prefix, where a sub-range is specified by [start:end]. Arguments start and end are interpreted as in slice notation, with null or Py.None representing "missing". prefix can also be a tuple of prefixes to look for.
      Overrides:
      startswith in class PyString
      Parameters:
      prefix - string to check for (or a PyTuple of them).
      start - start of slice.
      end - end of slice.
      Returns:
      true if this string slice starts with a specified prefix, otherwise false.
    • __repr__

      public PyString __repr__()
      Description copied from class: PyObject
      Equivalent to the standard Python __repr__ method. Each sub-class of PyObject is likely to re-define this method to provide for its own reproduction.
      Overrides:
      __repr__ in class PyString