swig/comedi.i: Fixed the "break backwards compatibility"

The lines:

%rename("%(strip:[COMEDI_])s", regextarget=1) "COMEDI_.*";
%rename("%(strip:[comedi_])s", regextarget=1) "comedi_.*";

caused errors on import due to _comedi function names not matching at
import time.  Stripping names is now completely done in the globals()
modification at the end, which works properly.

to indicate if the old names should be stripped or not.  This makes the
intent very clear in the generated comedi.py file.

[Reformatted commit message -- Ian Abbott]
This commit is contained in:
Joseph Timothy Foley 2017-03-13 23:42:36 +00:00 committed by Ian Abbott
parent f28772a982
commit 6a73a96cbf

View file

@ -44,11 +44,6 @@
//#define SWIGPYTHONONLYSHORT
#ifdef SWIGPYTHON
#ifdef SWIGPYTHONONLYSHORT
%rename("%(strip:[COMEDI_])s", regextarget=1) "COMEDI_.*";
%rename("%(strip:[comedi_])s", regextarget=1) "comedi_.*";
#endif
// These need to be explicitly written as unsigned ints
%rename(CR_FLAGS_MASK) _CR_FLAGS_MASK;
%rename(CR_INVERT) _CR_INVERT;
@ -118,19 +113,24 @@ unsigned int NI_AO_SCAN_BEGIN_SRC_RTSI(unsigned int rtsi_channel);
%array_class(lsampl_t, lsampl_array);
%array_class(comedi_insn, insn_array);
#ifndef SWIGPYTHONONLYSHORT
#ifdef SWIGPYTHONONLYSHORT
%insert("python") %{
# Add entries in module dictionary to strip comedi_/COMEDI_ prefix
import copy
delete_comedi_prefix = True
%}
#else
%insert("python") %{
delete_comedi_prefix = False
%}
#endif
%insert("python") %{
# Add entries in module dictionary without comedi_/COMEDI_ prefix
import re
myglobals = copy.copy(globals())
# Copy dictionary before
for k,v in myglobals.items():
for k,v in globals().copy().items():
if re.match('^comedi_', k, flags=re.IGNORECASE):
globals()[k[7:]] = v
# uncommenting following line removes compatibility with old code using
# comedi_ prefix:
# globals().pop(k)
del re, k, v
if delete_comedi_prefix:
globals().pop(k) # Break backwards compatibility
del re, k, v, delete_comedi_prefix
%}
#endif