4
4
want to make your own subclasses. In most cases, you will probably
5
5
override Connection.default_cursor with a non-standard Cursor class.
6
6
"""
7
+ import re
8
+ import sys
9
+
7
10
from MySQLdb import cursors
8
11
from MySQLdb .compat import unicode , PY2
9
- from _mysql_exceptions import Warning , Error , InterfaceError , DataError , \
10
- DatabaseError , OperationalError , IntegrityError , InternalError , \
11
- NotSupportedError , ProgrammingError
12
+ from _mysql_exceptions import (
13
+ Warning , Error , InterfaceError , DataError ,
14
+ DatabaseError , OperationalError , IntegrityError , InternalError ,
15
+ NotSupportedError , ProgrammingError ,
16
+ )
12
17
import _mysql
13
- import re
14
18
15
19
16
20
if not PY2 :
17
- # See http://bugs.python.org/issue24870
18
- _surrogateescape_table = [chr (i ) if i < 0x80 else chr (i + 0xdc00 ) for i in range (256 )]
21
+ if sys .version_info [:2 ] < (3 , 6 ):
22
+ # See http://bugs.python.org/issue24870
23
+ _surrogateescape_table = [chr (i ) if i < 0x80 else chr (i + 0xdc00 ) for i in range (256 )]
19
24
20
- def _fast_surroundescape (s ):
21
- return s .decode ('latin1' ).translate (_surrogateescape_table )
25
+ def _fast_surrogateescape (s ):
26
+ return s .decode ('latin1' ).translate (_surrogateescape_table )
27
+ else :
28
+ def _fast_surrogateescape (s ):
29
+ return s .decode ('ascii' , 'surrogateescape' )
22
30
23
31
24
32
def defaulterrorhandler (connection , cursor , errorclass , errorvalue ):
@@ -312,7 +320,7 @@ def literal(self, o):
312
320
# bytes to unicode and back again.
313
321
# See http://python.org/dev/peps/pep-0383/
314
322
if not PY2 and isinstance (s , (bytes , bytearray )):
315
- return _fast_surroundescape (s )
323
+ return _fast_surrogateescape (s )
316
324
return s
317
325
318
326
def begin (self ):
0 commit comments