D4772: wireprotov2: define type to represent pre-encoded object
indygreg (Gregory Szorc)
phabricator at mercurial-scm.org
Wed Oct 3 15:29:38 UTC 2018
This revision was automatically updated to reflect the committed changes.
Closed by commit rHGed919b90acda: wireprotov2: define type to represent pre-encoded object (authored by indygreg, committed by ).
REPOSITORY
rHG Mercurial
CHANGES SINCE LAST UPDATE
https://phab.mercurial-scm.org/D4772?vs=11443&id=11623
REVISION DETAIL
https://phab.mercurial-scm.org/D4772
AFFECTED FILES
mercurial/wireprotoframing.py
mercurial/wireprototypes.py
CHANGE DETAILS
diff --git a/mercurial/wireprototypes.py b/mercurial/wireprototypes.py
--- a/mercurial/wireprototypes.py
+++ b/mercurial/wireprototypes.py
@@ -10,6 +10,9 @@
hex,
)
from .i18n import _
+from .thirdparty import (
+ attr,
+)
from . import (
error,
util,
@@ -352,3 +355,15 @@
', '.sorted(validnames))
return compengines
+
+ at attr.s
+class encodedresponse(object):
+ """Represents response data that is already content encoded.
+
+ Wire protocol version 2 only.
+
+ Commands typically emit Python objects that are encoded and sent over the
+ wire. If commands emit an object of this type, the encoding step is bypassed
+ and the content from this object is used instead.
+ """
+ data = attr.ib()
diff --git a/mercurial/wireprotoframing.py b/mercurial/wireprotoframing.py
--- a/mercurial/wireprotoframing.py
+++ b/mercurial/wireprotoframing.py
@@ -22,6 +22,7 @@
encoding,
error,
util,
+ wireprototypes,
)
from .utils import (
cborutil,
@@ -840,10 +841,22 @@
yield createcommandresponseokframe(stream, requestid)
emitted = True
- for chunk in cborutil.streamencode(o):
- for frame in emitter.send(chunk):
+ # Objects emitted by command functions can be serializable
+ # data structures or special types.
+ # TODO consider extracting the content normalization to a
+ # standalone function, as it may be useful for e.g. cachers.
+
+ # A pre-encoded object is sent directly to the emitter.
+ if isinstance(o, wireprototypes.encodedresponse):
+ for frame in emitter.send(o.data):
yield frame
+ # A regular object is CBOR encoded.
+ else:
+ for chunk in cborutil.streamencode(o):
+ for frame in emitter.send(chunk):
+ yield frame
+
except Exception as e:
for frame in createerrorframe(stream, requestid,
'%s' % e,
To: indygreg, #hg-reviewers
Cc: mercurial-devel
More information about the Mercurial-devel
mailing list