473,512 Members | 15,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a type without a __mro__?

Can anybody suggest where to find (within the standard library) or how
to easily make (e.g. in a C extension) a type without a __mro__, except
for those (such as types.InstanceType) which are explicitly recorded in
the dispatch table copy._deepcopy_dispatch...?

Weird request, I know, so let me explain. There's a bug in the
forthcoming 2.3.5's copy.py which can be tickled only by such a type.

Fixing the bug is trivially easy (I'll just use inspect.getmro(cls)
instead of cls.__mro__), but I also want to add to test_copy.py a unit
test that tickles the bug, rather than just commit an ``untested fix''.

I'm stumped at finding a type that's suitable for reproducing the bug;
I've asked the submitter of the original bug report (which comes up with
Zope and some specific application -- unsuitable for a core python unit
test, sigh), I've asked on python-dev, I've even IM'd a couple of
Python-guru friends, but the friends are equally stumped and I'm getting
no answers on python-dev nor from the submitter of the bug report. So,
I thought I'd turn to the collective ingenuity of comp.lang.python...
thanks in advance for any help!
Alex
Jul 18 '05 #1
5 1585
Alex Martelli wrote:
Can anybody suggest where to find (within the standard library) or how
to easily make (e.g. in a C extension) a type without a __mro__, except
for those (such as types.InstanceType) which are explicitly recorded in
the dispatch table copy._deepcopy_dispatch...?


something like this?
import re
x = re.compile("")
x <_sre.SRE_Pattern object at 0x00B2F7A0> x.__mro__ Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: __mro__ import copy
type(x) in copy._deepcopy_dispatch

False

</F>

Jul 18 '05 #2
Fredrik Lundh wrote:
Alex Martelli wrote:

Can anybody suggest where to find (within the standard library) or how
to easily make (e.g. in a C extension) a type without a __mro__, except
for those (such as types.InstanceType) which are explicitly recorded in
the dispatch table copy._deepcopy_dispatch...?

something like this?

import re
x = re.compile("")
x
<_sre.SRE_Pattern object at 0x00B2F7A0>
x.__mro__
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: __mro__
import copy
type(x) in copy._deepcopy_dispatch


False

</F>


Unfortunately, it *does* have a __deepcopy__ method, so I don't think it
actually triggers the bug Alex is interested in:

Py> import re
Py> x = re.compile("")
Py> x.__mro__
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: __mro__
Py> from copy import deepcopy
Py> deepcopy(x)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "C:\Python24\lib\copy.py", line 172, in deepcopy
y = copier(memo)
TypeError: cannot deepcopy this pattern object
Py> x.__deepcopy__
<built-in method __deepcopy__ of _sre.SRE_Pattern object at 0x00B242C0>

Cheers,
Nick.

--
Nick Coghlan | nc******@email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Jul 18 '05 #3
Fredrik Lundh <fr*****@pythonware.com> wrote:
Alex Martelli wrote:
Can anybody suggest where to find (within the standard library) or how
to easily make (e.g. in a C extension) a type without a __mro__, except ^^^^^^
for those (such as types.InstanceType) which are explicitly recorded in
the dispatch table copy._deepcopy_dispatch...?


something like this?
import re
x = re.compile("")
x <_sre.SRE_Pattern object at 0x00B2F7A0> x.__mro__ Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: __mro__


Alas, no -- wish it were so easy! It does need to be a TYPE, not just
any object, to reproduce the bug that was reported about 2.3.5c1's
copy.py. The relevant code in copy.py does the equivalent of:
type(x).__mro__ (<type '_sre.SRE_Pattern'>, <type 'object'>)


not of just x.__mro__, which would easily be made to fail.

How a type(whatever) can end up without a __mro__ in 2.3.* is rather
murky to me; looks like something strange must be happening wrt the
type's flags, or something. Normal types such as _sre.SRE_Pattern, or
the Copyable type I put in _testcapi, just sprout an __mro__ without
even trying. Ah well, thanks anyway -- guess I'll commit the fix (using
inspect.getmro(cls) rather than cls.__mro__) even though I don't have a
unit test to show it's necessary and sufficient:-(
Alex
Jul 18 '05 #4
On Sat, Feb 05, 2005 at 11:37:10AM +0100, Alex Martelli wrote:
Can anybody suggest where to find (within the standard library) or how
to easily make (e.g. in a C extension) a type without a __mro__, except
for those (such as types.InstanceType) which are explicitly recorded in
the dispatch table copy._deepcopy_dispatch...?


would this do what you need?

class C(type):
def __getattribute__(self, attr):
if attr == '__mro__':
raise AttributeError, "What, *me*, a __mro__? Nevah!"
return super(C, self).__getattribute__(attr)

class D(object):
__metaclass__ = C

instances of D have a type that behaves as if it didn't have a
__mro__. This isn't exactly what you asked for, but it might be
enough.

--
John Lenton (jo**@grulic.org.ar) -- Random fortune:
El tiempo cura los dolores y las querellas porque cambiamos. Ya no somos la
misma persona.
-- Blaise Pascal. (1600-1662) Filósofo y escritor francés.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)

iD8DBQFCBbuNgPqu395ykGsRAiaJAJ9jAWy3eB2ih8xUXnjvWt cXk5qOiACcC+Z5
BLOF4ECf5gwqiFj5SqgHwEc=
=KyH9
-----END PGP SIGNATURE-----

Jul 18 '05 #5
John Lenton <jo**@grulic.org.ar> wrote:
class C(type):
def __getattribute__(self, attr):
if attr == '__mro__':
raise AttributeError, "What, *me*, a __mro__? Nevah!"
return super(C, self).__getattribute__(attr)

class D(object):
__metaclass__ = C


Yay -- *exactly*!!! This simple trick reproduces the problem AND shows
that using inspect.getmro fixes it. EXACTLY what we needed. Thanks!!!
Now I can make a proper patch with unittest as well as the fix.
Alex
Jul 18 '05 #6

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

Similar topics

21
4493
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
6
2422
by: Jon Slaughter | last post by:
Is there any way to get the class type from a pointer? I'm working on a class that acts as a set: I have two classes, one called FSet and the other called Element. FSet inherets Element and...
5
2048
by: Fabio Fracassi | last post by:
Hi, I belive what i am trying to do is not possible, but I hope someone can change my mind. Here is some code i'd like to write: template <class type> class engine1 {}; template <class...
51
4455
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is...
59
3404
by: Michael C | last post by:
eg void DoIt() { int i = FromString("1"); double d = FromString("1.1"); } int FromString(string SomeValue) {
669
25400
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Languageâ€, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
7
7798
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}"...
11
32226
by: Frederic Rentsch | last post by:
Hi all, If I derive a class from another one because I need a few extra features, is there a way to promote the base class to the derived one without having to make copies of all attributes? ...
21
3557
by: Chad | last post by:
Okay, so like recently the whole idea of using a Union in C finally sunk into my skull. Seriously, I think it probably took me 2 years to catch on what a Union really is. Belated, I mentioned this...
0
7153
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...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7517
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5676
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,...
0
4743
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...
0
3230
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...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1583
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.