#63 Play nicely with mxDateTime

closed-accepted
None
5
2014-08-26
2006-05-06
No

Related to version:

URL: http://svn.colorstudy.com/SQLObject/trunk
Repository UUID: 95a46c32-92d2-0310-94a5-8d71aeb3d4b3
Revision: 1748

Fixes problem:

File "/usr/local/zope/python/lib/python2.4/site-packages/
SQLObject-0.8dev_r1746-py2.4.egg/sqlobject/col.py", line 63, in ?
TimeType = type(DateTime.Time())
AttributeError: 'module' object has no attribute 'Time'

Description:

I'm using SQLObject with Zope and Python 2.4.
Path to Time is longer than the original code was expecting. And it
needs a param - DateTime.

Patch:

--- col.py.orig 2006-05-05 19:30:39.000000000 +0100
+++ col.py 2006-05-06 16:02:33.000000000 +0100
@@ -60,7 +60,7 @@

if mxdatetime_available:
DateTimeType = type(DateTime.now())
- TimeType = type(DateTime.Time())
+ TimeType = type(DateTime.DateTime.Time(DateTime.DateTime()))

if datetime_available:
default_datetime_implementation = DATETIME_IMPLEMENTATION

Discussion

  • Oleg Broytman

    Oleg Broytman - 2006-05-18

    Logged In: YES
    user_id=4799

    The patch is invalid. Try this from the command line:

    > python
    Python 2.4.3 (#2, Apr 4 2006, 23:50:10)
    [GCC 3.3.5 (Debian 1:3.3.5-13)] on linux2
    Type "help", "copyright", "credits" or "license" for more
    information.
    >>> from mx import DateTime
    >>> print type(DateTime.Time())
    <type 'DateTimeDelta'>
    >>> print type(DateTime.DateTime.Time(DateTime.DateTime()))
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    AttributeError: 'builtin_function_or_method' object has no
    attribute 'Time'

     
  • Oleg Broytman

    Oleg Broytman - 2006-05-18
    • status: open --> open-invalid
     
  • Russ Ferriday

    Russ Ferriday - 2006-05-18

    Logged In: YES
    user_id=478441

    Thanks for looking at this.

    OK, so at my command line, explicitly using the same python:

    /usr/local/zope/python/bin/python
    Python 2.4.2 (#1, Mar 28 2006, 15:06:21)
    [GCC 4.0.0 20041026 (Apple Computer, Inc. build 4061)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import DateTime
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    ImportError: No module named DateTime

    We see that DateTime is not importable.

    But this is not relevant to the case when I'm running in zope.

    This is where pdb comes into its own. I think this adventure is self
    explanatory:

    /usr/local/zope/python/lib/python2.4/site-packages/
    SQLObject-0.8dev_r1746-py2.4.egg/sqlobject/col.py
    > /usr/local/zope/python/lib/python2.4/site-packages/
    SQLObject-0.8dev_r1746-py2.4.egg/sqlobject/col.py(39)?()
    -> try:
    (Pdb) l
    34 from util.backports import count
    35
    36 NoDefault = sqlbuilder.NoDefault
    37 True, False = 1==1, 0==1
    38 import pdb; pdb.set_trace()
    39 -> try:
    40 import datetime
    41 except ImportError: # Python 2.2
    42 datetime_available = False
    43 else:
    44 datetime_available = True
    (Pdb) import datetime
    (Pdb) n
    > /usr/local/zope/python/lib/python2.4/site-packages/
    SQLObject-0.8dev_r1746-py2.4.egg/sqlobject/col.py(40)?()
    -> import datetime
    (Pdb) n
    > /usr/local/zope/python/lib/python2.4/site-packages/
    SQLObject-0.8dev_r1746-py2.4.egg/sqlobject/col.py(44)?()
    -> datetime_available = True
    (Pdb) l
    39 try:
    40 import datetime
    41 except ImportError: # Python 2.2
    42 datetime_available = False
    43 else:
    44 -> datetime_available = True
    45
    46 try:
    47 from mx import DateTime
    48 except ImportError:
    49 try:
    (Pdb) n
    > /usr/local/zope/python/lib/python2.4/site-packages/
    SQLObject-0.8dev_r1746-py2.4.egg/sqlobject/col.py(46)?()
    -> try:
    (Pdb) l
    41 except ImportError: # Python 2.2
    42 datetime_available = False
    43 else:
    44 datetime_available = True
    45
    46 -> try:
    47 from mx import DateTime
    48 except ImportError:
    49 try:
    50 import DateTime # old version of mxDateTime
    51 except ImportError:
    (Pdb) from mx import DateTime
    *** ImportError: No module named mx
    (Pdb) import DateTime
    (Pdb) n
    > /usr/local/zope/python/lib/python2.4/site-packages/
    SQLObject-0.8dev_r1746-py2.4.egg/sqlobject/col.py(47)?()
    -> from mx import DateTime
    (Pdb) n
    ImportError: 'No module named mx'
    > /usr/local/zope/python/lib/python2.4/site-packages/
    SQLObject-0.8dev_r1746-py2.4.egg/sqlobject/col.py(47)?()
    -> from mx import DateTime
    (Pdb) s
    > /usr/local/zope/python/lib/python2.4/site-packages/
    SQLObject-0.8dev_r1746-py2.4.egg/sqlobject/col.py(48)?()
    -> except ImportError:
    (Pdb) s
    > /usr/local/zope/python/lib/python2.4/site-packages/
    SQLObject-0.8dev_r1746-py2.4.egg/sqlobject/col.py(49)?()
    -> try:
    (Pdb) l
    44 datetime_available = True
    45
    46 try:
    47 from mx import DateTime
    48 except ImportError:
    49 -> try:
    50 import DateTime # old version of mxDateTime
    51 except ImportError:
    52 mxdatetime_available = False
    53 else:
    54 mxdatetime_available = True
    (Pdb) n
    > /usr/local/zope/python/lib/python2.4/site-packages/
    SQLObject-0.8dev_r1746-py2.4.egg/sqlobject/col.py(50)?()
    -> import DateTime # old version of mxDateTime
    (Pdb) n
    > /usr/local/zope/python/lib/python2.4/site-packages/
    SQLObject-0.8dev_r1746-py2.4.egg/sqlobject/col.py(54)?()
    -> mxdatetime_available = True
    (Pdb) n
    > /usr/local/zope/python/lib/python2.4/site-packages/
    SQLObject-0.8dev_r1746-py2.4.egg/sqlobject/col.py(58)?()
    -> DATETIME_IMPLEMENTATION = "datetime"
    (Pdb) l
    53 else:
    54 mxdatetime_available = True
    55 else:
    56 mxdatetime_available = True
    57
    58 -> DATETIME_IMPLEMENTATION = "datetime"
    59 MXDATETIME_IMPLEMENTATION = "mxDateTime"
    60
    61 if mxdatetime_available:
    62 DateTimeType = type(DateTime.now())
    63 TimeType = type(DateTime.DateTime.Time(DateTime.DateTime()))
    (Pdb) n
    > /usr/local/zope/python/lib/python2.4/site-packages/
    SQLObject-0.8dev_r1746-py2.4.egg/sqlobject/col.py(59)?()
    -> MXDATETIME_IMPLEMENTATION = "mxDateTime"
    (Pdb) n
    > /usr/local/zope/python/lib/python2.4/site-packages/
    SQLObject-0.8dev_r1746-py2.4.egg/sqlobject/col.py(61)?()
    -> if mxdatetime_available:
    (Pdb) l
    56 mxdatetime_available = True
    57
    58 DATETIME_IMPLEMENTATION = "datetime"
    59 MXDATETIME_IMPLEMENTATION = "mxDateTime"
    60
    61 -> if mxdatetime_available:
    62 DateTimeType = type(DateTime.now())
    63 TimeType = type(DateTime.DateTime.Time(DateTime.DateTime()))
    64
    65 if datetime_available:
    66 default_datetime_implementation = DATETIME_IMPLEMENTATION
    (Pdb) n
    > /usr/local/zope/python/lib/python2.4/site-packages/
    SQLObject-0.8dev_r1746-py2.4.egg/sqlobject/col.py(62)?()
    -> DateTimeType = type(DateTime.now())
    (Pdb) n
    > /usr/local/zope/python/lib/python2.4/site-packages/
    SQLObject-0.8dev_r1746-py2.4.egg/sqlobject/col.py(63)?()
    -> TimeType = type(DateTime.DateTime.Time(DateTime.DateTime()))
    (Pdb) n
    > /usr/local/zope/python/lib/python2.4/site-packages/
    SQLObject-0.8dev_r1746-py2.4.egg/sqlobject/col.py(65)?()
    -> if datetime_available:

    ...

    (Pdb) print sys.modules['DateTime']
    <module 'DateTime' from '/usr/local/zope/zope/lib/python/DateTime/
    __init__.pyc'>
    (Pdb)

    As you can see, in this context the changes give a valid Time Type, and
    SQLObject runs nicely. Without the changes, I get the message in the original
    post.

    I think my title was very misleading. What I'm actually doing is accomodating
    the "old" DateTime module, as mentioned on line 50.

    Is there some middle ground to make it possible to run SQLObject in Zope
    without patches?

     
  • Oleg Broytman

    Oleg Broytman - 2006-05-18
    • status: open-invalid --> open
     
  • Oleg Broytman

    Oleg Broytman - 2006-05-18

    Logged In: YES
    user_id=4799

    Can you instead of your fix replace the line 50

    import DateTime

    with

    from DateTime import DateTime

    and test if this helps?

     
  • Russ Ferriday

    Russ Ferriday - 2006-05-18

    Logged In: YES
    user_id=478441

    Yes, it helps as far as line 63 when I get this:
    File "/usr/local/zope/python/lib/python2.4/site-packages/
    SQLObject-0.8dev_r1746-py2.4.egg/sqlobject/col.py", line 63, in ?
    DateTimeType = type(DateTime.now())
    AttributeError: class DateTime has no attribute 'now'

     
  • Ian Bicking

    Ian Bicking - 2006-05-18

    Logged In: YES
    user_id=210337

    Does the confusion here have something to do with Zope's
    DateTime module being confused for mx.DateTime? I don't
    think we support Zope's DateTime at all; adding support for
    that would be separate, though I think mx.DateTime is
    usually available where Zope's DateTime is also available.

    Anyway, on some level this is definitely a problem with
    imports, not with the code. "DateTime" is meant to be the
    mx DateTime module, which contains "now", etc.; if it
    doesn't contain that, then the import is wrong, not the code.

     
  • Russ Ferriday

    Russ Ferriday - 2006-05-18

    Logged In: YES
    user_id=478441

    Thanks for your patience, Oleg, Ian,

    > Does the confusion here have something to do with Zope's
    > DateTime module being confused for mx.DateTime?

    Digging deeper, yes it does.
    It's Zope's DateTime.py that ends up being imported.

    (Pdb) print sys.modules['DateTime']
    <module 'DateTime' from
    '/usr/local/zope/zope/lib/python/DateTime/
    __init__.pyc'>

    When I add that kludge as shown in the original patch, Zope and SQLObject
    work fine together, as far as it goes. And I'm using DateTimeCol in several
    tables without issues.
    I'm open to suggestions as how to best handle this. Any more thoughts?

     
  • Oleg Broytman

    Oleg Broytman - 2006-05-18

    Logged In: YES
    user_id=4799

    Your patch certainly breaks old mx DateTime, so you should
    extend the patch to distinguish old mxDateTime (without mx
    top-level package) and non-mx DateTime from Zope.

    IWBN if you can also run test suite or at least
    test_datetime.py. The test suite requires py.test framework.

     
  • Russ Ferriday

    Russ Ferriday - 2006-05-18

    Logged In: YES
    user_id=478441

    OK. I'll follow up on that, and get back in a day or two.

     
  • Russ Ferriday

    Russ Ferriday - 2006-05-19

    Logged In: YES
    user_id=478441

    Ian/Oleg.
    I have not run your test suite on this - only have the deployment bundle
    which seems not have /tests.
    I don't expect this to interfere with anything else now, since I'm using only an
    exception to get to the new code.
    Best wishes,
    --r

    --- col.py.orig 2006-05-05 19:30:39.000000000 +0100
    +++ col.py 2006-05-19 10:38:18.000000000 +0100
    @@ -47,7 +47,7 @@
    from mx import DateTime
    except ImportError:
    try:
    - import DateTime # old version of mxDateTime
    + import DateTime # old version of mxDateTime, or Zope's Version if
    we're running with Zope
    except ImportError:
    mxdatetime_available = False
    else:
    @@ -60,8 +60,14 @@

    if mxdatetime_available:
    DateTimeType = type(DateTime.now())
    - TimeType = type(DateTime.Time())
    -
    + try:
    + TimeType = type(DateTime.Time())
    + except:
    + # accommodate Zope - which has an extra level in the path to Time.
    + #Note to Ian and Oleg: The type of DateTime.DateTime.Time() is
    + # %H:%M:%S, like the mx implementation.
    + TimeType = type(DateTime.DateTime.Time(DateTime.DateTime()))
    +
    if datetime_available:
    default_datetime_implementation = DATETIME_IMPLEMENTATION

     
  • Oleg Broytman

    Oleg Broytman - 2006-05-19

    Logged In: YES
    user_id=4799

    try:
    TimeType = type(DateTime.Time())
    except:

    I don't like this bare "except". What is the exception -
    TypeError? ValueError? NameError?

     
  • Russ Ferriday

    Russ Ferriday - 2006-05-19

    Logged In: YES
    user_id=478441

    Oleg,
    Yes, quite right. Sloppy of me.
    The exception is
    AttributeError: 'module' object has no attribute 'Time'
    Thanks for your help.
    --r

     
  • Oleg Broytman

    Oleg Broytman - 2006-05-19

    Logged In: YES
    user_id=4799

    Then instead of catching exception just test if
    hasattr(DateTime, "Time").

     
  • Russ Ferriday

    Russ Ferriday - 2006-05-19

    Logged In: YES
    user_id=478441

    Fine, thanks, Oleg. Here is it.

    --- col.py.orig 2006-05-05 19:30:39.000000000 +0100
    +++ col.py 2006-05-19 16:11:43.000000000 +0100
    @@ -47,7 +47,7 @@
    from mx import DateTime
    except ImportError:
    try:
    - import DateTime # old version of mxDateTime
    + import DateTime # old version of mxDateTime, or Zope's Version if
    we're running with Zope
    except ImportError:
    mxdatetime_available = False
    else:
    @@ -60,8 +60,11 @@

    if mxdatetime_available:
    DateTimeType = type(DateTime.now())
    - TimeType = type(DateTime.Time())
    -
    + if hasattr(DateTime, "Time"):
    + TimeType = type(DateTime.Time())
    + else:
    + TimeType = type(DateTime.DateTime.Time(DateTime.DateTime()))
    +
    if datetime_available:
    default_datetime_implementation = DATETIME_IMPLEMENTATION
    elif mxdatetime_available:

     
  • Ian Bicking

    Ian Bicking - 2006-05-19

    Logged In: YES
    user_id=210337

    Zope's DateTime isn't really a drop-in replacement for
    mxDateTime, but I suppose it may come close enough. Maybe
    this would work:

    import DateTime
    if hasattr(DateTime, 'Time'):
    # We probably have a Zope DateTime package
    DateTime = DateTime.DateTime

     
  • Russ Ferriday

    Russ Ferriday - 2006-05-19

    Logged In: YES
    user_id=478441

    But in the Zope package now() is in DateTime, and Time() is in
    DateTime.DateTime, so it's incorrect to do the equivalent of
    from DateTime import DateTime
    or what you suggest below, because now() will be undefined.

    And the logic of what you sketch is reversed, anyway.

     
  • Oleg Broytman

    Oleg Broytman - 2006-05-19

    Logged In: YES
    user_id=4799

    Applied in the revision 1785. Thank you!

     
  • Oleg Broytman

    Oleg Broytman - 2006-05-19
    • assigned_to: nobody --> phd
    • status: open --> closed-accepted
     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks