Skip to content

Commit 0c600ad

Browse files
committed
Pick up correct DBF_ enum values
The DBF_ enum values have changed in EPICS 3.16, so it is no longer safe to leave them baked into the Python code. This commit fixes the issue by passing the values from C to Python.
1 parent f6c4fe0 commit 0c600ad

File tree

3 files changed

+47
-22
lines changed

3 files changed

+47
-22
lines changed

python/softioc/fields.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,19 @@
44

55
import sys
66
from ctypes import *
7-
from .imports import get_field_offsets
7+
from .imports import get_field_offsets, get_DBF_values
88
import numpy
99

1010
from cothread.dbr import *
1111
from cothread.dbr import ca_timestamp, EPICS_epoch
1212

1313

14-
# Record field types, as defined in dbFldTypes.h
15-
DBF_STRING = 0
16-
DBF_CHAR = 1
17-
DBF_UCHAR = 2
18-
DBF_SHORT = 3
19-
DBF_USHORT = 4
20-
DBF_LONG = 5
21-
DBF_ULONG = 6
22-
DBF_FLOAT = 7
23-
DBF_DOUBLE = 8
24-
DBF_ENUM = 9
25-
DBF_MENU = 10
26-
DBF_DEVICE = 11
27-
DBF_INLINK = 12
28-
DBF_OUTLINK = 13
29-
DBF_FWDLINK = 14
30-
DBF_NOACCESS = 15
14+
# Pick up the DBF_ definitions from the C helper layer. This is safer than
15+
# defining the values here, as unfortunately the values have been known to
16+
# change between EPICS releases.
17+
for name, value in get_DBF_values().items():
18+
globals()[name] = value
19+
3120

3221
# Mapping from record field type to ctypes type. Type STRING is handled
3322
# specially, and types ENUM, MENU, DEVICE and FWDLINK aren't supported.

python/softioc/imports.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def auto_decode(result, func, args):
5959
db_put_field.errcheck = expect_success
6060

6161

62-
# char * get_EPICS_BASE()
62+
# const char *get_EPICS_BASE(void)
6363
#
6464
# Returns the path to EPICS_BASE
6565
get_EPICS_BASE = libPythonSupport.get_EPICS_BASE
@@ -68,6 +68,13 @@ def auto_decode(result, func, args):
6868
get_EPICS_BASE.errcheck = auto_decode
6969

7070

71+
# PyObject *get_DBF_values(void)
72+
#
73+
# Returns dictionary mapping DBF_ enum names to values
74+
get_DBF_values = libPythonSupport.get_DBF_values
75+
get_DBF_values.restype = py_object
76+
77+
7178
# void EpicsPvPutHook(struct asTrapWriteMessage *pmessage, int after)
7279
#
7380
# Hook for logging EPICS caput events

softIocApp/PythonSupport.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <Python.h>
12
#include <string.h>
23

34
#define db_accessHFORdb_accessC // Needed to get correct DBF_ values
@@ -9,13 +10,41 @@
910

1011

1112
/* Returns the EPICS_BASE path used to build this IOC. */
12-
13-
char * get_EPICS_BASE(void)
13+
const char *get_EPICS_BASE(void)
1414
{
15-
return EPICS_BASE;
15+
return EPICS_BASE; // Passed as #define from Makefile
1616
}
1717

1818

19+
/* Helper for function below. */
20+
#define ADD_ENUM(dict, name) \
21+
PyDict_SetItemString(dict, #name, PyInt_FromLong(name))
22+
23+
/* Alas, EPICS has changed the numerical assignments of the DBF_ enums between
24+
* versions, so to avoid unpleasant surprises, we compute thes values here in C
25+
* and pass them back to the Python layer. */
26+
PyObject *get_DBF_values(void)
27+
{
28+
PyObject *dict = PyDict_New();
29+
ADD_ENUM(dict, DBF_STRING);
30+
ADD_ENUM(dict, DBF_CHAR);
31+
ADD_ENUM(dict, DBF_UCHAR);
32+
ADD_ENUM(dict, DBF_SHORT);
33+
ADD_ENUM(dict, DBF_USHORT);
34+
ADD_ENUM(dict, DBF_LONG);
35+
ADD_ENUM(dict, DBF_ULONG);
36+
ADD_ENUM(dict, DBF_FLOAT);
37+
ADD_ENUM(dict, DBF_DOUBLE);
38+
ADD_ENUM(dict, DBF_ENUM);
39+
ADD_ENUM(dict, DBF_MENU);
40+
ADD_ENUM(dict, DBF_DEVICE);
41+
ADD_ENUM(dict, DBF_INLINK);
42+
ADD_ENUM(dict, DBF_OUTLINK);
43+
ADD_ENUM(dict, DBF_FWDLINK);
44+
ADD_ENUM(dict, DBF_NOACCESS);
45+
return dict;
46+
}
47+
1948

2049
/* Given an array of field names, this routine looks up each field name in
2150
* the EPICS database and returns the corresponding field offset. */

0 commit comments

Comments
 (0)