473,594 Members | 2,768 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

marshal bug?

I have been chasing a problem in my code since hours and it bolis down
to this
import marshal
marshal.dumps(s tr(123)) != marshal.dumps(s tr("123"))

Can someone please tell me why?
when
str(123) == str("123")

or are they different?

it also means that
if s = str(123)
marshal.dumps(s ) != marshal.dumps(m arshal.loads(ma rshal.dumps(s)) )

rgds
Anurag

Sep 28 '07 #1
5 1981
Thanks for the reply.

It seems problem is due to
"""
Any string in Python can be "interned" or not, the difference being
how/where the value is stored internally. The marshal module includes
such information in its output. What you are doing is probably
considered a misuse of the marshal module. I'd suggest using the
pickle
(or cPickle) modules instead.
"""
as mentioned by Gary Herron

Now is there a easy way to by pass it (hack around it)
I tried various options but all fail e.g.
i= 123; marshal.dumps(" %d"%123) != marshal.dumps(" %d"%i)

So maybe I have to change all my code to use pickle, which also
consumes for memory per string.
Sep 28 '07 #2


Anurag wrote:
I have been chasing a problem in my code since hours and it bolis down
to this
import marshal
marshal.dumps(s tr(123)) != marshal.dumps(s tr("123"))

Can someone please tell me why?
when
str(123) == str("123")

or are they different?

it also means that
if s = str(123)
marshal.dumps(s ) != marshal.dumps(m arshal.loads(ma rshal.dumps(s)) )

rgds
Anurag

Any string in Python can be "interned" or not, the difference being
how/where the value is stored internally. The marshal module includes
such information in its output. What you are doing is probably
considered a misuse of the marshal module. I'd suggest using the pickle
(or cPickle) modules instead.

Here's the relevant part of the manual for marshal:

version:
Indicates the format that the module uses. Version 0 is the
historical format, version 1 (added in Python 2.4) shares interned
strings and version 2 (added in Python 2.5) uses a binary format for
floating point numbers. The current version is 2



Gary Herron.
Sep 28 '07 #3
I'm not sure why, but marshal does dump the 2 differently. ie:
>
>marshal.dumps( str(123))
's\x03\x00\x00\ x00123'
>marshal.dumps( str("123"))
't\x03\x00\x00\ x00123'
I've just checked the source [1].

's' refers to a regular string, 't' refers to an interned[2] string.

In other words the difference between the 2 marshal dumps is an
internal python implementation detail. You probably shouldn't be
comparing marshal dumps in your app in the first place.

[1] http://coverage.livinglogic.de/Python/marshal.c.html. (Actually, I
checked the downloaded bz2, but this is the only URL for marshal.c I
could find)
[2] http://mindprod.com/jgloss/interned.html
Sep 28 '07 #4
En Fri, 28 Sep 2007 04:26:39 -0300, Anurag <an**********@g mail.com>
escribi�:
Now is there a easy way to by pass it (hack around it)
I tried various options but all fail e.g.
i= 123; marshal.dumps(" %d"%123) != marshal.dumps(" %d"%i)
You can't. Don't use marshal to compare objects. You appear to assume that
two objects that compare equal, should have the same marshalled
representation, and that is simply not true.

py[1, 2.0, 3+0j, "4"] == [1.0, 2+0j, 3, u"4"]
True
So maybe I have to change all my code to use pickle, which also
consumes for memory per string.
Neither marshal nor pickle guarantee that equal objects have equal
representations , so in this regard pickle won't help either.
Maybe if you explain what you really want to do someone could suggest a
solution.

--
Gabriel Genellina

Sep 28 '07 #5
[1] http://coverage.livinglogic.de/Python/marshal.c.html. (Actually, I
checked the downloaded bz2, but this is the only URL for marshal.c I
could find)
A better URL is

http://svn.python.org/projects/pytho...thon/marshal.c

or

http://svn.python.org/view/python/tr...thon/marshal.c

Regards,
Martin
Sep 28 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2423
by: syd | last post by:
Hello all, In my project, I have container classes holding lists of item classes. For example, a container class myLibrary might hold a list of item classes myNation and associated variables like myNation.name='USA' and myNation.continent='North America'. Bottom line, I was hoping to use this structure to marshal the classes to xml.
0
1886
by: William Stacey | last post by:
The following code works, but I can't figure out why. I take a struct with two members, a single byte and byte. I then marshal the whole struct to a byte. I create a new struct (without init'ing any members) and marshal the tmp array back to the new struct. The new struct shows the same data. The single type is easy, that just gets copied. The byte ref type is the interesting part. The address (i.e ref) of the original byte gets...
13
2156
by: Just Me | last post by:
The following almost works. The problem is Marshal.PtrToStringAuto seems to terminate at the first null so I don't get the full string. Any suggestions on how to fix this? Or how to improve the code? Thanks PS I added the +1 because as I understand the GetLogicalDriveStrings doc
6
2716
by: william.thorpe.b | last post by:
I have recently switched from VS2003 to VS2005 and at the same time from V1 to V2 of the .NET Compact Framework. The target is a Windows CE 5.0 device and an ARMV4I processor. System.Runtime.InteropServices.Marshal.WriteInt32 used to work fine but now is misbehaving. I wrote some native-code (C++) alternates to some Marshal methods and when I P/Invoke them they work fine; I can write to an address and I read back the same value that...
2
1903
by: Pierre Rouleau | last post by:
Hi all, When using Python 2.4.x on a Win32 box, marshal.loads(marshal.dumps(1e66666)) returns 1.0 instead of infinity as it should and does under Python 2.5 (also running on Win32 ). This problem was reported in another thread here by Peter Hansen http://groups-beta.google.com/group/comp.lang.python/browse_frm/thread/5c2b4b2a88c8df4/f216739705c9304f?lnk=gst&q=simplejson&rnum=5#f216739705c9304f Is this considered an important enough...
2
7195
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get Marshal.PtrToStructure to copy the all the data into the "other" array? unsafe public struct DeadReckoning {
0
2124
by: Charming12 | last post by:
Hi All, I have a strange problem and due to my inefficiency with IntPtr i am unable to figure it out. I have an structure something like: public struct Detail { public int age; public Detail(int _age)
0
2725
by: xrxst32 | last post by:
Hello there, I have some doubts about the best practice for using COM automation, the Runtime Callable Wrapper (RCW) and Marshal.ReleaseComObject. So the question is/are: Do I need to release each COM object explicit by a call to Marshal.ReleaseComObject, does the RCW take care of that or does it leaks unmanaged resources?
2
15920
by: O.B. | last post by:
I have operation within a class that marshals the data into a byte array. Below are three different ways that work. Are there any downsides to using one over the the other? public virtual byte ToRaw1() { byte byteArray = new byte; IntPtr pointer = Marshal.AllocHGlobal(Size); Marshal.StructureToPtr(this, pointer, false); Marshal.Copy(pointer, byteArray, 0, Size);
0
7937
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7874
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8244
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6647
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5738
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5403
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3854
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3893
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.