Merge lp:~rick-rickspencer3/lazr.restfulclient/add_gaurd_for_None_to_eq_functions into lp:lazr.restfulclient

Proposed by Rick Spencer
Status: Merged
Merge reported by: Leonard Richardson
Merged at revision: not available
Proposed branch: lp:~rick-rickspencer3/lazr.restfulclient/add_gaurd_for_None_to_eq_functions
Merge into: lp:lazr.restfulclient
Diff against target: 28 lines (+11/-0)
1 file modified
src/lazr/restfulclient/resource.py (+11/-0)
To merge this branch: bzr merge lp:~rick-rickspencer3/lazr.restfulclient/add_gaurd_for_None_to_eq_functions
Reviewer Review Type Date Requested Status
Michael Nelson (community) code Approve
Review via email: mp+16043@code.launchpad.net

Commit message

I add a guard condition to return False in the _eq_ functions in the case where the object getting compared is None. This resolves the case where there is an exception due to NoneType not having expected members, see bug 495326.

I did not write any tests, unfortunately. The tests for docs were not very complete, and I couldn't figure out the api from it or how to use test fixtures.

To post a comment you must log in.
Revision history for this message
Rick Spencer (rick-rickspencer3) wrote :

fix for bug 495326

Revision history for this message
Leonard Richardson (leonardr) wrote :

This is a good fix, but I won't land a branch without tests. I'm not sure what you mean by "the tests for docs were not very complete." My best guess is that you found the test fixtures confusing and couldn't locate the doctests.

An example should help you see how the test system works. To test your two changes I added tests to src/lazr/restfulclient/docs/entries.txt (section "Comparing entries") and src/lazr/restfulclient/docs/hosted-files.txt (section "Comparing hosted files"). My diff is below.

Since this branch now contains my code I'm going to get someone else to review it, and then I'll land it.

=== modified file 'src/lazr/restfulclient/NEWS.txt'
--- src/lazr/restfulclient/NEWS.txt 2009-12-15 12:28:41 +0000
+++ src/lazr/restfulclient/NEWS.txt 2009-12-17 14:55:45 +0000
@@ -5,6 +5,7 @@
 Development (XXXX-XX-XX)
 ========================
 - Added a proof-of-concept test for OAuth-signed anonymous access.
+- Fixed comparisons of entries and hosted files with None.

 0.9.10 (2009-10-23)
 ===================

=== modified file 'src/lazr/restfulclient/docs/entries.txt'
--- src/lazr/restfulclient/docs/entries.txt 2009-10-21 12:12:44 +0000
+++ src/lazr/restfulclient/docs/entries.txt 2009-12-17 14:41:24 +0000
@@ -402,6 +402,11 @@
     >>> recipe == another_recipe
     False

+An entry is never equal to None.
+
+ >>> recipe == None
+ False
+
 If one entry represents the current state of the server, and the other
 is out of date or has client-side modifications, they will not be
 considered equal.

=== modified file 'src/lazr/restfulclient/docs/hosted-files.txt'
--- src/lazr/restfulclient/docs/hosted-files.txt 2009-10-21 12:12:44 +0000
+++ src/lazr/restfulclient/docs/hosted-files.txt 2009-12-17 14:51:41 +0000
@@ -89,7 +89,6 @@
 Two hosted file objects are the same if they point to the same
 server-side resource.

-
     >>> cover = service.cookbooks['Everyday Greens'].cover
     >>> cover_2 = service.cookbooks['Everyday Greens'].cover
     >>> cover == cover_2
@@ -99,6 +98,11 @@
     >>> cover == other_cover
     False

+A hosted file is never equal to None.
+
+ >>> cover == None
+ False
+
 Error handling
 --------------

=== modified file 'src/lazr/restfulclient/resource.py'
--- src/lazr/restfulclient/resource.py 2009-10-23 13:22:44 +0000
+++ src/lazr/restfulclient/resource.py 2009-12-17 14:43:27 +0000
@@ -365,6 +365,8 @@
         retrieve or modify the hosted file contents is to open a
         filehandle, which goes direct to the server.
         """
+ if other is None:
+ return False
         return self._wadl_resource.url == other._wadl_resource.url

@@ -600,6 +602,7 @@
         contain the same values.
         """
         return (
+ other is not None and
             self.self_link == other.self_link and
             self.http_etag == other.http_etag and
             self._dirty_attributes == other._dirty_attributes)

Revision history for this message
Michael Nelson (michael.nelson) wrote :

Looks great guys!

At first I was confused about the purpose of the documentation:

> +An entry is never equal to None.
> +
> + >>> recipe == None
> + False

as recipe is obviously not None in the documentation, but then realised this is really showing that we don't fall over when comparing with None right? In which case, it probably belongs in a unit test in src/lazr/restfulclient/tests (or explicitly say here 'An entry can be compared with None without blowing up' ;), but not sure how useful that is as documentation)? But I'll leave that up to you Leonard.

Thanks!

review: Approve (code)
Revision history for this message
Leonard Richardson (leonardr) wrote :

Michael, good catch. I've reworded the doctest to make clear what's being tested and I will land the branch. I don't think a unit test works here because lazr.restfulclient uses doctests for everything that simulates usage of the client.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/lazr/restfulclient/resource.py'
2--- src/lazr/restfulclient/resource.py 2009-10-23 13:22:44 +0000
3+++ src/lazr/restfulclient/resource.py 2009-12-11 17:07:12 +0000
4@@ -365,6 +365,12 @@
5 retrieve or modify the hosted file contents is to open a
6 filehandle, which goes direct to the server.
7 """
8+
9+ #gaurd condition to ensure that other is not None
10+ #by definition, they are not equal if other is None
11+ if other is None:
12+ return False
13+
14 return self._wadl_resource.url == other._wadl_resource.url
15
16
17@@ -599,6 +605,11 @@
18 attributes are the same, and if their dirty attribute dicts
19 contain the same values.
20 """
21+ #gaurd condition to ensure that other is not None
22+ #by definition, they are not equal if other is None
23+ if other is None:
24+ return False
25+
26 return (
27 self.self_link == other.self_link and
28 self.http_etag == other.http_etag and

Subscribers

People subscribed via source and target branches