classification
Title: PEP 3121, 384 Refactoring applied to random module
Type: resource usage Stage: resolved
Components: Extension Modules Versions: Python 3.4
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Py_Finalize() doesn't clear all Python objects at exit
View: 1635741
Assigned To: loewis Nosy List: Robin.Schreiber, loewis, mark.dickinson, rhettinger, vstinner
Priority: normal Keywords: pep3121

Created on 2012-08-15 14:10 by Robin.Schreiber, last changed 2020-11-19 14:01 by vstinner. This issue is now closed.

Files
File name Uploaded Description Edit
_random_pep3121-384_v0.patch Robin.Schreiber, 2012-08-15 14:10
Messages (9)
msg168297 - (view) Author: Robin Schreiber (Robin.Schreiber) * (Python triager) Date: 2012-08-15 14:10
Changes proposed in PEP3121 and PEP384 have now been applied to the lsprof module!
msg168299 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2012-08-15 14:25
Martin, does this patch match your intent with PEP3121 and PEP384?
msg168437 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2012-08-17 08:24
Sure; I've mentored Robin throughout the summer with this, and this is his GSoC project.

As for the specific change: this is primarily to support PEP 3121, and allowing multiple interpreters to use a module without fear of global variables being shared across interpreters. 

In the current implementation, the Random type would be shared across all interpreters, with the change, each interpreter gets its own copy of the Random type. For that, the type needs to become a heap type, which is best done with the PEP 384 API (even though ABI stability is irrelevant for the random module).
msg168514 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2012-08-18 14:09
I tried to benchmark this patch but I'm getting a segfault:

Python 3.3.0b2+ (default:dc18d73e67a5, Aug 18 2012, 15:37:04) 
[GCC 4.4.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
Segmentation fault
msg168516 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2012-08-18 15:35
The Py_tp_bases line below doesn't look right.  I suspect that's what's causing the segfault.


+static PyType_Slot Random_Type_slots[] = {
+    {Py_tp_getattro, PyObject_GenericGetAttr},
+    {Py_tp_doc, random_doc},
+    {Py_tp_methods, random_methods},
+    {Py_tp_new, random_new},
+    {Py_tp_free, PyObject_Free},
+    {Py_tp_bases, },
+    {0, 0}
msg168518 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2012-08-18 16:16
With the Py_tp_bases line removed, all tests pass for me on a non-debug 64-bit build on OS X 10.6.  A quick timing of random.random() showed no distinguishable performance impact.
msg168519 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2012-08-18 17:13
Thanks, Mark. With your change applied I can't measure any performance
differences either.
msg372079 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-06-22 09:37
The PEP 384 part was fixed by:

commit 04f0bbfbedf8d2bb69b012f853de6648b1a9f27f
Author: Dino Viehland <dinoviehland@fb.com>
Date:   Fri Sep 13 11:12:27 2019 +0100

    bpo-38075: Port _randommodule.c to PEP-384 (GH-15798)
    
    - Migrate `Random_Type` to `PyType_FromSpec`
    - To simulate an old use of `PyLong_Type.tp_as_number->nb_absolute`, I added
      code to the module init function to stash `int.__abs__` for later
      use. Ideally we'd use `PyType_GetSlot()` instead, but it doesn't currently
      work for static types in CPython, and implementing it just for this case
      doesn't seem worth it.
    - Do exact check for long and dispatch to PyNumber_Absolute, use vector call when not exact.
msg381416 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-11-19 14:01
commit cc0cd43c0f96dac413e54855e9c77ec4b73bd2f8
Author: Christian Heimes <christian@python.org>
Date:   Thu Nov 19 08:46:29 2020 +0100

    bpo-1635741: Port _random to multiphase initialization (GH-23359)

See bpo-41111 "Convert a few stdlib extensions to the limited C API".
History
Date User Action Args
2020-11-19 14:01:02vstinnersetstatus: open -> closed
superseder: Py_Finalize() doesn't clear all Python objects at exit
messages: + msg381416

resolution: duplicate
stage: resolved
2020-06-22 09:37:08vstinnersetnosy: + vstinner
messages: + msg372079
2014-05-13 21:55:06skrahsetnosy: - skrah
2012-11-08 13:28:16Robin.Schreibersetkeywords: + pep3121, - patch
2012-08-27 03:42:36belopolskylinkissue15787 dependencies
2012-08-18 17:13:43skrahsetmessages: + msg168519
2012-08-18 16:16:53mark.dickinsonsetmessages: + msg168518
2012-08-18 15:35:40mark.dickinsonsetnosy: + mark.dickinson
messages: + msg168516
2012-08-18 14:09:14skrahsetnosy: + skrah
messages: + msg168514
2012-08-17 08:24:53loewissetmessages: + msg168437
2012-08-15 14:25:02rhettingersetassignee: loewis

messages: + msg168299
nosy: + loewis
2012-08-15 14:10:36Robin.Schreibercreate