1*3b6c3722Schristos /*
2*3b6c3722Schristos * file_py3.i: Typemaps for FILE* for Python 3
3*3b6c3722Schristos *
4*3b6c3722Schristos * Copyright (c) 2011, Karel Slany (karel.slany AT nic.cz)
5*3b6c3722Schristos * All rights reserved.
6*3b6c3722Schristos *
7*3b6c3722Schristos * Redistribution and use in source and binary forms, with or without
8*3b6c3722Schristos * modification, are permitted provided that the following conditions are met:
9*3b6c3722Schristos *
10*3b6c3722Schristos * * Redistributions of source code must retain the above copyright notice,
11*3b6c3722Schristos * this list of conditions and the following disclaimer.
12*3b6c3722Schristos * * Redistributions in binary form must reproduce the above copyright
13*3b6c3722Schristos * notice, this list of conditions and the following disclaimer in the
14*3b6c3722Schristos * documentation and/or other materials provided with the distribution.
15*3b6c3722Schristos * * Neither the name of the organization nor the names of its
16*3b6c3722Schristos * contributors may be used to endorse or promote products derived from this
17*3b6c3722Schristos * software without specific prior written permission.
18*3b6c3722Schristos *
19*3b6c3722Schristos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20*3b6c3722Schristos * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*3b6c3722Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*3b6c3722Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23*3b6c3722Schristos * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*3b6c3722Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*3b6c3722Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*3b6c3722Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*3b6c3722Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*3b6c3722Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*3b6c3722Schristos * POSSIBILITY OF SUCH DAMAGE.
30*3b6c3722Schristos */
31*3b6c3722Schristos
32*3b6c3722Schristos %{
33*3b6c3722Schristos #include <unistd.h>
34*3b6c3722Schristos #include <fcntl.h>
35*3b6c3722Schristos %}
36*3b6c3722Schristos
37*3b6c3722Schristos %types(FILE *);
38*3b6c3722Schristos
39*3b6c3722Schristos //#define SWIG_FILE3_DEBUG
40*3b6c3722Schristos
41*3b6c3722Schristos /* converts basic file descriptor flags onto a string */
42*3b6c3722Schristos %fragment("fdfl_to_str", "header") {
43*3b6c3722Schristos const char *
fdfl_to_str(int fdfl)44*3b6c3722Schristos fdfl_to_str(int fdfl) {
45*3b6c3722Schristos
46*3b6c3722Schristos static const char * const file_mode[] = {"w+", "w", "r"};
47*3b6c3722Schristos
48*3b6c3722Schristos if (fdfl & O_RDWR) {
49*3b6c3722Schristos return file_mode[0];
50*3b6c3722Schristos } else if (fdfl & O_WRONLY) {
51*3b6c3722Schristos return file_mode[1];
52*3b6c3722Schristos } else {
53*3b6c3722Schristos return file_mode[2];
54*3b6c3722Schristos }
55*3b6c3722Schristos }
56*3b6c3722Schristos }
57*3b6c3722Schristos
58*3b6c3722Schristos %fragment("is_obj_file", "header") {
59*3b6c3722Schristos int
is_obj_file(PyObject * obj)60*3b6c3722Schristos is_obj_file(PyObject *obj) {
61*3b6c3722Schristos int fd, fdfl;
62*3b6c3722Schristos if (!PyLong_Check(obj) && /* is not an integer */
63*3b6c3722Schristos PyObject_HasAttrString(obj, "fileno") && /* has fileno method */
64*3b6c3722Schristos (PyObject_CallMethod(obj, "flush", NULL) != NULL) && /* flush() succeeded */
65*3b6c3722Schristos ((fd = PyObject_AsFileDescriptor(obj)) != -1) && /* got file descriptor */
66*3b6c3722Schristos ((fdfl = fcntl(fd, F_GETFL)) != -1) /* got descriptor flags */
67*3b6c3722Schristos ) {
68*3b6c3722Schristos return 1;
69*3b6c3722Schristos }
70*3b6c3722Schristos else {
71*3b6c3722Schristos return 0;
72*3b6c3722Schristos }
73*3b6c3722Schristos }
74*3b6c3722Schristos }
75*3b6c3722Schristos
76*3b6c3722Schristos %fragment("obj_to_file","header", fragment="fdfl_to_str,is_obj_file") {
77*3b6c3722Schristos FILE *
obj_to_file(PyObject * obj)78*3b6c3722Schristos obj_to_file(PyObject *obj) {
79*3b6c3722Schristos int fd, fdfl;
80*3b6c3722Schristos FILE *fp;
81*3b6c3722Schristos if (is_obj_file(obj)) {
82*3b6c3722Schristos fd = PyObject_AsFileDescriptor(obj);
83*3b6c3722Schristos fdfl = fcntl(fd, F_GETFL);
84*3b6c3722Schristos fp = fdopen(dup(fd), fdfl_to_str(fdfl)); /* the FILE* must be flushed
85*3b6c3722Schristos and closed after being used */
86*3b6c3722Schristos #ifdef SWIG_FILE3_DEBUG
87*3b6c3722Schristos fprintf(stderr, "opening fd %d (fl %d \"%s\") as FILE %p\n",
88*3b6c3722Schristos fd, fdfl, fdfl_to_str(fdfl), (void *)fp);
89*3b6c3722Schristos #endif
90*3b6c3722Schristos return fp;
91*3b6c3722Schristos }
92*3b6c3722Schristos return NULL;
93*3b6c3722Schristos }
94*3b6c3722Schristos }
95*3b6c3722Schristos
96*3b6c3722Schristos /* returns -1 if error occurred */
97*3b6c3722Schristos /* caused magic SWIG Syntax errors when was commented out */
98*3b6c3722Schristos #if 0
99*3b6c3722Schristos %fragment("dispose_file", "header") {
100*3b6c3722Schristos int
101*3b6c3722Schristos dispose_file(FILE **fp) {
102*3b6c3722Schristos #ifdef SWIG_FILE3_DEBUG
103*3b6c3722Schristos fprintf(stderr, "flushing FILE %p\n", (void *)fp);
104*3b6c3722Schristos #endif
105*3b6c3722Schristos if (*fp == NULL) {
106*3b6c3722Schristos return 0;
107*3b6c3722Schristos }
108*3b6c3722Schristos if ((fflush(*fp) == 0) && /* flush file */
109*3b6c3722Schristos (fclose(*fp) == 0)) { /* close file */
110*3b6c3722Schristos *fp = NULL;
111*3b6c3722Schristos return 0;
112*3b6c3722Schristos }
113*3b6c3722Schristos return -1;
114*3b6c3722Schristos }
115*3b6c3722Schristos }
116*3b6c3722Schristos #endif
117*3b6c3722Schristos
118*3b6c3722Schristos %typemap(arginit, noblock = 1) FILE* {
119*3b6c3722Schristos $1 = NULL;
120*3b6c3722Schristos }
121*3b6c3722Schristos
122*3b6c3722Schristos /*
123*3b6c3722Schristos * added due to ub_ctx_debugout since since it is overloaded:
124*3b6c3722Schristos * takes void* and FILE*. In reality only FILE* but the wrapper
125*3b6c3722Schristos * and the function is declared in such way.
126*3b6c3722Schristos */
127*3b6c3722Schristos %typemap(typecheck, noblock = 1, fragment = "is_obj_file", precedence = SWIG_TYPECHECK_POINTER) FILE* {
128*3b6c3722Schristos $1 = is_obj_file($input);
129*3b6c3722Schristos }
130*3b6c3722Schristos
131*3b6c3722Schristos %typemap(check, noblock = 1) FILE* {
132*3b6c3722Schristos if ($1 == NULL) {
133*3b6c3722Schristos /* The generated wrapper function raises TypeError on mismatching types. */
134*3b6c3722Schristos SWIG_exception_fail(SWIG_TypeError, "in method '" "$symname" "', argument "
135*3b6c3722Schristos "$argnum"" of type '" "$type""'");
136*3b6c3722Schristos }
137*3b6c3722Schristos }
138*3b6c3722Schristos
139*3b6c3722Schristos %typemap(in, noblock = 1, fragment = "obj_to_file") FILE* {
140*3b6c3722Schristos $1 = obj_to_file($input);
141*3b6c3722Schristos }
142*3b6c3722Schristos
143*3b6c3722Schristos /*
144*3b6c3722Schristos * Commented out due the way how ub_ctx_debugout() uses the parameter.
145*3b6c3722Schristos * This typemap would cause the FILE* to be closed after return from
146*3b6c3722Schristos * the function. This caused Python interpreter to crash, since the
147*3b6c3722Schristos * function just stores the FILE* internally in ctx and use it for
148*3b6c3722Schristos * logging. So we'll leave the closing of the file on the OS.
149*3b6c3722Schristos */
150*3b6c3722Schristos /*%typemap(freearg, noblock = 1, fragment = "dispose_file") FILE* {
151*3b6c3722Schristos if (dispose_file(&$1) == -1) {
152*3b6c3722Schristos SWIG_exception_fail(SWIG_IOError, "closing file in method '" "$symname" "', argument "
153*3b6c3722Schristos "$argnum"" of type '" "$type""'");
154*3b6c3722Schristos }
155*3b6c3722Schristos }*/
156