Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

64/32-bit issue when unpickling random.Random #45014

Closed
melicertes mannequin opened this issue May 29, 2007 · 7 comments
Closed

64/32-bit issue when unpickling random.Random #45014

melicertes mannequin opened this issue May 29, 2007 · 7 comments
Assignees
Labels
stdlib Python modules in the Lib dir

Comments

@melicertes
Copy link
Mannequin

melicertes mannequin commented May 29, 2007

BPO 1727780
Nosy @loewis
Files
  • python_head_random.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/loewis'
    closed_at = <Date 2007-12-03.19:22:07.570>
    created_at = <Date 2007-05-29.17:08:37.000>
    labels = ['library']
    title = '64/32-bit issue when unpickling random.Random'
    updated_at = <Date 2007-12-03.19:22:07.569>
    user = 'https://bugs.python.org/melicertes'

    bugs.python.org fields:

    activity = <Date 2007-12-03.19:22:07.569>
    actor = 'loewis'
    assignee = 'loewis'
    closed = True
    closed_date = <Date 2007-12-03.19:22:07.570>
    closer = 'loewis'
    components = ['Library (Lib)']
    creation = <Date 2007-05-29.17:08:37.000>
    creator = 'melicertes'
    dependencies = []
    files = ['8441']
    hgrepos = []
    issue_num = 1727780
    keywords = ['patch']
    message_count = 7.0
    messages = ['32146', '55938', '55944', '55959', '55989', '57298', '58144']
    nosy_count = 4.0
    nosy_names = ['loewis', 'pm67nz', 'melicertes', 'sligocki']
    pr_nums = []
    priority = 'normal'
    resolution = 'accepted'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue1727780'
    versions = ['Python 2.5']

    @melicertes
    Copy link
    Mannequin Author

    melicertes mannequin commented May 29, 2007

    If an object containing a random.Random instance is pickled on a 64-bit platform, unpickling the result on a 32-bit platform can cause an OverflowError:

    File "/usr/local/lib/python2.4/random.py", line 140, in __setstate__
    self.setstate(state)
    File "/usr/local/lib/python2.4/random.py", line 125, in setstate
    super(Random, self).setstate(internalstate)
    OverflowError: long int too large to convert to int

    I wasn't able to find any other tickets referring specifically to this. This is in Python 2.4.1 for sure, may be in later versions.

    @melicertes melicertes mannequin added stdlib Python modules in the Lib dir labels May 29, 2007
    @sligocki
    Copy link
    Mannequin

    sligocki mannequin commented Sep 16, 2007

    I've had this same problem with 2.5.1

    Pickling random.getstate() on 64bit and then loading it back with
    random.setstate() on 32bit does not work (crashes because of trying cast
    64bit ints to 32bit).

    The other way around is even worse. It loads but is in a faulty state,
    where for example:
    >>> random.randrange(1000)
    49801980494

    This seems like a bug to me.

    @loewis
    Copy link
    Mannequin

    loewis mannequin commented Sep 16, 2007

    Would anybody of you like to work on a patch?

    @sligocki
    Copy link
    Mannequin

    sligocki mannequin commented Sep 17, 2007

    I've got a patch! The problem was that the state was being cast from a
    C-type unsigned long to a long.

    On 32-bit machines this makes large 32-bit longs negative.
    On 64-bit machines this preserves the sign of 32-bit values (because
    they are stored in 64-bit longs).

    My patch returns the values with PyLong_FromUnsignedLong() instead of
    PyInt_FromLong(), therefore there is no casting to long and both 32-bit
    and 64-bit machines produce the same result.

    I added code to read states from the old (buggy) version and decypher it
    appropriately (from either 32-bit or 64-bit source!). In other words,
    old pickles can now be opened on either architecture with the new patch.

    This patch is taken from the svn head, but also works on Python 2.5.1 .

    I haven't tested this patch fully on 64-bit machine yet. I'll let you
    know when I have.

    Cheers,
    -Shawn

    @sligocki
    Copy link
    Mannequin

    sligocki mannequin commented Sep 18, 2007

    Yep, tested it on a 64-bit machine and 2 32-bit machines and back and
    forth between them. It seems to resolve the problem.

    @loewis loewis mannequin assigned loewis Sep 23, 2007
    @pm67nz
    Copy link
    Mannequin

    pm67nz mannequin commented Nov 9, 2007

    For the record, and to prevent dilution of the count of times this bug has
    been encountered: this issue is a duplicate of bpo-1472695, which was
    later marked "won't fix" for no apparent reason. sligocki's patch is more
    thorough than mine was and I hope it has a better fate too.

    @loewis
    Copy link
    Mannequin

    loewis mannequin commented Dec 3, 2007

    Thanks for the patch. Committed as r59295.

    Because of the version change, the patch cannot be applied to 2.5.x

    @loewis loewis mannequin closed this as completed Dec 3, 2007
    @loewis loewis mannequin closed this as completed Dec 3, 2007
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    0 participants