Thanks to visit codestin.com
Credit goes to github.com

Skip to content

TST: Extract a helper function to test for reference cycles #10891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 22, 2018

Conversation

eric-wieser
Copy link
Member

This also means we can now test that our test is actually able to detect the type of failure we expect

Trying to give myself some tools to debug the failure at https://github.com/numpy/numpy/pull/10882/files#r180813166

@eric-wieser
Copy link
Member Author

eric-wieser commented Apr 12, 2018

An example of this in use:

In [1]: from numpy.testing import assert_no_gc_cycles

In [2]: def make_array_ctype(shape):
   ...:     import ctypes
   ...:     ct = ctypes.c_uint8
   ...:     for i in shape:
   ...:         ct = i * ct
   ...:

In [3]: assert_no_gc_cycles(make_array_ctype, (1,))
AssertionError: Reference cycles were found when calling make_array_ctype: 7 objects were collected, of which 6 are shown below:
  tuple object with id=2822255556536:
    (<class '_ctypes.Array'>,)
  PyCArrayType object with id=2822226500408:
    <class '__main__.c_ubyte_Array_1'>
  getset_descriptor object with id=2822252062256:
    <attribute '__dict__' of 'c_ubyte_Array_1' objects>
  getset_descriptor object with id=2822252062184:
    <attribute '__weakref__' of 'c_ubyte_Array_1' objects>
  tuple object with id=2822243712440:
    (<class '__main__.c_ubyte_Array_1'>,
     <class '_ctypes.Array'>,
     <class '_ctypes._CData'>,
     <class 'object'>)
  StgDict object with id=2822226211928:
    {'__dict__': <attribute '__dict__' of 'c_ubyte_Array_1' objects>,
     '__doc__': None,
     '__module__': '__main__',
     '__weakref__': <attribute '__weakref__' of 'c_ubyte_Array_1' objects>,
     '_length_': 1,
     '_type_': <class 'ctypes.c_ubyte'>}

Not clear to me if this is a ctypes bug, or if it is by design

@pv
Copy link
Member

pv commented Apr 12, 2018 via email

This also means we can now test that our test is actually able to detect the type of failure we expect

Trying to give myself some tools to debug the failure at https://github.com/numpy/numpy/pull/10882/files#r180813166
An example output for the test added in the previous commit is:

    AssertionError: Reference cycles were found when calling make_cycle: 1 objects were collected, of which 1 are shown below:
      list object with id=2279664872136:
        [<Recursion on list with id=2279664872136>,
         <Recursion on list with id=2279664872136>]
@eric-wieser
Copy link
Member Author

@pv: I don't know. My goal here was just to encapsulate the test we're already using, and add some extra diagnostics.

@eric-wieser
Copy link
Member Author

After a rethink: spot-on diagnosis of the problem I had in #10882. This test should indeed be collecting multiple times. I'll add another commit.

@charris
Copy link
Member

charris commented Apr 15, 2018

Python 2.7 is being difficult.

@eric-wieser
Copy link
Member Author

Hmm. Can you think of a reliable test that prevents the garbage collector from ever finishing collecting? If not, should I just change the test to suppress RuntimeError rather than actually test for it?

It's not always possible to guarantee this, so also adds a test to verify that we don't hang
@eric-wieser
Copy link
Member Author

eric-wieser commented Apr 16, 2018

Seems that sometimes the GC doesn't even try to finalize the objects, which avoids the pathological case anyway. I've detected this with a weakref, and just skipped the test if that happens.

This now passes on 2.7 on my machine, and hopefully will do the same on the travis 3.6 failure. (edit: it does)

@eric-wieser
Copy link
Member Author

Anything else needed here? Would assert_creates_no_cyclic_isolates be better, which uses the same GC terminology as the python docs?

@charris
Copy link
Member

charris commented Apr 21, 2018

LGTM, @pv any comments? Doesn't exactly decrease the amount of code, but is worth having something debugged and expected to work.

@charris charris merged commit b5c1bcf into numpy:master Apr 22, 2018
@charris
Copy link
Member

charris commented Apr 22, 2018

Thanks Eric.

@charris charris added the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label Apr 25, 2018
@charris
Copy link
Member

charris commented Apr 25, 2018

Hmm, needs a release note as it adds a new function to numpy/testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
05 - Testing 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes component: numpy.testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants