Skip to content

Commit

Permalink
Add configuration for setting program name.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Steffensmeier committed Nov 24, 2024
1 parent 1873107 commit e3e6552
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/main/c/Include/pyembed.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ typedef struct __JepThread JepThread;


void pyembed_preinit(JNIEnv*, jint, jint, jint, jint, jint, jint, jint,
jstring);
jstring, jstring);
void pyembed_startup(JNIEnv*, jobjectArray);
void pyembed_shutdown(JavaVM*);
void pyembed_shared_import(JNIEnv*, jstring);
Expand Down
5 changes: 3 additions & 2 deletions src/main/c/Jep/maininterpreter.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ JNIEXPORT void JNICALL Java_jep_MainInterpreter_setInitParams
jint optimizeFlag,
jint dontWriteBytecodeFlag,
jint hashRandomizationFlag,
jstring pythonHome)
jstring pythonHome,
jstring programName)
{
pyembed_preinit(env, noSiteFlag, noUserSiteDirectory, ignoreEnvironmentFlag,
verboseFlag, optimizeFlag, dontWriteBytecodeFlag,
hashRandomizationFlag, pythonHome);
hashRandomizationFlag, pythonHome, programName);
}

/*
Expand Down
17 changes: 13 additions & 4 deletions src/main/c/Jep/pyembed.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ void pyembed_preinit(JNIEnv *env,
jint optimizeFlag,
jint dontWriteBytecodeFlag,
jint hashRandomizationFlag,
jstring pythonHome)
jstring pythonHome,
jstring programName)
{
if (noSiteFlag >= 0) {
Py_NoSiteFlag = noSiteFlag;
Expand Down Expand Up @@ -270,6 +271,14 @@ void pyembed_preinit(JNIEnv *env,
// Python documentation says that the string should not be changed for
// the duration of the program so it can never be freed.
}
if (programName) {
const char* nameAsUTF = (*env)->GetStringUTFChars(env, programName, NULL);
wchar_t* nameForPython = Py_DecodeLocale(nameAsUTF, NULL);
(*env)->ReleaseStringUTFChars(env, programName, nameAsUTF);
Py_SetProgramName(nameForPython);
// Python documentation says that the string should not be changed for
// the duration of the program so it can never be freed.
}
}

/*
Expand Down Expand Up @@ -561,7 +570,7 @@ void pyembed_shared_import(JNIEnv *env, jstring module)
intptr_t pyembed_thread_init(JNIEnv *env, jobject cl, jobject caller,
jboolean hasSharedModules, jboolean usesubinterpreter,
jboolean isolated, jint useMainObmalloc, jint allowFork,
jint allowExec, jint allowThreads, jint allowDaemonThreads,
jint allowExec, jint allowThreads, jint allowDaemonThreads,
jint checkMultiInterpExtensions, jint ownGIL)
{
JepThread *jepThread;
Expand All @@ -587,8 +596,8 @@ intptr_t pyembed_thread_init(JNIEnv *env, jobject cl, jobject caller,
PyEval_AcquireThread(mainThreadState);

#if PY_MAJOR_VERSION > 3 || PY_MINOR_VERSION >= 12
PyInterpreterConfig config;
if (isolated) {
PyInterpreterConfig config;
if (isolated) {
config = (PyInterpreterConfig) _PyInterpreterConfig_INIT;
} else {
config = (PyInterpreterConfig) _PyInterpreterConfig_LEGACY_INIT;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/jep/MainInterpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ protected void initialize() throws Error {
setInitParams(pyConfig.noSiteFlag, pyConfig.noUserSiteDirectory,
pyConfig.ignoreEnvironmentFlag, pyConfig.verboseFlag,
pyConfig.optimizeFlag, pyConfig.dontWriteBytecodeFlag,
pyConfig.hashRandomizationFlag, pyConfig.pythonHome);
pyConfig.hashRandomizationFlag, pyConfig.pythonHome, pyConfig.programName);
}

thread = new Thread("JepMainInterpreter") {
Expand Down Expand Up @@ -293,7 +293,7 @@ public static void setJepLibraryPath(String path) throws IllegalStateException {
private static native void setInitParams(int noSiteFlag,
int noUserSiteDiretory, int ignoreEnvironmentFlag, int verboseFlag,
int optimizeFlag, int dontWriteBytecodeFlag,
int hashRandomizationFlag, String pythonHome);
int hashRandomizationFlag, String pythonHome, String programName);

private static native void initializePython(String[] mainInterpreterArgv);

Expand Down
36 changes: 25 additions & 11 deletions src/main/java/jep/PyConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
public class PyConfig {

/*
* -1 is used to indicate not set, in which case we will not set it in
* the native code and the setting will be Python's default. A value of 0
* or greater will cause the value to be set in the native code.
* -1 is used to indicate not set, in which case we will not set it in the
* native code and the setting will be Python's default. A value of 0 or
* greater will cause the value to be set in the native code.
*/

protected int noSiteFlag = -1;
Expand All @@ -57,6 +57,8 @@ public class PyConfig {

protected String pythonHome;

protected String programName;

/**
* Set the Py_NoSiteFlag variable on the python interpreter. This
* corresponds to the python "-S" flag and will prevent the "site" module
Expand Down Expand Up @@ -155,7 +157,7 @@ public PyConfig setHashRandomizationFlag(int hashRandomizationFlag) {
}

/**
* Set the home location on the python interpreter. THis is the location of
* Set the home location on the python interpreter. This is the location of
* the standard python libraries. This corresponds to the environment
* variable PYTHONHOME.
*
Expand All @@ -168,15 +170,27 @@ public PyConfig setPythonHome(String pythonHome) {
return this;
}

/**
* Set the Py_SetProgramName variable on the python interpreter. This is
* used to initialize executable and in early error messages during python
* initialization.
*
* @param programName
* the name of the executable python program.
* @return a reference to this PyConfig
*/
public PyConfig setProgramName(String programName) {
this.programName = programName;
return this;
}

@Override
public String toString() {
return "PyConfig [noSiteFlag=" + noSiteFlag + ", noUserSiteDirectory="
+ noUserSiteDirectory + ", ignoreEnvironmentFlag="
+ ignoreEnvironmentFlag + ", verboseFlag=" + verboseFlag
+ ", optimizeFlag=" + optimizeFlag + ", dontWriteBytecodeFlag="
+ dontWriteBytecodeFlag + ", hashRandomizationFlag="
+ hashRandomizationFlag + ", pythonHome=" + pythonHome + "]";
return "PyConfig [noSiteFlag=" + noSiteFlag + ", noUserSiteDirectory=" + noUserSiteDirectory
+ ", ignoreEnvironmentFlag=" + ignoreEnvironmentFlag + ", verboseFlag=" + verboseFlag
+ ", optimizeFlag=" + optimizeFlag + ", dontWriteBytecodeFlag=" + dontWriteBytecodeFlag
+ ", hashRandomizationFlag=" + hashRandomizationFlag + ", pythonHome=" + pythonHome + ", programName="
+ programName + "]";
}


}

0 comments on commit e3e6552

Please sign in to comment.