Code

Opened 8 years ago

Closed 7 years ago

#2920 closed defect (fixed)

Putting _() into builtins causes trouble with doctests

Reported by: mtredinnick Owned by: nobody
Component: Testing framework Version:
Severity: normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

We install a function called _() as an alias for gettext() into __builtins__ so that we don't have to import everywhere we want to translate strings.

The Python interactive shell uses the _ variable to store the most recent result. Any doctests run in the same sort of environment as the interactive shell and so this variable gets nuked. If a class has doctests and uses translation functionality, chaos ensues.

One solution is to import gettext() and alias it to _() in each modules' scope. A little awkward but not inherently bad. Maybe there's another solution I haven't thought of yet, though.

Original problem description in this django-users thread.

Attachments (0)

Change History (11)

comment:1 Changed 8 years ago by dave AT avaragado.org

Looks like a dupe of #2734.

comment:2 Changed 7 years ago by Michael Radziej <mir@…>

  • Triage Stage changed from Unreviewed to Design decision needed

marked #2734 as duplicate

comment:3 Changed 7 years ago by russellm

(In [4485]) Refs #2920 -- Replaced implicit uses of _() with explicit imports or calls to gettext(). At some point post 0.96, we need to remove the calls that put _ into the builtins.

comment:4 follow-up: Changed 7 years ago by mtredinnick

(In [4486]) Revert [4485] in order to fix accidental mod_python breakage. Refs #2920.

comment:5 Changed 7 years ago by mtredinnick

(In [4905]) Delayed the reading of settings.USE_I18N until the first use of the i18n
functions. This solves a few import problems we are seeing. Fixed #3687. Refs #2920.

comment:6 in reply to: ↑ 4 Changed 7 years ago by mtredinnick

Replying to mtredinnick:

(In [4486]) Revert [4485] in order to fix accidental mod_python breakage. Refs #2920.

For reference, the problems we were seeing are in this django-users thread. They should be fixed after [4905], so we can move onto part II of this ticket, which is removing _() from builtins.

comment:7 Changed 7 years ago by mtredinnick

(In [5230]) unicode: Replaced all implicit uses of _() from builtins with explicitly
importing ugettext and aliasing it to _. Refs #2920.

comment:8 Changed 7 years ago by russellm

  • Triage Stage changed from Design decision needed to Accepted

comment:9 Changed 7 years ago by Simon G. <dev@…>

  • Resolution set to fixed
  • Status changed from new to closed

This seems to be fixed?

comment:10 Changed 7 years ago by mtredinnick

  • Resolution fixed deleted
  • Status changed from closed to reopened

No it's not fixed. It's only fixed in Django's core because we work around it. Not fixed for everybody else writing tests.

comment:11 Changed 7 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from reopened to closed

(In [6582]) Fixed #2920 -- Removed _() from builtins.

This is backwards incompatible, but easy to work around.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.