Fix Python examples to compile and run under Python 3
This commit is contained in:
parent
fd93beadf4
commit
b3ca22dc33
4 changed files with 51 additions and 30 deletions
|
|
@ -89,21 +89,10 @@ to look at the <a href="http://www.python.org/sigs/distutils-sig/">distutils</a>
|
||||||
|
|
||||||
<h2>Compatibility</h2>
|
<h2>Compatibility</h2>
|
||||||
|
|
||||||
The examples have been extensively tested on the following platforms:
|
For Python 3, set the environment variable <tt>PY3=1</tt>.
|
||||||
|
This will ensure the 2to3 program is run prior to running any example.
|
||||||
<ul>
|
|
||||||
<li>Linux
|
|
||||||
<li>Solaris
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
All of the examples were last tested with the following configuration (9/1/2000):
|
|
||||||
|
|
||||||
<ul>
|
|
||||||
<li>Sparc Solaris 2.8.
|
|
||||||
<li>gcc-2.95.2
|
|
||||||
<li>Python 1.6b1.
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
|
<p>
|
||||||
Your mileage may vary. If you experience a problem, please let us know by
|
Your mileage may vary. If you experience a problem, please let us know by
|
||||||
contacting us on the <a href="http://www.swig.org/mail.html">mailing lists</a>.
|
contacting us on the <a href="http://www.swig.org/mail.html">mailing lists</a>.
|
||||||
</body>
|
</body>
|
||||||
|
|
|
||||||
|
|
@ -38,27 +38,44 @@ extern int gcd(int x, int y);
|
||||||
}
|
}
|
||||||
%#if PY_VERSION_HEX >= 0x03000000
|
%#if PY_VERSION_HEX >= 0x03000000
|
||||||
{
|
{
|
||||||
int l;
|
PyObject *utf8str = PyUnicode_AsUTF8String(s);
|
||||||
$2[i] = PyUnicode_AsStringAndSize(s, &l);
|
const char *cstr = PyBytes_AsString(utf8str);
|
||||||
|
$2[i] = strdup(cstr);
|
||||||
|
Py_DECREF(utf8str);
|
||||||
}
|
}
|
||||||
%#else
|
%#else
|
||||||
$2[i] = PyString_AsString(s);
|
$2[i] = PyString_AsString(s);
|
||||||
%#endif
|
%#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
$2[i] = 0;
|
$2[i] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
%typemap(freearg) (int argc, char *argv[]) {
|
||||||
|
%#if PY_VERSION_HEX >= 0x03000000
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < $1; i++) {
|
||||||
|
free($2[i]);
|
||||||
|
}
|
||||||
|
%#endif
|
||||||
|
}
|
||||||
|
|
||||||
extern int gcdmain(int argc, char *argv[]);
|
extern int gcdmain(int argc, char *argv[]);
|
||||||
|
|
||||||
%typemap(in) (char *bytes, int len) {
|
%typemap(in) (char *bytes, int len) {
|
||||||
|
|
||||||
%#if PY_VERSION_HEX >= 0x03000000
|
%#if PY_VERSION_HEX >= 0x03000000
|
||||||
|
char *cstr;
|
||||||
|
Py_ssize_t len;
|
||||||
|
PyObject *utf8str;
|
||||||
if (!PyUnicode_Check($input)) {
|
if (!PyUnicode_Check($input)) {
|
||||||
PyErr_SetString(PyExc_ValueError,"Expected a string");
|
PyErr_SetString(PyExc_ValueError,"Expected a string");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
$1 = PyUnicode_AsStringAndSize($input, &$2);
|
utf8str = PyUnicode_AsUTF8String($input);
|
||||||
|
PyBytes_AsStringAndSize(utf8str, &cstr, &len);
|
||||||
|
$1 = strndup(cstr, (size_t)len);
|
||||||
|
$2 = (int)len;
|
||||||
|
Py_DECREF(utf8str);
|
||||||
%#else
|
%#else
|
||||||
if (!PyString_Check($input)) {
|
if (!PyString_Check($input)) {
|
||||||
PyErr_SetString(PyExc_ValueError,"Expected a string");
|
PyErr_SetString(PyExc_ValueError,"Expected a string");
|
||||||
|
|
@ -69,6 +86,12 @@ extern int gcdmain(int argc, char *argv[]);
|
||||||
%#endif
|
%#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
%typemap(freearg) (char *bytes, int len) {
|
||||||
|
%#if PY_VERSION_HEX >= 0x03000000
|
||||||
|
free($1);
|
||||||
|
%#endif
|
||||||
|
}
|
||||||
|
|
||||||
extern int count(char *bytes, int len, char c);
|
extern int count(char *bytes, int len, char c);
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -79,9 +102,13 @@ extern int count(char *bytes, int len, char c);
|
||||||
|
|
||||||
%typemap(in) (char *str, int len) {
|
%typemap(in) (char *str, int len) {
|
||||||
%#if PY_VERSION_HEX >= 0x03000000
|
%#if PY_VERSION_HEX >= 0x03000000
|
||||||
$2 = PyUnicode_GetSize($input);
|
char *cstr;
|
||||||
$1 = (char *) malloc($2+1);
|
Py_ssize_t len;
|
||||||
memmove($1,PyUnicode_AsString($input),$2);
|
PyObject *utf8str = PyUnicode_AsUTF8String($input);
|
||||||
|
PyBytes_AsStringAndSize(utf8str, &cstr, &len);
|
||||||
|
$1 = strndup(cstr, (size_t)len);
|
||||||
|
$2 = (int)len;
|
||||||
|
Py_DECREF(utf8str);
|
||||||
%#else
|
%#else
|
||||||
$2 = PyString_Size($input);
|
$2 = PyString_Size($input);
|
||||||
$1 = (char *) malloc($2+1);
|
$1 = (char *) malloc($2+1);
|
||||||
|
|
|
||||||
|
|
@ -32,9 +32,6 @@ int printf(const char *fmt, ...);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Typemap just to make the example work */
|
|
||||||
%typemap(in) FILE * "$1 = PyFile_AsFile($input);";
|
|
||||||
|
|
||||||
int fprintf(FILE *, const char *fmt, ...);
|
int fprintf(FILE *, const char *fmt, ...);
|
||||||
|
|
||||||
/* Here is somewhat different example. A variable length argument
|
/* Here is somewhat different example. A variable length argument
|
||||||
|
|
@ -48,6 +45,13 @@ int fprintf(FILE *, const char *fmt, ...);
|
||||||
%varargs(20, char *x = NULL) printv;
|
%varargs(20, char *x = NULL) printv;
|
||||||
|
|
||||||
%inline %{
|
%inline %{
|
||||||
|
|
||||||
|
/* In Python 2 we could use PyFile_AsFile for converting Python sys.stdout to C's stdout.
|
||||||
|
This API disappeared in Python 3, so instead we use a helper function to get stdout */
|
||||||
|
FILE * stdout_stream(void) {
|
||||||
|
return stdout;
|
||||||
|
}
|
||||||
|
|
||||||
void printv(char *s, ...) {
|
void printv(char *s, ...) {
|
||||||
va_list ap;
|
va_list ap;
|
||||||
char *x;
|
char *x;
|
||||||
|
|
|
||||||
|
|
@ -13,13 +13,14 @@ for i in range(0,10):
|
||||||
# This will probably be garbled because %d is interpreted by C
|
# This will probably be garbled because %d is interpreted by C
|
||||||
example.printf("The value is %d\n")
|
example.printf("The value is %d\n")
|
||||||
|
|
||||||
|
stdout = example.stdout_stream()
|
||||||
# Call fprintf
|
# Call fprintf
|
||||||
example.fprintf(sys.stdout,"Hello World. I'm fprintf\n")
|
example.fprintf(stdout,"Hello World. I'm fprintf\n")
|
||||||
for i in range(0,10):
|
for i in range(0,10):
|
||||||
example.fprintf(sys.stdout,"i is %d\n" % i)
|
example.fprintf(stdout,"i is %d\n" % i)
|
||||||
|
|
||||||
# This won't be garbled since %d is not interpreted
|
# This won't be garbled since %d is not interpreted
|
||||||
example.fprintf(sys.stdout,"The value is %d\n")
|
example.fprintf(stdout,"The value is %d\n")
|
||||||
|
|
||||||
# This function calls our NULL-terminated function
|
# This function calls our NULL-terminated function
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue