Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typemap for SourceTime and src_time #77

Merged
merged 2 commits into from
Aug 4, 2017
Merged

Add typemap for SourceTime and src_time #77

merged 2 commits into from
Aug 4, 2017

Conversation

ChristopherHogan
Copy link
Contributor

This turned out to be complicated. I thought the tyepmap would be as simple as

$1 = PyObject_GetAttrString(pyobj, "swigobj");

but that returns a PyObject, so I had to manually do the conversion from PyObject to C++ object that SWIG normally does, plus still allow a SWIG proxy object. Users can now pass a subclass of meep.source.SourceTime, or meep.src_time.
@stevengj @HomerReid @oskooi

@ChristopherHogan
Copy link
Contributor Author

I just thought of a much nicer way to do this.

@ChristopherHogan
Copy link
Contributor Author

The other option is to do something like

%feature("pythonprepend") meep::fields::add_volume_source %{
    new_args = [arg.swigobj if isinstance(arg, mp.source.SourceTime) else arg for arg in args]
    args = new_args
%}

which is much simpler. The disadvantage is that we would have to do this manually for every function that takes a src_time as an argument (and for any other high-level Python object that stores a swigobj). I think I prefer the typemap, even though it's more complicated, because we can write it once and it will work for every case. Reopening.

int tmp_res = 0;

if(PyObject_IsInstance($input, py_source_time_object())) {
swig_obj = PyObject_GetAttrString($input, "swigobj");
Copy link
Collaborator

@stevengj stevengj Jul 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This returns a new reference, so you need to Py_DECREF(swig_obj) after the tmp_res = ... to avoid a memory leak.

However, swig_obj = $input does not create a new reference, so you will need a Py_INCREF(swig_obj) there to balance out the decref.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Fixed.

@stevengj stevengj merged commit 24d4629 into NanoComp:master Aug 4, 2017
@ChristopherHogan ChristopherHogan deleted the chogan/source_tm branch August 5, 2017 14:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants