You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
PEP 3121, 384 Refactoring applied to random module #59873
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).
skrah commented on Aug 18, 2012
skrahmannequin
Mannequin
More actions
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
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.
skrah commented on Aug 18, 2012
skrahmannequin
Mannequin
More actions
Thanks, Mark. With your change applied I can't measure any performance
differences either.
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.
Activity
RobinSchreiber commentedon Aug 15, 2012
Changes proposed in PEP-3121 and PEP-384 have now been applied to the lsprof module!
rhettinger commentedon Aug 15, 2012
Martin, does this patch match your intent with PEP-3121 and PEP-384?
loewis commentedon Aug 17, 2012
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).
skrah commentedon Aug 18, 2012
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 faultmdickinson commentedon Aug 18, 2012
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}
mdickinson commentedon Aug 18, 2012
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.
skrah commentedon Aug 18, 2012
Thanks, Mark. With your change applied I can't measure any performance
differences either.
vstinner commentedon Jun 22, 2020
The PEP-384 part was fixed by:
commit 04f0bbf
Author: Dino Viehland <dinoviehland@fb.com>
Date: Fri Sep 13 11:12:27 2019 +0100
vstinner commentedon Nov 19, 2020
commit cc0cd43
Author: Christian Heimes <christian@python.org>
Date: Thu Nov 19 08:46:29 2020 +0100
See bpo-41111 "Convert a few stdlib extensions to the limited C API".