Skip to content

Commit f4a934d

Browse files
author
Geert Vanderkelen
committed
WL7462: Consolidate Python v2 and v3 code
We consolidate the Python v2 and v3 code into a new location lib/ taking the Python v3 code as base. Overview of files which we rename, remove or move: python3/mysql -> lib/mysql python23/django -> lib/mysql/connector/django python23/fabric -> lib/mysql/connector/fabric python3/examples -> examples/ python2/ -> (removed) tests/py3/test_*.py -> tests/ tests/py2 -> (removed) tests/py3 -> (removed) The catch23 module is added to hold variables and functions to make it easier to keep compatibility with Python v2 and v3. Some code is refactored to resolve some performance issues introduced by making Python v3 code work better with v2. For example the BaseSocket.recv() method is using memoryview for versions of Python v2.7 and greater. We add a new module custom_types which currently only hold the HexLiteral type. We remove the config.py file from the example/ folder. The configuration is now included in each example script. The linting issues are written to pylint_output.txt in the root of the repository.
1 parent 54b1329 commit f4a934d

File tree

108 files changed

+2693
-14446
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+2693
-14446
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ build/
1010
MANIFEST
1111
cpy_server*/
1212
*_output.txt
13-
tests_*.log
13+
tests_*.log
14+
dev*.py
File renamed without changes.

python2/examples/dates.py renamed to examples/dates.py

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# -*- coding: utf-8 -*-
33

44
# MySQL Connector/Python - MySQL driver written in Python.
5-
# Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
5+
# Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved.
66

77
# MySQL Connector/Python is licensed under the terms of the GPLv2
88
# <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
@@ -24,89 +24,100 @@
2424
# along with this program; if not, write to the Free Software
2525
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2626

27-
from __future__ import print_function
28-
29-
import sys, os
30-
from datetime import datetime, tzinfo, timedelta
31-
32-
import mysql.connector
33-
3427
"""
3528
3629
Example using MySQL Connector/Python showing:
3730
* How to get datetime, date and time types
3831
* Shows also invalid dates returned and handled
3932
* Force sql_mode to be not set for the active session
33+
4034
"""
4135

36+
from datetime import datetime
37+
38+
import mysql.connector
39+
40+
4241
# Note that by default MySQL takes invalid timestamps. This is for
4342
# backward compatibility. As of 5.0, use sql modes NO_ZERO_IN_DATE,NO_ZERO_DATE
4443
# to prevent this.
45-
_adate = datetime(1977,6,14,21,10,00)
44+
_adate = datetime(1977, 6, 14, 21, 10, 00)
4645
DATA = [
4746
(_adate.date(), _adate, _adate.time()),
4847
('0000-00-00', '0000-00-00 00:00:00', '00:00:00'),
4948
('1000-00-00', '9999-00-00 00:00:00', '00:00:00'),
50-
]
49+
]
5150

5251
def main(config):
5352
output = []
5453
db = mysql.connector.Connect(**config)
5554
cursor = db.cursor()
56-
55+
5756
tbl = 'myconnpy_dates'
5857

5958
cursor.execute('SET sql_mode = ""')
60-
59+
6160
# Drop table if exists, and create it new
62-
stmt_drop = "DROP TABLE IF EXISTS %s" % (tbl)
61+
stmt_drop = "DROP TABLE IF EXISTS {0}".format(tbl)
6362
cursor.execute(stmt_drop)
64-
65-
stmt_create = """
66-
CREATE TABLE %s (
67-
`id` tinyint(4) NOT NULL AUTO_INCREMENT,
68-
`c1` date DEFAULT NULL,
69-
`c2` datetime NOT NULL,
70-
`c3` time DEFAULT NULL,
71-
`changed` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
72-
PRIMARY KEY (`id`)
73-
)""" % (tbl)
63+
64+
stmt_create = (
65+
"CREATE TABLE {0} ( "
66+
" `id` tinyint(4) NOT NULL AUTO_INCREMENT, "
67+
" `c1` date DEFAULT NULL, "
68+
" `c2` datetime NOT NULL, "
69+
" `c3` time DEFAULT NULL, "
70+
" `changed` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP "
71+
" ON UPDATE CURRENT_TIMESTAMP, "
72+
"PRIMARY KEY (`id`))"
73+
).format(tbl)
7474
cursor.execute(stmt_create)
75-
75+
7676
# not using executemany to handle errors better
77-
stmt_insert = ("INSERT INTO %s (c1,c2,c3) VALUES "
78-
"(%%s,%%s,%%s)" % (tbl))
79-
for d in DATA:
77+
stmt_insert = ("INSERT INTO {0} (c1,c2,c3) VALUES "
78+
"(%s,%s,%s)".format(tbl))
79+
for data in DATA:
8080
try:
81-
cursor.execute(stmt_insert, d)
82-
except (mysql.connector.errors.Error, TypeError) as e:
83-
output.append("Failed inserting %s\nError: %s\n" % (d,e))
81+
cursor.execute(stmt_insert, data)
82+
except (mysql.connector.errors.Error, TypeError) as exc:
83+
output.append("Failed inserting {0}\nError: {1}\n".format(
84+
data, exc))
8485
raise
8586

8687
# Read the names again and print them
87-
stmt_select = "SELECT * FROM %s ORDER BY id" % (tbl)
88+
stmt_select = "SELECT * FROM {0} ORDER BY id".format(tbl)
8889
cursor.execute(stmt_select)
8990

9091
for row in cursor.fetchall():
91-
output.append("%3s | %10s | %19s | %8s |" % (
92-
row[0],
93-
row[1],
94-
row[2],
95-
row[3],
96-
))
97-
92+
output.append("%3s | %10s | %19s | %8s |" % (
93+
row[0],
94+
row[1],
95+
row[2],
96+
row[3],
97+
))
98+
9899
# Cleaning up, dropping the table again
99100
cursor.execute(stmt_drop)
100101

101102
cursor.close()
102103
db.close()
103104
return output
104105

106+
105107
if __name__ == '__main__':
106108
#
107109
# Configure MySQL login and database to use in config.py
108110
#
109-
from config import Config
110-
config = Config.dbinfo().copy()
111+
config = {
112+
'host': 'localhost',
113+
'port': 3306,
114+
'database': 'test',
115+
'user': 'root',
116+
'password': '',
117+
'charset': 'utf8',
118+
'use_unicode': True,
119+
'get_warnings': True,
120+
}
121+
111122
out = main(config)
112123
print('\n'.join(out))

python2/examples/engines.py renamed to examples/engines.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# -*- coding: utf-8 -*-
33

44
# MySQL Connector/Python - MySQL driver written in Python.
5-
# Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
5+
# Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved.
66

77
# MySQL Connector/Python is licensed under the terms of the GPLv2
88
# <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
@@ -24,19 +24,17 @@
2424
# along with this program; if not, write to the Free Software
2525
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2626

27-
from __future__ import print_function
28-
29-
import sys, os
30-
31-
import mysql.connector
32-
3327
"""
3428
3529
Example using MySQL Connector/Python showing:
3630
* that show engines works..
3731
3832
"""
3933

34+
import sys, os
35+
36+
import mysql.connector
37+
4038
def main(config):
4139
output = []
4240
db = mysql.connector.Connect(**config)
@@ -54,10 +52,17 @@ def main(config):
5452
return output
5553

5654
if __name__ == '__main__':
57-
#
58-
# Configure MySQL login and database to use in config.py
59-
#
60-
from config import Config
61-
config = Config.dbinfo().copy()
55+
56+
config = {
57+
'host': 'localhost',
58+
'port': 3306,
59+
'database': 'test',
60+
'user': 'root',
61+
'password': '',
62+
'charset': 'utf8',
63+
'use_unicode': True,
64+
'get_warnings': True,
65+
}
66+
6267
out = main(config)
6368
print('\n'.join(out))

python3/examples/inserts.py renamed to examples/inserts.py

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# -*- coding: utf-8 -*-
33

44
# MySQL Connector/Python - MySQL driver written in Python.
5-
# Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
5+
# Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved.
66

77
# MySQL Connector/Python is licensed under the terms of the GPLv2
88
# <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
@@ -24,66 +24,72 @@
2424
# along with this program; if not, write to the Free Software
2525
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2626

27-
import sys, os
28-
29-
import mysql.connector
30-
3127
"""
32-
3328
Example using MySQL Connector/Python showing:
3429
* dropping and creating a table
3530
* inserting 3 rows using executemany()
3631
* selecting data and showing it
3732
3833
"""
3934

35+
import mysql.connector
36+
37+
4038
def main(config):
4139
output = []
42-
db = mysql.connector.Connect(**config)
43-
cursor = db.cursor()
44-
40+
cnx = mysql.connector.connect(**config)
41+
cur = cnx.cursor()
42+
4543
# Drop table if exists, and create it new
4644
stmt_drop = "DROP TABLE IF EXISTS names"
47-
cursor.execute(stmt_drop)
48-
49-
stmt_create = """
50-
CREATE TABLE names (
51-
id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
52-
name VARCHAR(30) DEFAULT '' NOT NULL,
53-
info TEXT DEFAULT '',
54-
age TINYINT UNSIGNED DEFAULT '30',
55-
PRIMARY KEY (id)
56-
)"""
57-
cursor.execute(stmt_create)
58-
59-
info = "abc"*10000
45+
cur.execute(stmt_drop)
46+
47+
stmt_create = (
48+
"CREATE TABLE names ("
49+
" id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT, "
50+
" name VARCHAR(30) DEFAULT '' NOT NULL, "
51+
" info TEXT DEFAULT '', "
52+
" age TINYINT UNSIGNED DEFAULT '30', "
53+
"PRIMARY KEY (id))"
54+
)
55+
cur.execute(stmt_create)
56+
57+
info = "abc" * 10000
6058

6159
# Insert 3 records
62-
names = ( ('Geert',info), ('Jan',info), ('Michel',info) )
63-
stmt_insert = "INSERT INTO names (name,info) VALUES (%s,%s)"
64-
cursor.executemany(stmt_insert, names)
65-
db.commit()
66-
60+
names = (('Geert', info, 30), ('Jan', info, 31), ('Michel', info, 32))
61+
stmt_insert = "INSERT INTO names (name, info, age) VALUES (%s, %s, %s)"
62+
cur.executemany(stmt_insert, names)
63+
cnx.commit()
64+
6765
# Read the names again and print them
6866
stmt_select = "SELECT id, name, info, age FROM names ORDER BY id"
69-
cursor.execute(stmt_select)
67+
cur.execute(stmt_select)
7068

71-
for row in cursor.fetchall():
69+
for row in cur.fetchall():
7270
output.append("%d | %s | %d\nInfo: %s..\n" % (
73-
row[0], row[1], row[3], row[2][20]))
74-
71+
row[0], row[1], row[3], row[2][:20]))
72+
7573
# Cleaning up, dropping the table again
76-
cursor.execute(stmt_drop)
74+
cur.execute(stmt_drop)
7775

78-
cursor.close()
79-
db.close()
76+
cur.close()
77+
cnx.close()
8078
return output
8179

80+
8281
if __name__ == '__main__':
83-
#
84-
# Configure MySQL login and database to use in config.py
85-
#
86-
import config
87-
config = config.Config.dbinfo().copy()
82+
83+
config = {
84+
'host': 'localhost',
85+
'port': 3306,
86+
'database': 'test',
87+
'user': 'root',
88+
'password': '',
89+
'charset': 'utf8',
90+
'use_unicode': True,
91+
'get_warnings': True,
92+
}
93+
8894
out = main(config)
8995
print('\n'.join(out))

0 commit comments

Comments
 (0)