-3

I am trying to migrate project from Python 2 to Python 3. For this I used 2to3.exe. It made most of the things correct.

But my real problem is :

1. Data is stored by Python 2 Code. This data is very huge and I can't change it.
2. While reading this data in Python 3 getting error for marshalling and text data. 

For text can we use decode function using latin-1?

Marshal e.g. :

Python 2 code

import marshal
ad = {'a': 2319, 'b': '1', 'c': '1'}
marshalled_data = marshal.dumps(ad)
#'{t\x01\x00\x00\x00ai\x0f\t\x00\x00t\x01\x00\x00\x00ct\x01\x00\x00\x001t\x01\x00\x00\x00bR\x02\x00\x00\x000'

Python 3 code

import marshal
marshalled_data = b'{t\x01\x00\x00\x00ai\x0f\t\x00\x00t\x01\x00\x00\x00ct\x01\x00\x00\x001t\x01\x00\x00\x00bR\x02\x00\x00\x000'
ad = marshal.loads(marshalled_data)

Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
ValueError: bad marshal data (unknown type code)

Currently I don't see any option to have marshalling reading python 2 data in Python 3 code base. Has anyone worked on it?

It will be very helpful if any pointer is provided for further investigations.

Sanjay Bhosale
  • 645
  • 2
  • 7
  • 17
  • 1
    "getting error"...what's the error? Show some code and a traceback. – Mark Tolonen Apr 16 '19 at 11:55
  • Existing project stores data through Python 2 code. And we need to upgrade our project to work on Python 3. As some data is marshalled or kept as string as it is, while reading data through Python 3 code gives error is invalid bytes and fails – Sanjay Bhosale Apr 16 '19 at 12:08

1 Answers1

0

Today I got solution for marshalling is as below:

from pwnypack import marshal
atext = b'{t\x01\x00\x00\x00ai\x0f\t\x00\x00t\x01\x00\x00\x00ct\x01\x00\x00\x001t\x01\x00\x00\x00bR\x02\x00\x00\x000'
marshal.marshal_loads(atext, origin={'version': 27})

atext is string generated by marshal.dumps of Python2.7 version

Don't know any other implications in it. But it solved problem mentioned in question.

Sanjay Bhosale
  • 645
  • 2
  • 7
  • 17