tests/test-byteify-strings.t
author Pierre-Yves David <pierre-yves.david@octobus.net>
Fri, 05 Apr 2024 11:05:54 +0200
changeset 51576 de5bf3fe0233
parent 50773 8250ecb53f30
permissions -rw-r--r--
revset: stop serializing node when using "%ln" Turning hundred of thousand of node from node to hex and back can be slow… what about we stop doing it? In many case were we are using node id we should be using revision id. However this is not a good reason to have a stupidly slow implementation of "%ln". This caught my attention again because the phase discovery during push make an extensive use of "%ln" or huge set. In absolute, that phase discovery probably should use "%ld" and need to improves its algorithmic complexity, but improving "%ln" seems simple and long overdue. This greatly speeds up `hg push` on repository with many drafts. Here are some relevant poulpe benchmarks: ### data-env-vars.name = mozilla-try-2023-03-22-zstd-sparse-revlog # benchmark.name = hg.command.push # bin-env-vars.hg.flavor = default # bin-env-vars.hg.py-re2-module = default # benchmark.variants.explicit-rev = all-out-heads # benchmark.variants.issue6528 = disabled # benchmark.variants.protocol = ssh # benchmark.variants.reuse-external-delta-parent = default ## benchmark.variants.revs = any-1-extra-rev before: 44.235070 after: 20.416329 (-53.85%, -23.82) ## benchmark.variants.revs = any-100-extra-rev before: 49.234697 after: 26.519829 (-46.14%, -22.71) ### benchmark.name = hg.command.bundle # bin-env-vars.hg.flavor = default # bin-env-vars.hg.py-re2-module = default # benchmark.variants.revs = all # benchmark.variants.type = none-streamv2 ## data-env-vars.name = heptapod-public-2024-03-25-zstd-sparse-revlog before: 10.138396 after: 7.750458 (-23.55%, -2.39) ## data-env-vars.name = mercurial-public-2024-03-22-zstd-sparse-revlog before: 1.263859 after: 0.700229 (-44.60%, -0.56) ## data-env-vars.name = mozilla-try-2023-03-22-zstd-sparse-revlog before: 399.484481 after: 346.5089 (-13.26%, -52.98) ## data-env-vars.name = pypy-2024-03-22-zstd-sparse-revlog before: 4.540080 after: 3.401700 (-25.07%, -1.14) ## data-env-vars.name = tryton-public-2024-03-22-zstd-sparse-revlog before: 2.975765 after: 1.870798 (-37.13%, -1.10)

#require py37

  $ byteify_strings () {
  >   "$PYTHON" "$TESTDIR/../contrib/byteify-strings.py" "$@"
  > }

Test version

  $ byteify_strings --version
  Byteify strings * (glob)

Test in-place

  $ cat > testfile.py <<EOF
  > obj['test'] = b"1234"
  > mydict.iteritems()
  > EOF
  $ byteify_strings testfile.py -i
  $ cat testfile.py
  obj[b'test'] = b"1234"
  mydict.iteritems()

Test with dictiter

  $ cat > testfile.py <<EOF
  > obj['test'] = b"1234"
  > mydict.iteritems()
  > EOF
  $ byteify_strings testfile.py --dictiter
  obj[b'test'] = b"1234"
  mydict.items()

Test kwargs-like objects

  $ cat > testfile.py <<EOF
  > kwargs['test'] = "123"
  > kwargs[test['testing']]
  > kwargs[test[[['testing']]]]
  > kwargs[kwargs['testing']]
  > kwargs.get('test')
  > kwargs.pop('test')
  > kwargs.get('test', 'testing')
  > kwargs.pop('test', 'testing')
  > kwargs.setdefault('test', 'testing')
  > 
  > opts['test'] = "123"
  > opts[test['testing']]
  > opts[test[[['testing']]]]
  > opts[opts['testing']]
  > opts.get('test')
  > opts.pop('test')
  > opts.get('test', 'testing')
  > opts.pop('test', 'testing')
  > opts.setdefault('test', 'testing')
  > 
  > commitopts['test'] = "123"
  > commitopts[test['testing']]
  > commitopts[test[[['testing']]]]
  > commitopts[commitopts['testing']]
  > commitopts.get('test')
  > commitopts.pop('test')
  > commitopts.get('test', 'testing')
  > commitopts.pop('test', 'testing')
  > commitopts.setdefault('test', 'testing')
  > EOF
  $ byteify_strings testfile.py --treat-as-kwargs kwargs opts commitopts
  kwargs['test'] = b"123"
  kwargs[test[b'testing']]
  kwargs[test[[[b'testing']]]]
  kwargs[kwargs['testing']]
  kwargs.get('test')
  kwargs.pop('test')
  kwargs.get('test', b'testing')
  kwargs.pop('test', b'testing')
  kwargs.setdefault('test', b'testing')
  
  opts['test'] = b"123"
  opts[test[b'testing']]
  opts[test[[[b'testing']]]]
  opts[opts['testing']]
  opts.get('test')
  opts.pop('test')
  opts.get('test', b'testing')
  opts.pop('test', b'testing')
  opts.setdefault('test', b'testing')
  
  commitopts['test'] = b"123"
  commitopts[test[b'testing']]
  commitopts[test[[[b'testing']]]]
  commitopts[commitopts['testing']]
  commitopts.get('test')
  commitopts.pop('test')
  commitopts.get('test', b'testing')
  commitopts.pop('test', b'testing')
  commitopts.setdefault('test', b'testing')

Test attr*() as methods

  $ cat > testfile.py <<EOF
  > setattr(o, 'a', 1)
  > util.setattr(o, 'ae', 1)
  > util.getattr(o, 'alksjdf', 'default')
  > util.addattr(o, 'asdf')
  > util.hasattr(o, 'lksjdf', 'default')
  > util.safehasattr(o, 'lksjdf', 'default')
  > @eh.wrapfunction(func, 'lksjdf')
  > def f():
  >     pass
  > @eh.wrapclass(klass, 'lksjdf')
  > def f():
  >     pass
  > EOF

Test without attr*() as methods

  $ cat > testfile.py <<EOF
  > setattr(o, 'a', 1)
  > util.setattr(o, 'ae', 1)
  > util.getattr(o, 'alksjdf', 'default')
  > util.addattr(o, 'asdf')
  > util.hasattr(o, 'lksjdf', 'default')
  > util.safehasattr(o, 'lksjdf', 'default')
  > @eh.wrapfunction(func, 'lksjdf')
  > def f():
  >     pass
  > @eh.wrapclass(klass, 'lksjdf')
  > def f():
  >     pass
  > EOF
  $ byteify_strings testfile.py
  setattr(o, 'a', 1)
  util.setattr(o, 'ae', 1)
  util.getattr(o, 'alksjdf', b'default')
  util.addattr(o, 'asdf')
  util.hasattr(o, 'lksjdf', b'default')
  util.safehasattr(o, 'lksjdf', b'default')
  @eh.wrapfunction(func, 'lksjdf')
  def f():
      pass
  @eh.wrapclass(klass, 'lksjdf')
  def f():
      pass

Test ignore comments

  $ cat > testfile.py <<EOF
  > # py3-transform: off
  > "none"
  > "of"
  > 'these'
  > s = """should"""
  > d = '''be'''
  > # py3-transform: on
  > "this should"
  > 'and this also'
  > 
  > # no-py3-transform
  > l = "this should be ignored"
  > l2 = "this shouldn't"
  > 
  > EOF
  $ byteify_strings testfile.py
  # py3-transform: off
  "none"
  "of"
  'these'
  s = """should"""
  d = '''be'''
  # py3-transform: on
  b"this should"
  b'and this also'
  
  # no-py3-transform
  l = "this should be ignored"
  l2 = b"this shouldn't"
  
Test triple-quoted strings

  $ cat > testfile.py <<EOF
  > """This is ignored
  > """
  > 
  > line = """
  >   This should not be
  > """
  > line = '''
  > Neither should this
  > '''
  > EOF
  $ byteify_strings testfile.py
  """This is ignored
  """
  
  line = b"""
    This should not be
  """
  line = b'''
  Neither should this
  '''

Test prefixed strings

  $ cat > testfile.py <<EOF
  > obj['test'] = b"1234"
  > obj[r'test'] = u"1234"
  > EOF
  $ byteify_strings testfile.py
  obj[b'test'] = b"1234"
  obj[r'test'] = u"1234"

Test multi-line alignment

  $ cat > testfile.py <<'EOF'
  > def foo():
  >     error.Abort(_("foo"
  >                  "bar"
  >                  "%s")
  >                % parameter)
  > {
  >     'test': dict,
  >     'test2': dict,
  > }
  > [
  >    "thing",
  >    "thing2"
  > ]
  > (
  >    "tuple",
  >    "tuple2",
  > )
  > {"thing",
  >  }
  > EOF
  $ byteify_strings testfile.py
  def foo():
      error.Abort(_(b"foo"
                    b"bar"
                    b"%s")
                  % parameter)
  {
      b'test': dict,
      b'test2': dict,
  }
  [
     b"thing",
     b"thing2"
  ]
  (
     b"tuple",
     b"tuple2",
  )
  {b"thing",
   }