Advertisement
Guest User

Making pylibpcap Python 3.2 Compliant = stream of concious

a guest
Feb 23rd, 2011
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.56 KB | None | 0 0
  1. -- got pylibpcap to compile for python 3. took moderate modifications. had to do the following
  2.  
  3. notes for converting python 2 code to python 3
  4. 1) change print stmts from '''print x''' to '''print (x)''' where x is generalized
  5.  
  6. 2) change places where maps and filters are used. python 2, these return lists, python 3, these return the respective classes. the fix is [i for i in map(func, iterable)] and [i for i in filter(func, iterable)]
  7. --- map(lambda x: (x, None), config_defines) ==> [i for i in map(lambda x: (x, None), config_defines)]
  8.  
  9. 3) in setup.py add '-py3' to the swig command parameters, update include dirs to contain python3 headers on the mac, and also the libdirs for python:
  10. <snip line: 23>
  11. # specify its directory here, otherwise set this to None
  12. libpcap_dir = None
  13. # libpcap_dir = "/home/wiml/netsrc/libpcap/libpcap-0.7.2"
  14. # libpcap_dir = "/home/wiml/netsrc/libpcap/libpcap-0.8.3"
  15. # libpcap_dir = "/home/wiml/netsrc/libpcap/cvs.tcpdump.org/libpcap"
  16.  
  17. library_dirs = ["/Library/Frameworks/Python.framework/Versions/3.2/lib/" ]
  18. if not libpcap_dir is None:
  19. library_dirs.append(libpcap_dir)
  20.  
  21. include_dirs = ["/Library/Frameworks/Python.framework/Versions/3.2/Headers" ]
  22. </snip>
  23.  
  24. Also updated library_dirs and include_dirs, accordingly:<snip: line 153>
  25. if libpcap_dir is None:
  26. pcap_extension = Extension("_pcapmodule",
  27. sourcefiles,
  28. include_dirs = include_dirs,
  29. define_macros = defines,
  30. library_dirs = library_dirs,
  31. libraries = [ "pcap" ]
  32. )
  33. else:
  34. include_dirs.append( libpcap_dir )
  35. # extension_objects=[ os.path.join(libpcap_dir, 'libpcap.a') ]
  36. pcap_extension = Extension("_pcapmodule",
  37. sourcefiles,
  38. include_dirs = include_dirs,
  39. define_macros = defines,
  40. library_dirs = library_dirs,
  41. libraries = [ "pcap" ]
  42. )
  43.  
  44.  
  45. 4) update docify.py and docify-shadow.py with the above. Also in docify.py, need to perform the following change (from http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=542984 ):
  46. if re.search('swigregister|SWIG_PyInstanceMethod_New',fname):
  47. #if re.search('swigregister',fname):
  48.  
  49. 5) update mk-constants.py (strings module no longer has replace, so the line needs to be modified (i just comment out the string.replace and write the file out...laziness):
  50. rcs = ' Id: mk-constants.py,v 1.7 2008/01/26 01:31:55 wiml Exp \n'
  51. fp.write(rcs)#string.replace(rcs, '$', ''))
  52.  
  53. 6) had to perform some basic foo to get the right Python stuff working:python3 setup.py build
  54. sudo python3 setup.py install --prefix=/Library/Frameworks/Python.framework/Versions/3.2/ #mac
  55.  
  56. Ok, so it appears 3.2 changes the C-API signifigantly, Surprise!
  57.  
  58. http://paste.pocoo.org/show/253494/ <-- swig patch that helped fix one problem, but I ran into another issue with PyLong_AS_INT
  59.  
  60.  
  61. So, Now I have run into a number of deprecations that is super frustrating. I think I need to revert the swig file and patch from the beginning, but the interface file may be an issue.
  62.  
  63. Converted the following thus far:
  64. PyString_* -> PyUnicode_* ===> Wrong needed to convert this to PyBytes_*
  65. PyInt_* -> PyLong_*
  66.  
  67.  
  68. SUCCESS!!
  69. [Ones on the left are my modfied versions, ones on the right are clean]
  70. Output from diff of pcap_interface.c:
  71. 0:pylibpcap-0.6.2 apridgen$ diff pcap_interface.c ~/pylibpcap-0.6.2/pcap_interface.c
  72. 248c248
  73. < outObject = Py_BuildValue("iy#f", header.len, buf, header.caplen,
  74. ---
  75. > outObject = Py_BuildValue("is#f", header.len, buf, header.caplen,
  76. 285c285
  77. < PyObject *linktype = PyLong_FromLong( (long) (links[i]) );
  78. ---
  79. > PyObject *linktype = PyInt_FromLong( (long) (links[i]) );
  80. 428c428
  81. < str = PyBytes_FromString(buf);
  82. ---
  83. > str = PyString_FromString(buf);
  84. 484c484
  85. < return PyBytes_FromStringAndSize( (const char *)sa, length );
  86. ---
  87. > return PyString_FromStringAndSize( (const char *)sa, length );
  88. 521c521
  89. < return PyBytes_FromFormat("<AF %d>", sa->sa_family);
  90. ---
  91. > return PyString_FromFormat("<AF %d>", sa->sa_family);
  92. 534c534
  93. < result = PyBytes_FromString(buf);
  94. ---
  95. > result = PyString_FromString(buf);
  96. 674c674
  97. < out=PyLong_FromLong(addr.s_addr);
  98. ---
  99. > out=PyInt_FromLong(addr.s_addr);
  100. 700c700
  101. < arglist = Py_BuildValue("iy#f", header->len, packetdata, header->caplen,
  102. ---
  103. > arglist = Py_BuildValue("is#f", header->len, packetdata, header->caplen,
  104.  
  105.  
  106.  
  107.  
  108.  
  109. Output of diff for pcap.i:
  110. 0:pylibpcap-0.6.2 apridgen$ diff pcap.i ~/pylibpcap-0.6.2/pcap.i
  111. 53c53
  112. < PyObject *v = PyLong_FromLong(pcapmodule_DLT[constantIndex].value);
  113. ---
  114. > PyObject *v = PyInt_FromLong(pcapmodule_DLT[constantIndex].value);
  115. 91,92c91,92
  116. < if (PyLong_CheckExact($input)) {
  117. < $1 = (unsigned long)PyLong_AsLong($input);
  118. ---
  119. > if (PyInt_CheckExact($input)) {
  120. > $1 = (unsigned long)PyInt_AS_LONG($input);
  121.  
  122.  
  123. Output for changes that need to be made for swig, as mentioned above:
  124. 0:pylibpcap-0.6.2 apridgen$ diff pcap.c ~/pylibpcap-0.6.2/pcap.c
  125. 2427,2429c2427,2428
  126. < type_pointer = PyCapsule_Import((char*)"swig_runtime_data" SWIG_RUNTIME_VERSION ".type_pointer" SWIG_TYPE_TABLE_NAME, 0);
  127. < // type_pointer = PyCObject_Import((char*)"swig_runtime_data" SWIG_RUNTIME_VERSION,
  128. < // (char*)"type_pointer" SWIG_TYPE_TABLE_NAME);
  129. ---
  130. > type_pointer = PyCObject_Import((char*)"swig_runtime_data" SWIG_RUNTIME_VERSION,
  131. > (char*)"type_pointer" SWIG_TYPE_TABLE_NAME);
  132. 2472c2471
  133. < SWIG_Python_DestroyModule(PyObject *capsule)
  134. ---
  135. > SWIG_Python_DestroyModule(void *vptr)
  136. 2474,2477c2473
  137. < swig_module_info *swig_module = (swig_module_info *)PyCapsule_GetPointer(capsule, (char*)"swig_runtime_data" SWIG_RUNTIME_VERSION);
  138. < if (swig_module == NULL)
  139. < return;
  140. <
  141. ---
  142. > swig_module_info *swig_module = (swig_module_info *) vptr;
  143. 2502,2503c2498
  144. < PyObject *pointer = PyCapsule_New((void *) swig_module, (char*)"swig_runtime_data" SWIG_RUNTIME_VERSION ".type_pointer" SWIG_TYPE_TABLE_NAME, SWIG_Python_DestroyModule);
  145. < //PyObject *pointer = PyCObject_FromVoidPtr((void *) swig_module, SWIG_Python_DestroyModule);
  146. ---
  147. > PyObject *pointer = PyCObject_FromVoidPtr((void *) swig_module, SWIG_Python_DestroyModule);
  148. 2526,2527c2521
  149. < descriptor = (swig_type_info *) PyCapsule_GetPointer(obj, (char*)"swig_type_info");
  150. < //descriptor = (swig_type_info *) PyCObject_AsVoidPtr(obj);
  151. ---
  152. > descriptor = (swig_type_info *) PyCObject_AsVoidPtr(obj);
  153. 2532,2533c2526
  154. < obj = PyCapsule_New(descriptor, (char*)"swig_type_info", NULL);
  155. < //obj = PyCObject_FromVoidPtr(descriptor, NULL);
  156. ---
  157. > obj = PyCObject_FromVoidPtr(descriptor, NULL);
  158.  
  159.  
  160. 0:pylibpcap-0.6.2 apridgen$ diff pcap.py ~/pylibpcap-0.6.2/pcap.py
  161. 81a82
  162. > if int(sys.version[0])>='2':
  163.  
  164.  
  165.  
  166. PyString_* -> PyUnicode_* ===> Wrong needed to convert this to PyBytes_*
  167. I also needed to change the s# to y#.
  168.  
  169. I had some other problems, but i got this little script to run, so that works for me.
  170.  
  171. building and installing on a mac:
  172. python3 setup.py build
  173. sudo python3 setup.py install --prefix=/Library/Frameworks/Python.framework/Versions/3.2/
  174.  
  175. python3
  176.  
  177. My obviously weak test script:
  178.  
  179. import pcap
  180. p = pcap.pcapObject()
  181. p.open_offline("../../traffic/lbl-internal.20041004-1303.port001.dump.anon")
  182. f = []
  183.  
  184. while True:
  185. c = p.next()
  186. if c is None:
  187. break
  188. f.append(c)
  189.  
  190. print(f)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement