Skip to content

Commit 2186a10

Browse files
committed
Safer methods Client.read and Model.read with an empty list
1 parent d6aa541 commit 2186a10

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

CHANGES.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ Changelog
1010
As an alternative, you can specify the ``protocol`` in the configuration
1111
file.
1212

13-
* Change the return value of ``Model.browse()`` method with an empty list.
14-
It returns an empty ``RecordList`` except if some other argument is
15-
provided (e.g. ``all_users = model('res.users').browse([], limit=None)``).
13+
* Change the return value of ``Model.browse()`` method if search domain is
14+
an empty list. It returns an empty ``RecordList`` except if some other
15+
argument is provided (e.g.
16+
``all_users = model('res.users').browse([], limit=None)``).
17+
18+
* Change the return value of ``Client.read()`` and ``Model.read()`` methods
19+
if search domain is an empty list: it returns ``False``.
1620

1721
* Improve error formatting for recent Odoo versions, in interactive mode.
1822

erppeek.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -778,21 +778,20 @@ def execute(self, obj, method, *params, **kwargs):
778778
ordered = single_id = False
779779
if method == 'read':
780780
assert params
781-
if issearchdomain(params[0]):
781+
if not (params[0] and isinstance(params[0], list)):
782+
single_id = True
783+
ids = [params[0]] if params[0] else False
784+
elif issearchdomain(params[0]):
782785
# Combine search+read
783786
search_params = self._searchargs(params[:1], kwargs, context)
784787
ordered = len(search_params) > 3 and search_params[3]
785788
ids = self._execute(obj, 'search', *search_params)
786-
elif isinstance(params[0], list):
789+
else:
787790
ordered = kwargs.pop('order', False) and params[0]
788-
ids = set(params[0])
789-
ids.discard(False)
791+
ids = set(params[0]) - {False}
790792
if not ids and ordered:
791793
return [False] * len(ordered)
792794
ids = sorted(ids)
793-
else:
794-
single_id = True
795-
ids = [params[0]] if params[0] else False
796795
if not ids:
797796
return ids
798797
if len(params) > 1:

tests/test_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,8 @@ def test_read_false(self):
521521
[False, False])
522522
self.assertEqual(read('foo.bar', [False], 'first_name', order=True),
523523
[False])
524+
self.assertEqual(read('foo.bar', [], 'first_name'), False)
525+
self.assertEqual(read('foo.bar', [], 'first_name', order=True), False)
524526

525527
self.assertCalls()
526528

tests/test_model.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,13 @@ def call_read(fields=None):
282282
)
283283
self.assertOutput('')
284284

285+
self.assertEqual(FooBar.read([]), False)
286+
self.assertEqual(FooBar.read([], order='name ASC'), False)
287+
self.assertEqual(FooBar.read([False]), [])
288+
self.assertEqual(FooBar.read([False, False]), [])
289+
self.assertCalls()
290+
self.assertOutput('')
291+
285292
# No longer supported since 1.6
286293
FooBar.read(searchterm)
287294
self.assertCalls(OBJ('foo.bar', 'read', [searchterm], None))

0 commit comments

Comments
 (0)