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

the python binds with output params is difficulty to use #991

Open
mingforpc opened this issue Jul 2, 2024 · 2 comments
Open

the python binds with output params is difficulty to use #991

mingforpc opened this issue Jul 2, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@mingforpc
Copy link

mingforpc commented Jul 2, 2024

in dwg.i ,

etc:

EXPORT bool dwg_dynapi_header_utf8text (const Dwg_Data *restrict dwg,
                                        const char *restrict fieldname,
                                        char **restrict textp, int *isnew,
                                        Dwg_DYNAPI_field *restrict fp);

the output params is textp, isnew, fp, in LibreDWG.py

def dwg_dynapi_common_utf8text(_obj, fieldname, textp, isnew, fp):
    return _LibreDWG.dwg_dynapi_common_utf8text(_obj, fieldname, textp, isnew, fp)

I cannot call this function success, always get error

TypeError: in method 'dwg_dynapi_entity_utf8text', argument 4 of type 'char **'

I found this https://stackoverflow.com/questions/15185010/how-to-receive-reference-and-pointer-arguments-in-python-swig , it seem need some modify in dwg.i.

But I am not familiar with SWIG

@rurban
Copy link
Contributor

rurban commented Jul 2, 2024

Yes, marking OUTPUT args as such would be good. fp is optional and can be NULL

@mingforpc
Copy link
Author

I add this at the first for dwg.i

/*****************************************************************************/
/*  LibreDWG - free implementation of the DWG file format                    */
/*                                                                           */
/*  Copyright (C) 2010-2019 Free Software Foundation, Inc.                   */
/*                                                                           */
/*  This library is free software, licensed under the terms of the GNU       */
/*  General Public License as published by the Free Software Foundation,     */
/*  either version 3 of the License, or (at your option) any later version.  */
/*  You should have received a copy of the GNU General Public License        */
/*  along with this program.  If not, see <http://www.gnu.org/licenses/>.    */
/*****************************************************************************/

/*
 * dwg.i: SWIG interface file
 * written by Rodrigo Rodrigues da Silva
 * ideas contributed by James Michael DuPont
 * rewritten and largely auto-generated by Reini Urban
 */

%module LibreDWG

%include "typemaps.i"  // 包含常用的类型映射


%typemap(in,numinputs=0) int *isnew (int tmp) %{
    $1 = &tmp;
%}

%typemap(argout) int *isnew (PyObject* obj){
  obj = PyInt_FromLong(*$1);
    $result = SWIG_Python_AppendOutput($result,obj);
}


// This input typemap declares that char** requires no input parameter.
// Instead, the address of a local char* is used to call the function.
%typemap(in,numinputs=0) char** (char* tmp) %{
    $1 = &tmp;
%}

// After the function is called, the char** parameter contains a malloc'ed char* pointer.
// Construct a Python Unicode object (I'm using Python 3) and append it to
// any existing return value for the wrapper.
%typemap(argout) char** (PyObject* obj) %{
    obj = PyUnicode_FromString(*$1);
    $result = SWIG_Python_AppendOutput($result,obj);
%}

// The malloc'ed pointer is no longer needed, so make sure it is freed.
%typemap(freearg) char** %{
  free(*$1);
%}

It works for me to call it like a, b, ok = dwg_dynapi_entity_utf8text(text, "TEXT", "text_value", None), but it also seem not work correctly, textp can not free at correct time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants