[cctbxbb] Unexpected (to me) behavior of flex.bool

Oleg Dolomanov oleg_dolomanov at hotmail.com
Fri Jun 16 23:25:21 PDT 2023


I think this always has been like this - all of the arrays (tiny, plain, shared) have constructures taking size and size + initialiser...

Since bool and int (False=0, True=anything but 0) are interconvertible in CPP - you may get a warning at compilation time but not much more. So, expect

flex.bool(2, 0) to create [False, False]
identical to the default initialiser:
flex.bool(2)

Cheers,
Oleg.
________________________________
From: cctbxbb-bounces at phenix-online.org <cctbxbb-bounces at phenix-online.org> on behalf of Pavel Afonine <pafonine at lbl.gov>
Sent: 17 June 2023 04:42
To: cctbx mailing list <cctbxbb at phenix-online.org>
Subject: [cctbxbb] Unexpected (to me) behavior of flex.bool

Hi All,

today it took me several hours to debug something that ended up being
the result of this behavior:

 >>> from scitbx.array_family import flex
 >>> x = flex.bool(True, 5)
 >>> y = flex.bool(5, True)
 >>> list(x)
[True]
 >>> list(y)
[True, True, True, True, True]
 >>>

I needed to pass a flex.bool array (with all True) into a C++ function
and I was doing it as "x" above. In that C++ function there was no size
check for incoming arrays (bad idea!) and then later in the code when
accessing elements of this flex.bool array I was getting "random" numbers!

I was pretty sure that flex.bool(True, 5) should lead to an assertion error!

Was this always like this (and I was just super-lucky to never run into
this issue in the past 20 years) or something has changed?

Pavel


_______________________________________________
cctbxbb mailing list
cctbxbb at phenix-online.org
https://gbr01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fphenix-online.org%2Fmailman%2Flistinfo%2Fcctbxbb&data=05%7C01%7C%7Ca6bf98c4bb8a4470fabc08db6ee4f354%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C638225701830132779%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=6LAweRYvY1HaKpz%2BrY5G9vY4W7G1pM4al5fLaHEsSH4%3D&reserved=0<http://phenix-online.org/mailman/listinfo/cctbxbb>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://phenix-online.org/pipermail/cctbxbb/attachments/20230617/e83fac4e/attachment.htm>


More information about the cctbxbb mailing list