39 Commits

Author SHA1 Message Date
Zuul
c7d4c0c74b Merge "pre-commit: Bump versions" 2024-01-28 09:00:01 +00:00
Stephen Finucane
5587e9555e pre-commit: Bump versions
hacking now has a native hook.

Change-Id: Ibdf50f4e1e1cfe6056f25322f66d4e95f5930dc0
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
2024-01-03 12:12:14 +00:00
Takashi Kajinami
c83a4c9968 jsonutils: Raise ValueError in case the input can't be converted
According to the warning it was planned that this change was made in
3.0.

Change-Id: I0c1011b935f52f5ea1329e0316f935a034fdfd0e
2023-11-08 17:41:29 +00:00
Sofia Enriquez
02037330d8 Fix json to_primitive when using IO OBjects
Currently, using Cinder's backup service with RBD the
backup-create operation gets stuck when logging
('use_json=True' must be set in the config file).

The oslo.log JSONFormatter gets stuck when passing an
RBDVolumeIOWrapper from os-brick. This happens via os-brick's
utils.trace() method which passes a connector containing
{'path': RBDVolumeIOWrapper}.
The oslo.log JSONFormatter format() method calls
oslo_serialization's jsonutils.to_primitive and passes in
this RBDVolumeIOWrapper object.
 
Therefore the to_primitive method eventually calls
RBDVolumeIOWrapper.read(). In order to fix this the current
path avoids mapping io.IOBase objects and fallback the wrapper
RBD volume object.

Co-authored-by: Eric Harney <eharney@redhat.com>
Closes-Bug: #1908607
Change-Id: I3c416e855cb5f0dc32d14b2749ba92aba8964574
2021-01-18 21:54:02 +00:00
Ben Nemec
909c17a779 Remove Python 3-specific conditional in dump_as_bytes
Now that we only support Python 3 we don't need logic to handle
Python 2.

Change-Id: I33747d25bc4c67ee13654ed132edf7413371c1dc
2020-03-06 17:17:33 +00:00
Stephen Finucane
ad356ea9c2 Drop use of six
Another one bites the dust.

Change-Id: I1fadcad8219322b569eeecd81e454a44641e8b1e
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
2020-03-02 10:10:22 +00:00
Stephen Finucane
9fee0c4f48 Remove long-dead workaround
We don't need to worry about versions of Mox released in 2011 any more

https://bugs.launchpad.net/nova/+bug/852095

Change-Id: Ifd2228071c70e516f11eefde29932813b7cf6a97
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
2020-02-27 15:41:09 +00:00
ZhijunWei
c9b464c099 Update hacking version
Use latest release 1.1.0 and compatible changes w.r.t pep8

Change-Id: I95d62bbce7fe20dd2f9ec8a96cdffeeefcda75be
2019-02-17 10:28:24 +08:00
John L. Villalovos
c1a7079c26 When serializing an exception return its 'repr'
When serializing an exception return its 'repr'. A 'repr' is a
printable representation of an object.

For example the exception: ValueError("an exception") will be returned
as the string: "ValueError('an exception',)"

Change-Id: Iac9f4624bcc4ff65e27c9ca20c6cbbe9481cf334
2017-10-03 09:41:52 -07:00
Victor Stinner
cdb2f60d26 jsonutils.to_primitive(): add fallback parameter
For example, to_primitive(fallback=repr) can be used to prevent
serialialization error like "ValueError: Circular reference detected"
when using the JSONFormatter of oslo.log.

If fallback is set, it is also used to convert itertools.count(),
"nasty" objects like types, and to handle TypeError.

Use fallback=six.text_type to convert objects to text.

This patch doesn't change the default behaviour.

Related-Bug: #1593641
Change-Id: Ie0f7f2d09355c3d2a9f7c5ee8f7e02dfea3b073b
2017-09-26 11:20:08 +02:00
ChangBo Guo(gcb)
fecad3c31f warn developers when can't convert value into primitive
We try to raise ValueError directly when can't convert the value into
proimitive in If9e8dd5cc2634168910d5f9f8d9302aeefa16097, but revert it
due to some reasons The better way is that notify consuming projects
with the future API behavior change before the change. It will raise
ValureError in version 3.0.

Partial-Bug: #1593641
Change-Id: I2eff07cd25f0565b380cb6e76628a896c8d0ec61
2017-09-19 09:37:39 +08:00
ChangBo Guo(gcb)
bbee6d1aa1 Revert "Explicitly raise ValueError in to_primitive"
This reverts commit 3727b2d6e051b7b07680df361f38b9dbb576462c.

It breaks nova's test, need log warning before we make major release.

Change-Id: Ibbfde6207fc8c066155758c7c1aa1521b95d4db1
2017-07-17 23:28:58 +08:00
Jenkins
2a29a5e836 Merge "Explicitly raise ValueError in to_primitive" 2017-07-05 11:26:43 +00:00
Ihar Hrachyshka
38ac21b523 Don't iterate through addresses in netaddr.IPNetwork
Currently, to_primitive tries to iterate through all addresses in the
network, because the type doesn't have a special handling that would
short curcuit it, but also has __iter__. This may be detrimental to
performance, up to the point of node crash due to memory exhaustion if
the passed network range is too large (think of 0.0.0.0/0 or even
2001::/64). This behavior also makes it impossible to restore the
original data format (CIDR).

This patch short curcuits the iteration by handling the IPNetwork type
as a special case, same as we do for IPAddress.

Change-Id: I6aecd2d057d282a655ff9e4918c164253142b188
Closes-Bug: #1698355
2017-06-16 11:46:36 -07:00
ChangBo Guo(gcb)
3727b2d6e0 Explicitly raise ValueError in to_primitive
The problem in the current version of to_primitive function
from jsonutils module is in the situation when the function
doesn't know how to convert an object to primitive. In that
case the function simply returns the same object which causes
the following exception later in json.dumps:
ValueError: Circular reference detected. This exception is not
obvious and is quite misleading. So I think it would be better
to explicitly raise ValueError here.

Closes-Bug: #1593641

Change-Id: If9e8dd5cc2634168910d5f9f8d9302aeefa16097
2017-05-17 15:55:14 +08:00
gecong1973
1df76ee42d Replace six.iteritems() with .items()
1.As mentioned in [1], we should avoid using
six.iteritems to achieve iterators. We can use
dict.items instead, as it will return iterators
in PY3 as well. And dict.items/keys will more readable.
2.In py2, the performance about list should be negligible,
see the link [2].
[1] https://wiki.openstack.org/wiki/Python3
[2] http://lists.openstack.org/pipermail/openstack-dev/2015-June/066391.html

Change-Id: Ifbe7929580ec61a2cd50232794c0cbd0cbf37edc
2016-12-12 11:11:28 +08:00
Gevorg Davoian
a73120aad6 Fix serialization of binary strings in Python3
Change-Id: I30e7c7b7cb2356a39bc51eb8c93f14c8881a38c2
Closes-Bug: #1599387
2016-08-01 16:58:08 +03:00
Edan David
1ec85e90cd Use {} instead of dict()
There is a performance impact when using dict() instead of {}
in CPython (up to 6 times longer).
Considering the to_primitive function is recursive this can
have quite an effect.

Measuring:
$ python2.7 -m timeit -n 1000000 -r 5 -v 'dict()'
raw times: 0.24 0.24 0.24 0.239 0.24
1000000 loops, best of 5: 0.239 usec per loop

$ python2.7 -m timeit -n 1000000 -r 5 -v '{}'
raw times: 0.0417 0.0413 0.0407 0.0411 0.042
1000000 loops, best of 5: 0.0407 usec per loop

For more information:
https://doughellmann.com/blog/2012/11/12/
the-performance-impact-of-using-dict-instead-of-in-cpython-2-7-2/

Change-Id: Ia0b5892773a19cbabe40313a3bc788580a943f53
2016-06-21 10:58:06 -04:00
Jenkins
0aa1638408 Merge "Support serializing ipaddress objs with jsonutils" 2016-06-09 08:18:25 +00:00
Gevorg Davoian
4fdaeff758 Replace TypeError by ValueError in msgpackutils
There are some differences in the behaviour of json and msgpack
serializers. Msgpack throws TypeError when it doesn't know
how to handle an object. Json throws ValueError instead. Regarding
the fact that serialization should be configurable (in particular,
serializers should have similar behaviour; right now transition from
json to msgpack leads to errors and fails) this patch proposes to
raise ValueError instead of TypeError on failures so that libraries
already using jsonutils would be able to also work with msgpackutils.

Change-Id: I3d21b7d136e5a426a3c4a70a953c82ddcd6ef5af
2016-06-08 08:41:36 +00:00
Jim Baker
8943c73662 Support serializing ipaddress objs with jsonutils
Fixes jsonutils.to_primitive so that it can serialize ipaddress
objects, as used by Python 3 or through the backport in
global-requirements.txt. Current support for netaddr is maintained.

Change-Id: I6aa1ecd5be754f61cdda4e47bc0e0180e232b366
Closes-bug: 1590076
2016-06-07 14:05:50 -06:00
Ian Cordasco
29a7702a99 Remove fallback to simplejson in jsonutils
OpenStack has ended support for Python 2.6 in all services and
libraries. We no longer need to support a fallback to simplejson on
Python 2.6 so we can remove this fallback logic.

Closes-bug: 1533337
Change-Id: I613541a37229ff2f3ce269cfa7496223d1ff37d0
2016-01-12 13:53:47 -06:00
Victor Stinner
2d085d2c17 Use versionadded and versionchanged in doc
Document in which version new types and functions were added using
".. versionadded:: x.y". Document changes using
".. versionchanged:: x.y."

For base64 module, add the versionadded tag in the module top
docstring, not on each type/function.

I used "git blame" + "git tag --contains=SHA1" to find these version,
and then I checked manually each version.

Change-Id: I4a891b18064fe7b857a79c57030ff31f7a0370b4
2015-10-15 14:38:50 +02:00
Sirushti Murugesan
96886ce299 Also check for the existence of items in to_primitive()
Change-Id: If4636566a642ddf2e6e13cc014abca4ad68d1977
2015-09-30 12:37:12 +05:30
Victor Stinner
92f2111b26 Add jsonutils.dump_as_bytes() function for py3
The jsonutils.dumps() function returns bytes on Python 2 and Unicode
oon Python 3. In some cases, we always want bytes. For example, a
HTTP body must be bytes. This function avoids an condition call to
.encode() depending on the type or on the Python version.

For example:

    body = jsonutils.dumps(data)
    if isinstance(body, six.text_type):
        body = body.encode('utf-8')

can be replaced with:

    body = jsonutils.dump_as_bytes(data)

Change-Id: Ib9d8f1309982762b54d8a91b1f24f64d0ae6723a
2015-09-23 15:23:43 +02:00
Brant Knudson
57364659c1 Stop using deprecated oslo_utils.timeutils.strtime
oslo_utils.timeutils.strtime is deprecated, so stop using it.

Change-Id: I5a6c54b99820174f50f716deae275c0114632b2d
2015-06-30 08:26:18 -05:00
Davanum Srinivas (dims)
15c82cc902 Revert "jsonutils: replace strtime() usage with isoformat()"
This is a wire format change that the projects are not ready for yet (example Nova).

We should do this as part of a major version bump (2.x) with ample time for folks to move up.

Nova review to prepare the Nova code base for this change:
Ib7e60ed94edd1eb409baf8c29c61237d85944787

This reverts commit 5b0827a3df584b3e26bb44e5358cc28ac4a91636.

Change-Id: Ic4bf047b40c1dad2f8d2bc0afda934e47dba1f9d
2015-06-01 15:43:37 -04:00
Julien Danjou
986ec5832e jsonutils: encode dict keys
Currently, only the dict value are converted with to_primitive().
There's no reason not to do it on keys too to support special types.

Change-Id: I5b447f2fcd814088483f67e54261131cfc87ae79
2015-05-13 12:11:21 +02:00
Julien Danjou
50e1abcb97 jsonutils: simplify simple value handling
There's a few code duplications because for some reason the code
handling the recursive part got some conversion code for non-recursive
value.

Let's stack non-recursive value conversion as the top, and then use
"recursive" only where needed.

Change-Id: Ic1ecc76aba5402129a936dfb4649df0806a564a4
2015-05-13 12:11:21 +02:00
Julien Danjou
5b0827a3df jsonutils: replace strtime() usage with isoformat()
Following discussion in change I8b5119e64369ccac3423dccc04421f99912df733
in oslo.utils, we plan to deprecated and remove strtime() which is to be
considered dangerous.

This changes the json serialization code to embed the timezone in the
encoded datetime string if it's available.

Change-Id: I262391987d25b483d2bec4ac3abe94b6e9d032ad
2015-05-13 12:11:12 +02:00
Joshua Harlow
4ada51b09d More docstring cleanups/tweaks
Ensure jsonutils and msgpackutils headers are linkable and
connect into there associated functions and classes so that
clicking on links works (instead of not working).

Change-Id: Ifa28421e7d21a85290ee13d04918325c3f991b92
2015-04-14 21:09:14 +00:00
Jenkins
530fea24cc Merge "jsonutils: add set() tests and simplify recursive code" 2015-02-16 18:29:01 +00:00
Jenkins
4bfc83e0b0 Merge "jsonutils: support UUID encoding" 2015-02-16 18:28:59 +00:00
Jenkins
f04e859855 Merge "Use default in dumps()" 2015-02-16 18:28:39 +00:00
Julien Danjou
22cea10c38 jsonutils: add set() tests and simplify recursive code
Change-Id: I6fd3a78d2caee2e4891d00530350407d407ff677
2015-02-05 21:04:26 +01:00
Julien Danjou
73f7155af2 jsonutils: support UUID encoding
Change-Id: I2c864f793b4ee6136442b678380bf21f732be643
2015-02-05 21:04:26 +01:00
Julien Danjou
2c244b288e Use default in dumps()
There's a different behaviour currently between dump() and dumps() as by
default dumps() use the to_primitive function provided by this module,
and dump() does not. This patch fixes that.

Change-Id: I8a116e3777901bc713e9812c1a654a51b43e702a
2015-02-05 21:04:25 +01:00
Doug Hellmann
402abdbcd0 Update Oslo imports to remove namespace package
Change-Id: I4ec9b2a310471e4e07867073e9577731ac34027d
Blueprint: drop-namespace-packages
2015-01-21 19:54:33 -05:00
Doug Hellmann
e8deb08b7f Move files out of the namespace package
Move the public API out of oslo.serialization to
oslo_serialization. Retain the ability to import from the old namespace
package for backwards compatibility for this release cycle.

bp/drop-namespace-packages

Change-Id: Ic60f809ea00ac77b0753556c6fd00b97e64d57ff
2015-01-05 15:40:06 -05:00