Commit 43f07a1a authored by Philip_Tzou's avatar Philip_Tzou
Browse files

Fix #1213 addInteractionGroup should accept `set` values as parameters.

apparently SWIG typemaps maps Python's list but not set with std::set.
Instead, Python's `set` is mapped with `std::unordered_set`.

The original code of `swigInputBuilder.py` is kindly in a mess. Too much
lines in a single method `writeMethods` which causes debug becomes
difficult. Also there are a lot of warnings about
[PEP8](https://www.python.org/dev/peps/pep-0008/).

A refactoring in the future may be a good idea.
parent 1857850c
...@@ -378,7 +378,6 @@ class SwigInputBuilder: ...@@ -378,7 +378,6 @@ class SwigInputBuilder:
self.fOut.write("\n%s};\n" % INDENT) self.fOut.write("\n%s};\n" % INDENT)
if len(enumNodes)>0: self.fOut.write("\n") if len(enumNodes)>0: self.fOut.write("\n")
def writeMethods(self, classNode): def writeMethods(self, classNode):
methodList=getClassMethodList(classNode, self.skipMethods) methodList=getClassMethodList(classNode, self.skipMethods)
...@@ -465,35 +464,44 @@ class SwigInputBuilder: ...@@ -465,35 +464,44 @@ class SwigInputBuilder:
(shortClassName, memberNode, (shortClassName, memberNode,
shortMethDefinition, methName, shortMethDefinition, methName,
isConstructors, isDestructor, templateType, templateName) = items isConstructors, isDestructor, templateType, templateName) = items
paramList=findNodes(memberNode, 'param') paramList = findNodes(memberNode, 'param')
#write pythonprepend blocks # write pythonprepend blocks
mArgsstring = getText("argsstring", memberNode) mArgsstring = getText("argsstring", memberNode)
if self.fOutPythonprepend and \ if self.fOutPythonprepend and \
len(paramList) and \ len(paramList) and \
mArgsstring.find('=0')<0: mArgsstring.find('=0') < 0:
key=(shortClassName, methName) text = '''
if key in self.configModule.STEAL_OWNERSHIP: %pythonprepend OpenMM::{shortClassName}::{methName}{mArgsstring} %{{{{{{0}}
for argNum in self.configModule.STEAL_OWNERSHIP[key]: %}}}}'''.format(shortClassName=shortClassName, methName=methName, mArgsstring=mArgsstring)
if self.SWIG_COMPACT_ARGUMENTS: textInside = ''
argName = 'args[%s]' % argNum key = (shortClassName, methName)
else: for argNum in self.configModule.STEAL_OWNERSHIP.get(key, []):
argName = getText('declname', paramList[argNum]) if self.SWIG_COMPACT_ARGUMENTS:
argName = 'args[%s]' % argNum
else:
argName = getText('declname', paramList[argNum])
text = ''' textInside += '''
%pythonprepend OpenMM::{shortClassName}::{methName}{mArgsstring} %{{
if not {argName}.thisown: if not {argName}.thisown:
s = ("the %s object does not own its corresponding OpenMM object" s = ("the %s object does not own its corresponding OpenMM object"
% self.__class__.__name__) % self.__class__.__name__)
raise Exception(s) raise Exception(s)'''.format(argName=argName)
%}}'''.format(argName=argName, shortClassName=shortClassName, methName=methName, mArgsstring=mArgsstring) for argNum in self.configModule.REQUIRE_ORDERED_SET.get(key, []):
self.fOutPythonprepend.write(text) if self.SWIG_COMPACT_ARGUMENTS:
argName = 'args[%s]' % argNum
else:
argName = getText('declname', paramList[argNum])
textInside += '''
{argName} = list({argName})'''.format(argName=argName)
if textInside:
self.fOutPythonprepend.write(text.format(textInside))
#write pythonappend blocks # write pythonappend blocks
if self.fOutPythonappend \ if self.fOutPythonappend \
and mArgsstring.find('=0')<0: and mArgsstring.find('=0') < 0:
key=(shortClassName, methName) key = (shortClassName, methName)
#print "key %s %s \n" % (shortClassName, methName) #print "key %s %s \n" % (shortClassName, methName)
addText='' addText=''
returnType = getText("type", memberNode) returnType = getText("type", memberNode)
......
...@@ -144,6 +144,12 @@ STEAL_OWNERSHIP = {("Platform", "registerPlatform") : [0], ...@@ -144,6 +144,12 @@ STEAL_OWNERSHIP = {("Platform", "registerPlatform") : [0],
("CompoundIntegrator", "addIntegrator") : [0], ("CompoundIntegrator", "addIntegrator") : [0],
} }
REQUIRE_ORDERED_SET = {("CustomNonbondedForce", "addInteractionGroup") : [0, 1],
("CustomNonbondedForce", "setInteractionGroupParameters") : [1, 2],
}
# This is a list of units to attach to return values and method args. # This is a list of units to attach to return values and method args.
# Indexed by (ClassName, MethodsName) # Indexed by (ClassName, MethodsName)
UNITS = { UNITS = {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment