Patchwork [V2] cbor: teach the encoder to handle python `long` type for Windows

login
register
mail settings
Submitter Matt Harbison
Date Sept. 5, 2018, 3:36 a.m.
Message ID <2cce57226251044ce340.1536118578@Envy>
Download mbox | patch
Permalink /patch/34325/
State Accepted
Headers show

Comments

Matt Harbison - Sept. 5, 2018, 3:36 a.m.
# HG changeset patch
# User Matt Harbison <matt_harbison@yahoo.com>
# Date 1536114578 14400
#      Tue Sep 04 22:29:38 2018 -0400
# Node ID 2cce57226251044ce3406ad92e4edbf01efcde72
# Parent  197521083166579f6c80d7532ec6e919af2fe2cf
cbor: teach the encoder to handle python `long` type for Windows

The tests for 2**32 and -7000000000 were blowing up, complaining about not
knowing how to encode type 'long'.  sys.maxint tops out at 2**31-1 on Windows,
but I guess is 2^63-1 on Linux?  I *think* we're OK on the decode side, as there
is an assertion that the decoded value is equal to the original primitive value.

I opted for the pycompat alias instead of swallowing the NameError because the
vendored cbor package uses an alias, and I see at least pywatchman and
templatefilters open codes their own aliases.
Yuya Nishihara - Sept. 6, 2018, 11:37 a.m.
On Tue, 04 Sep 2018 23:36:18 -0400, Matt Harbison wrote:
> # HG changeset patch
> # User Matt Harbison <matt_harbison@yahoo.com>
> # Date 1536114578 14400
> #      Tue Sep 04 22:29:38 2018 -0400
> # Node ID 2cce57226251044ce3406ad92e4edbf01efcde72
> # Parent  197521083166579f6c80d7532ec6e919af2fe2cf
> cbor: teach the encoder to handle python `long` type for Windows

Queued, thanks.

> @@ -190,6 +192,7 @@ def streamencodenone(v):
>  STREAM_ENCODERS = {
>      bytes: streamencodebytestring,
>      int: streamencodeint,
> +    pycompat.long: streamencodeint,   # For Windows

Removed the comment as the same thing would happen on x86 Linux.

FWIW, I didn't know Python allows duplicated keys in dict literal.

Patch

diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
--- a/mercurial/pycompat.py
+++ b/mercurial/pycompat.py
@@ -120,6 +120,8 @@  if ispy3:
     rawinput = input
     getargspec = inspect.getfullargspec
 
+    long = int
+
     # TODO: .buffer might not exist if std streams were replaced; we'll need
     # a silly wrapper to make a bytes stream backed by a unicode one.
     stdin = sys.stdin.buffer
@@ -384,6 +386,7 @@  else:
     ospardir = os.pardir
     ossep = os.sep
     osaltsep = os.altsep
+    long = long
     stdin = sys.stdin
     stdout = sys.stdout
     stderr = sys.stderr
diff --git a/mercurial/utils/cborutil.py b/mercurial/utils/cborutil.py
--- a/mercurial/utils/cborutil.py
+++ b/mercurial/utils/cborutil.py
@@ -10,6 +10,8 @@  from __future__ import absolute_import
 import struct
 import sys
 
+from .. import pycompat
+
 # Very short very of RFC 7049...
 #
 # Each item begins with a byte. The 3 high bits of that byte denote the
@@ -190,6 +192,7 @@  def streamencodenone(v):
 STREAM_ENCODERS = {
     bytes: streamencodebytestring,
     int: streamencodeint,
+    pycompat.long: streamencodeint,   # For Windows
     list: streamencodearray,
     tuple: streamencodearray,
     dict: streamencodemap,