Top Description Fields Constructors Methods
org.python.core

public Class PyShadowString

extends PyString
Class Inheritance
Known Direct Subclasses
org.python.core.PyShadowStringDerived
Annotations
@Untraversable
@ExposedType
name:shadowstr
base:PyString
isBaseType:true
Imports
java.util.regex.Pattern, org.python.expose.ExposedMethod, .ExposedNew, .ExposedType, .MethodType

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.

Field Summary

Modifier and TypeField and Description
protected PyString
shadow

The shadow string is additionally used for some comparisons, especially for __eq__.

protected PyList
targets

Contexts (expressed as a PyTuple of class name and method name) where shadow is allowed to match as well as the primary value.

public static final PyType
Inherited from org.python.core.PyString:
internedstring

Constructor Summary

AccessConstructor and Description
public
PyShadowString()

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

public
PyShadowString(String primary, String shadow)

Construct an instance specifying primary and shadow values.

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).

private
PyShadowString(PyObject primary, PyObject shadow)

Construct an instance specifying primary and shadow values (bytes objects expected).

private
PyShadowString(PyObject primary, PyObject shadow, PyList targets)

public
PyShadowString(PyType subtype, PyObject primary, PyObject shadow)

private
PyShadowString(PyType subtype, PyObject primary, PyObject shadow, PyList targets)

Method Summary

Modifier and TypeMethod and Description
public PyObject

Returns:

PyBoolean result (or null if not implemented)
__eq__
(PyObject
to compare
other
)

Overrides org.python.core.PyString.__eq__.

Compare this PyShadowString with another PyObject for equality.

public PyObject
public PyString
__repr__()

Overrides org.python.core.PyString.__repr__.

Equivalent to the standard Python __repr__ method.

public void
addTarget(String
class name to match or null to match anything.
className
,
String
method name to match or null to match anything.
methodName
)

Specify a context (class, method) in which the shadow string is allowed to match.

private static PyObject
asUnicode(PyObject o)

Prepare argument for addtarget, allowing string-like values or None.

private static Pattern
getPattern(PyObject o)

Convert a PyObject (specifying a regex) to a compiled pattern or null.

public PyString
getshadow()

Get the shadow value.

public PyList
getTargets()

Return a list of the tuples specifying the contexts in which the shadow value will be consulted during matching.

private boolean

Returns:

true iff in one of the named contexts
isTarget
()

Test whether the current code is executing in of one of the target contexts, by searching up the stack for a class and method pait that match.

pack-priv final PyObject
pack-priv final PyString
public final void
shadowstr_addtarget(PyObject classname, PyObject methodname)

public final PyString
public final PyObject
pack-priv static PyObject
shadowstr_new(PyNewWrapper new_, boolean init, PyType subtype, PyObject[] args, String[] keywords)

pack-priv final boolean
shadowstr_startswith(PyObject prefix, PyObject startObj, PyObject endObj)

public boolean
startswith(PyObject
string to check for (or a PyTuple of them).
prefix
)

Overrides org.python.core.PyString.startswith.

Equivalent to the Python str.startswith method testing whether a string starts with a specified prefix.

public boolean
startswith(PyObject
string to check for (or a PyTuple of them).
prefix
,
PyObject
start of slice.
start
)

Overrides org.python.core.PyString.startswith.

Equivalent to the Python str.startswith method, testing whether a string starts with a specified prefix, where a sub-range is specified by [start:].

public boolean
startswith(PyObject
string to check for (or a PyTuple of them).
prefix
,
PyObject
start of slice.
start
,
PyObject
end of slice.
end
)

Overrides org.python.core.PyString.startswith.

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].

private static final PyObject
testEqual(PyString string, PyObject other)

Test for equality, used as a helper to shadowstr___eq__, dealing with the possibility that other is another PyShadowString.

Inherited from org.python.core.PyString:
__add____cmp____complex____contains____float____format____ge____getnewargs____gt____int____invert____le____len____long____lt____mod____mul____ne____neg____pos____rmul____str____tojava____unicode___count_find_findLeft_findRight_lstrip_lstrip_replace_rfind_rsplit_rstrip_rstrip_split_strip_stripasDoubleasIntasLongasNameasStringasStringasU16BytesOrErroratofatoiatoiatolatolbuildFormattedStringcapitalizecentercharAtcharsFitWidthcheckIndexcountcountcountcountcountcountcreateInstancecreateInstancedecodedecodedecodedecode_UnicodeEscapeencodeencodeencodeencode_UnicodeEscapeencode_UnicodeEscapeendswithendswithendswithexpandtabsexpandtabsfindfindfindfindfindfindfromInternedfromSubstringgetBuffergetIntgetslicegetStringhashCodeindexindexindexindexindexindexinternedStringisalnumisalphaisBasicPlaneisdecimalisdigitislowerisnumericisspaceistitleisunicodeisupperjoinlengthljustljustlowerlstriplstriplstrippartitionprepareFormatterpygetrepeatreplacereplacerfindrfindrfindrfindrfindrfindrindexrindexrindexrindexrindexrindexrjustrpartitionrsplitrsplitrsplitrsplitrsplitrsplitfieldsrstriprstriprstripsplitsplitsplitsplitsplitsplitfieldssplitlinessplitlinesstr___add__str___cmp__str___contains__str___eq__str___format__str___ge__str___getitem__str___getnewargs__str___getslice__str___gt__str___hash__str___le__str___len__str___lt__str___mod__str___mul__str___ne__str___repr__str___rmul__str___str__str__formatter_field_name_splitstr__formatter_parserstr_capitalizestr_centerstr_countstr_decodestr_encodestr_endswithstr_expandtabsstr_findstr_formatstr_indexstr_isalnumstr_isalphastr_isdecimalstr_isdigitstr_islowerstr_isnumericstr_isspacestr_istitlestr_isunicodestr_isupperstr_joinstr_ljuststr_lowerstr_lstripstr_newstr_partitionstr_replacestr_rfindstr_rindexstr_rjuststr_rpartitionstr_rsplitstr_rstripstr_splitstr_splitlinesstr_startswithstr_stripstr_swapcasestr_titlestr_translatestr_upperstr_zfillstripstripstripsubSequencesubstringswapcasetitletoBytestoCodePointstoStringtranslatetranslatetranslatetranslatetranslateIndicesunicodeJoinunicodePartitionunicodeRpartitionunsupportedopMessageupperzfill

Field Detail

shadowback to summary
protected PyString shadow

The shadow string is additionally used for some comparisons, especially for __eq__. __eq__ will evaluate positive if the other string equals the primary value *or* the shadow. The shadow persists slicing (is sliced accordingly) and is taken into account by startswith.

targetsback to summary
protected PyList targets

Contexts (expressed as a PyTuple of class name and method name) where shadow is allowed to match as well as the primary value.

TYPEback to summary
public static final PyType TYPE

Hides org.python.core.PyString.TYPE.

Constructor Detail

PyShadowStringback to summary
public PyShadowString()

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

PyShadowStringback to summary
public PyShadowString(String primary, String shadow)

Construct an instance specifying primary and shadow values.

PyShadowStringback to summary
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.

Annotations
@Deprecated
PyShadowStringback to summary
private PyShadowString(PyObject primary, PyObject shadow)

Construct an instance specifying primary and shadow values (bytes objects expected).

PyShadowStringback to summary
private PyShadowString(PyObject primary, PyObject shadow, PyList targets)
PyShadowStringback to summary
public PyShadowString(PyType subtype, PyObject primary, PyObject shadow)
PyShadowStringback to summary
private PyShadowString(PyType subtype, PyObject primary, PyObject shadow, PyList targets)

Method Detail

__eq__back to summary
public PyObject __eq__(PyObject other)

Overrides org.python.core.PyString.__eq__.

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.

Parameters
other:PyObject

to compare

Returns:PyObject

PyBoolean result (or null if not implemented)

Annotations
@Override

__getslice__back to summary
public PyObject __getslice__(PyObject start, PyObject stop, PyObject step)

Overrides org.python.core.PySequence.__getslice__.

Annotations
@Override
__repr__back to summary
public PyString __repr__()

Overrides org.python.core.PyString.__repr__.

Doc from org.python.core.PyObject.__repr__.

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.

Annotations
@Override
addTargetback to summary
public void addTarget(String className, String methodName)

Specify a context (class, method) in which the shadow string is allowed to match.

Parameters
className:String

class name to match or null to match anything.

methodName:String

method name to match or null to match anything.

asUnicodeback to summary
private static PyObject asUnicode(PyObject o)

Prepare argument for addtarget, allowing string-like values or None.

getPatternback to summary
private static Pattern getPattern(PyObject o)

Convert a PyObject (specifying a regex) to a compiled pattern or null.

getshadowback to summary
public PyString getshadow()

Get the shadow value.

getTargetsback to summary
public PyList getTargets()

Return a list of the tuples specifying the contexts in which the shadow value will be consulted during matching.

isTargetback to summary
private boolean isTarget()

Test whether the current code is executing in of one of the target contexts, by searching up the stack for a class and method pait that match.

Returns:boolean

true iff in one of the named contexts

shadowstr___eq__back to summary
pack-priv final PyObject shadowstr___eq__(PyObject other)
Annotations
@ExposedMethod
type:BINARY
shadowstr___repr__back to summary
pack-priv final PyString shadowstr___repr__()
Annotations
@ExposedMethod
shadowstr_addtargetback to summary
public final void shadowstr_addtarget(PyObject classname, PyObject methodname)
Annotations
@ExposedMethod
defaults:null
shadowstr_getshadowback to summary
public final PyString shadowstr_getshadow()
Annotations
@ExposedMethod
shadowstr_gettargetsback to summary
public final PyObject shadowstr_gettargets()
Annotations
@ExposedMethod
shadowstr_newback to summary
pack-priv static PyObject shadowstr_new(PyNewWrapper new_, boolean init, PyType subtype, PyObject[] args, String[] keywords)
Annotations
@ExposedNew
shadowstr_startswithback to summary
pack-priv final boolean shadowstr_startswith(PyObject prefix, PyObject startObj, PyObject endObj)
Annotations
@ExposedMethod
defaults:null, null
startswithback to summary
public boolean startswith(PyObject prefix)

Overrides org.python.core.PyString.startswith.

Doc from org.python.core.PyString.startswith.

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.

Parameters
prefix:PyObject

string to check for (or a PyTuple of them).

Returns:boolean

true if this string slice starts with a specified prefix, otherwise false.

Annotations
@Override

startswithback to summary
public boolean startswith(PyObject prefix, PyObject start)

Overrides org.python.core.PyString.startswith.

Doc from org.python.core.PyString.startswith.

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.

Parameters
prefix:PyObject

string to check for (or a PyTuple of them).

start:PyObject

start of slice.

Returns:boolean

true if this string slice starts with a specified prefix, otherwise false.

Annotations
@Override

startswithback to summary
public boolean startswith(PyObject prefix, PyObject start, PyObject end)

Overrides org.python.core.PyString.startswith.

Doc from org.python.core.PyString.startswith.

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.

Parameters
prefix:PyObject

string to check for (or a PyTuple of them).

start:PyObject

start of slice.

end:PyObject

end of slice.

Returns:boolean

true if this string slice starts with a specified prefix, otherwise false.

Annotations
@Override

testEqualback to summary
private static final PyObject testEqual(PyString string, PyObject other)

Test for equality, used as a helper to shadowstr___eq__, dealing with the possibility that other is another PyShadowString.