Skip to content
This repository was archived by the owner on Jan 19, 2024. It is now read-only.

Commit e6118a9

Browse files
swailsChristopher Anderson
authored andcommitted
Fix context manager exit method in sample (#159)
* Fix context manager exit method in sample Assigning `self = None` does nothing -- it rebinds `self` to `None` and then goes out of scope, leaving the reference to the client intact. * Fix the rest of the examples with respect to cleanup * This fixes a number of minor issues * You can simply "raise" when inside an except block to re-raise the same exception. That way you get the original traceback. * This fixes Python 3 support by removing stuff allowed in Python 2 but forbidden in Python 3 (inconsistent indentation and e.message -> e) * Fixes what would be a NameError
1 parent 4c4c346 commit e6118a9

4 files changed

Lines changed: 29 additions & 29 deletions

File tree

samples/ChangeFeedManagement/Program.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def __enter__(self):
3737

3838
def __exit__(self, exception_type, exception_val, trace):
3939
# extra cleanup in here
40-
self = None
40+
self.obj = None
4141

4242
class ChangeFeedManagement:
4343

@@ -98,11 +98,11 @@ def run_sample():
9898
if e.status_code == 409:
9999
pass
100100
else:
101-
raise errors.HTTPFailure(e.status_code)
101+
raise
102102

103103
# setup collection for this sample
104104

105-
collection_definition = { 'id': CONTAINER_ID,
105+
collection_definition = { 'id': COLLECTION_ID,
106106
'partitionKey':
107107
{
108108
'paths': ['/address/state'],
@@ -127,7 +127,7 @@ def run_sample():
127127
ChangeFeedManagement.ReadFeedForTime(client, time)
128128

129129
except errors.HTTPFailure as e:
130-
print('\nrun_sample has caught an error. {0}'.format(e.message))
130+
print('\nrun_sample has caught an error. {0}'.format(e))
131131

132132
finally:
133133
print("\nrun_sample done")
@@ -137,4 +137,4 @@ def run_sample():
137137
run_sample()
138138

139139
except Exception as e:
140-
print("Top level Error: args:{0}, message:N/A".format(e.args))
140+
print("Top level Error: args:{0}, message:N/A".format(e.args))

samples/CollectionManagement/Program.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def __enter__(self):
6161

6262
def __exit__(self, exception_type, exception_val, trace):
6363
# extra cleanup in here
64-
self = None
64+
self.obj = None
6565

6666
class CollectionManagement:
6767
@staticmethod
@@ -98,7 +98,7 @@ def create_Container(client, id):
9898
if e.status_code == 409:
9999
print('A collection with id \'{0}\' already exists'.format(id))
100100
else:
101-
raise errors.HTTPFailure(e.status_code)
101+
raise
102102

103103
print("\n2.2 Create Collection - With custom index policy")
104104

@@ -120,7 +120,7 @@ def create_Container(client, id):
120120
if e.status_code == 409:
121121
print('A collection with id \'{0}\' already exists'.format(collection['id']))
122122
else:
123-
raise errors.HTTPFailure(e.status_code)
123+
raise
124124

125125
print("\n2.3 Create Collection - With custom offer throughput")
126126

@@ -134,7 +134,7 @@ def create_Container(client, id):
134134
if e.status_code == 409:
135135
print('A collection with id \'{0}\' already exists'.format(collection['id']))
136136
else:
137-
raise errors.HTTPFailure(e.status_code)
137+
raise
138138

139139
print("\n2.4 Create Collection - With Unique keys")
140140

@@ -150,7 +150,7 @@ def create_Container(client, id):
150150
if e.status_code == 409:
151151
print('A collection with id \'{0}\' already exists'.format(collection['id']))
152152
else:
153-
raise errors.HTTPFailure(e.status_code)
153+
raise
154154

155155
print("\n2.5 Create Collection - With Partition key")
156156

@@ -173,7 +173,7 @@ def create_Container(client, id):
173173
if e.status_code == 409:
174174
print('A collection with id \'{0}\' already exists'.format(collection['id']))
175175
else:
176-
raise errors.HTTPFailure(e.status_code)
176+
raise
177177

178178
print("\n2.6 Create Collection - With Partition key V2")
179179

@@ -197,7 +197,7 @@ def create_Container(client, id):
197197
if e.status_code == 409:
198198
print('A collection with id \'{0}\' already exists'.format(collection['id']))
199199
else:
200-
raise errors.HTTPFailure(e.status_code)
200+
raise
201201

202202
@staticmethod
203203
def manage_offer_throughput(client, id):
@@ -222,7 +222,7 @@ def manage_offer_throughput(client, id):
222222
if e.status_code == 404:
223223
print('A collection with id \'{0}\' does not exist'.format(id))
224224
else:
225-
raise errors.HTTPFailure(e.status_code)
225+
raise
226226

227227
print("\n3.2 Change Offer Throughput of Collection")
228228

@@ -254,7 +254,7 @@ def read_Container(client, id):
254254
if e.status_code == 404:
255255
print('A collection with id \'{0}\' does not exist'.format(id))
256256
else:
257-
raise errors.HTTPFailure(e.status_code)
257+
raise
258258

259259
@staticmethod
260260
def list_Containers(client):
@@ -284,7 +284,7 @@ def delete_Container(client, id):
284284
if e.status_code == 404:
285285
print('A collection with id \'{0}\' does not exist'.format(id))
286286
else:
287-
raise errors.HTTPFailure(e.status_code)
287+
raise
288288

289289
def run_sample():
290290

@@ -298,7 +298,7 @@ def run_sample():
298298
if e.status_code == 409:
299299
pass
300300
else:
301-
raise errors.HTTPFailure(e.status_code)
301+
raise
302302

303303
# query for a collection
304304
CollectionManagement.find_Container(client, COLLECTION_ID)
@@ -329,7 +329,7 @@ def run_sample():
329329
raise errors.HTTPFailure(e.status_code)
330330

331331
except errors.HTTPFailure as e:
332-
print('\nrun_sample has caught an error. {0}'.format(e.message))
332+
print('\nrun_sample has caught an error. {0}'.format(e))
333333

334334
finally:
335335
print("\nrun_sample done")
@@ -339,4 +339,4 @@ def run_sample():
339339
run_sample()
340340

341341
except Exception as e:
342-
print("Top level Error: args:{0}, message:{1}".format(e.args,e.message))
342+
print("Top level Error: args:{0}, message:{1}".format(e.args,e))

samples/DatabaseManagement/Program.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def __enter__(self):
4242

4343
def __exit__(self, exception_type, exception_val, trace):
4444
# extra cleanup in here
45-
self = None
45+
self.obj = None
4646

4747
class DatabaseManagement:
4848
@staticmethod
@@ -73,7 +73,7 @@ def create_database(client, id):
7373
if e.status_code == 409:
7474
print('A database with id \'{0}\' already exists'.format(id))
7575
else:
76-
raise errors.HTTPFailure(e.status_code)
76+
raise
7777

7878
@staticmethod
7979
def read_database(client, id):
@@ -93,7 +93,7 @@ def read_database(client, id):
9393
if e.status_code == 404:
9494
print('A database with id \'{0}\' does not exist'.format(id))
9595
else:
96-
raise errors.HTTPFailure(e.status_code)
96+
raise
9797

9898
@staticmethod
9999
def list_databases(client):
@@ -123,7 +123,7 @@ def delete_database(client, id):
123123
if e.status_code == 404:
124124
print('A database with id \'{0}\' does not exist'.format(id))
125125
else:
126-
raise errors.HTTPFailure(e.status_code)
126+
raise
127127

128128
def run_sample():
129129
with IDisposable(cosmos_client.CosmosClient(HOST, {'masterKey': MASTER_KEY} )) as client:
@@ -144,7 +144,7 @@ def run_sample():
144144
DatabaseManagement.delete_database(client, DATABASE_ID)
145145

146146
except errors.HTTPFailure as e:
147-
print('\nrun_sample has caught an error. {0}'.format(e.message))
147+
print('\nrun_sample has caught an error. {0}'.format(e))
148148

149149
finally:
150150
print("\nrun_sample done")
@@ -154,4 +154,4 @@ def run_sample():
154154
run_sample()
155155

156156
except Exception as e:
157-
print("Top level Error: args:{0}, message:{1}".format(e.args,e.message))
157+
print("Top level Error: args:{0}, message:{1}".format(e.args,e))

samples/DocumentManagement/Program.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def __enter__(self):
4747

4848
def __exit__(self, exception_type, exception_val, trace):
4949
# extra cleanup in here
50-
self = None
50+
self.obj = None
5151

5252
class DocumentManagement:
5353

@@ -153,7 +153,7 @@ def run_sample():
153153
if e.status_code == 409:
154154
pass
155155
else:
156-
raise errors.HTTPFailure(e.status_code)
156+
raise
157157

158158
# setup collection for this sample
159159
try:
@@ -164,14 +164,14 @@ def run_sample():
164164
if e.status_code == 409:
165165
print('Collection with id \'{0}\' was found'.format(COLLECTION_ID))
166166
else:
167-
raise errors.HTTPFailure(e.status_code)
167+
raise
168168

169169
DocumentManagement.CreateDocuments(client)
170170
DocumentManagement.ReadDocument(client,'SalesOrder1')
171171
DocumentManagement.ReadDocuments(client)
172172

173173
except errors.HTTPFailure as e:
174-
print('\nrun_sample has caught an error. {0}'.format(e.message))
174+
print('\nrun_sample has caught an error. {0}'.format(e))
175175

176176
finally:
177177
print("\nrun_sample done")
@@ -181,4 +181,4 @@ def run_sample():
181181
run_sample()
182182

183183
except Exception as e:
184-
print("Top level Error: args:{0}, message:N/A".format(e.args))
184+
print("Top level Error: args:{0}, message:N/A".format(e.args))

0 commit comments

Comments
 (0)