Patchwork [1,of,2] py3: use email.generator.BytesGenerator in patch.split()

login
register
mail settings
Submitter Denis Laxalde
Date Oct. 10, 2019, 8:58 a.m.
Message ID <707fc6aeadc33b02888e.1570697929@steppe.local>
Download mbox | patch
Permalink /patch/42179/
State Accepted
Headers show

Comments

Denis Laxalde - Oct. 10, 2019, 8:58 a.m.
# HG changeset patch
# User Denis Laxalde <denis.laxalde@logilab.fr>
# Date 1570697337 -7200
#      Thu Oct 10 10:48:57 2019 +0200
# Node ID 707fc6aeadc33b02888e057e549c05d886fb67b0
# Parent  52781d57313d512efb7150603104bea3ca11d0eb
py3: use email.generator.BytesGenerator in patch.split()

This fixes test-import.t on python3.

We add Generator alias in mail module to handle python2/python3
compatibility.
Pulkit Goyal - Oct. 10, 2019, 1:19 p.m.
On Thu, Oct 10, 2019 at 12:02 PM Denis Laxalde <denis@laxalde.org> wrote:
>
> # HG changeset patch
> # User Denis Laxalde <denis.laxalde@logilab.fr>
> # Date 1570697337 -7200
> #      Thu Oct 10 10:48:57 2019 +0200
> # Node ID 707fc6aeadc33b02888e057e549c05d886fb67b0
> # Parent  52781d57313d512efb7150603104bea3ca11d0eb
> py3: use email.generator.BytesGenerator in patch.split()
>
> This fixes test-import.t on python3.
>
> We add Generator alias in mail module to handle python2/python3
> compatibility.

Queued these, many thanks!
Yuya Nishihara - Oct. 10, 2019, 1:23 p.m.
On Thu, 10 Oct 2019 10:58:49 +0200, Denis Laxalde wrote:
> # HG changeset patch
> # User Denis Laxalde <denis.laxalde@logilab.fr>
> # Date 1570697337 -7200
> #      Thu Oct 10 10:48:57 2019 +0200
> # Node ID 707fc6aeadc33b02888e057e549c05d886fb67b0
> # Parent  52781d57313d512efb7150603104bea3ca11d0eb
> py3: use email.generator.BytesGenerator in patch.split()

Queued, thanks.

> diff --git a/mercurial/mail.py b/mercurial/mail.py
> index b478f38..8ee25bf 100644
> --- a/mercurial/mail.py
> +++ b/mercurial/mail.py
> @@ -420,6 +420,8 @@ def mimeencode(ui, s, charsets=None, dis

Added "import email.generator" in flight.

>  
>  if pycompat.ispy3:
>  
> +    Generator = email.generator.BytesGenerator
> +
>      def parse(fp):
>          ep = email.parser.Parser()
>          # disable the "universal newlines" mode, which isn't binary safe.
> @@ -436,6 +438,8 @@ if pycompat.ispy3:
>  
>  else:
>  
> +    Generator = email.generator.Generator
Pulkit Goyal - Oct. 10, 2019, 1:32 p.m.
On Thu, Oct 10, 2019 at 12:02 PM Denis Laxalde <denis@laxalde.org> wrote:
>
> # HG changeset patch
> # User Denis Laxalde <denis.laxalde@logilab.fr>
> # Date 1570697337 -7200
> #      Thu Oct 10 10:48:57 2019 +0200
> # Node ID 707fc6aeadc33b02888e057e549c05d886fb67b0
> # Parent  52781d57313d512efb7150603104bea3ca11d0eb
> py3: use email.generator.BytesGenerator in patch.split()
>
> This fixes test-import.t on python3.
>
> We add Generator alias in mail module to handle python2/python3
> compatibility.
>
> diff --git a/mercurial/mail.py b/mercurial/mail.py
> index b478f38..8ee25bf 100644
> --- a/mercurial/mail.py
> +++ b/mercurial/mail.py
> @@ -420,6 +420,8 @@ def mimeencode(ui, s, charsets=None, dis
>
>  if pycompat.ispy3:
>
> +    Generator = email.generator.BytesGenerator
> +
>      def parse(fp):
>          ep = email.parser.Parser()
>          # disable the "universal newlines" mode, which isn't binary safe.
> @@ -436,6 +438,8 @@ if pycompat.ispy3:
>
>  else:
>
> +    Generator = email.generator.Generator

fixed this to `email.Generator.Generator` in flight.
Denis Laxalde - Oct. 10, 2019, 1:40 p.m.
Pulkit Goyal a écrit :
> On Thu, Oct 10, 2019 at 12:02 PM Denis Laxalde <denis@laxalde.org> wrote:
> >
> > # HG changeset patch
> > # User Denis Laxalde <denis.laxalde@logilab.fr>
> > # Date 1570697337 -7200
> > #      Thu Oct 10 10:48:57 2019 +0200
> > # Node ID 707fc6aeadc33b02888e057e549c05d886fb67b0
> > # Parent  52781d57313d512efb7150603104bea3ca11d0eb
> > py3: use email.generator.BytesGenerator in patch.split()
> >
> > This fixes test-import.t on python3.
> >
> > We add Generator alias in mail module to handle python2/python3
> > compatibility.
> >
> > diff --git a/mercurial/mail.py b/mercurial/mail.py
> > index b478f38..8ee25bf 100644
> > --- a/mercurial/mail.py
> > +++ b/mercurial/mail.py
> > @@ -420,6 +420,8 @@ def mimeencode(ui, s, charsets=None, dis
> >
> >  if pycompat.ispy3:
> >
> > +    Generator = email.generator.BytesGenerator
> > +
> >      def parse(fp):
> >          ep = email.parser.Parser()
> >          # disable the "universal newlines" mode, which isn't binary safe.
> > @@ -436,6 +438,8 @@ if pycompat.ispy3:
> >
> >  else:
> >
> > +    Generator = email.generator.Generator
> 
> fixed this to `email.Generator.Generator` in flight.

Is this problematic? I did it on purpose as "email.generator" exists on
py2.7 and "email.Generator" appears to be a proxy to the latter. Not a
big deal though. (Should have mentioned this in the commit message.)
Pulkit Goyal - Oct. 10, 2019, 1:45 p.m.
On Thu, Oct 10, 2019 at 4:40 PM Denis Laxalde <denis@laxalde.org> wrote:
>
> Pulkit Goyal a écrit :
> > On Thu, Oct 10, 2019 at 12:02 PM Denis Laxalde <denis@laxalde.org> wrote:
> > >
> > > # HG changeset patch
> > > # User Denis Laxalde <denis.laxalde@logilab.fr>
> > > # Date 1570697337 -7200
> > > #      Thu Oct 10 10:48:57 2019 +0200
> > > # Node ID 707fc6aeadc33b02888e057e549c05d886fb67b0
> > > # Parent  52781d57313d512efb7150603104bea3ca11d0eb
> > > py3: use email.generator.BytesGenerator in patch.split()
> > >
> > > This fixes test-import.t on python3.
> > >
> > > We add Generator alias in mail module to handle python2/python3
> > > compatibility.
> > >
> > > diff --git a/mercurial/mail.py b/mercurial/mail.py
> > > index b478f38..8ee25bf 100644
> > > --- a/mercurial/mail.py
> > > +++ b/mercurial/mail.py
> > > @@ -420,6 +420,8 @@ def mimeencode(ui, s, charsets=None, dis
> > >
> > >  if pycompat.ispy3:
> > >
> > > +    Generator = email.generator.BytesGenerator
> > > +
> > >      def parse(fp):
> > >          ep = email.parser.Parser()
> > >          # disable the "universal newlines" mode, which isn't binary safe.
> > > @@ -436,6 +438,8 @@ if pycompat.ispy3:
> > >
> > >  else:
> > >
> > > +    Generator = email.generator.Generator
> >
> > fixed this to `email.Generator.Generator` in flight.
>
> Is this problematic? I did it on purpose as "email.generator" exists on
> py2.7 and "email.Generator" appears to be a proxy to the latter. Not a
> big deal though. (Should have mentioned this in the commit message.)

```
$ python
Python 2.7.15+ (default, Jul  9 2019, 16:51:35)
[GCC 7.4.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import email
>>> email.generator.Generator
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'generator'
>>> import email.generator
>>> email.generator.Generator
<class email.generator.Generator at 0x7fac41c84668>
>>>
```

looks like we need to do `import email.generator` also.

Patch

diff --git a/mercurial/mail.py b/mercurial/mail.py
index b478f38..8ee25bf 100644
--- a/mercurial/mail.py
+++ b/mercurial/mail.py
@@ -420,6 +420,8 @@  def mimeencode(ui, s, charsets=None, dis
 
 if pycompat.ispy3:
 
+    Generator = email.generator.BytesGenerator
+
     def parse(fp):
         ep = email.parser.Parser()
         # disable the "universal newlines" mode, which isn't binary safe.
@@ -436,6 +438,8 @@  if pycompat.ispy3:
 
 else:
 
+    Generator = email.generator.Generator
+
     def parse(fp):
         ep = email.parser.Parser()
         return ep.parse(fp)
diff --git a/mercurial/patch.py b/mercurial/patch.py
index 927b798..68ae21f 100644
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -11,7 +11,6 @@  from __future__ import absolute_import, 
 import collections
 import contextlib
 import copy
-import email
 import errno
 import hashlib
 import os
@@ -107,7 +106,7 @@  def split(stream):
     def mimesplit(stream, cur):
         def msgfp(m):
             fp = stringio()
-            g = email.Generator.Generator(fp, mangle_from_=False)
+            g = mail.Generator(fp, mangle_from_=False)
             g.flatten(m)
             fp.seek(0)
             return fp