One Hat Cyber Team
Your IP :
216.73.216.216
Server IP :
194.44.31.54
Server :
Linux zen.imath.kiev.ua 4.18.0-553.77.1.el8_10.x86_64 #1 SMP Fri Oct 3 14:30:23 UTC 2025 x86_64
Server Software :
Apache/2.4.37 (Rocky Linux) OpenSSL/1.1.1k
PHP Version :
5.6.40
Buat File
|
Buat Folder
Eksekusi
Dir :
~
/
home
/
mellit
/
cvs-repository
/
pari-python
/
Edit File:
pari.cpp,v
head 1.3; access; symbols; locks; strict; comment @// @; 1.3 date 2007.10.17.12.52.35; author mellit; state Exp; branches; next 1.2; 1.2 date 2007.10.14.14.15.34; author mellit; state Exp; branches; next 1.1; 1.1 date 2007.10.14.12.51.48; author mellit; state Exp; branches; next ; desc @@ 1.3 log @*** empty log message *** @ text @#include <pari.h> #include <Python.h> /* ----------------------------------------------------- */ extern "C" { static PyTypeObject* type_ptr; static PyObject *pari_error; PyObject* pari_module; static long pari_prec; #define SET_ERROR PyErr_SetString(pari_error, errmessage[pari_errno]) #define PRINT_ERROR printf("Pari error: %s\n", errmessage[pari_errno]) struct PyGEN { PyObject_HEAD GEN val; //this value must be on the heap (always use gclone) }; static PyGEN* create_gen(GEN initVal) { // the initVal is cloned and disposed if(initVal==NULL) return NULL; CATCH(CATCH_ALL) { SET_ERROR; return NULL; } TRY { GEN val = gclone(initVal); PyGEN* self; self = PyObject_New(PyGEN, type_ptr); self->val = val; cgiv(initVal); return self; } ENDCATCH } static void gen_dealloc(PyObject* _self) { PyGEN* self = (PyGEN*) _self; CATCH(CATCH_ALL) { PRINT_ERROR; } TRY { gunclone(self->val); } ENDCATCH PyObject_DEL(self); } static PyObject* gen_str(PyObject* _self) { PyGEN* self = (PyGEN*) _self; CATCH(CATCH_ALL) { SET_ERROR; return NULL; } TRY { char* res = GENtostr(self->val); //printf("string returned %s\n", res); PyObject* ret = Py_BuildValue("s", res); free(res); return ret; } ENDCATCH } static GEN genFromLong(PyObject* obj) { int sign = _PyLong_Sign(obj); int bits = _PyLong_NumBits(obj); if(PyErr_Occurred()) return NULL; int byte_length = bits/8+1; // include the sign bit int long_length = (byte_length - 1) / BYTES_IN_LONG + 1; byte_length = long_length*BYTES_IN_LONG; unsigned char* byte_arr = (unsigned char*) malloc(byte_length); _PyLong_AsByteArray((PyLongObject*) obj, byte_arr, byte_length, true, true); if(PyErr_Occurred()) { free(byte_arr); return NULL; } CATCH(CATCH_ALL) { free(byte_arr); SET_ERROR; return NULL; } TRY { GEN res = cgeti(long_length+2); res[1] = evalsigne(_PyLong_Sign(obj)) | evallgefint(long_length+2); GEN ptr = int_LSW(res); int shift = 0; for(int i = 0; i<byte_length; i+=BYTES_IN_LONG) { *ptr=0; for(int j = 0; j<BYTES_IN_LONG; j++) { long val = 0xFFL&byte_arr[i+j]; if(sign<0) { val = 0xFFL&(-shift - val); shift = (val>=1-shift) ? 1 : 0; } *ptr += val<<(8*j); } ptr = int_nextW(ptr); } res = int_normalize(res, 0); free(byte_arr); return res; } ENDCATCH } static GEN gen_cast(PyObject* obj) { GEN res = NULL; CATCH(CATCH_ALL) { SET_ERROR; return NULL; } TRY { if(obj->ob_type==type_ptr) { res = ((PyGEN*) obj)->val; } else if(obj->ob_type==&PyLong_Type) { res = genFromLong(obj); } else if(obj->ob_type==&PyInt_Type) { res = stoi(PyInt_AsLong(obj)); } else if(obj->ob_type==&PyFloat_Type) { res = dbltor(PyFloat_AsDouble(obj)); } else { res = stoi(PyInt_AsLong(obj)); } } ENDCATCH if(PyErr_Occurred()) return NULL; return res; } typedef GEN(*gunary) (GEN); static PyObject* gen_unary(gunary op, PyObject* _obj1) { PyGEN* obj1 = (PyGEN*) _obj1; CATCH(CATCH_ALL) { SET_ERROR; return NULL; } TRY { return (PyObject*) create_gen(op(obj1->val)); } ENDCATCH } #define GEN_UNARY(op) \ static PyObject* \ gen_##op(PyObject* _obj1) { \ return gen_unary(g##op, _obj1); \ } typedef GEN (*gbinary) (GEN, GEN); static PyObject* gen_binary(gbinary op, PyObject* _obj1, PyObject* _obj2) { pari_sp __av = avma; GEN gen1 = gen_cast(_obj1); GEN gen2 = gen_cast(_obj2); if(gen1==NULL || gen2==NULL) { avma = __av; return NULL; } CATCH(CATCH_ALL) { SET_ERROR; return NULL; } TRY { PyObject* res = (PyObject*) create_gen(op(gen1, gen2)); avma = __av; return res; } ENDCATCH } #define GEN_BINARY(op) \ static PyObject* gen_##op(PyObject* _obj1, PyObject* _obj2) { \ return gen_binary(g##op, _obj1, _obj2); \ } GEN gpow2(GEN x, GEN y) { return gpow(x, y, pari_prec); } GEN gabs1(GEN x) { return gabs(x, pari_prec); } GEN_BINARY(add) GEN_BINARY(sub) GEN_BINARY(mul) GEN_BINARY(div) GEN_BINARY(mod) GEN_BINARY(pow2) GEN_UNARY(neg) GEN_UNARY(abs1) static PyObject* gen_pow_tern(PyObject* _obj1, PyObject* _obj2, PyObject*) { return gen_pow2(_obj1, _obj2); } static PyNumberMethods gen_as_number = { gen_add, // binaryfunc nb_add; gen_sub, //binaryfunc nb_subtract; gen_mul, //binaryfunc nb_multiply; gen_div, //binaryfunc nb_divide; gen_mod, //binaryfunc nb_remainder; 0, //binaryfunc nb_divmod; gen_pow_tern, //ternaryfunc nb_power; gen_neg, //unaryfunc nb_negative; 0, //unaryfunc nb_positive; gen_abs1, //unaryfunc nb_absolute; 0, //inquiry nb_nonzero; 0, //unaryfunc nb_invert; 0, //binaryfunc nb_lshift; 0, //binaryfunc nb_rshift; 0, //binaryfunc nb_and; 0, //binaryfunc nb_xor; 0, //binaryfunc nb_or; 0, //coercion nb_coerce; 0, //unaryfunc nb_int; 0, //unaryfunc nb_long; 0, //unaryfunc nb_float; 0, //unaryfunc nb_oct; 0 //unaryfunc nb_hex; }; static PyTypeObject pari_GEN = { PyObject_HEAD_INIT(NULL) 0, "pari.GEN", //char *tp_name; /* For printing */ sizeof(PyGEN), 0, //int tp_basicsize, tp_itemsize; /* For allocation */ gen_dealloc, //destructor tp_dealloc; 0, //printfunc tp_print; 0, //getattrfunc tp_getattr; 0, //setattrfunc tp_setattr; 0, //cmpfunc tp_compare; gen_str, //reprfunc tp_repr; /* Method suites for standard classes */ &gen_as_number, //PyNumberMethods *tp_as_number; 0, //PySequenceMethods *tp_as_sequence; 0, //PyMappingMethods *tp_as_mapping; /* More standard operations (here for binary compatibility) */ 0, //hashfunc tp_hash; 0, //ternaryfunc tp_call; gen_str, //reprfunc tp_str; 0, //getattrofunc tp_getattro; 0, //setattrofunc tp_setattro; /* Functions to access object as input/output buffer */ 0, //PyBufferProcs *tp_as_buffer; /* Flags to define presence of optional/expanded features */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES, //long tp_flags; 0, //char *tp_doc; /* Documentation string */ }; static PyObject * ex_foo(PyObject *self, PyObject *args) { printf("Hello, world\n"); Py_INCREF(Py_None); return Py_None; } static PyObject * pari_eval(PyObject *self, PyObject *args) { char* s; if(!PyArg_ParseTuple(args, "s", &s)) return NULL; //printf("parsed %s\n", s); GEN g = gp_read_str(s); //printf("GP executed %X\n", g); char* res = GENtostr(g); printf(" *** result: %s\n", res); free(res); Py_INCREF(Py_None); return Py_None; } static PyObject * pari_parse(PyObject *self, PyObject *args) { char* s; if(!PyArg_ParseTuple(args, "s", &s)) return NULL; //printf("parsed %s\n", s); CATCH(CATCH_ALL) { SET_ERROR; return NULL; } TRY { GEN g = gp_read_str(s); PyObject *ret = (PyObject*) create_gen(g); return ret; } ENDCATCH } static PyObject * pari_cast(PyObject *self, PyObject *args) { PyObject* obj; if(!PyArg_ParseTuple(args, "O", &obj)) return NULL; //printf("parsed %s\n", s); CATCH(CATCH_ALL) { SET_ERROR; return NULL; } TRY { return (PyObject*) create_gen(gen_cast(obj)); } ENDCATCH } static PyObject * pari_var(PyObject *self, PyObject *args) { char* s; if(!PyArg_ParseTuple(args, "s", &s)) return NULL; //printf("parsed %s\n", s); CATCH(CATCH_ALL) { SET_ERROR; return NULL; } TRY { long v = fetch_user_var(s); GEN g = pol_x[v]; PyObject *ret = (PyObject*) create_gen(g); if(ret!=NULL) { if(PyModule_AddObject(pari_module, s, ret)<0) { return NULL; } } return ret; } ENDCATCH } static struct PyMethodDef pari_methods[] = { {"eval", pari_eval, METH_VARARGS, "Evaluates expression in pari and prints the result"}, {"parse", pari_parse, METH_VARARGS, "Evaluates string expression in pari and returns an object"}, {"cast", pari_cast, METH_VARARGS, "Transforms a Python object, e.g. a number, to a pari object"}, {"var", pari_var, METH_VARARGS, "Creates a variable in PARI and stores the object with the same name in the module"}, {"foo", ex_foo, METH_VARARGS, "foo() doc string"}, {NULL, NULL} }; PyMODINIT_FUNC initpari() { pari_GEN.ob_type = &PyType_Type; type_ptr = &pari_GEN; // init PARI pari_init(64000000, 500000); // init module pari_module = Py_InitModule("pari", pari_methods); // create exception type pari_error = PyErr_NewException("pari.error", NULL, NULL); Py_INCREF(pari_error); PyModule_AddObject(pari_module, "error", pari_error); // default precision pari_prec=20; } } @ 1.2 log @*** empty log message *** @ text @d5 1 d7 8 a14 1 typedef struct { d18 16 a33 1 } PyGEN; d36 7 a42 2 gen_dealloc(PyGEN* self) { gunclone(self->val); d46 173 a218 1 static struct PyTypeObject pari_GEN = { d228 1 a228 1 0, //reprfunc tp_repr; d232 1 a232 1 0, //PyNumberMethods *tp_as_number; d240 1 a240 1 0, //reprfunc tp_str; d248 1 a248 1 0, //long tp_flags; a251 35 /* Assigned meaning in release 2.0 */ /* call function for all accessible objects */ 0, //traverseproc tp_traverse; /* delete references to contained objects */ 0, //inquiry tp_clear; /* Assigned meaning in release 2.1 */ /* rich comparisons */ 0, //richcmpfunc tp_richcompare; /* weak reference enabler */ 0, //long tp_weaklistoffset; /* Added in release 2.2 */ /* Iterators */ 0, //getiterfunc tp_iter; 0, //iternextfunc tp_iternext; /* Attribute descriptor and subclassing stuff */ 0, //struct PyMethodDef *tp_methods; 0, //struct memberlist *tp_members; 0, //struct getsetlist *tp_getset; 0, //struct _typeobject *tp_base; 0, //PyObject *tp_dict; 0, //descrgetfunc tp_descr_get; 0, //descrsetfunc tp_descr_set; 0, //long tp_dictoffset; 0, //initproc tp_init; 0, //allocfunc tp_alloc; 0, //newfunc tp_new; 0, //destructor tp_free; /* Low-level free-memory routine */ 0, //PyObject *tp_bases; 0, //PyObject *tp_mro; /* method resolution order */ 0 //PyObject *tp_defined; d271 1 a271 2 //printf("string returned %s\n", res); PyObject* ret = Py_BuildValue("s", res); d273 2 a274 2 Py_INCREF(ret); return ret; d278 1 a278 1 pari_pari(PyObject *self, PyObject *args) { d282 42 a323 2 GEN g = gp_read_str(s); d327 4 a330 2 {"eval", pari_eval, METH_VARARGS, "Evaluates expression in pari and returns a string"}, {"pari", pari_pari, METH_VARARGS, "Evaluates expression in pari and returns an object"}, d338 3 d342 10 a351 1 Py_InitModule("pari", pari_methods); @ 1.1 log @*** empty log message *** @ text @d6 83 d113 9 d124 1 a131 4 // freopen("CONIN$","rb",stdin); // reopen stdin handle as console window input // freopen("CONOUT$","wb",stdout); // reopen stout handle as console window output // freopen("CONOUT$","wb",stderr); // reopen stderr handle as console window output // function_that_is_not_used_anywhere(); @
Simpan