def test__c_intern_handles_refcount(self): if self.module is _static_tuple_py: return # Not applicable unique_str1 = 'unique str ' + osutils.rand_chars(20) unique_str2 = 'unique str ' + osutils.rand_chars(20) key = self.module.StaticTuple(unique_str1, unique_str2) self.assertRefcount(1, key) self.assertFalse(key in self.module._interned_tuples) self.assertFalse(key._is_interned()) key2 = self.module.StaticTuple(unique_str1, unique_str2) self.assertRefcount(1, key) self.assertRefcount(1, key2) self.assertEqual(key, key2) self.assertIsNot(key, key2) key3 = key.intern() self.assertIs(key, key3) self.assertTrue(key in self.module._interned_tuples) self.assertEqual(key, self.module._interned_tuples[key]) # key and key3, but we 'hide' the one in _interned_tuples self.assertRefcount(2, key) del key3 self.assertRefcount(1, key) self.assertTrue(key._is_interned()) self.assertRefcount(1, key2) key3 = key2.intern() # key3 now points to key as well, and *not* to key2 self.assertRefcount(2, key) self.assertRefcount(1, key2) self.assertIs(key, key3) self.assertIsNot(key3, key2) del key2 del key3 self.assertRefcount(1, key)
def test_intern(self): unique_str1 = 'unique str ' + osutils.rand_chars(20) unique_str2 = 'unique str ' + osutils.rand_chars(20) key = self.module.StaticTuple(unique_str1, unique_str2) self.assertFalse(key in self.module._interned_tuples) key2 = self.module.StaticTuple(unique_str1, unique_str2) self.assertEqual(key, key2) self.assertIsNot(key, key2) key3 = key.intern() self.assertIs(key, key3) self.assertTrue(key in self.module._interned_tuples) self.assertEqual(key, self.module._interned_tuples[key]) key2 = key2.intern() self.assertIs(key, key2)
def __init__(self, filename, mode='wb', new_mode=None): global _hostname self._fd = None if _hostname is None: _hostname = osutils.get_host_name() self.tmpfilename = '%s.%d.%s.%s.tmp' % (filename, _pid, _hostname, osutils.rand_chars(10)) self.realfilename = filename flags = os.O_EXCL | os.O_CREAT | os.O_WRONLY | osutils.O_NOINHERIT if mode == 'wb': flags |= osutils.O_BINARY elif mode != 'wt': raise ValueError("invalid AtomicFile mode %r" % mode) if new_mode is not None: local_mode = new_mode else: local_mode = 0o666 # Use a low level fd operation to avoid chmodding later. # This may not succeed, but it should help most of the time self._fd = os.open(self.tmpfilename, flags, local_mode) if new_mode is not None: # Because of umask issues, we may need to chmod anyway # the common case is that we won't, though. st = os.fstat(self._fd) if stat.S_IMODE(st.st_mode) != new_mode: osutils.chmod_if_possible(self.tmpfilename, new_mode)
def test__c_keys_are_not_immortal(self): if self.module is _static_tuple_py: return # Not applicable unique_str1 = 'unique str ' + osutils.rand_chars(20) unique_str2 = 'unique str ' + osutils.rand_chars(20) key = self.module.StaticTuple(unique_str1, unique_str2) self.assertFalse(key in self.module._interned_tuples) self.assertRefcount(1, key) key = key.intern() self.assertRefcount(1, key) self.assertTrue(key in self.module._interned_tuples) self.assertTrue(key._is_interned()) del key # Create a new entry, which would point to the same location key = self.module.StaticTuple(unique_str1, unique_str2) self.assertRefcount(1, key) # This old entry in _interned_tuples should be gone self.assertFalse(key in self.module._interned_tuples) self.assertFalse(key._is_interned())
def create_plugin_file(self, contents): """Create a file to be used as a plugin. This is created in a temporary directory, so that we are sure that it doesn't start in the plugin path. """ os.mkdir('tmp') plugin_name = 'bzr_plugin_a_%s' % (osutils.rand_chars(4),) with open('tmp/' + plugin_name + '.py', 'wb') as f: f.write(contents) return plugin_name
def __init__(self, repository, last_revision_info, tags): from breezy.tag import DisabledTags, MemoryTags self.repository = repository self._last_revision_info = last_revision_info self._revision_history_cache = None if tags is not None: self.tags = MemoryTags(tags) else: self.tags = DisabledTags(self) self._partial_revision_history_cache = [] self._last_revision_info_cache = None self._revision_id_to_revno_cache = None self._partial_revision_id_to_revno_cache = {} self._partial_revision_history_cache = [] self.base = "memory://" + osutils.rand_chars(10)