PageRenderTime 1031ms CodeModel.GetById 34ms RepoModel.GetById 9ms app.codeStats 0ms

/trunk/Examples/python/variables/index.html

#
HTML | 100 lines | 83 code | 17 blank | 0 comment | 0 complexity | 22c0665050da7b79fee887ef39e574f4 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. <html>
  2. <head>
  3. <title>SWIG:Examples:python:variables</title>
  4. </head>
  5. <body bgcolor="#ffffff">
  6. <tt>SWIG/Examples/python/variables/</tt>
  7. <hr>
  8. <H2>Wrapping C Global Variables</H2>
  9. <p>
  10. When a C global variable appears in an interface file, SWIG tries to
  11. wrap it using a technique known as "variable linking." The idea is
  12. pretty simple---we try to create a Python variable that magically
  13. retrieves or updates the value of the underlying C variable when it is
  14. accessed. Click <a href="example.i">here</a> to see a SWIG interface with some variable
  15. declarations in it.
  16. <h2>Manipulating Variables from Python</h2>
  17. Before going any further, it is important to understand some important
  18. differences between C and Python variables. In C, a variable is
  19. simply a name that refers to a specific location in memory. For
  20. example, when you declare a global variable '<tt>double a</tt>' you
  21. know that somewhere in memory, 8 bytes have been set aside to hold a
  22. <tt>double</tt> and that <tt>a</tt> is bound to this location for the
  23. life of the program. In Python, variable creation is nothing more
  24. than a naming operation. For example, when you say '<tt>a = 3</tt>',
  25. 'a' becomes a name that refers to some object '3'. Later on, if you say
  26. '<tt>a = 7.5</tt>, the name 'a' is bound to an entirely different object
  27. containing the value '7.5' (the contents of the original object are not
  28. changed). The end result of this is that a variable in Python can refer
  29. to a virtually unlimited number of different objects (memory locations)
  30. over the lifetime of a program.
  31. <p>
  32. Because of Python's somewhat unusual variable assignment semantics, it is not
  33. possible to directly link a C global variable into an equivalent Python variable.
  34. Instead, all C global variables are accessed as attributes of a special object
  35. called 'cvar'. For example, if you had a global variable
  36. <blockquote>
  37. <pre>
  38. double foo;
  39. </pre>
  40. </blockquote>
  41. it will be accessed in the Python module as <tt>cvar.foo</tt>. Click
  42. <a href="example.py">here</a> to see a script that updates and prints
  43. out the values of the variables using this technique.
  44. <h2>Key points</h2>
  45. <ul>
  46. <li>When a global variable has the type "<tt>char *</tt>", SWIG manages it as a character
  47. string. However, whenever the value of such a variable is set from Python, the old
  48. value is destroyed using <tt>free()</tt> or <tt>delete</tt> (the choice of which depends
  49. on whether or not SWIG was run with the -c++ option).
  50. <li><tt>signed char</tt> and <tt>unsigned char</tt> are handled as small 8-bit integers.
  51. <li>String array variables such as '<tt>char name[256]</tt>' are managed as Python strings, but
  52. when setting the value, the result is truncated to the maximum length of the array. Furthermore, the string is assumed to be null-terminated.
  53. <li>When structures and classes are used as global variables, they are mapped into pointers.
  54. Getting the "value" returns a pointer to the global variable. Setting the value of a structure results in a memory copy from a pointer to the global.
  55. </ul>
  56. <h2>Creating read-only variables</h2>
  57. The <tt>%immutable</tt> and <tt>%mutable</tt> directives can be used to
  58. specify a collection of read-only variables. For example:
  59. <blockquote>
  60. <pre>
  61. %immutable;
  62. int status;
  63. double blah;
  64. ...
  65. %mutable;
  66. </pre>
  67. </blockquote>
  68. The <tt>%immutable</tt> directive remains in effect until it is explicitly disabled
  69. using the <tt>%mutable</tt> directive.
  70. <h2>Comments</h2>
  71. <ul>
  72. <li>Management of global variables is one of the most problematic aspects
  73. of C/C++ wrapping because the scripting interface and resulting memory management
  74. is much trickier than simply creating a wrapper function.
  75. <p>
  76. <li>Because of the potential for a namespace conflict, you should not use
  77. the <tt>from module import *</tt> statement for a SWIG module with global
  78. variables. Doing so will cause a collision on the 'cvar' object should
  79. more than one module be loaded in this manner.
  80. </ul>
  81. </body>
  82. </html>
  83. <hr>