After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 647249 - Memory Error when trying to instantiate a disguised struct
Memory Error when trying to instantiate a disguised struct
Status: RESOLVED FIXED
Product: pygobject
Classification: Bindings
Component: introspection
3.2.x
Other Linux
: Low minor
: GNOME 3.10
Assigned To: Nobody's working on this now (help wanted and appreciated)
Python bindings maintainers
: 680140 (view as bug list)
Depends on: 647250
Blocks: 708060
 
 
Reported: 2011-04-09 07:37 UTC by Thomas Bechtold
Modified: 2014-01-04 16:55 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Script to show memory errors in structs (531 bytes, text/x-python)
2013-02-20 21:45 UTC, Simon Feltman
Details

Description Thomas Bechtold 2011-04-09 07:37:33 UTC
I tried to use GDateTime with python (ubuntu natty) and got the following error:

$ python
Python 2.7.1+ (r271:86832, Mar 24 2011, 00:39:14) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gi
>>> from gi.repository import GLib
>>> GLib.DateTime()
Traceback (most recent call last):
  • File "<stdin>", line 1 in <module>
    MemoryError >>>

Comment 1 Tomeu Vizoso 2011-04-09 07:42:43 UTC
This cannot work, so we should be giving a better error message (and probably having an override for __init__).

Note that in GDateTime's case, it is a boxed but for some reason g-i doesn't realize. See https://bugzilla.gnome.org/show_bug.cgi?id=647250
Comment 2 Johan (not receiving bugmail) Dahlin 2011-04-09 12:21:13 UTC
GLib.DateTime should probably just map to datetime.datetime
Comment 3 Johan (not receiving bugmail) Dahlin 2011-04-10 21:55:41 UTC
(In reply to comment #2)
> GLib.DateTime should probably just map to datetime.datetime

Which can be done by marking it as a foreign type in gobject-introspection, see how cairo.context is handled for an example.
Comment 4 johnp 2011-04-11 15:10:48 UTC
(In reply to comment #3)
> (In reply to comment #2)
> > GLib.DateTime should probably just map to datetime.datetime
> 
> Which can be done by marking it as a foreign type in gobject-introspection, see
> how cairo.context is handled for an example.

Actually it can't unless we all agree that it should be tagged as foreign in glib.  Perhaps we should extend is_foreign to overrides so we can override some bits in python that might not be applicable to all of gi.
Comment 5 Thomas Bechtold 2011-04-22 10:02:08 UTC
i get the same error when i try to create a GTimeZone.

>>> tz = GLib.TimeZone()
Traceback (most recent call last):
  • File "<stdin>", line 1 in <module>
    MemoryError


I don't understand why eg GDate works, but GDateTime and GTimeZone not.
Comment 6 johnp 2011-04-25 23:18:04 UTC
Annotations issues which don't list the true size of the struct so we can't malloc it.  Use GLib.TimeZone.new, new_utc or new_local.  These structs which pretend to be ref counted objects are hard to deal with in a generic way.
Comment 7 Thomas Bechtold 2011-04-26 06:14:23 UTC
doesn't work with new:

>>> from gi.repository import GLib
>>> t = GLib.TimeZone.new()
Traceback (most recent call last):
  • File "<stdin>", line 1 in <module>
AttributeError: type object 'TimeZone' has no attribute 'new'
>>> t = GLib.TimeZone().new()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
MemoryError
>>> t = GLib.TimeZone().new
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
MemoryError
Comment 8 Sebastian Pölsterl 2012-04-24 19:48:37 UTC
I can confirm that with pygobject 3.2.0 the issues mentioned in comment 7 still remain.
Comment 9 Simon Feltman 2013-02-08 11:14:49 UTC
These now seem to work:

tz = GLib.TimeZone.new('foo')
dt = GLib.DateTime.new(tz, 2013, 2, 28, 0, 0, 0.0)
dt.format('%a %A %b %B %c %C %d %e %F')

'Thu Thursday Feb February Thu Feb 28 00:00:00 2013 20 28 28 2013-02-28'

Perhaps the __new__ of these structures could be overridden to use "new" and this bug can be closed out.
Comment 10 Simon Feltman 2013-02-20 21:45:59 UTC
Created attachment 236984 [details]
Script to show memory errors in structs

It seems there are a few of these so instead of specific overrides a general solution should be worked on. I think raising an exception listing the other available constructors would be a good idea. Something like:

>>> dt = GLib.DateTime()
ValueError: DateTime cannot be created directly. Please use one of the following class methods: new, new_local, new_now, new_now_local, ...


The attached script shows the follow have the same problem:

| GLib.Bytes | MemoryError |
| GLib.Checksum | MemoryError |
| GLib.DateTime | MemoryError |
| GLib.HashTable | MemoryError |
| GLib.IOChannel | TypeError |
| GLib.MarkupParseContext | MemoryError |
| GLib.MatchInfo | MemoryError |
| GLib.Regex | MemoryError |
| GLib.Thread | MemoryError |
| GLib.TimeZone | MemoryError |
| GLib.VariantType | MemoryError |
| Gtk.CssSection | MemoryError |
| Gtk.Gradient | MemoryError |
| Gtk.PaperSize | MemoryError |
| Gtk.RecentInfo | MemoryError |
| Gtk.SelectionData | MemoryError |
| Gtk.SymbolicColor | MemoryError |
| Gtk.TargetList | MemoryError |
| Gtk.TreeRowReference | MemoryError |
| Gdk.FrameTimings | MemoryError |
Comment 11 Simon Feltman 2013-02-21 10:54:02 UTC
The fix for this ends up looking exactly like the fix for bug 639972, except for Boxed not Struct. The only addition I would make (in both cases) is to add a more explicit instruction to help the API user out:

ValueError: Cannot create GLib.DateTime, try using a constructor such as: GLib.DateTime.new(...)

Listing all the available constructors would be too much work and the above gives enough of a hint that most people can figure out how to proceed on their own.
Comment 12 Simon Feltman 2014-01-02 04:37:15 UTC
I've committed a change where we now get a TypeError similar to how disguised structs are handled but for boxed and union based objects. The error message gives a nice clue to look at the help which lists out available constructors:
https://git.gnome.org/browse/pygobject/commit/?id=e835984

We now have all the breadcrumbs in place so at least a developer can figure stuff out a little easier:

% ipython
>>> from gi.repository import GLib
>>> tz = GLib.TimeZone()
TypeError: boxed cannot be created directly; try using a constructor, see: help(GLib.TimeZone)

>>> GLib.TimeZone?
:Constructors:
    new(identifier:str=None)
    new_local()
    new_utc()

>>> tz = GLib.TimeZone.new_local()
>>> tz
    <GTimeZone at 0x2e8f600>

I'm going to leave this ticket open pending work on the idea that the default constructor could also dispatch to other constructors based on keyword arguments (essentially constructor overloading based upon keywords passed).
Comment 13 Simon Feltman 2014-01-02 05:05:49 UTC
Dispatching constructor related work will occur in bug 721226.
Comment 14 Simon Feltman 2014-01-04 16:55:19 UTC
*** Bug 680140 has been marked as a duplicate of this bug. ***