Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: How to define a bytes literal in Python 2.x for porting to Python3.x using 2to3?

Reply
Thread Tools

Re: How to define a bytes literal in Python 2.x for porting to Python3.x using 2to3?

 
 
Terry Reedy
Guest
Posts: n/a
 
      01-01-2011
On 1/1/2011 5:57 AM, Stefan Behnel wrote:
> Terry Reedy, 01.01.2011 11:08:
>> On 1/1/2011 4:08 AM, Baptiste Lepilleur wrote:
>>
>>> Is there a way to mark string literals so that 2to3 automatically
>>> prefixes them with 'b'? Is there a simpler trick?

>>
>> Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit
>> (Intel)] on win32
>> Type "copyright", "credits" or "license()" for more information.
>> >>> b=b'abc'
>> >>> b

>> 'abc'
>>
>> The b prefix does nothing in 2.7. It was specifically added for this type
>> of porting problem.

>
> More precisely, it was added in Python 2.6, so older Python versions
> will consider it a syntax error.


'so' here means 'as a consequence' rather than 'with the intention' .

> To support older Python versions, you need to write your own wrapper
> functions for bytes literals that do nothing in Python 2 and convert the
> literal back to a bytes literal in Python 3. That's ugly, but there's no
> other way to do it.


I think the developers expected that most maintained and updated 2.x
code, especially code targeted at 3.x also, would be migrated to 2.6+.

--
Terry Jan Reedy

 
Reply With Quote
 
 
 
 
Martin v. Loewis
Guest
Posts: n/a
 
      01-01-2011
>> To support older Python versions, you need to write your own wrapper
>> functions for bytes literals that do nothing in Python 2 and convert the
>> literal back to a bytes literal in Python 3. That's ugly, but there's no
>> other way to do it.

>
> I think the developers expected that most maintained and updated 2.x
> code, especially code targeted at 3.x also, would be migrated to 2.6+.


Unfortunately, that assumption has hurt Python 3 migration
significantly. It gave the impression that, as long as you need to
support Python 2.5 and earlier, there is no way you could possibly
support Python 3 as well, and that, therefore, starting to support
Python 3 is pointless for many years to come.

I personally never shared that assumption, and encourage people to
ignore these gimmicks that had been added to 2.6 to ease porting.
Instead, people should first determine what Python versions their
users want to see supported, and then look for solutions that cover
all these versions.

In the case of byte literals, the solution is fairly straight-forward,
and only moderately ugly.

Regards,
Martin
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      01-01-2011
On 1/1/2011 5:07 PM, Martin v. Loewis wrote:

>> I think the developers expected that most maintained and updated 2.x
>> code, especially code targeted at 3.x also, would be migrated to 2.6+.

>
> Unfortunately, that assumption has hurt Python 3 migration
> significantly. It gave the impression that, as long as you need to
> support Python 2.5 and earlier, there is no way you could possibly
> support Python 3 as well, and that, therefore, starting to support
> Python 3 is pointless for many years to come.
>
> I personally never shared that assumption, and encourage people to
> ignore these gimmicks that had been added to 2.6 to ease porting.
> Instead, people should first determine what Python versions their
> users want to see supported, and then look for solutions that cover
> all these versions.


You have shown that it is easier than some people thought. I think two
key ideas are these.

1. Code running in multiple versions has to be syntactically correct in
every detail all versions in order to be compiled without error.
However, version specific syntax *can* be used in modules that are
conditionally imported and therefore conditionally compiled and executed.

2. The syntax of function calls has hardly changed and using the common
subset is no limitation for the overwhelming majority of uses. Moreover,
function names can be conditionally bound to version-specific function
objects, whether builtin or imported.

--
Terry Jan Reedy

 
Reply With Quote
 
Martin v. Loewis
Guest
Posts: n/a
 
      01-01-2011
> 1. Code running in multiple versions has to be syntactically correct in
> every detail all versions in order to be compiled without error.
> However, version specific syntax *can* be used in modules that are
> conditionally imported and therefore conditionally compiled and executed.


I also encourage people to use 2to3. Then this requirement (must be
syntactically correct in all Python versions) goes away: it is ok to
write source that doesn't compile on Python 3, and still *run* it
on Python 3.

OTOH, writing code that only supports newer 2.x versions isn't helped
by 2to3, so compatibility within 2.x is more important to consider than
compatibility between 2.x and 3.x.

Regards,
Martin
 
Reply With Quote
 
Stefan Behnel
Guest
Posts: n/a
 
      01-02-2011
Terry Reedy, 01.01.2011 23:47:
> 1. Code running in multiple versions has to be syntactically correct in
> every detail all versions in order to be compiled without error. However,
> version specific syntax *can* be used in modules that are conditionally
> imported and therefore conditionally compiled and executed.


This is something that might also be a suitable solution for the OP's
problem. The format strings can be by externalised into an importable
module, which can then be duplicated to use the 'b' bytes prefix for Python 3.

The obvious drawback is that this moves the strings out of the immediate
sight of someone reading the sources, and that it requires the two string
modules to be kept in sync. But at least for the synchronisation, a
simplistic conversion tool run during installation could do the trick.

Stefan

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net 2 07-29-2008 09:50 AM
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net Building Controls 0 07-29-2008 01:37 AM
Ratio of Bytes Delayed to Bytes Sent netproj Cisco 0 12-21-2005 08:08 PM
What's wrong with rpc-literal? Why use doc-literal? Anonieko Ramos ASP .Net Web Services 0 09-27-2004 09:06 AM



Advertisments