View | Details | Raw Unified | Return to bug 664 | Differences between
and this patch

Collapse All | Expand All

(-)b/Makefile.in (-1 / +2 lines)
 Lines 25-30   DNSTAP_SRC=@DNSTAP_SRC@ Link Here 
25
DNSTAP_OBJ=@DNSTAP_OBJ@
25
DNSTAP_OBJ=@DNSTAP_OBJ@
26
WITH_PYTHONMODULE=@WITH_PYTHONMODULE@
26
WITH_PYTHONMODULE=@WITH_PYTHONMODULE@
27
WITH_PYUNBOUND=@WITH_PYUNBOUND@
27
WITH_PYUNBOUND=@WITH_PYUNBOUND@
28
PY_MAJOR_VERSION=@PY_MAJOR_VERSION@
28
PYTHON_SITE_PKG=@PYTHON_SITE_PKG@
29
PYTHON_SITE_PKG=@PYTHON_SITE_PKG@
29
PYTHONMOD_INSTALL=@PYTHONMOD_INSTALL@
30
PYTHONMOD_INSTALL=@PYTHONMOD_INSTALL@
30
PYTHONMOD_UNINSTALL=@PYTHONMOD_UNINSTALL@
31
PYTHONMOD_UNINSTALL=@PYTHONMOD_UNINSTALL@
 Lines 393-399   libunbound_wrap.lo libunbound_wrap.o: libunbound/python/libunbound_wrap.c \ Link Here 
393
	unbound.h
394
	unbound.h
394
libunbound/python/libunbound_wrap.c:	$(srcdir)/libunbound/python/libunbound.i unbound.h
395
libunbound/python/libunbound_wrap.c:	$(srcdir)/libunbound/python/libunbound.i unbound.h
395
	@-if test ! -d libunbound/python; then $(INSTALL) -d libunbound/python; fi
396
	@-if test ! -d libunbound/python; then $(INSTALL) -d libunbound/python; fi
396
	$(SWIG) -python -o $@ $(CPPFLAGS) $(srcdir)/libunbound/python/libunbound.i
397
	$(SWIG) -python -o $@ $(CPPFLAGS) -DPY_MAJOR_VERSION=$(PY_MAJOR_VERSION) $(srcdir)/libunbound/python/libunbound.i
397
398
398
# Pyunbound python unbound wrapper
399
# Pyunbound python unbound wrapper
399
_unbound.la:	libunbound_wrap.lo libunbound.la
400
_unbound.la:	libunbound_wrap.lo libunbound.la
(-)b/configure.ac (+2 lines)
 Lines 475-480   if test x_$ub_test_python != x_no; then Link Here 
475
		AC_ERROR([Python version >= 2.4.0 is required])
475
		AC_ERROR([Python version >= 2.4.0 is required])
476
	fi
476
	fi
477
477
478
      PY_MAJOR_VERSION="`$PYTHON -c "import sys; print(sys.version_info.major)"`"
479
      AC_SUBST(PY_MAJOR_VERSION)
478
      # Have Python
480
      # Have Python
479
      AC_DEFINE(HAVE_PYTHON,1,[Define if you have Python libraries and header files.])
481
      AC_DEFINE(HAVE_PYTHON,1,[Define if you have Python libraries and header files.])
480
      LIBS="$PYTHON_LDFLAGS $LIBS"
482
      LIBS="$PYTHON_LDFLAGS $LIBS"
(-)b/libunbound/python/file_py3.i (+155 lines)
Added Link Here 
1
/*
2
 * file_py3.i: Typemaps for FILE* for Python 3
3
 *
4
 * Copyright (c) 2011, Karel Slany (karel.slany AT nic.cz)
5
 * All rights reserved.
6
 * 
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions are met:
9
 * 
10
 *     * Redistributions of source code must retain the above copyright notice,
11
 *       this list of conditions and the following disclaimer.
12
 *     * Redistributions in binary form must reproduce the above copyright
13
 *       notice, this list of conditions and the following disclaimer in the
14
 *       documentation and/or other materials provided with the distribution.
15
 *     * Neither the name of the organization nor the names of its
16
 *       contributors may be used to endorse or promote products derived from this
17
 *       software without specific prior written permission.
18
 * 
19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
 * POSSIBILITY OF SUCH DAMAGE.
30
 */
31
32
%{
33
#include <unistd.h>
34
#include <fcntl.h>
35
%}
36
37
%types(FILE *);
38
39
//#define SWIG_FILE3_DEBUG
40
41
/* converts basic file descriptor flags onto a string */
42
%fragment("fdfl_to_str", "header") {
43
const char *
44
fdfl_to_str(int fdfl) {
45
46
  static const char * const file_mode[] = {"w+", "w", "r"};
47
48
  if (fdfl & O_RDWR) {
49
    return file_mode[0];
50
  } else if (fdfl & O_WRONLY) {
51
    return file_mode[1];
52
  } else {
53
    return file_mode[2];
54
  }
55
}
56
}
57
58
%fragment("is_obj_file", "header") {
59
int
60
is_obj_file(PyObject *obj) {
61
  int fd, fdfl;
62
  if (!PyLong_Check(obj) &&                                /* is not an integer */
63
      PyObject_HasAttrString(obj, "fileno") &&             /* has fileno method */
64
      (PyObject_CallMethod(obj, "flush", NULL) != NULL) && /* flush() succeeded */
65
      ((fd = PyObject_AsFileDescriptor(obj)) != -1) &&     /* got file descriptor */
66
      ((fdfl = fcntl(fd, F_GETFL)) != -1)                  /* got descriptor flags */
67
    ) {
68
    return 1;
69
  }
70
  else {
71
    return 0;
72
  }
73
}
74
}
75
76
%fragment("obj_to_file","header", fragment="fdfl_to_str,is_obj_file") {
77
FILE *
78
obj_to_file(PyObject *obj) {
79
  int fd, fdfl;
80
  FILE *fp;
81
  if (is_obj_file(obj)) {
82
    fd = PyObject_AsFileDescriptor(obj);
83
    fdfl = fcntl(fd, F_GETFL);
84
    fp = fdopen(dup(fd), fdfl_to_str(fdfl)); /* the FILE* must be flushed
85
                                                and closed after being used */
86
#ifdef SWIG_FILE3_DEBUG
87
    fprintf(stderr, "opening fd %d (fl %d \"%s\") as FILE %p\n",
88
            fd, fdfl, fdfl_to_str(fdfl), (void *)fp);
89
#endif
90
    return fp;
91
  }
92
  return NULL;
93
}
94
}
95
96
/* returns -1 if error occurred */
97
/* caused magic SWIG Syntax errors when was commented out */
98
#if 0
99
%fragment("dispose_file", "header") {
100
int
101
dispose_file(FILE **fp) {
102
#ifdef SWIG_FILE3_DEBUG
103
  fprintf(stderr, "flushing FILE %p\n", (void *)fp);
104
#endif
105
  if (*fp == NULL) {
106
    return 0;
107
  }
108
  if ((fflush(*fp) == 0) &&  /* flush file */
109
      (fclose(*fp) == 0)) {  /* close file */
110
    *fp = NULL;
111
    return 0;
112
  }
113
  return -1;
114
}
115
}
116
#endif
117
118
%typemap(arginit, noblock = 1) FILE* {
119
  $1 = NULL;
120
}
121
122
/*
123
 * added due to ub_ctx_debugout since since it is overloaded:
124
 * takes void* and FILE*. In reality only FILE* but the wrapper
125
 * and the function is declared in such way.
126
 */
127
%typemap(typecheck, noblock = 1, fragment = "is_obj_file", precedence = SWIG_TYPECHECK_POINTER) FILE* {
128
  $1 = is_obj_file($input);
129
}
130
131
%typemap(check, noblock = 1) FILE* {
132
  if ($1 == NULL) {
133
    /* The generated wrapper function raises TypeError on mismatching types. */
134
    SWIG_exception_fail(SWIG_TypeError, "in method '" "$symname" "', argument "
135
                        "$argnum"" of type '" "$type""'");
136
  }
137
}
138
139
%typemap(in, noblock = 1, fragment = "obj_to_file") FILE* {
140
  $1 = obj_to_file($input);
141
}
142
143
/*
144
 * Commented out due the way how ub_ctx_debugout() uses the parameter.
145
 * This typemap would cause the FILE* to be closed after return from
146
 * the function. This caused Python interpreter to crash, since the
147
 * function just stores the FILE* internally in ctx and use it for
148
 * logging. So we'll leave the closing of the file on the OS.
149
 */
150
/*%typemap(freearg, noblock = 1, fragment = "dispose_file") FILE* {
151
  if (dispose_file(&$1) == -1) {
152
    SWIG_exception_fail(SWIG_IOError, "closing file in method '" "$symname" "', argument "
153
                        "$argnum"" of type '" "$type""'");
154
  }
155
}*/
(-)b/libunbound/python/libunbound.i (+4 lines)
 Lines 60-66    Link Here 
60
%}
60
%}
61
61
62
//%include "doc.i"
62
//%include "doc.i"
63
#if PY_MAJOR_VERSION >= 3
64
%include "file_py3.i" // python 3 FILE *
65
#else
63
%include "file.i"
66
%include "file.i"
67
#endif
64
68
65
%feature("docstring") strerror "Convert error value to a human readable string."
69
%feature("docstring") strerror "Convert error value to a human readable string."
66
70
(-)b/pythonmod/interface.i (-14 / +14 lines)
 Lines 37-43    Link Here 
37
%include "stdint.i" // uint_16_t can be known type now
37
%include "stdint.i" // uint_16_t can be known type now
38
38
39
%inline %{
39
%inline %{
40
   //converts [len][data][len][data][0] string to a List of labels (PyStrings)
40
   //converts [len][data][len][data][0] string to a List of labels (PyBytes)
41
   PyObject* GetNameAsLabelList(const char* name, int len) {
41
   PyObject* GetNameAsLabelList(const char* name, int len) {
42
     PyObject* list;
42
     PyObject* list;
43
     int cnt=0, i;
43
     int cnt=0, i;
 Lines 79-86   struct query_info { Link Here 
79
%inline %{
79
%inline %{
80
   enum enum_rr_class  { 
80
   enum enum_rr_class  { 
81
      RR_CLASS_IN = 1,
81
      RR_CLASS_IN = 1,
82
      RR_CLASS_CH	= 3,
82
      RR_CLASS_CH = 3,
83
      RR_CLASS_HS	= 4,
83
      RR_CLASS_HS = 4,
84
      RR_CLASS_NONE = 254,
84
      RR_CLASS_NONE = 254,
85
      RR_CLASS_ANY = 255,
85
      RR_CLASS_ANY = 255,
86
   };
86
   };
 Lines 164-170   struct query_info { Link Here 
164
       char buf[LDNS_MAX_DOMAINLEN+1];
164
       char buf[LDNS_MAX_DOMAINLEN+1];
165
       buf[0] = '\0';
165
       buf[0] = '\0';
166
       dname_str((uint8_t*)dname, buf);
166
       dname_str((uint8_t*)dname, buf);
167
       return PyString_FromString(buf);
167
       return PyBytes_FromString(buf);
168
   }
168
   }
169
%}
169
%}
170
170
 Lines 440-446   struct comm_reply { Link Here 
440
     reply_addr2str(reply, dest, 64);
440
     reply_addr2str(reply, dest, 64);
441
     if (dest[0] == 0)
441
     if (dest[0] == 0)
442
        return Py_None;
442
        return Py_None;
443
     return PyString_FromString(dest);
443
     return PyBytes_FromString(dest);
444
  }
444
  }
445
445
446
  PyObject* _comm_reply_family_get(struct comm_reply* reply) {
446
  PyObject* _comm_reply_family_get(struct comm_reply* reply) {
 Lines 448-456   struct comm_reply { Link Here 
448
        int af = (int)((struct sockaddr_in*) &(reply->addr))->sin_family;
448
        int af = (int)((struct sockaddr_in*) &(reply->addr))->sin_family;
449
449
450
        switch(af) {
450
        switch(af) {
451
           case AF_INET: return PyString_FromString("ip4");
451
           case AF_INET: return PyBytes_FromString("ip4");
452
           case AF_INET6: return PyString_FromString("ip6"); 
452
           case AF_INET6: return PyBytes_FromString("ip6");
453
           case AF_UNIX: return PyString_FromString("unix");
453
           case AF_UNIX: return PyBytes_FromString("unix");
454
        }
454
        }
455
455
456
        return Py_None;
456
        return Py_None;
 Lines 711-723   struct delegpt { Link Here 
711
711
712
%inline %{
712
%inline %{
713
   PyObject* _get_dp_dname(struct delegpt* dp) {
713
   PyObject* _get_dp_dname(struct delegpt* dp) {
714
      return PyString_FromStringAndSize((char*)dp->name, dp->namelen);
714
      return PyBytes_FromStringAndSize((char*)dp->name, dp->namelen);
715
   } 
715
   } 
716
   PyObject* _get_dp_dname_components(struct delegpt* dp) {
716
   PyObject* _get_dp_dname_components(struct delegpt* dp) {
717
      return GetNameAsLabelList((char*)dp->name, dp->namelen);
717
      return GetNameAsLabelList((char*)dp->name, dp->namelen);
718
   }
718
   }
719
   PyObject* _get_dpns_dname(struct delegpt_ns* dpns) {
719
   PyObject* _get_dpns_dname(struct delegpt_ns* dpns) {
720
      return PyString_FromStringAndSize((char*)dpns->name, dpns->namelen);
720
      return PyBytes_FromStringAndSize((char*)dpns->name, dpns->namelen);
721
   }
721
   }
722
   PyObject* _get_dpns_dname_components(struct delegpt_ns* dpns) {
722
   PyObject* _get_dpns_dname_components(struct delegpt_ns* dpns) {
723
      return GetNameAsLabelList((char*)dpns->name, dpns->namelen);
723
      return GetNameAsLabelList((char*)dpns->name, dpns->namelen);
 Lines 728-734   struct delegpt { Link Here 
728
     delegpt_addr_addr2str(target, dest, 64);
728
     delegpt_addr_addr2str(target, dest, 64);
729
     if (dest[0] == 0)
729
     if (dest[0] == 0)
730
        return Py_None;
730
        return Py_None;
731
     return PyString_FromString(dest);
731
     return PyBytes_FromString(dest);
732
  }
732
  }
733
733
734
%}
734
%}
 Lines 842-848   int checkList(PyObject *l) Link Here 
842
       for (i=0; i < PyList_Size(l); i++) 
842
       for (i=0; i < PyList_Size(l); i++) 
843
       {
843
       {
844
           item = PyList_GetItem(l, i);
844
           item = PyList_GetItem(l, i);
845
           if (!PyString_Check(item))
845
           if (!PyBytes_Check(item))
846
              return 0;
846
              return 0;
847
       }
847
       }
848
       return 1;
848
       return 1;
 Lines 864-875   int pushRRList(sldns_buffer* qb, PyObject *l, uint32_t default_ttl, int qsec, Link Here 
864
864
865
        len = sldns_buffer_remaining(qb);
865
        len = sldns_buffer_remaining(qb);
866
        if(qsec) {
866
        if(qsec) {
867
                if(sldns_str2wire_rr_question_buf(PyString_AsString(item),
867
                if(sldns_str2wire_rr_question_buf(PyBytes_AsString(item),
868
                        sldns_buffer_current(qb), &len, NULL, NULL, 0, NULL, 0)
868
                        sldns_buffer_current(qb), &len, NULL, NULL, 0, NULL, 0)
869
                        != 0)
869
                        != 0)
870
                        return 0;
870
                        return 0;
871
        } else {
871
        } else {
872
                if(sldns_str2wire_rr_buf(PyString_AsString(item),
872
                if(sldns_str2wire_rr_buf(PyBytes_AsString(item),
873
                        sldns_buffer_current(qb), &len, NULL, default_ttl,
873
                        sldns_buffer_current(qb), &len, NULL, default_ttl,
874
                        NULL, 0, NULL, 0) != 0)
874
                        NULL, 0, NULL, 0) != 0)
875
                        return 0;
875
                        return 0;
(-)b/pythonmod/pythonmod.c (-2 / +7 lines)
 Lines 133-139   int pythonmod_init(struct module_env* env, int id) Link Here 
133
   /* Initialize Python libraries */
133
   /* Initialize Python libraries */
134
   if (!Py_IsInitialized()) 
134
   if (!Py_IsInitialized()) 
135
   {
135
   {
136
      Py_SetProgramName("unbound");
136
#if PY_MAJOR_VERSION >= 3
137
      wchar_t progname[8];
138
      mbstowcs(progname, "unbound", 8);
139
#else
140
      char *progname = "unbound";
141
#endif
142
      Py_SetProgramName(progname);
137
      Py_NoSiteFlag = 1;
143
      Py_NoSiteFlag = 1;
138
      Py_Initialize();
144
      Py_Initialize();
139
      PyEval_InitThreads();
145
      PyEval_InitThreads();
140
- 

Return to bug 664