summaryrefslogtreecommitdiffstats
path: root/Objects
Commit message (Collapse)AuthorAgeFilesLines
* It's a fact: for binary operators, *under certain circumstances*,Guido van Rossum2001-09-281-28/+48
| | | | | | | | | | | | | __rop__ now takes precendence over __op__. Those circumstances are: - Both arguments are new-style classes - Both arguments are new-style numbers - Their implementation slots for tp_op differ - Their types differ - The right argument's type is a subtype of the left argument's type Also did this for the ternary operator (pow) -- only the binary case is dealt with properly though, since __rpow__ is not supported anyway.
* Ouch. The wrapper for __rpow__ was the same as for __pow__, resultingGuido van Rossum2001-09-281-1/+15
| | | | in bizarre outcomes. Test forthcoming.
* Merge branch changes (coercion, rich comparisons) into trunk.Guido van Rossum2001-09-274-11/+106
|
* add_operators(): the __floordiv__ and __truediv__ descriptors (andGuido van Rossum2001-09-251-0/+10
| | | | | | | their 'i' and 'r' variants) were not being generated if the corresponding nb_ slots were present in the type object. I bet this is because floor and true division were introduced after I last looked at that part of the code.
* - Provisional support for pickling new-style objects. (*)Guido van Rossum2001-09-251-11/+50
| | | | | | | | | | | | | | | | | | | - Made cls.__module__ writable. - Ensure that obj.__dict__ is returned as {}, not None, even upon first reference; it simply springs into life when you ask for it. (*) The pickling support is provisional for the following reasons: - It doesn't support classes with __slots__. - It relies on additional support in copy_reg.py: the C method __reduce__, defined in the object class, really calls calling copy_reg._reduce(obj). Eventually the Python code in copy_reg.py needs to be migrated to C, but I'd like to experiment with the Python implementation first. The _reduce() code also relies on an additional helper function, _reconstructor(), defined in copy_reg.py; this should also be reimplemented in C.
* Change repr() of a new-style class to say <class 'ClassName'> ratherGuido van Rossum2001-09-251-4/+11
| | | | | | than <type 'ClassName'>. Exception: if it's a built-in type or an extension type, continue to call it <type 'ClassName>. Call me a wimp, but I don't want to break more user code than necessary.
* Make __class__ assignment possible, when the object structures are theGuido van Rossum2001-09-251-4/+83
| | | | | | | | | | | | | | same. I hope the test for structural equivalence is stringent enough. It only allows the assignment if the old and new types: - have the same basic size - have the same item size - have the same dict offset - have the same weaklist offset - have the same GC flag bit - have a common base that is the same except for maybe the dict and weaklist (which may have been added separately at the same offsets in both types)
* Make properties discoverable from Python:Tim Peters2001-09-241-25/+56
| | | | | | | | | | | | | - property() now takes 4 keyword arguments: fget, fset, fdel, doc. Note that the real purpose of the 'f' prefix is to make fdel fit in ('del' is a keyword, so can't used as a keyword argument name). - These map to visible readonly attributes 'fget', 'fset', 'fdel', and '__doc__' in the property object. - fget/fset/fdel weren't discoverable from Python before. - __doc__ is new, and allows to associate a docstring with a property.
* Another comparison patch-up: comparing a type with a dynamic metatypeGuido van Rossum2001-09-241-1/+2
| | | | to one with a static metatype raised an obscure error.
* Do the same thing to complex that I did to str: the rich comparisonGuido van Rossum2001-09-241-7/+10
| | | | | function returns NotImplemented when comparing objects whose tp_richcompare slot is not itself.
* Change string comparison so that it applies even when one (or both)Guido van Rossum2001-09-241-3/+4
| | | | | arguments are subclasses of str, as long as they don't override rich comparison.
* Fix the baffler that Tim reported: sometimes the repr() of an objectGuido van Rossum2001-09-241-2/+2
| | | | | looks like <X object at ...>, sometimes it says <X instance at ...>. Make this uniformly say <X object at ...>.
* Generalize file.writelines() to allow iterable objects.Tim Peters2001-09-231-31/+31
|
* Add the __getattr__ hook back. The rules are now:Guido van Rossum2001-09-211-0/+39
| | | | | | | - if __getattribute__ exists, it is called first; if it doesn't exists, PyObject_GenericGetAttr is called first. - if the above raises AttributeError, and __getattr__ exists, it is called.
* Change the name of the __getattr__ special method for new-styleGuido van Rossum2001-09-211-4/+4
| | | | | | | | classes to __getattribute__, to make it crystal-clear that it doesn't have the same semantics as overriding __getattr__ on classic classes. This is a halfway checkin -- I'll proceed to add a __getattr__ hook that works the way it works in classic classes.
* Fix a bug in rendering of \\ by repr() -- it rendered as \\\ insteadGuido van Rossum2001-09-211-0/+1
| | | | of \\.
* Add optional docstrings to getset descriptors. Fortunately, there'sGuido van Rossum2001-09-207-19/+34
| | | | | | | | | | no backwards compatibility to worry about, so I just pushed the 'closure' struct member to the back -- it's never used in the current code base (I may eliminate it, but that's more work because the getter and setter signatures would have to change.) As examples, I added actual docstrings to the getset attributes of a few types: file.closed, xxsubtype.spamdict.state.
* Add optional docstrings to member descriptors. For backwardsGuido van Rossum2001-09-209-34/+57
| | | | | | | | | | | | | | | compatibility, this required all places where an array of "struct memberlist" structures was declared that is referenced from a type's tp_members slot to change the type of the structure to PyMemberDef; "struct memberlist" is now only used by old code that still calls PyMember_Get/Set. The code in PyObject_GenericGetAttr/SetAttr now calls the new APIs PyMember_GetOne/SetOne, which take a PyMemberDef argument. As examples, I added actual docstrings to the attributes of a few types: file, complex, instance method, super, and xxsubtype.spamlist. Also converted the symtable to new style getattr.
* Fix Unicode .join() method to raise a TypeError for sequenceMarc-André Lemburg2001-09-201-1/+11
| | | | | | | | | | elements which are not Unicode objects or strings. (This matches the string.join() behaviour.) Fix a memory leak in the .join() method which occurs in case the Unicode resize fails. Restore the test_unicode output.
* _PyObject_GetDictPtr(): when the offset is negative, always align --Guido van Rossum2001-09-201-11/+6
| | | | we can't trust that tp_basicsize is aligned. Fixes SF bug #462848.
* Implement the changes proposed in patch #413333. unicode(obj) nowMarc-André Lemburg2001-09-201-42/+55
| | | | | works just like str(obj) in that it tries __str__/tp_str on the object in case it finds that the object is not a string or buffer.
* Patch #435971: UTF-7 codec by Brian Quinlan.Marc-André Lemburg2001-09-201-0/+300
|
* SF bug [#463093] File methods need doc strings.Tim Peters2001-09-201-14/+94
| | | | Now they don't.
* Patch #462849: Pass Unicode objects to file's .write method.Martin v. Löwis2001-09-191-3/+8
|
* Add additional coercion support for "self subtypes" to int, long,Guido van Rossum2001-09-193-1/+22
| | | | float (compare the recent checkin to complex). Added tests for these.
* complex_coerce(): add explicit PyComplex_Check() test. Previously,Guido van Rossum2001-09-191-0/+5
| | | | | | | | complex_coerce() would never be called with a complex argument, because PyNumber_Coerce[Ex] doesn't bother calling the type's coercion method if the values already have the same type. But now, of course, it's possible to pass an instance of a complex *subtype*, and those must be accepted.
* Hopefully fix 3-way comparisons. This unfortunately adds yet anotherGuido van Rossum2001-09-182-6/+21
| | | | | | | | hack, and it's even more disgusting than a PyInstance_Check() call. If the tp_compare slot is the slot used for overrides in Python, it's always called. Add some tests that show what should work too.
* Properly repr classes without module names.Martin v. Löwis2001-09-181-1/+1
|
* wrap_cmpfunc(): added a safety check for the __cmp__ wrapper. We canGuido van Rossum2001-09-181-0/+9
| | | | | | only safely call a type's tp_compare slot if the second argument is also an instance of the same type. I hate to think what e.g. int_compare() would do with a second argument that's a float!
* Redo the PyMethod attributes using a dir()-friendly approach, creatingGuido van Rossum2001-09-181-37/+69
| | | | | | | | | | | | | | | | | descriptors for each attribute. The getattr() implementation is similar to PyObject_GenericGetAttr(), but delegates to im_self instead of looking in __dict__; I couldn't do this as a wrapper around PyObject_GenericGetAttr(). XXX A problem here is that this is a case of *delegation*. dir() doesn't see exactly the same attributes that are actually defined; e.g. if the delegate is a Python function object, it supports attributes like func_code etc., but these are not visible to dir(); on the other hand, dynamic function attributes (stored in the function's __dict__) *are* visible to dir(). Maybe we need a mechanism to tell dir() about the delegation mechanism? I vaguely recall seeing a request in the newsgroup for a more formal definition of attribute delegation too. Sigh, time for a new PEP.
* type_new(): Didn't compile anymore, due to change in struct memberlistTim Peters2001-09-181-1/+1
| | | | definition. Guido, what else did you forget to check in <wink>?
* Rewrite function attributes to use the generic routines properly.Guido van Rossum2001-09-171-72/+118
| | | | | This uses the new "restricted" feature of structmember, and getset descriptors for some of the type checks.
* PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,Tim Peters2001-09-171-0/+45
| | | | | | | | | | | | | | | and are lists, and then just the string elements (if any)). There are good and bad reasons for this. The good reason is to support dir() "like before" on objects of extension types that haven't migrated to the class introspection API yet. The bad reason is that Python's own method objects are such a type, and this is the quickest way to get their im_self etc attrs to "show up" via dir(). It looks much messier to move them to the new scheme, as their current getattr implementation presents a view of their attrs that's a untion of their own attrs plus their im_func's attrs. In particular, methodobject.__dict__ actually returns methodobject.im_func.__dict__, and if that's important to preserve it doesn't seem to fit the class introspection model at all.
* merge_class_dict(): Clear the error if __bases__ doesn't exist.Tim Peters2001-09-161-1/+3
|
* A fix for SF bug #461546 (bug in long_mul).Guido van Rossum2001-09-152-14/+20
| | | | | | | | | Both int and long multiplication are changed to be more careful in their assumptions about when one of the arguments is a sequence: the assumption that at least one of the arguments must be an int (or long, respectively) is still held, but the assumption that these don't smell like sequences is no longer true: a subtype of int or long may well have a sequence-repeat thingie!
* tp_new_wrapper(): A subtle change in the check for safe use.Guido van Rossum2001-09-141-1/+1
| | | | Allow staticbase != type, as long as their tp_new slots are the same.
* Add call_maybe(): a variant of call_method() that returnsGuido van Rossum2001-09-141-10/+67
| | | | | | NotImplemented when the lookup fails, and use this for binary operators. Also lookup_maybe() which doesn't raise an exception when the lookup fails (still returning NULL).
* call_method():Guido van Rossum2001-09-141-36/+27
| | | | | | | | | | | | | | | | | | | | | | | - Don't turn a non-tuple argument into a one-tuple. Rather, the caller must pass a format that causes Py_VaBuildValue() to return a tuple. - Speed things up by calling PyObject_Call (which is fairly low-level and straightforward) rather than PyObject_CallObject (which calls PyEval_CallObjectWithKeywords which calls PyObject_Call, and nothing is really done in the mean time except some tests for NULL args and valid types, which are already guaranteed). - Cosmetics. Other places: - Make sure that the format argument to call_method() is surrounded by parentheses, so it will cause a tuple to be created. - Replace a few calls to PyEval_CallObject() with a surefire tuple for args to calls to PyObject_Call(). (A few calls to PyEval_CallObject() remain that have NULL for args.)
* PyObject_CallObject(): this may as well call PyEval_CallObject()Guido van Rossum2001-09-141-16/+1
| | | | | | | | | | | | | | | directly, as the only thing done here (replace NULL args with an empty tuple) is also done there. XXX Maybe we should take one step further and equate the two at the macro level? That's harder though because PyEval_Call* is declared in a header that's not included standard. But it is silly that PyObject_CallObject calls PyEval_CallObject which calls back to PyObject_Call. Maybe PyEval_CallObject should be moved into this file instead? All I know is that there are too many call APIs! The differences between PyObject_Call and PyEval_CallObjectWithKeywords is that the latter allows args to be NULL, and does explicit type checks for args and kwds.
* _PyObject_Dump(): print the type of the object. This is by far theGuido van Rossum2001-09-141-2/+8
| | | | most frequently interesting information IMO. Also tidy up the output.
* The end of [#460467] file objects should be subclassable.Tim Peters2001-09-141-30/+63
| | | | | | | | A surprising number of changes to split tp_new into tp_new and tp_init. Turned out the older PyFile_FromFile() didn't initialize the memory it allocated in all (error) cases, which caused new sanity asserts elsewhere to fail left & right (and could have, e.g., caused file_dealloc to try decrefing random addresses).
* Changed the dict implementation to take "string shortcuts" only whenTim Peters2001-09-141-13/+16
| | | | | | | | | | | | | | | | keys are true strings -- no subclasses need apply. This may be debatable. The problem is that a str subclass may very well want to override __eq__ and/or __hash__ (see the new example of case-insensitive strings in test_descr), but go-fast shortcuts for strings are ubiquitous in our dicts (and subclass overrides aren't even looked for then). Another go-fast reason for the change is that PyCheck_StringExact() is a quicker test than PyCheck_String(), and we make such a test on virtually every access to every dict. OTOH, a str subclass may also be perfectly happy using the base str eq and hash, and this change slows them a lot. But those cases are still hypothetical, while Python's own reliance on true-string dicts is not.
* Get rid of builtin_open() entirely (the C code and docstring, not theTim Peters2001-09-131-2/+2
| | | | | builtin function); Guido pointed out that it could be just another name in the __builtin__ dict for the file constructor now.
* Now that file objects are subclassable, you can get at the file constructorTim Peters2001-09-131-0/+8
| | | | | | just by doing type(f) where f is any file object. This left a hole in restricted execution mode that rexec.py can't plug by itself (although it can plug part of it; the rest is plugged in fileobject.c now).
* type_call(): Change in policy. The keyword args (if any) are now passedTim Peters2001-09-131-1/+1
| | | | | | on to the tp_new slot (if non-NULL), as well as to the tp_init slot (if any). A sane type implementing both tp_new and tp_init should probably pay attention to the arguments in only one of them.
* based upon a suggestion in c.l.py, this slight expansion of theSkip Montanaro2001-09-131-1/+1
| | | | OverflowError message seems reasonable.
* SF bug [#460467] file objects should be subclassable.Tim Peters2001-09-131-23/+100
| | | | Preliminary support. What's here works, but needs fine-tuning.
* Again perhaps the end of [#460020] bug or feature: unicode() and subclasses.Tim Peters2001-09-122-12/+20
| | | | | Inhibited complex unary plus optimization when applied to a complex subtype. Added PyComplex_CheckExact macro. Some comments and minor code fiddling.
* If interning an instance of a string subclass, intern a real string objectTim Peters2001-09-121-4/+20
| | | | | | with the same value instead. This ensures that a string (or string subclass) object's ob_sinterned pointer is always a str (or NULL), and that the dict of interned strings only has strs as keys.
* str_subtype_new, unicode_subtype_new:Tim Peters2001-09-122-15/+26
| | | | | | | | + These were leaving the hash fields at 0, which all string and unicode routines believe is a legitimate hash code. As a result, hash() applied to str and unicode subclass instances always returned 0, which in turn confused dict operations, etc. + Changed local names "new"; no point to antagonizing C++ compilers.