FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-32604: Expose the subinterpreters C-API in a "private" stdlib module. by ericsnowcurrently · Pull Request #1748 · python/cpython · GitHub

Repository navigation

bpo-32604: Expose the subinterpreters C-API in a "private" stdlib module. - #1748

Merged
ericsnowcurrently merged 78 commits into
python:masterfrom
ericsnowcurrently:low-level-interpreters-module
Jan 30, 2018
Merged

ericsnowcurrently merged 78 commits into
python:masterfrom
ericsnowcurrently:low-level-interpreters-module

Conversation

ericsnowcurrently commented May 23, 2017 •
edited
Loading

Copy link
Copy Markdown
Member

ericsnowcurrently force-pushed the low-level-interpreters-module branch from e0611a8 to bee1baa Compare May 23, 2017 04:38
ericsnowcurrently changed the title Expose the subinterpreters C-API in a "private" stdlib module. bpo-30439: Expose the subinterpreters C-API in a "private" stdlib module. May 23, 2017
ericsnowcurrently force-pushed the low-level-interpreters-module branch from bee1baa to 060a720 Compare May 23, 2017 23:42
Comment thread Doc/library/_interpreters.rst Outdated
@@ -0,0 +1,69 @@
:mod:`_interpreters` --- Low-level interpreters API

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

There should be a warning about the module being private, and/or the API being subject to change without notice, IMHO.

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Good point. For the most part I was following the precedent of the _thread module. However, in this case the module is relatively unstable still and there is no "interpreters" module yet. So a warning makes sense.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Given the not-yet-approved PEP, I think it would be a good idea to use a name that makes it clear that this really is intended as an API for the CPython test suite, rather than for general use.

That would mean going with something like _xxsubinterpreters for now.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Note: one consequence of that is that redistributors that remove the test suite would likely remove this module as well, and I think that's a desirable outcome: we want this as a learning tool for the further development of PEP 554 as a proposal for 3.8 (and to find and fix bugs in the subinterpreter support in 3.7), not as an inadvertent commitment to a particular API.

Comment thread Lib/test/test__interpreters.py Outdated
import threading
import _interpreters
def f():
_interpreters.run_string(id, dedent('''

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I don't understand where the id variable is coming from... is it the built-in id function?

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Good catch. That's a typo that I didn't notice because the resulting exception in the thread does not cause the process to fail.

FYI, adding the missing "id = _interpreters.create()" line causes the test to fail (correctly); the subinterpreter didn't get cleaned up before we reached Py_Finalize(), resulting in Py_FatalError. I'll sort that out separately.

ericsnowcurrently force-pushed the low-level-interpreters-module branch from 060a720 to 2fd0e64 Compare August 22, 2017 20:01
ericsnowcurrently force-pushed the low-level-interpreters-module branch 2 times, most recently from 114bda4 to 52ca360 Compare September 7, 2017 17:58
ericsnowcurrently force-pushed the low-level-interpreters-module branch from 52ca360 to 9991d75 Compare November 27, 2017 23:24
ericsnowcurrently force-pushed the low-level-interpreters-module branch from 9991d75 to e9d9b04 Compare December 5, 2017 01:29
ericsnowcurrently force-pushed the low-level-interpreters-module branch from 81de3c8 to bfc025f Compare December 12, 2017 05:12
ericsnowcurrently force-pushed the low-level-interpreters-module branch 2 times, most recently from e05d02e to 396e294 Compare December 23, 2017 00:30
ericsnowcurrently force-pushed the low-level-interpreters-module branch 3 times, most recently from 5266080 to c86647a Compare January 13, 2018 00:49
ericsnowcurrently force-pushed the low-level-interpreters-module branch 2 times, most recently from ff4ae72 to 0981dbf Compare January 20, 2018 01:44
ericsnowcurrently changed the title bpo-30439: Expose the subinterpreters C-API in a "private" stdlib module. bpo-32604: Expose the subinterpreters C-API in a "private" stdlib module. Jan 20, 2018
ericsnowcurrently force-pushed the low-level-interpreters-module branch from 0981dbf to 0be6314 Compare January 20, 2018 02:13
Comment thread Doc/library/_interpreters.rst Outdated

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Can we call the function run as per the PEP?

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Note that the PEP is all about the high-level API we want to expose to users. In contrast, run_string() is the low-level function focused strictly as an analog to PyRun_String*(). As we support other runnable things (e.g. code objects, functions), we will add corresponding low-level functions. The eventual high-level method (i.e. Interpreter.run() will call the appropriate low-level function.

Also, I'm removing the docs for now.

Comment thread Include/internal/pystate.h Outdated

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

The actual layout of the struct should go into an "internal" header file:

"Include/pystate.h":

typedef struct _xid _PyCrossInterpreterData;

"Include/internal/pystate.h":

struct _xid {
    void *data;
    // etc
}

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I haven't touched Include/pystate.h. If you are suggesting that I move the typedef (but not the layout) there, I don't see any benefit to that for 3.7. This is not a public API for now.

Comment thread Modules/_interpretersmodule.c Outdated

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

You don't need to check for NULL here, PyThreadState_Get aborts if it can't lookup the state.

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

fixed

Comment thread Modules/_interpretersmodule.c Outdated

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

per PEP 7 please use curly braces always :)

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

fixed

Comment thread Modules/_interpretersmodule.c Outdated

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Check if shared is NULL

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

fixed

Comment thread Modules/_interpretersmodule.c Outdated

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Redundant? And need to free err?

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I'd meant to return err on the next line rather than NULL. :)

ericsnowcurrently force-pushed the low-level-interpreters-module branch from 8ae8507 to 2ebc68c Compare January 27, 2018 19:15

Copy link
Copy Markdown
Member Author

FYI, I've temporarily removed the Windows support to see if anything else breaks. I'll add it back in soon. I do not plan on merging this without it.

ericsnowcurrently force-pushed the low-level-interpreters-module branch 2 times, most recently from ebbf591 to 9718b91 Compare January 27, 2018 21:07
ericsnowcurrently force-pushed the low-level-interpreters-module branch 2 times, most recently from edec009 to 092fccc Compare January 27, 2018 22:50
ericsnowcurrently force-pushed the low-level-interpreters-module branch from 092fccc to b07e928 Compare January 27, 2018 23:19

Copy link
Copy Markdown
Member Author

@zooba I suspect my problem is with Py_BUILD_CORE. When I don't set it I don't get the symbols out of Include/internal. When I do set it I end up missing stuff from python37.lib at link time. I tried adding a "Link" directive right below where I set Py_BUILD_CORE, but that did not help. :/ Any ideas on what I need to do?

Copy link
Copy Markdown
Member Author

@python/windows-team Any of you have ideas on how I can get this PR to link on Windows?

Copy link
Copy Markdown
Member Author

In the interest of the feature freeze I'm going to land the PR without Windows support. :/ The risk for beta1 is low since the PR does not have any OS-specific code and the new module is only for use in the test suite. I'll work on getting Windows support done ASAP.

//
// We use the ID rather than the PyInterpreterState to avoid issues
// with deleted interpreters.
int64_t interp;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Something we'll need to keep an eye on here is not re-using interpreter IDs within the life of a process (otherwise we'll still run into issues with deleted interpreters).

Not that I've ever worked on systems where we introduced bugs by adding a freelist for objects where we assumed IDs would never be re-used or anything ;)

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

We picked int64_t so that it was both really large and would overflow to negative. When the next ID is negative PyInterpreterState_New() raises RuntimError.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

7 participants


Back | FazBrowse Home | New Git URL