473,626 Members | 3,246 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Package name with '.' in them: Python Bug ?

Hi,

Under some naming conditions of module files, it seems that python lost
class static variables values.
It seems only to append when importing a "badly" named module that
itself import a module with a static variable in it (looks complex, but
see the example below, pretty simple):

First a working example:
<<<< file: aTest.py
#! /usr/bin/env python
import imp
import A
name='B'
fp, pathname, description = imp.find_module (name)
B=imp.load_modu le(name, fp, pathname, description)
a=A.A(1)
b=B.B()

<<<< file: A.py
class A:
a=None
def __init__(self,a =None):
if (a):
A.a=a
def __str__(self):
return str(A.a)
<<<< file: B.py
import A
class B:
def __init__(self):
a=A.A()
print a


Execution:
$ ./aTest.py
1

The value 1 obtained as expected (A.a is a static value and keept as such).

If now I just *rename* file B to B.1, without any change in the code
(except name='B' become name='B.1' in aTest.py), content of B.1.py file
being exacty the same as content of B.py, I get:

$ ./aTest.py
None

Renaming B.py to B.1.py made A unable to keeps the value of it's static
variable.

Bug tested with:
Python 1.5.2
Python 2.2.2
Python 2.3.3

Any idea ? Is it well a bug ? Some feature I didnt understood ? I read
about submodule naminig using dots as separator, but I cant relate it
towhat I saw here.

Thanks.

Yannick

--
_/ Yannick Patois \______________ _______________ _______________ _______
| web: http://feelingsurfer.net/garp/ | Garp sur irc undernet |
| email: pa****@calvix.o rg | |
| ATTAC dans le Pays de Gex: http://attacgex.ouvaton.org |
Jul 18 '05 #1
23 2146

"Yannick Patois" <pa****@calvix. org> wrote in message
news:c3******** **@sunnews.cern .ch...
Hi,

Under some naming conditions of module files, it seems that python lost
class static variables values.
Since 'static variable' is not a Python concept, I do not know what you
mean.
It seems only to append when importing a "badly" named module that
itself import a module with a static variable in it (looks complex, but
see the example below, pretty simple):

First a working example:
<<<< file: aTest.py
#! /usr/bin/env python
import imp
import A
name='B'
fp, pathname, description = imp.find_module (name)
B=imp.load_modu le(name, fp, pathname, description)
a=A.A(1)
b=B.B()
>>>>
<<<< file: A.py
class A:
a=None
def __init__(self,a =None):
if (a):
A.a=a
def __str__(self):
return str(A.a) >>>>
<<<< file: B.py
import A
class B:
def __init__(self):
a=A.A()
print a >>>>

Execution:
$ ./aTest.py
1

The value 1 obtained as expected (A.a is a static value and keept as

such).

Given that you rebound A.a to 1 from None, I especially do not know what
you mean.
If now I just *rename* file B to B.1, without any change in the code
(except name='B' become name='B.1' in aTest.py), content of B.1.py file
being exacty the same as content of B.py, I get:
As you seem to be aware, Python expects modules to have a 'nice' name
consisting of
legal_Python_in dentifier.py. If you evade this, you are a bit on your own
and may run into untested corner cases.
$ ./aTest.py
None

Renaming B.py to B.1.py made A unable to keeps the value of it's static
variable.
It seems to me that it *kept* its original value. I would put more print
statements into both A.A and B.B and maybe even aTest.py to see where
execution first differs.
Bug tested with:
Python 1.5.2
Python 2.2.2
Python 2.3.3

Any idea ? Is it well a bug ? Some feature I didnt understood ? I read
about submodule naminig using dots as separator, but I cant relate it
towhat I saw here.


import b.b1 would normally mean import file (module) b1 in the b package
directory. But imp obviously did not find fine '1' in a 'B' directory.

Terry J. Reedy


Jul 18 '05 #2
Hi,
Terry Reedy wrote:
"Yannick Patois" <pa****@calvix. org> wrote in message
Under some naming conditions of module files, it seems that python lost
class static variables values. Since 'static variable' is not a Python concept, I do not know what you
mean.


As you're so smart and you read the following, how do you call 'a' in
class A ?
That's what I want to qualify.

The value 1 obtained as expected (A.a is a static value and keept as

such).
Given that you rebound A.a to 1 from None, I especially do not know what
you mean.


I dont understand.

If now I just *rename* file B to B.1, without any change in the code
(except name='B' become name='B.1' in aTest.py), content of B.1.py file
being exacty the same as content of B.py, I get:


As you seem to be aware, Python expects modules to have a 'nice' name
consisting of
legal_Python_in dentifier.py.


Which are ? Where to find this info ?
If you evade this, you are a bit on your own
and may run into untested corner cases.
Untested area contains bugs, that's why it's interesting to test them.
$ ./aTest.py
None
Renaming B.py to B.1.py made A unable to keeps the value of it's static
variable.


It seems to me that it *kept* its original value.


However you called it, it doesnt behave the same (and doesnt behave the
way python should work). The change is only a file name, no more.
I would put more print
statements into both A.A and B.B and maybe even aTest.py to see where
execution first differs.
It breaks in B, if I remember well.
Any idea ? Is it well a bug ? Some feature I didnt understood ? I read
about submodule naminig using dots as separator, but I cant relate it
towhat I saw here.


import b.b1 would normally mean import file (module) b1 in the b package
directory.


It's not the case here: would have it be that the import statement would
have broke with "cant find that module".
But imp obviously did not find fine '1' in a 'B' directory.


It didnt seek for it, it just loaded 'B.1' as asked. Everything seems to
work, except that some internal python dictionnary has likely been
corrupted.

Correct behavior would either be "break on 'cant import module 1 from
B'" or "correctly import B.1 without corrupting anything."

To me, that's look to be a bug.

Yannick

--
_/ Yannick Patois \______________ _______________ _______________ _______
| web: http://feelingsurfer.net/garp/ | Garp sur irc undernet |
| email: pa****@calvix.o rg | |
| ATTAC dans le Pays de Gex: http://attacgex.ouvaton.org |
Jul 18 '05 #3
On Fri, 19 Mar 2004 19:12:03 +0100, Yannick Patois wrote:
Hi,
Terry Reedy wrote:
"Yannick Patois" <pa****@calvix. org> wrote in message
Under some naming conditions of module files, it seems that python lost
class static variables values. Since 'static variable' is not a Python concept, I do not know what you
mean.


As you're so smart and you read the following, how do you call 'a' in
class A ?
That's what I want to qualify.


There's no need to be hostile. I'd call it a class variable. It's
certainly not static in any sense of the word I'd use.
The value 1 obtained as expected (A.a is a static value and keept as

such).
Given that you rebound A.a to 1 from None, I especially do not know
what you mean.


I dont understand.


You explicitly construct the A instance with 1, so what do you expect to
happen? A.a is set to None as a class variable, then changed to 1 by the
constructor.
If now I just *rename* file B to B.1, without any change in the code
(except name='B' become name='B.1' in aTest.py), content of B.1.py file
being exacty the same as content of B.py, I get:
As you seem to be aware, Python expects modules to have a 'nice' name
consisting of
legal_Python_in dentifier.py.


Which are ? Where to find this info ?


Check the Python Reference Manual.
If you evade this, you are a bit on your own
and may run into untested corner cases.
Untested area contains bugs, that's why it's interesting to test them.


The only potential bug here is that perhaps python should have raised an
error when the import happened.
To me, that's look to be a bug.

Yannick


I think the bug is in how you understand Python, not in python.

Stephen
Jul 18 '05 #4

"Yannick Patois" <pa****@calvix. org> wrote in message
news:c3******** **@sunnews.cern .ch...
Hi,

Under some naming conditions of module files, it seems that python lost
class static variables values.
There is no such thing as a static variable in Python.
It seems only to append when importing a "badly" named module that
itself import a module with a static variable in it (looks complex, but
see the example below, pretty simple):
There is no such thing as a static variable in Python.

First a working example:
<<<< file: aTest.py
#! /usr/bin/env python
import imp
import A
name='B'
fp, pathname, description = imp.find_module (name)
B=imp.load_modu le(name, fp, pathname, description)
a=A.A(1)
b=B.B()
>>>>
<<<< file: A.py
class A:
a=None
def __init__(self,a =None):
if (a):
A.a=a
def __str__(self):
return str(A.a) >>>>
<<<< file: B.py
import A
class B:
def __init__(self):
a=A.A()
print a >>>>

Execution:
$ ./aTest.py
1

The value 1 obtained as expected (A.a is a static value and keept as

such).
If now I just *rename* file B to B.1, without any change in the code
(except name='B' become name='B.1' in aTest.py), content of B.1.py file
being exacty the same as content of B.py, I get:
[...]

"B.1" is not a legal file name from an import perspective. The manual
description for find_module says: "The function does not handle hierarchical
module names (names containing dots)."

John Roth


Thanks.

Yannick

Jul 18 '05 #5

"Yannick Patois" <pa****@calvix. org> wrote in message
news:c3******** **@sunnews.cern .ch...
Terry Reedy wrote:
Since 'static variable' is not a Python concept, I do not know what you
mean.
how do you call 'a' in class A ?
(non-method) attribute, possibly 'member' (of class A).

Given that you rebound A.a to 1 from None, I especially do not know what you mean.


I dont understand.


When you call this class
<<<< file: A.py
class A:
a=None
def __init__(self,a =None):
if (a):
A.a=a
with the statement a=A.A(1), you enter __init__ with a==1, trigger the
if(a) [if(1)]conditional and execute A.a=a [A.a=1], thereby rebinding class
attribute a from None, as originally set, to 1. Leaving aside the misuse
of 'static' in C, 'static' usually means 'unchanging', whereas you change
A.a.

As you seem to be aware, Python expects modules to have a 'nice' name
consisting of
legal_Python_in dentifier.py.


Which are ? Where to find this info ?


Tutorial I presume, definitely Reference Manual under 'names' and/or
'identifiers'.
If you evade this, you are a bit on your own
and may run into untested corner cases.


Untested area contains bugs, that's why it's interesting to test them.


Yes, I have done similar things.
I would put more print statements into both A.A and B.B
and maybe even aTest.py to see where execution first differs.


To restate: if I were to investigate further, I would add prints
statements, since that has so far worked well for me with Python. What you
do is up to you.
It breaks in B, if I remember well.


In what way? Since the line 'a=A.A(1)', should still rebind A.a, it is
puzzling that the following line 'b=B.B()' should act any differently. The
only thing I can think of is that the name change caused the line 'import
A' to import A under a different internal name so that module A in B is no
longer that same as module A in aTest. I would test this by putting 'print
id(A)' in both files after the imports. I would also add

import sys
print sys.modules

to the end of aTest. I suspect that this will clear up the mystery.

Terry J. Reedy

Jul 18 '05 #6
Yannick Patois <pa****@calvix. org> wrote in message news:<c3******* ***@sunnews.cer n.ch>...
If now I just *rename* file B to B.1, without any change in the code
(except name='B' become name='B.1' in aTest.py), content of B.1.py file
being exacty the same as content of B.py, I get:
.....
Any idea ? Is it well a bug ? Some feature I didnt understood ? I read
about submodule naminig using dots as separator, but I cant relate it
towhat I saw here.


Salut Yannick:

J'ose offrir une réponse....

I work with submodules. They're in some folder, which has subfolders
(call them fbm, uti, and a few others). If I place an empty file in
my root folder called __init__.py, I can refer to modules in the
subfolders by a dot notation. For example, if I have a file
process.py in subfolder fbm, I can say:
import fbm.process

I speculate that when you renamed B to B.1, Python interpreted your
intent to import a module called 1 in subfolder B.

J'espère que cela aide un peu!

Stewart
Jul 18 '05 #7
Stephen Robert Norris wrote:
On Fri, 19 Mar 2004 19:12:03 +0100, Yannick Patois wrote:
The only potential bug here is that perhaps python should have raised an
error when the import happened.


The doc explicitly says:
"his function does not handle hierarchical module names (names
containing dots)."

So you may say that it's up to the programmer to comply with the doc,
and shouldnt expect things to work well if not.

But, I dont want to handle a hierarchical module: just a non
hierarchical one that appends to have a dot in it's name, and it's not
so explicit that this would break.

The fact that it "almost" work is even more strange, and error prone.

But I dont know, I wont say that this has to be fixed in any way outside
of the programmer's code.

Yannick

--
_/ Yannick Patois \______________ _______________ _______________ _______
| web: http://feelingsurfer.net/garp/ | Garp sur irc undernet |
| email: pa****@calvix.o rg | |
| ATTAC dans le Pays de Gex: http://attacgex.ouvaton.org |
Jul 18 '05 #8
Terry Reedy wrote:
"Yannick Patois" <pa****@calvix. org> wrote in message To restate: if I were to investigate further, I would add prints
statements, since that has so far worked well for me with Python. What you
do is up to you.


Since it seems to be definitly illegal, it would only worth further work
if I would somehow 'fix' python to work with this kind of names, which
is both above my skills and likely unwanted. Having a clear error
message at import time would be nice anyway.

Yannick

--
_/ Yannick Patois \______________ _______________ _______________ _______
| web: http://feelingsurfer.net/garp/ | Garp sur irc undernet |
| email: pa****@calvix.o rg | |
| ATTAC dans le Pays de Gex: http://attacgex.ouvaton.org |
Jul 18 '05 #9
> But I dont know, I wont say that this has to be fixed in any way outside
of the programmer's code.


I've never seen python modules named like: valid_name.1.py

Considering that you have shown that it does not work, and that there is
documentation stating that it is not /meant/ to work, then the error is
in the way /you/ name /your/ modules, not the way Python handles
/incorrectly/ named modules.

- Josiah
Jul 18 '05 #10

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

Similar topics

23
2210
by: Rotem | last post by:
Hi, while working on something in my current project I have made several improvements to the logging package in Python, two of them are worth mentioning: 1. addition of a logging record field %(function)s, which results in the name of the entity which logged the record. My version even deduces the class name in the case which the logger is a bound method, and assuming the name of the "self" variable is indeed "self".
2
3224
by: 63q2o4i02 | last post by:
Hi, I'm using python 2.4 and windows XP. I have two packages in the windows version of python in site-packages. They are PyVisa and ctypes, and both live in c:\python24\lib\site-packages I'd like to move these to the cygwin version of python on the same system. I tried copying the PyVisa and ctypes directorices (including
11
3848
by: fortepianissimo | last post by:
Say I have the following package organization in a system I'm developing: A |----B |----C |----D I have a module, say 'foo', that both package D and B require. What is the best practice in terms of creating a 'common' package that hosts
14
1564
by: ToddLMorgan | last post by:
Summary: How should multiple (related) projects be arranged (structured) and configured so that the following is possible: o Sharing common code (one of the projects would be a "common" project referenced by all others and likely the others would share at least the common project and possibly more as times goes on) o Clear separation of "production" code and "test" code (ie to readily ship source and test as separate components. Usually...
2
8196
by: patrimith | last post by:
Hi List, I am used to the following with Java: import some.package.MyClass; name = MyClass.class.getName(); The value for name will be "some.package.MyClass". For Python, I find:
0
856
by: Tim Cook | last post by:
Hi All, I would like feedback on the proper/best 'Pythonic' approach. This is a rather subjective question. Where is the trade-off between package name lengths and faithfulness to the specifications? I am implementing a set of specifications for healthcare IT for Python
0
1061
by: Roger Ineichen | last post by:
Hi Tim For a usecase like this, I personaly recommend to defina all interfaces in one module which probably is a namespace if you need alot of interfaces to define. e.g. openehr.interfaces.foobar.IFooBar
7
2181
by: alito | last post by:
Hi all, I am new to using packages to group my modules. I can't figure out how to run a module that uses relative imports without writing a wrapper that imports that module. Everything I try it complains that I am attempting a relative import in a non-package. eg ~/python/testpackage$ ls config.py importer.py __init__.py
0
1914
by: Steven Samuel Cole | last post by:
Hi Stephane, thanks for your reply! :-) I do not get any notification or warning or whatever from dpkg, all output I get when running # sudo dpkg -i python-<package name>_0.0.1-4927-1_all.deb is Selecting previously deselected package python-<package name>. (Reading database ... 15026 files and directories currently installed.)
0
8203
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
8711
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...
0
8642
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8512
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7203
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...
0
4094
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
4206
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1515
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.