0 2 0
default
Jean-Francois Pieronne - 1 month ago 2020-01-02 11:58:55
jf.pieronne@laposte.net
Add setEncoding and getEncoding function
2 files changed with 43 insertions and 9 deletions:
↑ Collapse Diff ↑
...
 
@@ -47,6 +47,8 @@ log = getLogger(__name__)
47 47
 
setFlags(M_RETURN_TUPLES | M_SQLCODE_NOT_RETURNED |
48 48
 
              M_USE_CALLBACK | M_ITERATOR_CLOSE_CURSOR | M_RETURN_UNICODE)
49 49
 

	
50
 
setEncoding('iso-8859--1')
51
 

	
50 52
 
def use_mxdatetimes():
51 53
 
    """Set callbacks to return mx.DateTime object"""
52 54
 
    from rdb.mxdatetimes import (DateVMS_or_None, DateTime_or_None,
...
 
@@ -112,6 +112,7 @@ static PyObject* format_TIME_callback  =
112 112
 
static PyObject* format_TIMESTAMP_callback  = NULL;
113 113
 
static PyObject* format_DATEVMS_callback = NULL;
114 114
 
static PyObject* type_scaledint = NULL;
115
 
static char rdb_encoding[256]; 
115 116
 

	
116 117
 
static PyObject *_rdb_RdbError;
117 118
 
static PyObject *_rdb_Warning;
...
 
@@ -1756,6 +1757,34 @@ static PyObject *rdb_getFlags(PyObject *
1756 1757
 
  return PyInt_FromLong(Flags);
1757 1758
 
}
1758 1759
 

	
1760
 
PyDoc_STRVAR(rdb_setEncoding__doc__, 
1761
 
"setFlags(v)  -> int\nSet the encoding and return the previous one.");
1762
 

	
1763
 
static PyObject *rdb_setEncoding(PyObject *self, PyObject *args) {
1764
 
  char* par;
1765
 
  PyObject* prev;
1766
 

	
1767
 
  if (!PyArg_ParseTuple(args, "s:setEncoding", &par))
1768
 
      return NULL;
1769
 

	
1770
 
  if (strlen(par) >= sizeof(rdb_encoding))
1771
 
      return NULL;
1772
 

	
1773
 
  prev =  PyString_FromString(rdb_encoding);
1774
 
  strcpy(rdb_encoding, par);
1775
 
  return prev;
1776
 
}
1777
 

	
1778
 
PyDoc_STRVAR(rdb_getEncoding__doc__, 
1779
 
"getEncoding() -> int\nReturn the current encoding.");
1780
 

	
1781
 
static PyObject *rdb_getEncoding(PyObject *self, PyObject *args) {
1782
 
  if (!PyArg_ParseTuple(args, ":getEncoding"))
1783
 
      return NULL;
1784
 

	
1785
 
  return  PyString_FromString(rdb_encoding);
1786
 
}
1787
 

	
1759 1788
 
PyDoc_STRVAR(rdb_getDatabases__doc__, "");
1760 1789
 
static PyObject *
1761 1790
 
rdb_getDatabases(PyObject *dummy, PyObject *args)
...
 
@@ -2934,7 +2963,7 @@ int get_in_params(sqlda sqlda_in, mem_pt
2934 2963
 
	}
2935 2964
 

	
2936 2965
 
	if (PyUnicode_Check(item)) {
2937
 
	  PyObject *pyinput = PyUnicode_AsEncodedString(item, "iso-8859--1",
2966
 
	  PyObject *pyinput = PyUnicode_AsEncodedString(item, rdb_encoding,
2938 2967
 
							NULL);
2939 2968
 
	  if (pyinput == NULL) {
2940 2969
 
	    Py_XDECREF(item);
...
 
@@ -2984,7 +3013,7 @@ int get_in_params(sqlda sqlda_in, mem_pt
2984 3013
 
      len = sqlda_in->SQLVAR[param].SQLOCTET_LEN;
2985 3014
 

	
2986 3015
 
      if (PyUnicode_Check(item)) {
2987
 
	  PyObject *pyinput = PyUnicode_AsEncodedString(item, "iso-8859--1",
3016
 
	  PyObject *pyinput = PyUnicode_AsEncodedString(item, rdb_encoding,
2988 3017
 
							NULL);
2989 3018
 
	  if (pyinput == NULL) {
2990 3019
 
	    Py_XDECREF(item);
...
 
@@ -3030,7 +3059,7 @@ int get_in_params(sqlda sqlda_in, mem_pt
3030 3059
 
      if (item == Py_None) break;
3031 3060
 

	
3032 3061
 
      if (PyUnicode_Check(item)) {
3033
 
	  PyObject *pyinput = PyUnicode_AsEncodedString(item, "iso-8859--1",
3062
 
	  PyObject *pyinput = PyUnicode_AsEncodedString(item, rdb_encoding,
3034 3063
 
							NULL);
3035 3064
 
	  if (pyinput == NULL) {
3036 3065
 
	    Py_XDECREF(item);
...
 
@@ -3252,7 +3281,7 @@ int get_in_params(sqlda sqlda_in, mem_pt
3252 3281
 
      }
3253 3282
 

	
3254 3283
 
      if (PyUnicode_Check(item)) {
3255
 
	  PyObject *pyinput = PyUnicode_AsEncodedString(item, "iso-8859--1",
3284
 
	  PyObject *pyinput = PyUnicode_AsEncodedString(item, rdb_encoding,
3256 3285
 
							NULL);
3257 3286
 
	  if (pyinput == NULL) {
3258 3287
 
	    Py_XDECREF(item);
...
 
@@ -3326,7 +3355,7 @@ int get_in_params(sqlda sqlda_in, mem_pt
3326 3355
 
	if (item == Py_None) break;
3327 3356
 

	
3328 3357
 
      if (PyUnicode_Check(item)) {
3329
 
	  PyObject *pyinput = PyUnicode_AsEncodedString(item, "iso-8859--1",
3358
 
	  PyObject *pyinput = PyUnicode_AsEncodedString(item, rdb_encoding,
3330 3359
 
							NULL);
3331 3360
 
	  if (pyinput == NULL) {
3332 3361
 
	    Py_XDECREF(item);
...
 
@@ -3457,7 +3486,7 @@ int display_row(sqlda sqlda_out, mem_ptr
3457 3486
 
	    }
3458 3487
 
	    if (RDB_RETURN_UNICODE && PyString_Check(po)) {
3459 3488
 
	        PyObject* po2 = PyUnicode_Decode(PyString_AsString(po),
3460
 
				      PyString_Size(po), "iso-8859--1", NULL);
3489
 
				      PyString_Size(po), rdb_encoding, NULL);
3461 3490
 
	        if (po2 == NULL) {
3462 3491
 
	            Py_DECREF(po);
3463 3492
 
		    PyMem_Free(cptr);
...
 
@@ -3479,7 +3508,7 @@ int display_row(sqlda sqlda_out, mem_ptr
3479 3508
 
	      Py_DECREF(arglist);
3480 3509
 
	    } else {
3481 3510
 
	      if (RDB_RETURN_UNICODE && PyString_Check(po))
3482
 
		  po = PyUnicode_Decode(cptr, len, "iso-8859--1", NULL);
3511
 
		  po = PyUnicode_Decode(cptr, len, rdb_encoding, NULL);
3483 3512
 
	      else
3484 3513
 
	          po = PyString_FromStringAndSize(cptr, len);
3485 3514
 
	    }
...
 
@@ -3490,7 +3519,7 @@ int display_row(sqlda sqlda_out, mem_ptr
3490 3519
 
	    (*sqSetItem)(lst, param, po); 
3491 3520
 
	  } else {
3492 3521
 
	    if (RDB_RETURN_UNICODE)
3493
 
		  po = PyUnicode_Decode(cptr, len, "iso-8859--1", NULL);
3522
 
		  po = PyUnicode_Decode(cptr, len, rdb_encoding, NULL);
3494 3523
 
	    else
3495 3524
 
  	        po = PyString_FromStringAndSize(cptr, len);
3496 3525
 
	    if (po == NULL) {
...
 
@@ -3524,7 +3553,7 @@ int display_row(sqlda sqlda_out, mem_ptr
3524 3553
 
#endif
3525 3554
 
	if (RDB_RETURN_UNICODE)
3526 3555
 
	    po = PyUnicode_Decode(str, sqlda_out->SQLVAR[param].SQLOCTET_LEN,
3527
 
	                          "iso-8859--1", NULL);
3556
 
	                          rdb_encoding, NULL);
3528 3557
 
	else
3529 3558
 
	    po = PyString_FromStringAndSize(str,
3530 3559
 
	                               sqlda_out->SQLVAR[param].SQLOCTET_LEN);
...
 
@@ -3756,6 +3785,8 @@ static PyMethodDef _rdb_methods[] = {
3756 3785
 
   rdb_freeContexts__doc__},
3757 3786
 
  {"getFlags", (PyCFunction)rdb_getFlags, METH_VARARGS, rdb_getFlags__doc__},
3758 3787
 
  {"setFlags", (PyCFunction)rdb_setFlags, METH_VARARGS, rdb_setFlags__doc__},
3788
 
  {"getEncoding", (PyCFunction)rdb_getEncoding, METH_VARARGS, rdb_getEncoding__doc__},
3789
 
  {"setEncoding", (PyCFunction)rdb_setEncoding, METH_VARARGS, rdb_setEncoding__doc__},
3759 3790
 
  {"setDateTimeType", (PyCFunction)rdb_setDateTimeType, METH_VARARGS,
3760 3791
 
   rdb_setDateTimeType__doc__},
3761 3792
 
  {"setScaledIntCallback", (PyCFunction)rdb_setScaledIntCallback, METH_VARARGS,
...
 
@@ -3878,6 +3909,7 @@ void init_rdb() {
3878 3909
 
			      RDB_M_RETURN_UNICODE) < 0)
3879 3910
 
    return;
3880 3911
 

	
3912
 

	
3881 3913
 
#if 0
3882 3914
 
  RdbError = PyErr_NewException("_rdb.error", NULL, d);
3883 3915
 
  Py_INCREF(RdbError);
0 comments (0 inline, 0 general)