Skip to content

Commit 913ef60

Browse files
author
rhettinger
committed
* Migrate set() and frozenset() from the sandbox.
* Install the unittests, docs, newsitem, include file, and makefile update. * Exercise the new functions whereever sets.py was being used. Includes the docs for libfuncs.tex. Separate docs for the types are forthcoming. git-svn-id: http://svn.python.org/projects/python/trunk@34647 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 474ed9e commit 913ef60

21 files changed

Lines changed: 2338 additions & 40 deletions

Doc/lib/libfuncs.tex

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,17 @@ \section{Built-in Functions \label{built-in-funcs}}
477477
and is known to vary.}
478478
\end{funcdesc}
479479

480+
\begin{funcdesc}{frozenset}{\optional{iterable}}
481+
Return a frozenset object whose elements are taken from \var{iterable}.
482+
Frozensets are sets that have no update methods but can be hashed and
483+
used as members of other sets or as dictionary keys. The elements of
484+
a frozenset must be immutable themselves. To represent sets of sets,
485+
the inner sets should also be \class{frozenset} objects. If
486+
\var{iterable} is not specified, returns a new empty set,
487+
\code{frozenset([])}.
488+
\versionadded{2.4}
489+
\end{funcdesc}
490+
480491
\begin{funcdesc}{getattr}{object, name\optional{, default}}
481492
Return the value of the named attributed of \var{object}. \var{name}
482493
must be a string. If the string is the name of one of the object's
@@ -897,6 +908,14 @@ \section{Built-in Functions \label{built-in-funcs}}
897908
\code{round(0.5)} is \code{1.0} and \code{round(-0.5)} is \code{-1.0}).
898909
\end{funcdesc}
899910

911+
\begin{funcdesc}{set}{\optional{iterable}}
912+
Return a set whose elements are taken from \var{iterable}. The elements
913+
must be immutable. To represent sets of sets, the inner sets should
914+
be \class{frozenset} objects. If \var{iterable} is not specified,
915+
returns a new empty set, \code{set([])}.
916+
\versionadded{2.4}
917+
\end{funcdesc}
918+
900919
\begin{funcdesc}{setattr}{object, name, value}
901920
This is the counterpart of \function{getattr()}. The arguments are an
902921
object, a string and an arbitrary value. The string may name an

Include/Python.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
#include "listobject.h"
8787
#include "dictobject.h"
8888
#include "enumobject.h"
89+
#include "setobject.h"
8990
#include "methodobject.h"
9091
#include "moduleobject.h"
9192
#include "funcobject.h"

Include/setobject.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
/* Set object interface */
3+
4+
#ifndef Py_SETOBJECT_H
5+
#define Py_SETOBJECT_H
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
/*
11+
This data structure is shared by set and frozenset objects.
12+
*/
13+
14+
typedef struct {
15+
PyObject_HEAD
16+
PyObject *data;
17+
long hash; /* only used by frozenset objects */
18+
} PySetObject;
19+
20+
PyAPI_DATA(PyTypeObject) PySet_Type;
21+
PyAPI_DATA(PyTypeObject) PyFrozenSet_Type;
22+
23+
#ifdef __cplusplus
24+
}
25+
#endif
26+
#endif /* !Py_SETOBJECT_H */

Lib/_strptime.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from re import compile as re_compile
1717
from re import IGNORECASE
1818
from datetime import date as datetime_date
19-
from sets import ImmutableSet as sets_ImmutableSet
2019
try:
2120
from thread import allocate_lock as _thread_allocate_lock
2221
except:
@@ -165,11 +164,11 @@ def __calc_timezone(self):
165164
time.tzset()
166165
except AttributeError:
167166
pass
168-
no_saving = sets_ImmutableSet(["utc", "gmt", time.tzname[0].lower()])
167+
no_saving = frozenset(["utc", "gmt", time.tzname[0].lower()])
169168
if time.daylight:
170-
has_saving = sets_ImmutableSet([time.tzname[1].lower()])
169+
has_saving = frozenset([time.tzname[1].lower()])
171170
else:
172-
has_saving = sets_ImmutableSet()
171+
has_saving = frozenset()
173172
self.timezone = (no_saving, has_saving)
174173

175174

Lib/test/regrtest.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@
7575
import random
7676
import cStringIO
7777
import warnings
78-
from sets import Set
7978

8079
# I see no other way to suppress these warnings;
8180
# putting them in test_grammar.py has no effect:
@@ -306,7 +305,7 @@ def main(tests=None, testdir=None, verbose=0, quiet=0, generate=0,
306305
e = _ExpectedSkips()
307306
plat = sys.platform
308307
if e.isvalid():
309-
surprise = Set(skipped) - e.getexpected() - Set(resource_denieds)
308+
surprise = set(skipped) - e.getexpected() - set(resource_denieds)
310309
if surprise:
311310
print count(len(surprise), "skip"), \
312311
"unexpected on", plat + ":"
@@ -948,7 +947,7 @@ def __init__(self):
948947
self.valid = False
949948
if sys.platform in _expectations:
950949
s = _expectations[sys.platform]
951-
self.expected = Set(s.split())
950+
self.expected = set(s.split())
952951

953952
if not os.path.supports_unicode_filenames:
954953
self.expected.add('test_pep277')

Lib/test/test___all__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from test import test_support
33

44
from test.test_support import verify, verbose
5-
from sets import Set
65
import sys
76
import warnings
87

@@ -43,8 +42,8 @@ def check_all(self, modname):
4342
exec "from %s import *" % modname in names
4443
if names.has_key("__builtins__"):
4544
del names["__builtins__"]
46-
keys = Set(names)
47-
all = Set(sys.modules[modname].__all__)
45+
keys = set(names)
46+
all = set(sys.modules[modname].__all__)
4847
verify(keys==all, "%s != %s" % (keys, all))
4948

5049
def test_all(self):

Lib/test/test_builtin.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import test.test_support, unittest
44
from test.test_support import fcmp, have_unicode, TESTFN, unlink
5-
from sets import Set
65

76
import sys, warnings, cStringIO
87
warnings.filterwarnings("ignore", "hex../oct.. of negative int",
@@ -1104,9 +1103,9 @@ def get_vars_f2():
11041103
get_vars_f2 = staticmethod(get_vars_f2)
11051104

11061105
def test_vars(self):
1107-
self.assertEqual(Set(vars()), Set(dir()))
1106+
self.assertEqual(set(vars()), set(dir()))
11081107
import sys
1109-
self.assertEqual(Set(vars(sys)), Set(dir(sys)))
1108+
self.assertEqual(set(vars(sys)), set(dir(sys)))
11101109
self.assertEqual(self.get_vars_f0(), {})
11111110
self.assertEqual(self.get_vars_f2(), {'a': 1, 'b': 2})
11121111
self.assertRaises(TypeError, vars, 42, 42)

Lib/test/test_enumerate.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import unittest
2-
from sets import Set
32

43
from test import test_support
54

@@ -105,8 +104,8 @@ def test_argumentcheck(self):
105104
def test_tuple_reuse(self):
106105
# Tests an implementation detail where tuple is reused
107106
# whenever nothing else holds a reference to it
108-
self.assertEqual(len(Set(map(id, list(enumerate(self.seq))))), len(self.seq))
109-
self.assertEqual(len(Set(map(id, enumerate(self.seq)))), min(1,len(self.seq)))
107+
self.assertEqual(len(set(map(id, list(enumerate(self.seq))))), len(self.seq))
108+
self.assertEqual(len(set(map(id, enumerate(self.seq)))), min(1,len(self.seq)))
110109

111110
class MyEnum(enumerate):
112111
pass

Lib/test/test_glob.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from test.test_support import run_unittest, TESTFN
33
import glob
44
import os
5-
from sets import Set
65

76
def mkdirs(fname):
87
if os.path.exists(fname) or fname == '':
@@ -62,7 +61,7 @@ def glob(self, *parts):
6261
return glob.glob(p)
6362

6463
def assertSequencesEqual_noorder(self, l1, l2):
65-
self.assertEqual(Set(l1), Set(l2))
64+
self.assertEqual(set(l1), set(l2))
6665

6766
def test_glob_literal(self):
6867
eq = self.assertSequencesEqual_noorder

Lib/test/test_pyclbr.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from types import ClassType, FunctionType, MethodType
88
import pyclbr
99
from unittest import TestCase
10-
from sets import Set
1110

1211
# This next line triggers an error on old versions of pyclbr.
1312

@@ -24,7 +23,7 @@ class PyclbrTest(TestCase):
2423

2524
def assertListEq(self, l1, l2, ignore):
2625
''' succeed iff {l1} - {ignore} == {l2} - {ignore} '''
27-
missing = (Set(l1) ^ Set(l2)) - Set(ignore)
26+
missing = (set(l1) ^ set(l2)) - set(ignore)
2827
if missing:
2928
print >>sys.stderr, "l1=%r\nl2=%r\nignore=%r" % (l1, l2, ignore)
3029
self.fail("%r missing" % missing.pop())

0 commit comments

Comments
 (0)