Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Lib/test/test_py2xwarn.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ def test_next(self):
w.reset()
self.assertNoWarning(iterator_marks.__next__(), w)

def test_random(self):
expected = "String repr of random.random() is longer in 3.x, change code accordingly"
import random
with check_py2x_warnings(("", Py2xWarning)) as w:
self.assertWarning(str(random.random()), w, expected)

def test_truncate0(self):
expected = "Calling truncate(0) on text stream without seek(0)" + \
" may produce inconsistent results. Use seek(0) before truncate(0)"
Expand Down
10 changes: 9 additions & 1 deletion Modules/_randommodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@

#include "Python.h"
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pg_float.h"
#ifdef HAVE_PROCESS_H
# include <process.h> // getpid()
#endif
Expand Down Expand Up @@ -177,7 +178,14 @@ _random_Random_random_impl(RandomObject *self)
/*[clinic end generated code: output=117ff99ee53d755c input=afb2a59cbbb00349]*/
{
uint32_t a=genrand_uint32(self)>>5, b=genrand_uint32(self)>>6;
return PyFloat_FromDouble((a*67108864.0+b)*(1.0/9007199254740992.0));
double x = (a * 67108864.0 + b) * (1.0 / 9007199254740992.0);
if (Py_Py2xWarningFlag){
return PgFloat_FromDouble(x,1);
}
else{
return PyFloat_FromDouble(x);
}

}

/* initializes mt[N] with a seed */
Expand Down
68 changes: 68 additions & 0 deletions Modules/pg_float.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include "pg_float.h"

typedef struct {
PyFloatObject base;
unsigned char from_random; // 1 = from random, 0 = not from random
} PgFloatObject;

static PyTypeObject PgFloat_Type;
static int g_ready = 0;

extern int Py_Py2xWarningFlag;

static PyObject *
PgFloat_str(PyObject *v)
{
PgFloatObject *self = (PgFloatObject *)v;

if (Py_Py2xWarningFlag && self->from_random && PyErr_WarnEx(
PyExc_Py2xWarning,
"String repr of random.random() is longer in 3.x, change code accordingly",
2) < 0){
return NULL;
}

return PyFloat_Type.tp_str(v);
}

static PyTypeObject PgFloat_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "pg_float.PgFloat",
.tp_basicsize = sizeof(PgFloatObject),
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_base = &PyFloat_Type,
.tp_str = PgFloat_str,
};

int
PgFloat_Initialize(void)
{
if (g_ready) return 0;
if (PyType_Ready(&PgFloat_Type) < 0)
return -1;
g_ready = 1;
return 0;
}

PyObject *
PgFloat_FromDouble(double x, int from_random)
{
if (!g_ready && PgFloat_Initialize() < 0)
return NULL;

PgFloatObject *op = (PgFloatObject *)PyObject_New(PgFloatObject, &PgFloat_Type);
if (!op) return NULL;
op->base.ob_fval = x;
op->from_random = from_random ? 1 : 0;
return (PyObject *)op;
}

PyTypeObject *
PgFloat_TypeObj(void)
{
if (!g_ready && PgFloat_Initialize() < 0)
return NULL;
return &PgFloat_Type;
}
12 changes: 12 additions & 0 deletions Modules/pg_float.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef PG_FLOAT_H
#define PG_FLOAT_H

#include "Python.h"

PyAPI_FUNC(int) PgFloat_Initialize(void);

PyAPI_FUNC(PyObject*) PgFloat_FromDouble(double x, int from_random);

PyAPI_FUNC(PyTypeObject*) PgFloat_TypeObj(void);

#endif /* PG_FLOAT_H */
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ def detect_simple_extensions(self):
self.addext(Extension('_datetime', ['_datetimemodule.c']))
self.addext(Extension('_zoneinfo', ['_zoneinfo.c']))
# random number generator implemented in C
self.addext(Extension("_random", ["_randommodule.c"]))
self.addext(Extension("_random", ["_randommodule.c", "pg_float.c"]))
self.addext(Extension("_bisect", ["_bisectmodule.c"]))
self.addext(Extension("_heapq", ["_heapqmodule.c"]))
# C-optimized pickle replacement
Expand Down