File: pyutils.c

package info (click to toggle)
swftools 0.9.2%2Bds1-3
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 9,488 kB
  • sloc: ansic: 122,576; sh: 8,494; cpp: 8,020; yacc: 2,260; lisp: 904; makefile: 581; python: 304
file content (85 lines) | stat: -rw-r--r-- 1,922 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <Python.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char* setError(char*format, ...)
{
    char buf[1024];
    int l;
    va_list arglist;
    va_start(arglist, format);
    vsnprintf(buf, sizeof(buf)-1, format, arglist);
    va_end(arglist);
    l = strlen(buf);
    while(l && buf[l-1]=='\n') {
	buf[l-1] = 0;
	l--;
    }
    return strdup(buf);
}

static int verbose = 0;
void mylog(char*format, ...)
{
    char buf[1024];
    int l;
    va_list arglist;
    if(!verbose)
	return;
    va_start(arglist, format);
    vsnprintf(buf, sizeof(buf)-1, format, arglist);
    va_end(arglist);
    l = strlen(buf);
    while(l && buf[l-1]=='\n') {
	buf[l-1] = 0;
	l--;
    }
    fprintf(stderr, "[SWF] %s\n", buf);
    fflush(stderr);
}

#define PY_NONE Py_BuildValue("s", 0)

PyObject* FindMethodMore(PyObject*ret, PyMethodDef f[], PyObject*self, char* a)
{
    if(ret==NULL) {
	ret = Py_FindMethod(f, self, a);
    } else {
	if(!strcmp(a, "__methods__")) {
	    /* we are being dir()ed. Complete the function table */
	    PyObject* add = Py_FindMethod(f, self, a);
	    int t;
	    mylog("taglist_getattr: append common funtions %08x %08x\n", ret, add);
	    for(t=0;t<PyList_Size(add);t++)
		PyList_Append(ret, PyList_GetItem(add, t));
	}
    }
    return ret;
}

void dummy_dealloc(PyObject* self)
{
    PyObject_Del(self);
}

PyMethodDef* addMethods(PyMethodDef*obj1, PyMethodDef*obj2) 
{
    int num1=0,num2=0;
    if(obj1) for(num1=0;obj1[num1].ml_name;num1++);
    if(obj2) for(num2=0;obj2[num2].ml_name;num2++);
    PyMethodDef* result = malloc(sizeof(PyMethodDef)*(num1+num2+1));
    if(obj1)
	memcpy(result, obj1, num1*sizeof(PyMethodDef));
    if(obj2)
	memcpy(&result[num1], obj2, (num2+1)*sizeof(PyMethodDef));
    if(obj1)
	free(obj1);
    return result;
}
void setVerbosity(int _verbose)
{
    verbose = _verbose;
    mylog("setting verbosity to %d", verbose);
}