[squeak-dev] The Trunk: KernelTests-nice.244.mcz

Eliot Miranda eliot.miranda at gmail.com
Sun Feb 24 01:34:37 UTC 2013


On Sat, Feb 23, 2013 at 4:08 PM, Bob Arning <arning315 at comcast.net> wrote:
> I tried it and did not see a problem. That is an empty string, isn't it?

Yes, and you're getting away with it because the WriteStream
implementation doesn't use become: to grow strings.  But back in the
day it did and would have resulted in a non-zero string.

> What could go wrong?

The method writes to a literal (depending on WriteStream
implementation), modifying the method.  Try this:

| s |
s := WriteStream on: 'hello there'.
{ thisContext method decompile asString.
  s nextPutAll: 'BOO!'.
  thisContext method decompile asString }

you get:

#('DoIt
	| t1 |
	t1 := WriteStream on: ''hello there''.
	^ {thisContext method decompile asString. t1 nextPutAll: ''BOO!''.
thisContext method decompile asString}'
   'BOO!'
   'DoIt
	| t1 |
	t1 := WriteStream on: ''BOO!o there''.
	^ {thisContext method decompile asString. t1 nextPutAll: ''BOO!''.
thisContext method decompile asString}')

Ouch.

Literals should be immutable.  They're not.  It requires some VM work
to provide per-object write-protection (already done for an old
Newspeak interpreter VM) and some image changes.  We did this for
VisualWorks ten years or so ago and, while it caused some pain in old
code, it was a very good thing to have done.  For example, DB-mapping
systems like GemStone can make excellent use of per-object
write-protection.

>
> On 2/23/13 6:35 PM, Eliot Miranda wrote:
>
> Please, please, *please* let's not do things like WriteStream on: ''.
> Just decompile the method and you'll see why.  Please, please,
> *PLEASE* use either WriteStream on: String new or WriteStream on: ''
> copy.  Modifying literals is a baaaad idea.
>
>
> On Sat, Feb 23, 2013 at 11:49 AM,  <commits at source.squeak.org> wrote:
>
> Nicolas Cellier uploaded a new version of KernelTests to project The Trunk:
> http://source.squeak.org/trunk/KernelTests-nice.244.mcz
>
> ==================== Summary ====================
>
> Name: KernelTests-nice.244
> Author: nice
> Time: 23 February 2013, 8:49:05.862 pm
> UUID: 7b3445ea-0910-4ee4-a6c7-c7305f930764
> Ancestors: KernelTests-fbs.243
>
> Avoid using ReadWriteStream gratuitously when we just need a WriteStream.
>
> =============== Diff against KernelTests-fbs.243 ===============
>
> Item was changed:
>   ----- Method: DateAndTimeEpochTest>>testPrintOn (in category 'testing')
> -----
>   testPrintOn
> +       | ref ws |
> +       ref := '1901-01-01T00:00:00+00:00'.
> +       ws := '' writeStream.
> +       aDateAndTime printOn: ws.
> +       self assert: ws contents = ref.
> +       ref  := 'a TimeZone(ETZ)'.
> +       ws := '' writeStream.
> +       aTimeZone printOn:  ws.
> +       self assert: ws contents = ref!
> -       | cs rw |
> -       cs := ReadStream on: '1901-01-01T00:00:00+00:00'.
> -       rw := ReadWriteStream on: ''.
> -       aDateAndTime printOn: rw.
> -       self assert: rw contents = cs contents.
> -       cs  := ReadStream on: 'a TimeZone(ETZ)'.
> -       rw := ReadWriteStream on: ''.
> -       aTimeZone printOn:  rw.
> -       self assert: rw contents = cs contents!
>
> Item was changed:
>   ----- Method: DateAndTimeLeapTest>>testPrintOn (in category 'testing')
> -----
>   testPrintOn
> +       | ref ws |
> +       ref := '2004-02-29T13:33:00+02:00'.
> +       ws := '' writeStream.
> +       aDateAndTime printOn: ws.
> +       self assert: ws contents = ref.
> +       ref  := 'a TimeZone(UTC)'.
> +       ws := '' writeStream.
> +       aTimeZone printOn:  ws.
> +       self assert: ws contents = ref  !
> -       | cs rw |
> -       cs := ReadStream on: '2004-02-29T13:33:00+02:00'.
> -       rw := ReadWriteStream on: ''.
> -       aDateAndTime printOn: rw.
> -       self assert: rw contents = cs contents.
> -       cs  := ReadStream on: 'a TimeZone(UTC)'.
> -       rw := ReadWriteStream on: ''.
> -       aTimeZone printOn:  rw.
> -       self assert: rw contents = cs contents  !
>
> Item was changed:
>   ----- Method: DateTest>>testPrintOn (in category 'testing') -----
>   testPrintOn
> +       | ref ws |
> +       ref := '23 January 2004'.
> +       ws := '' writeStream.
> +       aDate printOn: ws.
> +       self assert: ws contents = ref!
> -       | cs rw |
> -       cs := ReadStream on: '23 January 2004'.
> -       rw := ReadWriteStream on: ''.
> -       aDate printOn: rw.
> -       self assert: rw contents = cs contents!
>
> Item was changed:
>   ----- Method: DateTest>>testPrintOnFormat (in category 'testing') -----
>   testPrintOnFormat
> +       | ref ws |
> +       ref :='04*Jan*23'.
> +       ws := '' writeStream.
> +       aDate printOn: ws format: #(3 2 1 $* 2 2).
> +       self assert: ws contents = ref!
> -       | cs rw |
> -       cs := ReadStream on: '04*Jan*23'.
> -       rw := ReadWriteStream on: ''.
> -       aDate printOn: rw format: #(3 2 1 $* 2 2).
> -       self assert: rw contents = cs contents!
>
> Item was changed:
>   ----- Method: DateTest>>testStoreOn (in category 'testing') -----
>   testStoreOn
> +       | ref ws |
> +       ref := '''23 January 2004'' asDate'.
> +       ws := '' writeStream.
> +       aDate storeOn: ws.
> +       self assert: ws contents = ref!
> -       | cs rw |
> -       cs := ReadStream on: '''23 January 2004'' asDate'.
> -       rw := ReadWriteStream on: ''.
> -       aDate storeOn: rw.
> -       self assert: rw contents = cs contents!
>
> Item was changed:
>   ----- Method: DurationTest>>testPrintOn (in category 'testing') -----
>   testPrintOn
> +       | ref ws |
> +       ref := '1:02:03:04.000000005'.
> +       ws := '' writeStream.
> +       aDuration printOn: ws.
> +       self assert: ws contents = ref!
> -       |cs rw |
> -       cs := ReadStream on: '1:02:03:04.000000005'.
> -       rw := ReadWriteStream on: ''.
> -      aDuration printOn: rw.
> -      self assert: rw contents = cs contents.!
>
> Item was changed:
>   ----- Method: IntegerTest>>testPrintOnBaseShowRadix (in category 'tests -
> printing') -----
>   testPrintOnBaseShowRadix
>         | s |
> +       s := '' writeStream.
> -       s := ReadWriteStream on: ''.
>         123 printOn: s base: 10 showRadix: false.
>         self assert: (s contents = '123').
>
> +       s := '' writeStream.
> -       s := ReadWriteStream on: ''.
>         123 printOn: s base: 10 showRadix: true.
>         self assert: (s contents = '10r123').
>
> +       s := '' writeStream.
> -       s := ReadWriteStream on: ''.
>         123 printOn: s base: 8 showRadix: false.
>         self assert: (s contents = '173').
>
> +       s := '' writeStream.
> -       s := ReadWriteStream on: ''.
>         123 printOn: s base: 8 showRadix: true.
>         self assert: (s contents = '8r173').!
>
> Item was changed:
>   ----- Method: StopwatchTest>>testPrintOn (in category 'testing') -----
>   testPrintOn
> +       | ref ws |
> +       ref := 'a Stopwatch(suspended:0:00:00:00)'.
> +       ws := '' writeStream.
> +       aStopwatch printOn: ws.
> +       self assert: ws contents = ref!
> -       | cs rw |
> -       cs := ReadStream on: 'a Stopwatch(suspended:0:00:00:00)'.
> -       rw := ReadWriteStream on: ''.
> -       aStopwatch printOn: rw.
> -       self assert: rw contents = cs contents!
>
> Item was changed:
>   ----- Method: TimeStampTest>>testPrintOn (in category 'testing') -----
>   testPrintOn
> +       | ref ws |
> +       ref := '2 January 2004 12:34:56 am'.
> +       ws := '' writeStream.
> +       aTimeStamp printOn: ws.
> +       self assert: ws contents = ref!
> -       | cs rw |
> -       cs := ReadStream on: '2 January 2004 12:34:56 am'.
> -       rw := ReadWriteStream on: ''.
> -       aTimeStamp printOn: rw.
> -       self assert: rw contents = cs contents!
>
> Item was changed:
>   ----- Method: TimeStampTest>>testStoreOn (in category 'testing') -----
>   testStoreOn
> +       | ref ws |
> +       ref := '''2 January 2004 12:34:56 am'' asTimeStamp'.
> +       ws := '' writeStream.
> +       aTimeStamp storeOn: ws.
> +       self assert: ws contents = ref!
> -       | cs rw |
> -       cs := ReadStream on: '''2 January 2004 12:34:56 am'' asTimeStamp'.
> -       rw := ReadWriteStream on: ''.
> -       aTimeStamp storeOn: rw.
> -       self assert: rw contents = cs contents!
>
> Item was changed:
>   ----- Method: TimespanTest>>testPrintOn (in category 'testing') -----
>   testPrintOn
> +       | ref ws |
> +       ref := 'a Timespan(2005-01-01T00:00:00+00:00D7:00:00:00)'.
> +       ws := '' writeStream.
> +       aTimespan printOn: ws.
> +       self assert: ws contents = ref
> -       | cs rw |
> -       cs := ReadStream on: 'a
> Timespan(2005-01-01T00:00:00+00:00D7:00:00:00)'.
> -       rw := ReadWriteStream on: ''.
> -       aTimespan  printOn: rw.
> -       self assert: rw contents = cs contents
>   !
>
> Item was changed:
>   ----- Method: YearMonthWeekTest>>testMonthPrintOn (in category 'testing')
> -----
>   testMonthPrintOn
> +       | aMonth ws |
> +       aMonth := Month starting: DateAndTime new duration: 31 days.
> +       ws := '' writeStream.
> +       aMonth printOn: ws.
> +       self assert: ws contents = 'January 1901'.!
> -       | aMonth cs rw |
> -       aMonth := Month starting: DateAndTime new duration: 31 days.
> -       cs := ReadStream on: 'January 1901'.
> -       rw := ReadWriteStream on: ''.
> -      aMonth printOn: rw.
> -      self assert: rw contents = cs contents.!
>
> Item was changed:
>   ----- Method: YearMonthWeekTest>>testYearPrintOn (in category 'testing')
> -----
>   testYearPrintOn
> +       | aYear ws |
> +       aYear := Year starting: DateAndTime new duration: 365 days.
> +       ws := '' writeStream.
> +       aYear printOn: ws.
> +       self assert: ws contents = 'a Year (1901)'.!
> -       | aYear cs rw |
> -       aYear := Year starting: DateAndTime new duration: 365 days.
> -       cs := ReadStream on: 'a Year (1901)'.
> -       rw := ReadWriteStream on: ''.
> -      aYear printOn: rw.
> -      self assert: rw contents = cs contents.!
>
>
>
>
>
>
>
>



-- 
best,
Eliot


More information about the Squeak-dev mailing list