Skip to content

Update PyConfig to align with PEP 587.#586

Merged
bsteffensmeier merged 5 commits into
ninia:dev_4.3from
bsteffensmeier:pyconfig-pep-587
Jan 23, 2025
Merged

Update PyConfig to align with PEP 587.#586
bsteffensmeier merged 5 commits into
ninia:dev_4.3from
bsteffensmeier:pyconfig-pep-587

Conversation

@bsteffensmeier

Copy link
Copy Markdown
Member

The PyConfig Java class is currently used to set Python Global Configuration Variables but this part of the Python c-api has been deprecated since Python 3.12 and is scheduled for removal in 3.14. PEP-587 describes a new mechanism for handling Python Initialization Configuration. This change removes the use of the deprecated variables and switches things over to the new way of doing things. This change should be fully backwards compatible for anyone using Jep, however many public methods in PyConfig are now deprecated.

Now that the Python c-api includes a PyConfig structure I changed our PyConfig class in Java to have fields and setters corresponding to the c-api structure. Making these match up keeps the native code simple and I think this will be easier to document and maintain moving forward but it makes this change confusing because several of the fields were inverted in the transition. As an example there was previously a configuration variable called DontWriteBytecode and it has been replaced with a field called WriteBytecode so if previously DontWriteByteCode was true instead now WriteByteCode should be false. In the java PyConfig class this means the existing setDontWriteBytecodeFlag() method is deprecated and a new setWriteBytecode() has been added.

Comment thread src/main/c/Include/pyembed.h Outdated
void pyembed_preinit(JNIEnv*, jint, jint, jint, jint, jint, jint, jint,
jstring, jstring);
void pyembed_startup(JNIEnv*, jobjectArray);
void pyembed_startup(JNIEnv*, jobjectArray, jint, jint,jstring, jint, jstring,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to add a space between jint,jstring.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

* if called after the Python interpreter is initialized
*
* @since 3.7
* @deprecated Use {@link PyConfig#setArgv(String...)} instead.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How confident are you that this new alternative will have the same behavior?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the primary goal of setting sys.argv for shared modules I am confident that the TestSharedArgv classes verifying the behavior is the same. I am not quite as confident that there are no other side effects. Both the old call to PySys_SetArgvEx and the new call to PyConfig_SetBytesArgv have documented optional side affects to set the path or parse the command line for options however in both cases we are setting it up to not do anything extra so as far as I can tell they are doing the same thing.

Comment thread src/main/java/jep/PyConfig.java Outdated
* standard Python libraries.
*
* @param home
* the directory tpuse for Python home.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spelling "to use".

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Comment thread src/main/java/jep/PyConfig.java Outdated
@Deprecated
public PyConfig setNoSiteFlag(int noSiteFlag) {
this.noSiteFlag = noSiteFlag;
this.siteImport = (noSiteFlag == 0) ? 1 : 0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly stylistic choice, but I think it might be slightly cleaner if you had these deprecated methods call the new non-deprecated methods to set the values. Just a thought.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that is nicer, fixed.

@ndjensen ndjensen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants