Skip to content

Commit

Permalink
Removed PyShadowString since it is now included in Jython itself (as …
Browse files Browse the repository at this point in the history
…of 2.7.1). Switched to target-based mechanism for faking native os.name and sys.platform, see http://bugs.jython.org/issue2557. Improved injection of sys.dlopenflags; it is done now without creating a PythonInterpreter.
  • Loading branch information
Stewori committed Jun 10, 2017
1 parent 77125b2 commit 2057b0e
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 217 deletions.
48 changes: 44 additions & 4 deletions JyNI-Java/src/JyNI/JyNI.java
Expand Up @@ -72,6 +72,8 @@ public class JyNI {

public static final int RTLD_JyNI_DEFAULT = RTLD_LAZY | RTLD_GLOBAL;//RTLD_NOW;

public static final String DLOPENFLAGS_NAME = "dlopenflags".intern();

public static final int Py_LT = 0;
public static final int Py_LE = 1;
public static final int Py_EQ = 2;
Expand Down Expand Up @@ -389,11 +391,13 @@ public static int getDLVerbose()
public static int getDLOpenFlags()
{
try {
return ((PyInteger) Py.getThreadState().getSystemState().__getattr__("dlopenflags")).asInt();
return ((PyInteger) Py.getThreadState().getSystemState().__getattr__(
DLOPENFLAGS_NAME)).asInt();
} catch (Exception e1)
{
try {
return ((PyInteger) Py.getSystemState().__getattr__("dlopenflags")).asInt();
return ((PyInteger) Py.getSystemState().__getattr__(
DLOPENFLAGS_NAME)).asInt();
} catch (Exception e2)
{
return RTLD_JyNI_DEFAULT;
Expand All @@ -404,11 +408,47 @@ public static int getDLOpenFlags()
public static void setDLOpenFlags(int n)
{
try {
Py.getThreadState().getSystemState().__setattr__("dlopenflags", new PyInteger(n));
Py.getThreadState().getSystemState().__setattr__(
DLOPENFLAGS_NAME, Py.newInteger(n));
} catch (Exception e1)
{
try {
Py.getSystemState().__setattr__(DLOPENFLAGS_NAME, Py.newInteger(n));
} catch (Exception e2)
{
return;
}
}
}

public static PyObject sys_getdlopenflags(PyObject[] args, String[] kws)
{
try {
return Py.getThreadState().getSystemState().__getattr__(DLOPENFLAGS_NAME);
} catch (Exception e1)
{
try {
return Py.getSystemState().__getattr__(DLOPENFLAGS_NAME);
} catch (Exception e2)
{
return Py.newInteger(RTLD_JyNI_DEFAULT);
}
}
}

public static void sys_setdlopenflags(PyObject[] args, String[] kws)
{
System.out.println(408+" "+args.length+" "+kws.length);
if (args.length != 1) throw Py.makeException(Py.TypeError,
Py.newString("setdlopenflags() takes exactly 1 argument ("+args.length+" given)"));
if (!(args[0] instanceof PyInteger)) throw Py.makeException(Py.TypeError,
Py.newString("integer argument expected, got "+args[0].getType().getName()));
try {
Py.getThreadState().getSystemState().__setattr__(DLOPENFLAGS_NAME, args[0]);
} catch (Exception e1)
{
try {
Py.getSystemState().__setattr__("dlopenflags", new PyInteger(n));
Py.getSystemState().__setattr__(DLOPENFLAGS_NAME, args[0]);
} catch (Exception e2)
{
return;
Expand Down
14 changes: 1 addition & 13 deletions JyNI-Java/src/JyNI/JyNIImporter.java
Expand Up @@ -201,19 +201,7 @@ public PyObject find_module(String name, PyObject path) {
}

public PyObject load_module(String name) {
// System.out.println("JyNI load... "+name);
// This stuff should move to JyNIInitializer, but there it currently
// breaks sysconfig.py. Is fixed in Jython 2.7.1 final.
PySystemState sysState = Py.getSystemState();
if (!(sysState.getPlatform() instanceof PyShadowString)) {
sysState.setPlatform(new PyShadowString(sysState.getPlatform(),
JyNI.getNativePlatform()));
PyModule osModule = (PyModule) imp.importName("os", true);
String _name = osModule.__getattr__("_name".intern()).toString();
String nameval = "name".intern();
PyObject osname = osModule.__getattr__(nameval);
osModule.__setattr__(nameval, new PyShadowString(osname, _name));
}
//System.out.println("JyNI load... "+name);

//ToDo:
//Maybe check via
Expand Down
62 changes: 18 additions & 44 deletions JyNI-Java/src/JyNI/JyNIInitializer.java
Expand Up @@ -37,14 +37,13 @@
import java.io.File;
import java.util.Properties;
import org.python.core.JythonInitializer;
import org.python.core.Py;
import org.python.core.PyModule;
import org.python.core.PyObject;
import org.python.core.PySystemState;
import org.python.core.Py;
import org.python.core.imp;
import org.python.core.PyModule;
import org.python.core.PyShadowString;
import org.python.core.adapter.ExtensiblePyObjectAdapter;
import org.python.core.finalization.FinalizeTrigger;
import org.python.util.PythonInterpreter;
import org.python.modules.gc;
import org.python.modules._weakref.GlobalRef;

Expand Down Expand Up @@ -82,56 +81,31 @@ public void initialize(Properties preProperties, Properties postProperties, Stri

initState.path_hooks.append(importer);

// Currently this monkeypatching is done in JyNIImporter on first module import,
// because doing it here during initialization - although the more complete, better
// approach - currently breaks sysconfig.py.
// initState.setPlatform(new PyJavaPlatformString(initState.getPlatform()));
// PyModule osModule = (PyModule) imp.importName("os", true);
// osModule.__setattr__("name".intern(), new PyOSNameString());

// Currently hosted in JyNIImporter until sysconfig.py is adjusted in Jython 2.7.1:
// initState.setPlatform(new PyShadowString(initState.getPlatform(),
// JyNI.getNativePlatform()));
// PyModule osModule = (PyModule) imp.importName("os", true);
// String _name = osModule.__getattr__("_name".intern()).toString();
// String nameval = "name".intern();
// PyObject osname = osModule.__getattr__(nameval);
// osModule.__setattr__(nameval, new PyShadowString(osname, _name));
PyModule osModule = (PyModule) imp.importName("os", true);
PyShadowString os_name = ((PyShadowString) osModule.__getattr__("name".intern()));

// We make sure that JyNI.jar is not only on classpath, but also on Jython-path:
String[] cp = System.getProperty("java.class.path").split(File.pathSeparator);
for (int i = 0; i < cp.length; ++i) {
// System.out.println(cp[i]);
if (cp[i].endsWith("JyNI.jar")) {
initState.path.add(0, cp[i]);

//initState.path.add(0, cp[i]+"/lib-tk");
}
}
// initializeState(initState);
// }
//
// public void initializeState(PySystemState state) {
// state.path_hooks.append(new JyNIImporter());
// initializeBasic();
// }
//
// private void initializeBasic() {
//add the JyNI-Importer to list of import hooks:
//if (initialized) return;

PythonInterpreter pint = new PythonInterpreter();

// We add some targets for receiving native os.name and sys.platform values:
// (this list will need constant maintenance as extension support grows)
os_name.addTarget("ctypes.*", null);
os_name.addTarget("OpenGL.*", null);
// Since we do it right after initialization, this will affect PySystemState.defaultPlatform
// and thus all subsequently created PySystemStates too.
// That's why we needn't do it in PySystemStateJyNI.
((PyShadowString) initState.platform).addTarget("OpenGL.*", null);

//add support for sys.setdlopenflags and sys.getdlopenflags as available in common CPython:
pint.exec("import sys");
//pint.exec("import JyNI.JyNI");
pint.exec("sys.dlopenflags = "+JyNI.RTLD_JyNI_DEFAULT);
//pint.exec("sys.setdlopenflags = JyNI.JyNI.setDLOpenFlags");
//pint.exec("sys.getdlopenflags = JyNI.JyNI.getDLOpenFlags");
//pint.exec("sys.setdlopenflags = lambda n: (sys.dlopenflags = n)");
pint.exec("def setdlopenflags(n): sys.dlopenflags = n");
pint.exec("sys.setdlopenflags = setdlopenflags");
pint.exec("sys.getdlopenflags = lambda: sys.dlopenflags");
pint.cleanup();
initState.__setattr__(JyNI.DLOPENFLAGS_NAME, Py.newInteger(JyNI.RTLD_JyNI_DEFAULT));
initState.__setattr__("setdlopenflags".intern(), Py.newJavaFunc(JyNI.class, "sys_setdlopenflags"));
initState.__setattr__("getdlopenflags".intern(), Py.newJavaFunc(JyNI.class, "sys_getdlopenflags"));

//Set up Jython hooks for JyNI:
FinalizeTrigger.factory = new JyNIFinalizeTriggerFactory();
Expand Down
125 changes: 0 additions & 125 deletions JyNI-Java/src/JyNI/PyShadowString.java

This file was deleted.

32 changes: 7 additions & 25 deletions JyNI-Java/src/JyNI/PySystemStateJyNI.java
Expand Up @@ -30,8 +30,8 @@

package JyNI;

import org.python.core.Py;
import org.python.core.PySystemState;
import org.python.util.PythonInterpreter;

/**
* A variant of PySystemState that supports the sys-functions
Expand All @@ -46,37 +46,19 @@
*
* PythonInterpreter pint = new PythonInterpreter(new PySystemStateJyNI());
*
* This is equivalent to calling
*
* PythonInterpreter pint = new PythonInterpreter(new PySystemState());
* pint.exec("import sys");
* pint.exec("import JyNI.JyNI");
* pint.exec("sys.dlopenflags = JyNI.JyNI.RTLD_NOW");
* pint.exec("def setdlopenflags(n): sys.dlopenflags = n");
* pint.exec("sys.setdlopenflags = setdlopenflags");
* pint.exec("sys.getdlopenflags = lambda: sys.dlopenflags");
*
* @author Stefan Richthofer
*
*/

public class PySystemStateJyNI extends PySystemState {
//protected int dlopenflags = JyNI.RTLD_NOW;
//public int getdlopenflags() {return dlopenflags;}
//public void setdlopenflags(int n) {this.dlopenflags = n;}

public PySystemStateJyNI()
{
super();
PythonInterpreter pint = new PythonInterpreter(this);
pint.exec("import sys");
pint.exec("import JyNI.JyNI");
pint.exec("sys.dlopenflags = JyNI.JyNI.RTLD_JyNI_DEFAULT");
//pint.exec("sys.setdlopenflags = JyNI.JyNI.setDLOpenFlags");
//pint.exec("sys.getdlopenflags = JyNI.JyNI.getDLOpenFlags");
//pint.exec("sys.setdlopenflags = lambda n: (sys.dlopenflags = n)");
pint.exec("def setdlopenflags(n): sys.dlopenflags = n");
pint.exec("sys.setdlopenflags = setdlopenflags");
pint.exec("sys.getdlopenflags = lambda: sys.dlopenflags");
pint.cleanup();
// See comment in JyNIInitializer for why we needn't put stuff here like
// ((PyShadowString) initState.platform).addTarget("OpenGL.*", null);
__setattr__(JyNI.DLOPENFLAGS_NAME, Py.newInteger(JyNI.RTLD_JyNI_DEFAULT));
__setattr__("setdlopenflags".intern(), Py.newJavaFunc(JyNI.class, "sys_setdlopenflags"));
__setattr__("getdlopenflags".intern(), Py.newJavaFunc(JyNI.class, "sys_getdlopenflags"));
}
}
6 changes: 3 additions & 3 deletions JyNI-Lib/ctypes/__init__.py
Expand Up @@ -183,7 +183,7 @@ class WinFunctionType(_CFuncPtr):
if WINFUNCTYPE.__doc__:
WINFUNCTYPE.__doc__ = CFUNCTYPE.__doc__.replace("CFUNCTYPE", "WINFUNCTYPE")

elif _os.name == "posix": #isPosix:
elif _os.name == "posix":
from _ctypes import dlopen as _dlopen

from _ctypes import sizeof, byref, addressof, alignment, resize
Expand Down Expand Up @@ -417,7 +417,7 @@ def __repr__(self):
id(self) & (_sys.maxint*2 + 1))

def __getattr__(self, name):
#print "CDLL getattr: "+str(name)
#print "CDLL getattr: "+str(name)
if name.startswith('__') and name.endswith('__'):
raise AttributeError(name)
func = self.__getitem__(name)
Expand Down Expand Up @@ -480,7 +480,7 @@ def __init__(self, dlltype):
self._dlltype = dlltype

def __getattr__(self, name):
#print "LibraryLoader getattr: "+str(name)
#print "LibraryLoader getattr: "+str(name)
if name[0] == '_':
raise AttributeError(name)
dll = self._dlltype(name)
Expand Down

0 comments on commit 2057b0e

Please sign in to comment.