@@ -69,9 +69,14 @@ typedef struct {
69
69
PyObject * converter ;
70
70
} _mysql_ConnectionObject ;
71
71
72
- #define check_connection (c ) if (!(c->open)) return _mysql_Exception(c)
72
+ #define check_connection (c , func ) \
73
+ if (!(c->open)) { \
74
+ PyErr_SetString(_mysql_ProgrammingError, func "() is called for closed connection"); \
75
+ return NULL; \
76
+ };
77
+
73
78
#define result_connection (r ) ((_mysql_ConnectionObject *)r->conn)
74
- #define check_result_connection (r ) check_connection(result_connection(r))
79
+ #define check_result_connection (r , func ) check_connection(result_connection(r), func )
75
80
76
81
extern PyTypeObject _mysql_ConnectionObject_Type ;
77
82
@@ -750,6 +755,7 @@ static PyObject *
750
755
_mysql_ConnectionObject_fileno (
751
756
_mysql_ConnectionObject * self )
752
757
{
758
+ check_connection (self , "fileno" );
753
759
return PyInt_FromLong (self -> connection .net .fd );
754
760
}
755
761
@@ -761,16 +767,11 @@ _mysql_ConnectionObject_close(
761
767
_mysql_ConnectionObject * self ,
762
768
PyObject * noargs )
763
769
{
764
- if (self -> open ) {
765
- Py_BEGIN_ALLOW_THREADS
766
- mysql_close (& (self -> connection ));
767
- Py_END_ALLOW_THREADS
768
- self -> open = 0 ;
769
- } else {
770
- PyErr_SetString (_mysql_ProgrammingError ,
771
- "closing a closed connection" );
772
- return NULL ;
773
- }
770
+ check_connection (self , "close" );
771
+ Py_BEGIN_ALLOW_THREADS
772
+ mysql_close (& (self -> connection ));
773
+ Py_END_ALLOW_THREADS
774
+ self -> open = 0 ;
774
775
_mysql_ConnectionObject_clear (self );
775
776
Py_RETURN_NONE ;
776
777
}
@@ -786,7 +787,7 @@ _mysql_ConnectionObject_affected_rows(
786
787
PyObject * noargs )
787
788
{
788
789
my_ulonglong ret ;
789
- check_connection (self );
790
+ check_connection (self , "affected_rows" );
790
791
ret = mysql_affected_rows (& (self -> connection ));
791
792
if (ret == (my_ulonglong )- 1 )
792
793
return PyInt_FromLong (-1 );
@@ -823,7 +824,7 @@ _mysql_ConnectionObject_dump_debug_info(
823
824
PyObject * noargs )
824
825
{
825
826
int err ;
826
- check_connection (self );
827
+ check_connection (self , "dump_debug_info" );
827
828
Py_BEGIN_ALLOW_THREADS
828
829
err = mysql_dump_debug_info (& (self -> connection ));
829
830
Py_END_ALLOW_THREADS
@@ -842,6 +843,7 @@ _mysql_ConnectionObject_autocommit(
842
843
{
843
844
int flag , err ;
844
845
if (!PyArg_ParseTuple (args , "i" , & flag )) return NULL ;
846
+ check_connection (self , "autocommit" );
845
847
Py_BEGIN_ALLOW_THREADS
846
848
err = mysql_autocommit (& (self -> connection ), flag );
847
849
Py_END_ALLOW_THREADS
@@ -858,6 +860,7 @@ _mysql_ConnectionObject_get_autocommit(
858
860
_mysql_ConnectionObject * self ,
859
861
PyObject * args )
860
862
{
863
+ check_connection (self , "get_autocommit" );
861
864
if (self -> connection .server_status & SERVER_STATUS_AUTOCOMMIT ) {
862
865
Py_RETURN_TRUE ;
863
866
}
@@ -873,6 +876,7 @@ _mysql_ConnectionObject_commit(
873
876
PyObject * noargs )
874
877
{
875
878
int err ;
879
+ check_connection (self , "commit" );
876
880
Py_BEGIN_ALLOW_THREADS
877
881
err = mysql_commit (& (self -> connection ));
878
882
Py_END_ALLOW_THREADS
@@ -890,13 +894,13 @@ _mysql_ConnectionObject_rollback(
890
894
PyObject * noargs )
891
895
{
892
896
int err ;
897
+ check_connection (self , "rollback" );
893
898
Py_BEGIN_ALLOW_THREADS
894
899
err = mysql_rollback (& (self -> connection ));
895
900
Py_END_ALLOW_THREADS
896
901
if (err ) return _mysql_Exception (self );
897
- Py_INCREF (Py_None );
898
- return Py_None ;
899
- }
902
+ Py_RETURN_NONE ;
903
+ }
900
904
901
905
static char _mysql_ConnectionObject_next_result__doc__ [] =
902
906
"If more query results exist, next_result() reads the next query\n\
@@ -917,6 +921,7 @@ _mysql_ConnectionObject_next_result(
917
921
PyObject * noargs )
918
922
{
919
923
int err ;
924
+ check_connection (self , "next_result" );
920
925
Py_BEGIN_ALLOW_THREADS
921
926
err = mysql_next_result (& (self -> connection ));
922
927
Py_END_ALLOW_THREADS
@@ -939,6 +944,7 @@ _mysql_ConnectionObject_set_server_option(
939
944
int err , flags = 0 ;
940
945
if (!PyArg_ParseTuple (args , "i" , & flags ))
941
946
return NULL ;
947
+ check_connection (self , "set_server_option" );
942
948
Py_BEGIN_ALLOW_THREADS
943
949
err = mysql_set_server_option (& (self -> connection ), flags );
944
950
Py_END_ALLOW_THREADS
@@ -963,6 +969,7 @@ _mysql_ConnectionObject_sqlstate(
963
969
_mysql_ConnectionObject * self ,
964
970
PyObject * noargs )
965
971
{
972
+ check_connection (self , "sqlstate" );
966
973
return PyString_FromString (mysql_sqlstate (& (self -> connection )));
967
974
}
968
975
@@ -977,6 +984,7 @@ _mysql_ConnectionObject_warning_count(
977
984
_mysql_ConnectionObject * self ,
978
985
PyObject * noargs )
979
986
{
987
+ check_connection (self , "warning_count" );
980
988
return PyInt_FromLong (mysql_warning_count (& (self -> connection )));
981
989
}
982
990
@@ -991,7 +999,7 @@ _mysql_ConnectionObject_errno(
991
999
_mysql_ConnectionObject * self ,
992
1000
PyObject * noargs )
993
1001
{
994
- check_connection (self );
1002
+ check_connection (self , "errno" );
995
1003
return PyInt_FromLong ((long )mysql_errno (& (self -> connection )));
996
1004
}
997
1005
@@ -1006,7 +1014,7 @@ _mysql_ConnectionObject_error(
1006
1014
_mysql_ConnectionObject * self ,
1007
1015
PyObject * noargs )
1008
1016
{
1009
- check_connection (self );
1017
+ check_connection (self , "error" );
1010
1018
return PyString_FromString (mysql_error (& (self -> connection )));
1011
1019
}
1012
1020
@@ -1249,7 +1257,7 @@ _mysql_ResultObject_describe(
1249
1257
PyObject * d ;
1250
1258
MYSQL_FIELD * fields ;
1251
1259
unsigned int i , n ;
1252
- check_result_connection (self );
1260
+ check_result_connection (self , "describe" );
1253
1261
n = mysql_num_fields (self -> result );
1254
1262
fields = mysql_fetch_fields (self -> result );
1255
1263
if (!(d = PyTuple_New (n ))) return NULL ;
@@ -1284,7 +1292,7 @@ _mysql_ResultObject_field_flags(
1284
1292
PyObject * d ;
1285
1293
MYSQL_FIELD * fields ;
1286
1294
unsigned int i , n ;
1287
- check_result_connection (self );
1295
+ check_result_connection (self , "field_flags" );
1288
1296
n = mysql_num_fields (self -> result );
1289
1297
fields = mysql_fetch_fields (self -> result );
1290
1298
if (!(d = PyTuple_New (n ))) return NULL ;
@@ -1523,7 +1531,7 @@ _mysql_ResultObject_fetch_row(
1523
1531
if (!PyArg_ParseTupleAndKeywords (args , kwargs , "|ii:fetch_row" , kwlist ,
1524
1532
& maxrows , & how ))
1525
1533
return NULL ;
1526
- check_result_connection (self );
1534
+ check_result_connection (self , "fetch_row" );
1527
1535
if (how >= (int )sizeof (row_converters )) {
1528
1536
PyErr_SetString (PyExc_ValueError , "how out of range" );
1529
1537
return NULL ;
@@ -1592,7 +1600,7 @@ _mysql_ConnectionObject_change_user(
1592
1600
if (!PyArg_ParseTupleAndKeywords (args , kwargs , "s|ss:change_user" ,
1593
1601
kwlist , & user , & pwd , & db ))
1594
1602
return NULL ;
1595
- check_connection (self );
1603
+ check_connection (self , "change_user" );
1596
1604
Py_BEGIN_ALLOW_THREADS
1597
1605
r = mysql_change_user (& (self -> connection ), user , pwd , db );
1598
1606
Py_END_ALLOW_THREADS
@@ -1612,7 +1620,7 @@ _mysql_ConnectionObject_character_set_name(
1612
1620
PyObject * noargs )
1613
1621
{
1614
1622
const char * s ;
1615
- check_connection (self );
1623
+ check_connection (self , "character_set_name" );
1616
1624
s = mysql_character_set_name (& (self -> connection ));
1617
1625
return PyString_FromString (s );
1618
1626
}
@@ -1630,7 +1638,7 @@ _mysql_ConnectionObject_set_character_set(
1630
1638
const char * s ;
1631
1639
int err ;
1632
1640
if (!PyArg_ParseTuple (args , "s" , & s )) return NULL ;
1633
- check_connection (self );
1641
+ check_connection (self , "set_character_set" );
1634
1642
Py_BEGIN_ALLOW_THREADS
1635
1643
err = mysql_set_character_set (& (self -> connection ), s );
1636
1644
Py_END_ALLOW_THREADS
@@ -1669,7 +1677,7 @@ _mysql_ConnectionObject_get_character_set_info(
1669
1677
PyObject * result ;
1670
1678
MY_CHARSET_INFO cs ;
1671
1679
1672
- check_connection (self );
1680
+ check_connection (self , "get_character_set_info" );
1673
1681
mysql_get_character_set_info (& (self -> connection ), & cs );
1674
1682
if (!(result = PyDict_New ())) return NULL ;
1675
1683
if (cs .csname )
@@ -1701,7 +1709,7 @@ _mysql_ConnectionObject_get_native_connection(
1701
1709
PyObject * noargs )
1702
1710
{
1703
1711
PyObject * result ;
1704
- check_connection (self );
1712
+ check_connection (self , "_get_native_connection" );
1705
1713
result = PyCapsule_New (& (self -> connection ),
1706
1714
"_mysql.connection.native_connection" , NULL );
1707
1715
return result ;
@@ -1730,7 +1738,7 @@ _mysql_ConnectionObject_get_host_info(
1730
1738
_mysql_ConnectionObject * self ,
1731
1739
PyObject * noargs )
1732
1740
{
1733
- check_connection (self );
1741
+ check_connection (self , "get_host_info" );
1734
1742
return PyString_FromString (mysql_get_host_info (& (self -> connection )));
1735
1743
}
1736
1744
@@ -1744,7 +1752,7 @@ _mysql_ConnectionObject_get_proto_info(
1744
1752
_mysql_ConnectionObject * self ,
1745
1753
PyObject * noargs )
1746
1754
{
1747
- check_connection (self );
1755
+ check_connection (self , "get_proto_info" );
1748
1756
return PyInt_FromLong ((long )mysql_get_proto_info (& (self -> connection )));
1749
1757
}
1750
1758
@@ -1758,7 +1766,7 @@ _mysql_ConnectionObject_get_server_info(
1758
1766
_mysql_ConnectionObject * self ,
1759
1767
PyObject * noargs )
1760
1768
{
1761
- check_connection (self );
1769
+ check_connection (self , "get_server_info" );
1762
1770
return PyString_FromString (mysql_get_server_info (& (self -> connection )));
1763
1771
}
1764
1772
@@ -1774,7 +1782,7 @@ _mysql_ConnectionObject_info(
1774
1782
PyObject * noargs )
1775
1783
{
1776
1784
const char * s ;
1777
- check_connection (self );
1785
+ check_connection (self , "info" );
1778
1786
s = mysql_info (& (self -> connection ));
1779
1787
if (s ) return PyString_FromString (s );
1780
1788
Py_INCREF (Py_None );
@@ -1808,7 +1816,7 @@ _mysql_ConnectionObject_insert_id(
1808
1816
PyObject * noargs )
1809
1817
{
1810
1818
my_ulonglong r ;
1811
- check_connection (self );
1819
+ check_connection (self , "insert_id" );
1812
1820
Py_BEGIN_ALLOW_THREADS
1813
1821
r = mysql_insert_id (& (self -> connection ));
1814
1822
Py_END_ALLOW_THREADS
@@ -1827,7 +1835,7 @@ _mysql_ConnectionObject_kill(
1827
1835
unsigned long pid ;
1828
1836
int r ;
1829
1837
if (!PyArg_ParseTuple (args , "k:kill" , & pid )) return NULL ;
1830
- check_connection (self );
1838
+ check_connection (self , "kill" );
1831
1839
Py_BEGIN_ALLOW_THREADS
1832
1840
r = mysql_kill (& (self -> connection ), pid );
1833
1841
Py_END_ALLOW_THREADS
@@ -1847,7 +1855,7 @@ _mysql_ConnectionObject_field_count(
1847
1855
_mysql_ConnectionObject * self ,
1848
1856
PyObject * noargs )
1849
1857
{
1850
- check_connection (self );
1858
+ check_connection (self , "field_count" );
1851
1859
return PyInt_FromLong ((long )mysql_field_count (& (self -> connection )));
1852
1860
}
1853
1861
@@ -1859,7 +1867,7 @@ _mysql_ResultObject_num_fields(
1859
1867
_mysql_ResultObject * self ,
1860
1868
PyObject * noargs )
1861
1869
{
1862
- check_result_connection (self );
1870
+ check_result_connection (self , "num_fields" );
1863
1871
return PyInt_FromLong ((long )mysql_num_fields (self -> result ));
1864
1872
}
1865
1873
@@ -1874,7 +1882,7 @@ _mysql_ResultObject_num_rows(
1874
1882
_mysql_ResultObject * self ,
1875
1883
PyObject * noargs )
1876
1884
{
1877
- check_result_connection (self );
1885
+ check_result_connection (self , "num_rows" );
1878
1886
return PyLong_FromUnsignedLongLong (mysql_num_rows (self -> result ));
1879
1887
}
1880
1888
@@ -1904,7 +1912,7 @@ _mysql_ConnectionObject_ping(
1904
1912
{
1905
1913
int r , reconnect = -1 ;
1906
1914
if (!PyArg_ParseTuple (args , "|I" , & reconnect )) return NULL ;
1907
- check_connection (self );
1915
+ check_connection (self , "ping" );
1908
1916
if (reconnect != -1 ) {
1909
1917
my_bool recon = (my_bool )reconnect ;
1910
1918
mysql_options (& self -> connection , MYSQL_OPT_RECONNECT , & recon );
@@ -1931,7 +1939,7 @@ _mysql_ConnectionObject_query(
1931
1939
char * query ;
1932
1940
int len , r ;
1933
1941
if (!PyArg_ParseTuple (args , "s#:query" , & query , & len )) return NULL ;
1934
- check_connection (self );
1942
+ check_connection (self , "query" );
1935
1943
1936
1944
Py_BEGIN_ALLOW_THREADS
1937
1945
r = mysql_real_query (& (self -> connection ), query , len );
@@ -1955,7 +1963,7 @@ _mysql_ConnectionObject_send_query(
1955
1963
int len , r ;
1956
1964
MYSQL * mysql = & (self -> connection );
1957
1965
if (!PyArg_ParseTuple (args , "s#:query" , & query , & len )) return NULL ;
1958
- check_connection (self );
1966
+ check_connection (self , "send_query" );
1959
1967
1960
1968
Py_BEGIN_ALLOW_THREADS
1961
1969
r = mysql_send_query (mysql , query , len );
@@ -1976,7 +1984,7 @@ _mysql_ConnectionObject_read_query_result(
1976
1984
{
1977
1985
int r ;
1978
1986
MYSQL * mysql = & (self -> connection );
1979
- check_connection (self );
1987
+ check_connection (self , "reqd_query_result" );
1980
1988
1981
1989
Py_BEGIN_ALLOW_THREADS
1982
1990
r = (int )mysql_read_query_result (mysql );
@@ -2006,7 +2014,7 @@ _mysql_ConnectionObject_select_db(
2006
2014
char * db ;
2007
2015
int r ;
2008
2016
if (!PyArg_ParseTuple (args , "s:select_db" , & db )) return NULL ;
2009
- check_connection (self );
2017
+ check_connection (self , "select_db" );
2010
2018
Py_BEGIN_ALLOW_THREADS
2011
2019
r = mysql_select_db (& (self -> connection ), db );
2012
2020
Py_END_ALLOW_THREADS
@@ -2026,7 +2034,7 @@ _mysql_ConnectionObject_shutdown(
2026
2034
PyObject * noargs )
2027
2035
{
2028
2036
int r ;
2029
- check_connection (self );
2037
+ check_connection (self , "shutdown" );
2030
2038
Py_BEGIN_ALLOW_THREADS
2031
2039
r = mysql_shutdown (& (self -> connection ), SHUTDOWN_DEFAULT );
2032
2040
Py_END_ALLOW_THREADS
@@ -2048,7 +2056,7 @@ _mysql_ConnectionObject_stat(
2048
2056
PyObject * noargs )
2049
2057
{
2050
2058
const char * s ;
2051
- check_connection (self );
2059
+ check_connection (self , "stat" );
2052
2060
Py_BEGIN_ALLOW_THREADS
2053
2061
s = mysql_stat (& (self -> connection ));
2054
2062
Py_END_ALLOW_THREADS
@@ -2070,7 +2078,7 @@ _mysql_ConnectionObject_store_result(
2070
2078
PyObject * arglist = NULL , * kwarglist = NULL , * result = NULL ;
2071
2079
_mysql_ResultObject * r = NULL ;
2072
2080
2073
- check_connection (self );
2081
+ check_connection (self , " store_result " );
2074
2082
arglist = Py_BuildValue ("(OiO )", self , 0 , self -> converter );
2075
2083
if (!arglist ) goto error ;
2076
2084
kwarglist = PyDict_New ();
@@ -2108,7 +2116,7 @@ _mysql_ConnectionObject_thread_id(
2108
2116
PyObject * noargs )
2109
2117
{
2110
2118
unsigned long pid ;
2111
- check_connection (self );
2119
+ check_connection (self , "thread_id" );
2112
2120
Py_BEGIN_ALLOW_THREADS
2113
2121
pid = mysql_thread_id (& (self -> connection ));
2114
2122
Py_END_ALLOW_THREADS
@@ -2129,7 +2137,7 @@ _mysql_ConnectionObject_use_result(
2129
2137
PyObject * arglist = NULL , * kwarglist = NULL , * result = NULL ;
2130
2138
_mysql_ResultObject * r = NULL ;
2131
2139
2132
- check_connection (self );
2140
+ check_connection (self , "use_result" );
2133
2141
arglist = Py_BuildValue ("(OiO)" , self , 1 , self -> converter );
2134
2142
if (!arglist ) return NULL ;
2135
2143
kwarglist = PyDict_New ();
@@ -2187,7 +2195,7 @@ _mysql_ResultObject_data_seek(
2187
2195
{
2188
2196
unsigned int row ;
2189
2197
if (!PyArg_ParseTuple (args , "i:data_seek" , & row )) return NULL ;
2190
- check_result_connection (self );
2198
+ check_result_connection (self , "data_seek" );
2191
2199
mysql_data_seek (self -> result , row );
2192
2200
Py_INCREF (Py_None );
2193
2201
return Py_None ;
0 commit comments