Skip to content

Commit 4643bd9

Browse files
committed
Apparently FreeBSD enables some HW floating-point exceptions by default.
This can cause core dumps when Python runs. Python relies on the 754- (and C99-) mandated default "non-stop" mode for FP exceptions. This patch from Ben Laurie disables at least one FP exception on FreeBSD at Python startup time.
1 parent 5b26abb commit 4643bd9

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

Include/pyport.h

+10-5
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,15 @@ extern "C" {
255255
* if the returned result is a NaN, or if a C89 box returns HUGE_VAL
256256
* in non-overflow cases.
257257
* X is evaluated more than once.
258+
* Some platforms have better way to spell this, so expect some #ifdef'ery.
258259
*/
260+
#ifdef __FreeBSD__
261+
#define Py_OVERFLOWED(X) isinf(X)
262+
#else
259263
#define Py_OVERFLOWED(X) ((X) != 0.0 && (errno == ERANGE || \
260264
(X) == Py_HUGE_VAL || \
261265
(X) == -Py_HUGE_VAL))
266+
#endif
262267

263268
/* Py_SET_ERANGE_ON_OVERFLOW(x)
264269
* If a libm function did not set errno, but it looks like the result
@@ -320,7 +325,7 @@ extern "C" {
320325
#if defined(__GNUC__) && (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)
321326
#define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
322327
#else
323-
#define Py_DEPRECATED(VERSION_UNUSED)
328+
#define Py_DEPRECATED(VERSION_UNUSED)
324329
#endif
325330

326331
/**************************************************************************
@@ -395,17 +400,17 @@ extern double hypot(double, double);
395400
/* Declarations for symbol visibility.
396401
397402
PyAPI_FUNC(type): Declares a public Python API function and return type
398-
PyAPI_DATA(type): Declares public Python data and its type
403+
PyAPI_DATA(type): Declares public Python data and its type
399404
PyMODINIT_FUNC: A Python module init function. If these functions are
400-
inside the Python core, they are private to the core.
401-
If in an extension module, it may be declared with
405+
inside the Python core, they are private to the core.
406+
If in an extension module, it may be declared with
402407
external linkage depending on the platform.
403408
404409
As a number of platforms support/require "__declspec(dllimport/dllexport)",
405410
we support a HAVE_DECLSPEC_DLL macro to save duplication.
406411
*/
407412

408-
/*
413+
/*
409414
All windows ports, except cygwin, are handled in PC/pyconfig.h
410415
BeOS is only other autoconf platform requiring special linkage handling
411416
and both these use __declspec()

Misc/ACKS

+1
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ Cameron Laird
303303
Detlef Lannert
304304
Soren Larsen
305305
Piers Lauder
306+
Ben Laurie
306307
Chris Lawrence
307308
Christopher Lee
308309
Inyeol Lee

Modules/python.c

+15
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,23 @@
22

33
#include "Python.h"
44

5+
#ifdef __FreeBSD__
6+
#include <floatingpoint.h>
7+
#endif
8+
59
int
610
main(int argc, char **argv)
711
{
12+
/* 754 requires that FP exceptions run in "no stop" mode by default,
13+
* and until C vendors implement C99's ways to control FP exceptions,
14+
* Python requires non-stop mode. Alas, some platforms enable FP
15+
* exceptions by default. Here we disable them.
16+
*/
17+
#ifdef __FreeBSD__
18+
fp_except_t m;
19+
20+
m = fpgetmask();
21+
fpsetmask(m & ~FP_X_OFL);
22+
#endif
823
return Py_Main(argc, argv);
924
}

0 commit comments

Comments
 (0)