Fix Python examples to compile and run under Python 3

This commit is contained in:
William S Fulton 2013-05-26 22:34:07 +01:00
commit b3ca22dc33
4 changed files with 51 additions and 30 deletions

View file

@ -32,9 +32,6 @@ int printf(const char *fmt, ...);
}
#endif
/* Typemap just to make the example work */
%typemap(in) FILE * "$1 = PyFile_AsFile($input);";
int fprintf(FILE *, const char *fmt, ...);
/* 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;
%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, ...) {
va_list ap;
char *x;

View file

@ -13,13 +13,14 @@ for i in range(0,10):
# This will probably be garbled because %d is interpreted by C
example.printf("The value is %d\n")
stdout = example.stdout_stream()
# 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):
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
example.fprintf(sys.stdout,"The value is %d\n")
example.fprintf(stdout,"The value is %d\n")
# This function calls our NULL-terminated function