unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* [Request] How about adding with-locale?
@ 2012-03-24  4:37 Nala Ginrut
  2012-03-24  4:43 ` Nala Ginrut
  2012-04-02 15:29 ` Ludovic Courtès
  0 siblings, 2 replies; 10+ messages in thread
From: Nala Ginrut @ 2012-03-24  4:37 UTC (permalink / raw)
  To: guile-devel

[-- Attachment #1: Type: text/plain, Size: 696 bytes --]

Hi folks!
I encountered a bug in my project for these days. Finally I realized it's
because of my locale is zh_CN.UTF-8 in default. But in many protocols, it
needs
the result of strftime be in English. Anyway, I think there should be an
elegant way to change locale temperately. So I think maybe with-locale
would be useful.

---------------------------------------------------------------------
(define-syntax-rule (with-locale i c e0 e1 ...)
  (let ([old (setlocale i)])
    (dynamic-wind
    (lambda () (setlocale i c))
    (lambda () (begin e0 e1 ...))
    (lambda () (setlocale i old)))))
---------------------------------------------------------------------

What you guys think?

Regards.

[-- Attachment #2: Type: text/html, Size: 767 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Request] How about adding with-locale?
  2012-03-24  4:37 [Request] How about adding with-locale? Nala Ginrut
@ 2012-03-24  4:43 ` Nala Ginrut
  2012-03-24  6:54   ` Mark H Weaver
  2012-04-02 15:29 ` Ludovic Courtès
  1 sibling, 1 reply; 10+ messages in thread
From: Nala Ginrut @ 2012-03-24  4:43 UTC (permalink / raw)
  To: guile-devel

[-- Attachment #1: Type: text/plain, Size: 938 bytes --]

I know it's not the best implementation, I didn't consider the
multi-threads situation.
But it's food of thinking.

Regards.

On Sat, Mar 24, 2012 at 12:37 PM, Nala Ginrut <nalaginrut@gmail.com> wrote:

> Hi folks!
> I encountered a bug in my project for these days. Finally I realized it's
> because of my locale is zh_CN.UTF-8 in default. But in many protocols, it
> needs
> the result of strftime be in English. Anyway, I think there should be an
> elegant way to change locale temperately. So I think maybe with-locale
> would be useful.
>
> ---------------------------------------------------------------------
> (define-syntax-rule (with-locale i c e0 e1 ...)
>   (let ([old (setlocale i)])
>     (dynamic-wind
>     (lambda () (setlocale i c))
>     (lambda () (begin e0 e1 ...))
>     (lambda () (setlocale i old)))))
> ---------------------------------------------------------------------
>
> What you guys think?
>
> Regards.
>

[-- Attachment #2: Type: text/html, Size: 1228 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Request] How about adding with-locale?
  2012-03-24  4:43 ` Nala Ginrut
@ 2012-03-24  6:54   ` Mark H Weaver
  2012-03-24  8:45     ` Nala Ginrut
  2012-04-02 15:27     ` Ludovic Courtès
  0 siblings, 2 replies; 10+ messages in thread
From: Mark H Weaver @ 2012-03-24  6:54 UTC (permalink / raw)
  To: Nala Ginrut; +Cc: guile-devel

Nala Ginrut <nalaginrut@gmail.com> writes:
> I know it's not the best implementation, I didn't consider the
> multi-threads situation.

Yes, unfortunately there is no way to set the locale on a per-thread
basis.  This is a limitation of the C library.

>     in many protocols, it needs
>     the result of strftime be in English.

For protocol use, IMO it's better to avoid 'strftime' and other
locale-specific formatting tools.  For example, 'write-date' in
module/web/http.scm is a simple date formatter based on SRFI-19.

    Regards,
      Mark



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Request] How about adding with-locale?
  2012-03-24  6:54   ` Mark H Weaver
@ 2012-03-24  8:45     ` Nala Ginrut
  2012-03-24 10:24       ` Mark H Weaver
  2012-04-02 15:27     ` Ludovic Courtès
  1 sibling, 1 reply; 10+ messages in thread
From: Nala Ginrut @ 2012-03-24  8:45 UTC (permalink / raw)
  To: Mark H Weaver; +Cc: guile-devel

[-- Attachment #1: Type: text/plain, Size: 1289 bytes --]

On Sat, Mar 24, 2012 at 2:54 PM, Mark H Weaver <mhw@netris.org> wrote:

> Nala Ginrut <nalaginrut@gmail.com> writes:
> > I know it's not the best implementation, I didn't consider the
> > multi-threads situation.
>
> Yes, unfortunately there is no way to set the locale on a per-thread
> basis.  This is a limitation of the C library.
>
>
Will it be a proper solution if I use "monitor"?
------------------------------------------------------------------
(define-syntax-rule (with-locale i c e0 e1 ...)
  (let ([old (setlocale i)])
    (monitor
      (dynamic-wind
       (lambda () (setlocale i c))
       (lambda () (begin e0 e1 ...))
       (lambda () (setlocale i old))))))
------------------------------------------------------------------

But I believe it will drag my program based on multi-threads much slowly.
And I'm considering to drop my old design, so that I can set locale to "C"
in default.


> >     in many protocols, it needs
> >     the result of strftime be in English.
>
> For protocol use, IMO it's better to avoid 'strftime' and other
> locale-specific formatting tools.  For example, 'write-date' in
> module/web/http.scm is a simple date formatter based on SRFI-19.
>
>
I guess I should change the old code into new Guile web API. It's more
completely.

Regards.

[-- Attachment #2: Type: text/html, Size: 1992 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Request] How about adding with-locale?
  2012-03-24  8:45     ` Nala Ginrut
@ 2012-03-24 10:24       ` Mark H Weaver
  2012-03-24 12:42         ` Nala Ginrut
  0 siblings, 1 reply; 10+ messages in thread
From: Mark H Weaver @ 2012-03-24 10:24 UTC (permalink / raw)
  To: Nala Ginrut; +Cc: guile-devel

Nala Ginrut <nalaginrut@gmail.com> writes:
> Will it be a proper solution if I use "monitor"?

No, that wouldn't work.  For one thing, what if the body of
'with-locale' takes a long time?  Also, consider this:

  Thread 1: (with-locale "foo" (strftime ...))
  Thread 2: (format ...)

If the 'format' and the 'strftime' happen simultaneously, then 'format'
will use the "foo" locale, which is incorrect.

During 'with-locale', we would have to prevent other threads from doing
_any_ locale-dependent operation.  In a system like Guile, where a
process may contain arbitrary C code and shared libraries, there's no
sane way for us to do this.  In any case, it would not scale well.

    Regards,
      Mark



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Request] How about adding with-locale?
  2012-03-24 10:24       ` Mark H Weaver
@ 2012-03-24 12:42         ` Nala Ginrut
  2012-03-24 17:56           ` Mark H Weaver
  0 siblings, 1 reply; 10+ messages in thread
From: Nala Ginrut @ 2012-03-24 12:42 UTC (permalink / raw)
  To: Mark H Weaver; +Cc: guile-devel

[-- Attachment #1: Type: text/plain, Size: 1094 bytes --]

According to our talking on IRC. I dropped locale specific procedure
setlocale in my code. I choose (web http) write-date instead.
And my conclusion is to avoid locale specific things in multi-thread
context. Writing a new procedure for the same
issue is a better solution.


Regards.

On Sat, Mar 24, 2012 at 6:24 PM, Mark H Weaver <mhw@netris.org> wrote:

> Nala Ginrut <nalaginrut@gmail.com> writes:
> > Will it be a proper solution if I use "monitor"?
>
> No, that wouldn't work.  For one thing, what if the body of
> 'with-locale' takes a long time?  Also, consider this:
>
>  Thread 1: (with-locale "foo" (strftime ...))
>  Thread 2: (format ...)
>
> If the 'format' and the 'strftime' happen simultaneously, then 'format'
> will use the "foo" locale, which is incorrect.
>
> During 'with-locale', we would have to prevent other threads from doing
> _any_ locale-dependent operation.  In a system like Guile, where a
> process may contain arbitrary C code and shared libraries, there's no
> sane way for us to do this.  In any case, it would not scale well.
>
>    Regards,
>      Mark
>

[-- Attachment #2: Type: text/html, Size: 1565 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Request] How about adding with-locale?
  2012-03-24 12:42         ` Nala Ginrut
@ 2012-03-24 17:56           ` Mark H Weaver
  0 siblings, 0 replies; 10+ messages in thread
From: Mark H Weaver @ 2012-03-24 17:56 UTC (permalink / raw)
  To: Nala Ginrut; +Cc: guile-devel

Nala Ginrut <nalaginrut@gmail.com> writes:
> According to our talking on IRC. I dropped locale specific procedure
> setlocale in my code. I choose (web http) write-date instead. 
> And my conclusion is to avoid locale specific things in multi-thread
> context.

It's okay to use locale-dependent operations in a multi-thread program,
as long as you use only one locale for the entire process.

Even in a single-thread program, locale-dependent operations are not
appropriate for protocol use, IMO anyway.  For security reasons,
protocol code should be as simple as possible, but locale-sensitive code
tends to be much more complex.  Also, it seems to me that locale
formatters/parsers are designed for _human_ interfaces, which have a
fundamentally different set of requirements than a protocol interface.

   Regards,
     Mark



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Request] How about adding with-locale?
  2012-03-24  6:54   ` Mark H Weaver
  2012-03-24  8:45     ` Nala Ginrut
@ 2012-04-02 15:27     ` Ludovic Courtès
  2012-04-02 16:28       ` Nala Ginrut
  1 sibling, 1 reply; 10+ messages in thread
From: Ludovic Courtès @ 2012-04-02 15:27 UTC (permalink / raw)
  To: guile-devel

Hi,

Mark H Weaver <mhw@netris.org> skribis:

> Nala Ginrut <nalaginrut@gmail.com> writes:
>> I know it's not the best implementation, I didn't consider the
>> multi-threads situation.
>
> Yes, unfortunately there is no way to set the locale on a per-thread
> basis.  This is a limitation of the C library.

There’s uselocale(3) in POSIX 2008, implemented in GNU and Darwin.

Could be a useful addition to (ice-9 i18n)?

Ludo’.




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Request] How about adding with-locale?
  2012-03-24  4:37 [Request] How about adding with-locale? Nala Ginrut
  2012-03-24  4:43 ` Nala Ginrut
@ 2012-04-02 15:29 ` Ludovic Courtès
  1 sibling, 0 replies; 10+ messages in thread
From: Ludovic Courtès @ 2012-04-02 15:29 UTC (permalink / raw)
  To: guile-devel

Hi!

Did you see that (ice-9 i18n) provides “locale objects”, which provides
some flexibility compared to ‘setlocale’?

How would it help for your use case?

Thanks,
Ludo’.




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Request] How about adding with-locale?
  2012-04-02 15:27     ` Ludovic Courtès
@ 2012-04-02 16:28       ` Nala Ginrut
  0 siblings, 0 replies; 10+ messages in thread
From: Nala Ginrut @ 2012-04-02 16:28 UTC (permalink / raw)
  To: Ludovic Courtès; +Cc: guile-devel

@ludo: Please do it as you wish. I tried write-date in (web http). And
locale is not suitable rfc-2822 according to previous discussion. But
I'm glad to see more POSIX API be added.


On Mon, Apr 2, 2012 at 11:27 PM, Ludovic Courtès <ludo@gnu.org> wrote:
>
> Hi,
>
> Mark H Weaver <mhw@netris.org> skribis:
>
> > Nala Ginrut <nalaginrut@gmail.com> writes:
> >> I know it's not the best implementation, I didn't consider the
> >> multi-threads situation.
> >
> > Yes, unfortunately there is no way to set the locale on a per-thread
> > basis.  This is a limitation of the C library.
>
> There’s uselocale(3) in POSIX 2008, implemented in GNU and Darwin.
>
> Could be a useful addition to (ice-9 i18n)?
>
> Ludo’.
>
>



^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2012-04-02 16:28 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-03-24  4:37 [Request] How about adding with-locale? Nala Ginrut
2012-03-24  4:43 ` Nala Ginrut
2012-03-24  6:54   ` Mark H Weaver
2012-03-24  8:45     ` Nala Ginrut
2012-03-24 10:24       ` Mark H Weaver
2012-03-24 12:42         ` Nala Ginrut
2012-03-24 17:56           ` Mark H Weaver
2012-04-02 15:27     ` Ludovic Courtès
2012-04-02 16:28       ` Nala Ginrut
2012-04-02 15:29 ` Ludovic Courtès

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).